Subversion Repositories SmartDukaan

Rev

Rev 1121 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.content.security;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Singleton class which stores permissions for a particular role. Utility functions for managing roles can be found here.
 * 
 * @author rajveer
 *
 */
public class RoleManager {
        
        private static RoleManager roleManager;
        private Map<Role, List<Action>> permissions;
        
        
        static{
                synchronized(RoleManager.class){
                        roleManager = new RoleManager();
                }
        }
        
        private RoleManager(){
                //TODO get the object from bdb
                permissions = new HashMap<Role, List<Action>>();
                List<Action> developerActions = new ArrayList<Action>();
                developerActions.add(Action.EDIT);
                developerActions.add(Action.VIEW);
                developerActions.add(Action.COMPLETE);
                permissions.put(Role.DEVELOPER, developerActions);
                
                List<Action> editorActions = new ArrayList<Action>();
                editorActions.add(Action.EDIT);
                editorActions.add(Action.VIEW);
                editorActions.add(Action.COMPLETE);
                editorActions.add(Action.ASSIGN);
                editorActions.add(Action.READY);
                permissions.put(Role.EDITOR, editorActions);
                
                List<Action> adminActions = new ArrayList<Action>();
                adminActions.add(Action.EDIT);
                adminActions.add(Action.VIEW);
                adminActions.add(Action.CREATE);
                adminActions.add(Action.COMPLETE);
                adminActions.add(Action.ASSIGN);
                adminActions.add(Action.READY);
                adminActions.add(Action.DELETE);
                permissions.put(Role.ADMIN, adminActions);
        }
        
        /**
         * Get the singleton instance of role manager
         * @return
         */
        public static RoleManager getRoleManager(){
                return roleManager;
        }
        
        /**
         * Create new role. If already exists, does nothing. 
         * @param role
         */
        public void createRole(Role role){
                if(!permissions.containsKey(role)){
                        permissions.put(role, null);
                }
        }
        
        /**
         * utility function to remove a role.
         * 
         * @param role
         */
        public void dropRole(Role role){
                if(permissions.containsKey(role)){
                        permissions.remove(role);
                }       
        }
        
        /**
         * Method to grant permission for an action to an role
         * 
         * @param type role to which permission to be granted
         * @param action action which to be added for role
         */
        public void grantPermission(Role type, Action action){
                List<Action> actions = permissions.get(type);
                if(actions == null){
                        actions = new ArrayList<Action>();
                }
                if(!actions.contains(action)){
                        actions.add(action);
                }
        }

        /**
         * utility function to remove permissions from a role.
         * 
         * @param type
         * @param action
         */
        public void revokePermission(Role type, Action action){
                List<Action> actions = permissions.get(type);
                if(actions != null){
                        actions.remove(action);
                }
        }

        
        public boolean hasPermission(Role role, Action action){
                return permissions.get(role).contains(action);
        }
}