RoleService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace common\services;
  3. use common\components\AjaxException;
  4. use common\models\BaseRole;
  5. use common\models\BaseRoleClientRules;
  6. use common\models\BaseRoleMenu;
  7. use common\models\BaseRoleServerRules;
  8. use Throwable;
  9. use yii\base\InvalidConfigException;
  10. use yii\db\ActiveQuery;
  11. use yii\db\StaleObjectException;
  12. class RoleService
  13. {
  14. /**
  15. * @param $id
  16. * @return BaseRole
  17. * @throws AjaxException
  18. */
  19. public static function getRoleById($id): BaseRole
  20. {
  21. /** @var BaseRole $BaseRole */
  22. $BaseRole = self::getQuery()->andWhere(["id" => $id])->one();
  23. if (!$BaseRole) {
  24. throw new AjaxException("该角色不存在!");
  25. }
  26. return $BaseRole;
  27. }
  28. /**
  29. * @return ActiveQuery
  30. */
  31. public static function getQuery(): ActiveQuery
  32. {
  33. return BaseRole::find();
  34. }
  35. /**
  36. * @return void
  37. * @throws AjaxException
  38. */
  39. protected static function validateRoleRules(): void
  40. {
  41. if (!isset($_GET['role_rules'])) {
  42. throw new AjaxException("权限为空!");
  43. }
  44. if (!is_array($_GET['role_rules'])) {
  45. throw new AjaxException("权限不合法!");
  46. }
  47. }
  48. /**
  49. * @param $name
  50. * @param $code
  51. * @param $status
  52. * @return void
  53. * @throws AjaxException
  54. */
  55. public static function add($name, $code, $status): void
  56. {
  57. //验证数据
  58. self::validateRoleRules();
  59. //角色查重
  60. $info = self::getQuery()->andWhere(['name' => $name])->one();
  61. if ($info) {
  62. throw new AjaxException("{$name}已存在!");
  63. }
  64. //插入
  65. $time = strtotime('now');
  66. $roleParams = [
  67. 'name' => $name,
  68. 'code' => $code,
  69. 'status' => $status,
  70. 'create_time' => date('Y-m-d H:i:s', $time),
  71. 'update_time' => date('Y-m-d H:i:s', $time),
  72. ];
  73. $insertInfo = new BaseRole($roleParams);
  74. $insertInfo->save();
  75. $newId = $insertInfo->id;
  76. // 插入权限
  77. self::saveClientAndServerRules($newId, $_GET["role_rules"]);
  78. }
  79. /**
  80. * @return void
  81. * @throws AjaxException
  82. * @throws InvalidConfigException
  83. */
  84. public static function update(): void
  85. {
  86. //验证数据
  87. self::validateRoleRules();
  88. //验证id
  89. if (!isset($_GET['id'])) {
  90. throw new AjaxException("id不存在!");
  91. }
  92. $roleId = $_GET["id"];
  93. $role = self::getRoleById($roleId);
  94. //角色查重
  95. $info = self::getQuery()
  96. ->andWhere(['name' => $_GET['name']])
  97. ->andWhere(["<>", "id", $roleId])
  98. ->one();
  99. if ($info) {
  100. throw new AjaxException("{$_GET['name']}已存在!");
  101. }
  102. //修改
  103. ActiveRecordService::getInstance()->setAttributeFromGetAndPost($role);
  104. if (!$role->save()) {
  105. throw new AjaxException("角色更新失败!");
  106. }
  107. // 删除之前的角色客户端权限
  108. BaseRoleMenu::deleteAll(['role_id' => $roleId]);
  109. // 插入权限
  110. self::saveClientAndServerRules($roleId, $_GET["role_rules"], true);
  111. }
  112. /**
  113. * @param $id
  114. * @return void
  115. * @throws StaleObjectException
  116. * @throws Throwable
  117. */
  118. public static function delete($id): void
  119. {
  120. $role = self::getRoleById($id);
  121. $role->delete();
  122. BaseRoleServerRules::deleteAll(['role_id' => $id]);
  123. BaseRoleClientRules::deleteAll(['role_id' => $id]);
  124. }
  125. /**
  126. * @param $id
  127. * @return string|null
  128. * @throws AjaxException
  129. */
  130. public static function getRoleNameById($id): ?string
  131. {
  132. $role = self::getRoleById($id);
  133. return $role->name;
  134. }
  135. /**
  136. * @param $roleId
  137. * @param $roleRules
  138. * @param bool $isUpdate
  139. * @return void
  140. */
  141. protected static function saveClientAndServerRules($roleId, $roleRules, bool $isUpdate = false): void
  142. {
  143. $menuUrlList = [];
  144. $serverUrlList = [];
  145. foreach ($roleRules as $oneRule) {
  146. //如果$oneRule不是以@开头,则放进服务器权限列表
  147. if (!str_starts_with($oneRule, "@")) {
  148. $serverUrlList[] = $oneRule;
  149. } else {
  150. $menuUrlList[] = $oneRule;
  151. }
  152. }
  153. $RoleServerRules = null;
  154. $RoleClientRules = null;
  155. if ($isUpdate) {
  156. $RoleServerRules = BaseRoleServerRules::findOne(["role_id" => $roleId]);
  157. $RoleClientRules = BaseRoleClientRules::findOne(["role_id" => $roleId]);
  158. }
  159. //保存服务端角色权限
  160. if (!$RoleServerRules) {
  161. $RoleServerRules = new BaseRoleServerRules();
  162. }
  163. $RoleServerRules->role_id = $roleId;
  164. $RoleServerRules->rules = json_encode(array_values($serverUrlList));
  165. $RoleServerRules->save();
  166. //保存客户端角色权限
  167. if (!$RoleClientRules) {
  168. $RoleClientRules = new BaseRoleClientRules();
  169. }
  170. $RoleClientRules->role_id = $roleId;
  171. $RoleClientRules->rules = json_encode(array_values($menuUrlList));
  172. $RoleClientRules->save();
  173. }
  174. }