147 lines
3.5 KiB
147 lines
3.5 KiB
/****************************************************************************** |
|
* |
|
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. |
|
* All rights reserved. |
|
* |
|
******************************************************************************/ |
|
|
|
package com.fourelementscapital.auth; |
|
|
|
import java.util.ArrayList; |
|
import java.util.HashMap; |
|
import java.util.Iterator; |
|
import java.util.Map; |
|
|
|
|
|
/** |
|
* UserThemeAccessPermission provides functions to convert theme access permissions into 'rwx' permission group. |
|
* Example : a user has 'X' permission in 'computing' theme. The 'computing' theme then will be included in 'rwx' permission group. |
|
* Another Example : a user has 'U' permission in 'etrading' theme. The 'etrading' theme then will be included in 'rx' permission group. |
|
*/ |
|
public class UserThemeAccessPermission { |
|
|
|
/** |
|
* Username |
|
*/ |
|
private String username; |
|
|
|
/** |
|
* 'rwx' permission group |
|
*/ |
|
private ArrayList<String> rwx = new ArrayList<String>(); |
|
|
|
/** |
|
* 'rx' permission group |
|
*/ |
|
private ArrayList<String> rx = new ArrayList<String>(); |
|
|
|
/** |
|
* 'r' permission group |
|
*/ |
|
private ArrayList<String> r = new ArrayList<String>(); |
|
|
|
|
|
/** |
|
* Constructor. Creates a new UserThemeAccessPermission object |
|
* @param un Username |
|
*/ |
|
public UserThemeAccessPermission(String un) { |
|
this.username=un; |
|
} |
|
|
|
/** |
|
* Get rwx permission group |
|
* @return rwx list |
|
*/ |
|
public ArrayList<String> getRwx() { |
|
return rwx; |
|
} |
|
|
|
/** |
|
* Get rx permission group |
|
* @return rx list |
|
*/ |
|
public ArrayList<String> getRx() { |
|
return rx; |
|
} |
|
|
|
/** |
|
* Get r permission group |
|
* @return r list |
|
*/ |
|
public ArrayList<String> getR() { |
|
return r; |
|
} |
|
|
|
/** |
|
* Add permission with user's themes |
|
* @param themes |
|
* @throws Exception |
|
*/ |
|
public void addPermissionWithThemes(Map<String,String> themes) throws Exception { |
|
try{ |
|
Map<String,String> resolved = this.replacePermissions(themes); |
|
for(Iterator<String> it=resolved.keySet().iterator();it.hasNext();){ |
|
String theme=it.next(); |
|
String val=resolved.get(theme); |
|
if(val!=null && val.trim().equalsIgnoreCase("rwx")) getRwx().add(theme); |
|
if(val!=null && val.trim().equalsIgnoreCase("rx")) getRx().add(theme); |
|
if(val!=null && val.trim().equalsIgnoreCase("r")) getR().add(theme); |
|
} |
|
} catch(Exception e) { |
|
throw e; |
|
} |
|
} |
|
|
|
/** |
|
* Replace the permission letter with 'rwx'. |
|
* @param data |
|
* @return themes with 'rwx' permissions |
|
* @throws Exception |
|
*/ |
|
public static Map<String,String> replacePermissions(Map<String,String> data) throws Exception { |
|
HashMap<String,String> perm = new HashMap<String,String>(); |
|
perm.put("X","rwx"); |
|
perm.put("S","rwx"); |
|
perm.put("B","rwx"); |
|
perm.put("C","rwx"); |
|
perm.put("U","rx"); |
|
perm.put("N","r"); |
|
perm.put("M","rwx"); |
|
for(String key: data.keySet()){ |
|
data.put(key, perm.get(data.get(key))); |
|
} |
|
return data; |
|
} |
|
|
|
/** |
|
* Overriding to make unique objects per username |
|
* @param obj Instance of AuthUser or other object |
|
* @return true if obj's username is equal with class' username |
|
*/ |
|
public boolean equals(Object obj ) { |
|
if(obj instanceof UserThemeAccessPermission){ |
|
UserThemeAccessPermission other=(UserThemeAccessPermission)obj; |
|
if(other.username.equals(this.username)){ |
|
return true; |
|
}else{ |
|
return false; |
|
} |
|
}else{ |
|
return false; |
|
} |
|
} |
|
|
|
/** |
|
* Override toString() method |
|
* @return username string |
|
*/ |
|
public String toString() { |
|
return ""+this.username; |
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|