예제 #1
0
 /**
  *
  * 列举共享信息
  *
  * @since 1.1.0
  */
 public function handlerSetPermission()
 {
     $this->_file = UserFile::model()->findByAttributes(array('id' => $this->_id, 'is_deleted' => 0));
     if (is_null($this->_file)) {
         throw new ApiException('Not Found');
     }
     //判断此用户是否有 分配 权限
     $file_path = $this->_file["file_path"];
     $hasPermissionAllot = Yii::app()->privilege->hasPermission($file_path, MPrivilege::PERMISSION_GRANT);
     if (!$hasPermissionAllot) {
         $this->result["msg"] = Yii::t('front_common', 'Can not grant permission');
         throw new ApiException('Can not grant permission');
     }
     //
     // 将逗号分割的id组装成数组
     //
     if (!empty($this->_slaves)) {
         $this->_slaves = array_slice(explode(',', $this->_slaves), 0);
     } else {
         $this->_slaves = array();
     }
     $this->_permissions = array_slice(explode(',', $this->_permissions), 0);
     $tmpUser = $this->_slaves;
     //查询出此路径下的直接权限的所有用户
     $privileges = MUserPrivilege::model()->findAll('file_path=:file_path', array(':file_path' => $this->_file["file_path"]));
     $currentUser = MUserManager::getInstance()->getCurrentUser();
     foreach ($privileges as $pri) {
         //分配权限时如果权限表中的权限的所有者是自己, 则不进行处理
         if ($pri["user_id"] == $currentUser["id"]) {
             continue;
         }
         //如果此用户不存在则进行添加权限,已经存在则进行修改权限,否则进行删除权限操作
         if (in_array($pri["user_id"], $tmpUser)) {
             $newPermission = $this->getUserPermission($this->_slaves, $pri["user_id"]);
             //进行是否事件生成判断
             $this->updatePrivelegeEvent($pri, $newPermission);
             //更新权限
             Yii::app()->privilege->updatedPrivilege($pri["user_id"], $this->_file["file_path"], $newPermission);
             $tmpUser = CUtils::arrayRemove($tmpUser, $pri["user_id"]);
         } else {
             $pri->delete();
             $this->deletePrivelegeEvent($pri["user_id"], $pri["file_path"], unserialize($pri["permission"]));
         }
     }
     //创建权限
     foreach ($tmpUser as $index => $userId) {
         $permission = $this->getUserPermission($this->_slaves, $userId);
         $this->createPrivelegeEvent($userId, $this->_file["file_path"], $permission);
         Yii::app()->privilege->createPrivilege($userId, $this->_file["file_path"], $permission);
     }
 }