示例#1
0
 /**
  * @service role add
  * @return Gpf_Rpc_Form
  */
 public function add(Gpf_Rpc_Params $params)
 {
     $form = new Gpf_Rpc_Form($params);
     $origRole = new Gpf_Db_Role();
     $origRole->setId($form->getFieldValue('roleid'));
     $origRole->load();
     $newRole = new Gpf_Db_Role();
     $newRole->setName($form->getFieldValue('name'));
     $newRole->setAccountId(Gpf_Session::getInstance()->getAuthUser()->getAccountId());
     $newRole->setRoleType($origRole->getRoleType());
     $newRole->insert();
     if (strlen($origRole->getAccountId())) {
         //it is custom role, copy privileges from db
         $select = new Gpf_SqlBuilder_SelectBuilder();
         $select->select->addConstant($newRole->getId(), 'roleid');
         $select->select->add(Gpf_Db_Table_RolePrivileges::OBJECT, Gpf_Db_Table_RolePrivileges::OBJECT);
         $select->select->add(Gpf_Db_Table_RolePrivileges::PRIVILEGE, Gpf_Db_Table_RolePrivileges::PRIVILEGE);
         $select->from->add(Gpf_Db_Table_RolePrivileges::getName());
         $select->where->add(Gpf_Db_Table_Roles::ID, '=', $origRole->getId());
         $insert = new Gpf_SqlBuilder_InsertBuilder();
         $insert->setTable(Gpf_Db_Table_RolePrivileges::getInstance());
         $insert->fromSelect($select);
         $insert->execute();
     } else {
         //it is default role, copy privileges from php settings
         $privileges = Gpf_Application::getInstance()->getRoleDefaultPrivileges($origRole->getId());
         foreach ($privileges as $objectName => $privilegeList) {
             foreach ($privilegeList as $right) {
                 $privilege = new Gpf_Db_RolePrivilege();
                 $privilege->setRoleId($newRole->getId());
                 $privilege->setObject($objectName);
                 $privilege->setPrivilege($right);
                 $privilege->insert();
             }
         }
     }
     return $form;
 }