示例#1
0
 public function editAction()
 {
     //add role if it is provided
     parent::editAction();
     if ($this->_hasParam('roles_list')) {
         $objUser = $this->view->object;
         // when the list of roles is submitted directly with user form
         $arrExistingRoles = array();
         foreach ($objUser->getRoles() as $objRole) {
             $arrExistingRoles[$objRole->getId()] = $objRole->getId();
         }
         $arrNewIds = array();
         $arrIds = explode(",", $this->_getParam('roles_list'));
         foreach ($arrIds as $nRoleId) {
             $nRoleId = trim($nRoleId);
             if ($nRoleId == '') {
                 continue;
             }
             $arrNewIds[$nRoleId] = $nRoleId;
             if (!isset($arrExistingRoles[$nRoleId])) {
                 // need to add a role
                 $objUserRole = User_UserRole::Table()->createRow();
                 $objUserRole->ucur_user_id = $objUser->getId();
                 $objUserRole->ucur_role_id = $nRoleId;
                 $objUserRole->save(false);
             }
         }
         // walking through existing roles, delete IDs
         foreach ($arrExistingRoles as $nRoleId) {
             if (!isset($arrNewIds[$nRoleId])) {
                 // this role has to be deleted
                 $objUserRole = User_UserRole::Table()->findRole($objUser->getId(), $nRoleId);
                 if (is_object($objUserRole)) {
                     $objUserRole->delete();
                 }
             }
         }
         $objUser->cleanCache();
         $this->view->object = $objUser;
     } else {
         if ($this->_hasParam('role') && $this->_getParam('role') != '') {
             $strRole = $this->_getParam('role');
             $objRole = User_Role::Table()->findByName($strRole);
             if (!is_object($objRole)) {
                 throw new App_Exception('Invalid User Role');
             }
             $objUser = $this->view->object;
             if (!$objUser->hasRole($strRole)) {
                 $objUserRole = User_UserRole::Table()->createRow();
                 $objUserRole->ucur_user_id = $objUser->getId();
                 $objUserRole->ucur_role_id = $objRole->getId();
                 $objUserRole->save(false);
                 $objUser->cleanCache();
             }
         }
     }
 }
示例#2
0
 /**
  * @return void
  */
 protected function _addDefaultAccounts()
 {
     $cfgDefaultAccounts = App_Application::getInstance()->getConfig()->user->list;
     if (is_object($cfgDefaultAccounts)) {
         $cfgDefaultAccount = null;
         $tblRole = User_Role::Table();
         $tblUserRole = User_UserRole::Table();
         /** @var $cfgDefaultAccount User_Account */
         foreach ($cfgDefaultAccounts as $cfgDefaultAccount) {
             $objAccount = $this->_addDefaultAccount($cfgDefaultAccount->toArray());
             if (is_object($cfgDefaultAccount->roles)) {
                 // add roles for a user...
                 $arrRoles = $cfgDefaultAccount->roles;
                 foreach ($arrRoles as $strRoleName) {
                     $objRole = $tblRole->findByName($strRoleName);
                     if (is_object($objRole) && !is_object($tblUserRole->findRole($objAccount->getId(), $objRole->getId()))) {
                         $objUserRole = $tblUserRole->createRow();
                         $objUserRole->ucur_user_id = $objAccount->getId();
                         $objUserRole->ucur_role_id = $objRole->getId();
                         $objUserRole->save();
                     }
                 }
             }
         }
     }
 }
示例#3
0
 /**
  * @param string $strRole
  * @return void
  */
 public function removeRole($strRole)
 {
     $objRole = User_Role::Table()->findByName($strRole);
     if (!is_object($objRole)) {
         throw new App_Exception('Invalid user role ' . $strRole);
     }
     $nRoleId = $objRole->getId();
     $objUserRole = User_UserRole::Table()->findRole($this->getId(), $nRoleId);
     if (is_object($objUserRole)) {
         $objUserRole->delete();
         $this->cleanCache();
     }
 }