/** * (返回更新的记录数) userPowerUpdate : * @param $currentUID integer 当前登录的用户ID * @param $setUID integer 需要设置单位权限的用户ID * @param $permission integer 单位权限级别 * @param $unitcode string 单位编码 * @param int $type 更新类型 * @return 返回更新的记录数 * @throws \yii\db\Exception */ private function userPowerUpdate($currentUID, $setUID, $permission, $unitcode, $type = self::UPDATE_SELF) { /** @var $result 返回更新的记录数*/ $result = 0; /** @var $adminRole string 在Preferences中配置,classmark:sSystem */ $adminRole = Preferences::get('sSystem', 'adminRole'); //超级管理员 /** @var $role \yii\rbac\Role[] 当前用户角色数组*/ $role = Yii::$app->authManager->getRolesByUser(Yii::$app->user->identity->id); /** @var $is_admin boolean 是否为超级管理员*/ $is_admin = array_key_exists($adminRole, $role); $unitlist = $unitcode; switch ($type) { case self::UPDATE_CHILDLIST: $unitlist = Unit::getChildList($unitcode); break; case self::UPDATE_PARENTLIST: $unitlist = Unit::getParentList($unitcode); break; } $SQL = "REPLACE INTO `map_unit`(`user_id`, `unitcode`, `user_power`) " . " SELECT {$setUID}, cur_mu.unitcode, CASE WHEN cur_mu.user_power >= :user_power THEN :user_power ELSE cur_mu.user_power END FROM " . " (SELECT unitcode, user_power FROM `map_unit` WHERE `user_id` = :currentUID AND FIND_IN_SET(unitcode,:unitlist)) cur_mu " . " LEFT JOIN (SELECT unitcode, user_power FROM `map_unit` WHERE `user_id` = :setUID ) set_mu ON (cur_mu.unitcode = set_mu.unitcode) " . " WHERE (set_mu.user_power <= cur_mu.user_power and set_mu.user_power <> :user_power or set_mu.user_power IS NULL)"; //超级管理员 if ($is_admin) { $SQL = "REPLACE INTO `map_unit`(`user_id`, `unitcode`, `user_power`) " . " SELECT {$setUID}, u.unitcode, {$permission} FROM " . " (SELECT unitcode FROM `unit` WHERE FIND_IN_SET(unitcode,:unitlist)) u " . " LEFT JOIN (SELECT unitcode, user_power FROM `map_unit` WHERE `user_id` = :setUID ) set_mu ON (u.unitcode = set_mu.unitcode) " . " WHERE :currentUID > 0 and (set_mu.user_power <> :user_power or set_mu.user_power IS NULL)"; } $result = Yii::$app->db->createCommand($SQL)->bindValues([':currentUID' => $currentUID, ':user_power' => $permission, ':unitlist' => $unitlist, ':setUID' => $setUID])->execute(); if ($permission == MapUnit::USER_POWER_DENY) { //清除禁止访问的 MapUnit::deleteAll(['user_power' => MapUnit::USER_POWER_DENY]); } return $result; }
/** * @inheritDoc */ public function afterDelete() { parent::afterDelete(); //TODO: 后期要继续添加 删除人员、配偶等等相关联表的代码 deleteAll不触发事件,需要记录的用delete() //删除map_unit表中unitcode是:$_delUnitcode 的 MapUnit::deleteAll(['unitcode' => $this->_delUnitcode]); }