Example #1
0
 /**
  * Sets the list of permissions from the database
  * 
  * @return	boolean	if the function correctly gets the list of permissions it returns true else false
  *
  * @todo	this function
  */
 public function setPermissions()
 {
     $super = false;
     $permission = DataObjectFactory::Factory('Permission');
     $permission_types = $permission->getEnumOptions('type');
     // Get the roles the user is assigned to for this company
     $this->setRoles();
     // someone with no roles can't have access to anything
     if (empty($this->roles)) {
         return false;
     }
     // get the permissions (ids) for the roles the user has access to
     // constrained by the permissions the company has access to
     // Problem here is that company permissions are related to modules
     // but user permissions are related to modules, controllers, actions
     $hp = DataObjectFactory::Factory('HasPermission');
     $role_permissions = $hp->getPermissionID(null, $this->roles);
     // TODO: A user may have access to more than one company, but we may want
     // assign different permissions to the user for the different companies
     // Company permissions currently disabled
     //get the permissions data for the permissions the user has access to
     $user_permissions = new PermissionCollection();
     if (!empty($role_permissions)) {
         $data = $user_permissions->getPermissions($role_permissions);
     }
     $permissions = array();
     if (count($data) > 0) {
         foreach ($data as $permission) {
             $permissions[$permission['id']] = $permission;
         }
     }
     $this->permissions = $permissions;
     //we need to make sure that roles haven't been given access to things that the company doesn't have access to
     /*
     		$company_permissions=new CompanypermissionCollection();
     		$company_permissions->getPermissions(EGS_COMPANY_ID, 'position');
     		$this->permissions=array();
     		
     		foreach($company_permissions as $permission) {
     			if(isset($mod_permissions[$permission->permission])) {
     				$this->permissions[$permission->permission]=$mod_permissions[$permission->permission];
     			}
     		}
     		$this->permissions=$this->permissions+$permissions;
     		$this->tree = $this->getPermissionTree($this->permissions);
     */
     // Build the Permissions tree for the user's menu access
     $this->tree = array();
     foreach ($permissions as $permission) {
         if ($permission['display'] == 't' && (empty($permission['parent_id']) || !empty($permission['parent_id']) && $permissions[$permission['parent_id']]['display'] == 't')) {
             // set a starting value for older style permissions
             $link_params = array(strtolower($permission_types[$permission['type']]) => $permission['permission']);
             // we can work out the correct link if the permission has a module_id
             if (!empty($permission['module_id'])) {
                 $module = DataObjectFactory::Factory('ModuleObject');
                 $controller = DataObjectFactory::Factory('ModuleComponent');
                 switch ($permission['type']) {
                     case 'a':
                         $link_params['action'] = $permission['permission'];
                         $link_params['controller'] = str_replace('controller', '', $controller->load_identifier_value($permission['component_id']));
                         $link_params['module'] = $module->load_identifier_value($permission['module_id']);
                         break;
                     case 'c':
                         $link_params['controller'] = str_replace('controller', '', $controller->load_identifier_value($permission['component_id']));
                         $link_params['module'] = $module->load_identifier_value($permission['module_id']);
                         break;
                     case 'm':
                         $link_params['module'] = $module->load_identifier_value($permission['module_id']);
                         break;
                 }
             }
             if ($permission['has_parameters'] == 't') {
                 $permission_parameters = DataObjectFactory::Factory('PermissionParameters');
                 $permission_parameters->idField = 'name';
                 $cc = new ConstraintChain();
                 $cc->add(new Constraint('permissionsid', '=', $permission['id']));
                 $link_params += $permission_parameters->getAll($cc);
             }
             $key = !isset($permission['parent_id']) ? 0 : $permission['parent_id'];
             $this->tree[$key][$permission['position']] = array('id' => $permission['id'], 'title' => $permission['title'], 'type' => $permission['type'], 'link' => $link_params);
             $this->tree[$key][$permission['position']]['new_type_permission'] = !empty($permission['module_id']);
         }
     }
     foreach ($this->tree as $treekey => $treenode) {
         if ($treekey == 0) {
             foreach ($treenode as $position => $permission) {
                 $this->buildTree($permission);
             }
         }
     }
     ksort($this->tree);
     foreach ($this->tree as $treekey => $position) {
         ksort($position);
         $this->tree[$treekey] = $position;
     }
     return true;
 }