/****************************************************************************** * * 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 rwx = new ArrayList(); /** * 'rx' permission group */ private ArrayList rx = new ArrayList(); /** * 'r' permission group */ private ArrayList r = new ArrayList(); /** * 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 getRwx() { return rwx; } /** * Get rx permission group * @return rx list */ public ArrayList getRx() { return rx; } /** * Get r permission group * @return r list */ public ArrayList getR() { return r; } /** * Add permission with user's themes * @param themes * @throws Exception */ public void addPermissionWithThemes(Map themes) throws Exception { try{ Map resolved = this.replacePermissions(themes); for(Iterator 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 replacePermissions(Map data) throws Exception { HashMap perm = new HashMap(); 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; } }