企微短剧业务系统

RoleService.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. <?php
  2. /**
  3. * Created by:PhpStorm
  4. * Author:chenzhiyuan
  5. * Date: 2022/4/19
  6. * Time: 1:41 下午
  7. */
  8. namespace App\Service\Admin;
  9. use App\Log;
  10. use App\Models\System\AdminManageRole;
  11. use App\Models\System\Permission;
  12. use App\Models\System\Role;
  13. use App\Models\System\RolePermission;
  14. use App\Models\System\Users;
  15. use App\Support\EmailQueue;
  16. use Illuminate\Support\Facades\DB;
  17. class RoleService
  18. {
  19. /**
  20. * 获取用户可以操作的权限集
  21. * @param $adminId
  22. * @param $sysGroupId
  23. * @param array $permissionIds
  24. * @return array|\Illuminate\Support\Collection
  25. */
  26. public static function getPermissionIdsOfUser($adminId, $sysGroupId, $permissionIds=[])
  27. {
  28. try {
  29. # 获取当前用户的角色信息
  30. $roleIds = AdminManageRole::select('role_id')->where('sys_user_id', $adminId)
  31. ->where('is_delete', 0)->distinct()->pluck('role_id');
  32. if(empty($roleIds))
  33. return [];
  34. # 判断当前用户是否有超级权限
  35. $isAdminAuth = Role::whereIn('id', $roleIds)->where('role_type', 10)->exists();
  36. if($isAdminAuth) { # 拥有超级权限,获取主账号所属的角色信息
  37. if(empty($sysGroupId)) {
  38. $sysGroupId = Users::query()->where('id', $adminId)->value('group_admin_id');
  39. }
  40. $roleIds = AdminManageRole::select('role_id')->where('sys_user_id', $sysGroupId)
  41. ->where('is_delete', 0)->distinct()->pluck('role_id');
  42. }
  43. $permissionIds = RolePermission::select('permission_id')->whereIn('role_id', $roleIds)
  44. ->where(function($query) use($permissionIds) {
  45. if(!empty($permissionIds)) $query->whereIn('permission_id', $permissionIds);
  46. })
  47. ->where('is_delete', 0)->distinct()->pluck('permission_id');
  48. } catch (\Exception $e) {
  49. Log::logError('获取用户可以操作的权限集异常', [
  50. 'line' => $e->getLine(),
  51. 'msg' => $e->getMessage(),
  52. 'admin_id' => $adminId
  53. ], 'GetPermissionIdsOfUser');
  54. return [];
  55. }
  56. return $permissionIds;
  57. }
  58. /**
  59. * 判断用户所操作的权限集是否合法
  60. * @param $permissionIds mixed 用户拥有的权限集
  61. * @param $permissionInput array 用户当前操作的权限集
  62. * @return bool
  63. * */
  64. public static function checkPermission($permissionIds, $permissionInput)
  65. {
  66. if(empty($permissionIds)) return false;
  67. if(!is_array($permissionIds)) $permissionIds = $permissionIds->toArray();
  68. foreach ($permissionInput as $id) {
  69. if(!in_array($id, $permissionIds)) return false;
  70. }
  71. return true;
  72. }
  73. /**
  74. * 添加角色,并赋予角色管理的权限
  75. * @param $viewType
  76. * @param $adminId
  77. * @param $sysGroupId
  78. * @param $isSystemAdmin
  79. * @param $name
  80. * @param $desc
  81. * @param $roleType
  82. * @param $permissionIdArr
  83. * @return integer
  84. */
  85. public static function create_role($viewType,$adminId,$sysGroupId,$isSystemAdmin,$name,$desc,$roleType,$permissionIdArr = [])
  86. {
  87. try {
  88. if(!$isSystemAdmin && ($adminId != $sysGroupId)) {
  89. # 获取当前登录用户可操作的权限
  90. $permissionIds = RoleService::getPermissionIdsOfUser($adminId, $sysGroupId);
  91. # 校验当下操作的权限集是否合法
  92. $check = RoleService::checkPermission($permissionIds, $permissionIdArr);
  93. if(!$check) return 4506;
  94. }
  95. DB::begintransaction();
  96. # 新增角色
  97. $roleId = Role::query()->create([
  98. 'view_type' => $viewType,
  99. 'role_type' => $roleType,
  100. 'name' => $name,
  101. 'admin_id' => $adminId,
  102. 'sys_group_id' => $isSystemAdmin ? $adminId : $sysGroupId,
  103. 'desc' => $desc,
  104. ])->id;
  105. if(!$roleId) {
  106. Log::logError('Role表写入数据失败', [
  107. 'view_type' => $viewType,
  108. 'admin_id' => $adminId,
  109. 'sys_group_id' => $sysGroupId,
  110. 'name' => $name,
  111. 'desc' => $desc,
  112. 'permission_ids' => $permissionIdArr
  113. ], 'CreateRole');
  114. return 4502;
  115. }
  116. # 获取可分配权限的侧边栏信息
  117. $permissionIdViewType = Permission::query()->where("is_delete",0)
  118. ->whereIn("id",$permissionIdArr)
  119. ->pluck("view_type","id")
  120. ->toArray();
  121. # 角色权限绑定
  122. foreach ($permissionIdArr as $permissionId){
  123. RolePermission::query()->updateOrInsert(
  124. [
  125. 'role_id' => $roleId,
  126. 'permission_id' => $permissionId
  127. ],
  128. [
  129. 'is_delete' => 0,
  130. 'view_type' => $permissionIdViewType[$permissionId],
  131. ]
  132. );
  133. }
  134. DB::commit();
  135. } catch (\Exception $e) {
  136. DB::rollBack();
  137. Log::logError('角色创建过程发生异常', [
  138. 'line' => $e->getLine(),
  139. 'msg' => $e->getMessage(),
  140. 'param' => [
  141. 'view_type' => $viewType,
  142. 'admin_id' => $adminId,
  143. 'sys_group_id' => $sysGroupId,
  144. 'name' => $name,
  145. 'desc' => $desc,
  146. 'permission_ids' => $permissionIdArr
  147. ]
  148. ], 'CreateRole');
  149. return 4501;
  150. }
  151. return 0;
  152. }
  153. /**
  154. * 删除角色
  155. * @param $roleId
  156. * @param $adminId
  157. * @param $sysGroupId
  158. * @param $isSystemAdmin
  159. * @return bool
  160. */
  161. public static function del_role($roleId,$adminId,$sysGroupId,$isSystemAdmin)
  162. {
  163. try {
  164. $roleInfo = Role::query()->where("id",$roleId)
  165. ->where(function($query) use($isSystemAdmin, $sysGroupId, $adminId) {
  166. if($isSystemAdmin) {
  167. $query->where('sys_group_id', $adminId);
  168. } else {
  169. $query->where('sys_group_id', $sysGroupId);
  170. }
  171. })
  172. ->where("system_role",0)
  173. ->where("is_delete",0)
  174. ->first();
  175. if(empty($roleInfo)) return 4505;
  176. # 判断是否有成员属于该角色
  177. $isUsed = AdminManageRole::where('role_id', $roleId)->where('is_delete', 0)->count();
  178. if($isUsed) return 4509;
  179. DB::begintransaction();
  180. //删除角色
  181. $result = Role::query()->where("id",$roleId)->update(['is_delete'=>1]);
  182. if(!$result) {
  183. DB::rollBack();
  184. return 4507;
  185. }
  186. $permissionCount = RolePermission::where("role_id",$roleId)->where('is_delete', 0)->count();
  187. if($permissionCount) {
  188. //删除角色以及权限控制
  189. $result = RolePermission::query()->where("role_id",$roleId)->update(['is_delete'=>1]);
  190. if(!$result) {
  191. DB::rollBack();
  192. return 4508;
  193. }
  194. }
  195. DB::commit();
  196. } catch (\Exception $e) {
  197. DB::rollBack();
  198. Log::logError('角色删除过程发生异常', [
  199. 'line' => $e->getLine(),
  200. 'msg' => $e->getMessage(),
  201. 'param' => [
  202. 'role_id' => $roleId,
  203. 'admin_id' => $adminId,
  204. 'sys_group_id' => $sysGroupId,
  205. ]
  206. ], 'DelRole');
  207. return 4501;
  208. }
  209. return 0;
  210. }
  211. /***
  212. * 编辑角色
  213. * @param $viewType
  214. * @param $adminId
  215. * @param $sysGroupId
  216. * @param $isSystemAdmin
  217. * @param $roleId
  218. * @param $name
  219. * @param $desc
  220. * @param $roleType
  221. * @param array $permissionIdArr
  222. * @return bool
  223. */
  224. public static function edit_role($viewType,$adminId,$sysGroupId,$isSystemAdmin,$roleId,$name,$desc,$roleType,$permissionIdArr = [])
  225. {
  226. try {
  227. if(!$isSystemAdmin && ($adminId != $sysGroupId)) {
  228. # 获取当前登录用户可操作的权限
  229. $permissionIds = RoleService::getPermissionIdsOfUser($adminId, $sysGroupId);
  230. # 校验当下操作的权限集是否合法
  231. $check = RoleService::checkPermission($permissionIds, $permissionIdArr);
  232. if(!$check) return 4506;
  233. }
  234. /**检查角色存在***/
  235. $roleInfo = Role::query()->where("id",$roleId)
  236. ->where("view_type",$viewType)
  237. ->where("is_delete",0)->first();
  238. if(empty($roleInfo)) {
  239. return 4505;
  240. }
  241. // Log::logInfo('获取角色信息完成', [$roleId], 'EditRoleDebug');
  242. DB::begintransaction();
  243. /**编辑角色**/
  244. Role::query()->where("id",$roleId)
  245. ->where("is_delete",0)
  246. ->update([
  247. 'role_type' => $roleType,
  248. 'admin_id' => $adminId,
  249. 'sys_group_id' => $isSystemAdmin ? $adminId : $sysGroupId,
  250. 'name' => $name,
  251. 'desc' => $desc,
  252. ]);
  253. // Log::logInfo('编辑角色信息完成', [$roleId], 'EditRoleDebug');
  254. $permissionIdViewType = Permission::query()->where("is_delete",0)
  255. ->whereIn("id",$permissionIdArr)
  256. ->pluck("view_type","id")
  257. ->toArray();
  258. // Log::logInfo('获取编辑后角色对应权限信息完成', [$roleId], 'EditRoleDebug');
  259. // 获取本次被删除的权限
  260. $nowRolePermissionList = RolePermission::query()->select('permission_id')
  261. ->where('role_id', $roleId)->where('is_delete', 0)
  262. ->pluck('permission_id')->toArray();
  263. $disablePermissionList = array_diff($nowRolePermissionList, $permissionIdArr);
  264. $disablePermissionList = array_values($disablePermissionList);
  265. // Log::logInfo('获取所有权限列表并找到已删除的权限', [$roleId], 'EditRoleDebug');
  266. /**角色权限相绑定**/
  267. foreach ($permissionIdArr as $permissionId){
  268. if(!isset($permissionIdViewType[$permissionId])){ //权限被删除
  269. continue;
  270. }
  271. RolePermission::query()->updateOrInsert(
  272. [
  273. 'role_id' => $roleId,
  274. 'permission_id' => $permissionId
  275. ],
  276. [
  277. 'is_delete' => 0,
  278. 'view_type' => $permissionIdViewType[$permissionId],
  279. ]
  280. );
  281. }
  282. // Log::logInfo('新增权限处理完成', [$roleId], 'EditRoleDebug');
  283. if(!empty($disablePermissionList)) {
  284. // 删除当前角色的绑定关系
  285. // RolePermission::query()->where('role_id', $roleId)
  286. // ->whereIn('permission_id', $disablePermissionList)->update(['is_delete' => 1]);
  287. // Log::logInfo('当前角色已删除权限处理完成,开始循环处理当前角色创建角色账号', [$roleId], 'EditRoleDebug');
  288. $alreadyDealRoleIdList = [];
  289. self::dealRole($roleId, $disablePermissionList, $alreadyDealRoleIdList);
  290. $res = RolePermission::query()->whereIn('role_id', $alreadyDealRoleIdList)
  291. ->whereIn('permission_id', $disablePermissionList)->update(['is_delete' => 1]);
  292. if(!$res) {
  293. DB::rollBack();
  294. return 4504;
  295. }
  296. }
  297. DB::commit();
  298. } catch(\Exception $e) {
  299. DB::rollBack();
  300. Log::logError('角色编辑过程发生异常', [
  301. 'line' => $e->getLine(),
  302. 'msg' => $e->getMessage(),
  303. 'param' => [
  304. 'view_type' => $viewType,
  305. 'admin_id' => $adminId,
  306. 'sys_group_id' => $sysGroupId,
  307. 'name' => $name,
  308. 'desc' => $desc,
  309. 'permission_ids' => $permissionIdArr
  310. ]
  311. ], 'EditRole');
  312. return 4504;
  313. }
  314. return 0;
  315. }
  316. /*
  317. * 1.将起始操作的角色权限绑定关系解除
  318. * 2.查询起始操作角色关联的用户列表
  319. * 3.依次查询关联用户是否仍拥有要解除关系的权限
  320. * 4.若用户仍然拥有要删除的权限,则由该用户创建的角色权限无须处理
  321. * 5.若用户完全失去要删除的权限,那么由该用户创建的角色也将不能用户该权限
  322. * 6.解除用户创建的角色与要删除权限之间的关联关系,并依次判断创建的角色是否需要进行下一步处理(回到第一步)
  323. * */
  324. public static function dealRole($roleId, $disablePermissionList, &$alreadyDealRoleIdList = [])
  325. {
  326. if(in_array($roleId, $alreadyDealRoleIdList)) {
  327. return true;
  328. } else {
  329. $alreadyDealRoleIdList[] = $roleId;
  330. }
  331. // 查询当前操作角色关联的用户
  332. $userIdList = AdminManageRole::query()->where('role_id', $roleId)->where('is_delete', 0)
  333. ->pluck('sys_user_id')->toArray();
  334. // Log::logInfo('查询当前操作角色关联的用户', [
  335. // 'role_id' => $roleId,
  336. // 'user_id_list' => $userIdList,
  337. // 'already' => $alreadyDealRoleIdList
  338. // ], 'EditRoleDebug');
  339. if(empty($userIdList)) return true;
  340. foreach($userIdList as $userId) {
  341. // 查询用户是否还有该权限
  342. $enablePermissionIdList = self::getPermissionIdsOfUser($userId, null, []);
  343. // Log::logInfo('查询用户是否还有该权限', ['role_id' => $roleId, 'user_id' => $userId], 'EditRoleDebug');
  344. $diffPermissionIdList = array_intersect($disablePermissionList, $enablePermissionIdList->toArray());
  345. // 当前用户没有该权限,则继续查找用户创建的角色列表,并将创建的角色依次解除绑定后继续判断创建角色关联的用户权限
  346. if(!empty($diffPermissionIdList)) {
  347. // 判断要解除绑定的权限集与求差集之后的权限集是否完全一致
  348. if(array_diff($diffPermissionIdList, $disablePermissionList) ||
  349. array_diff($disablePermissionList, $diffPermissionIdList)) {
  350. Log::logError('角色编辑过程发生异常-权限集不一致', [
  351. 'role_id' => $roleId,
  352. 'operate_user_id' => $userId,
  353. 'disable_permission' => $disablePermissionList,
  354. 'diff_permission' => $diffPermissionIdList,
  355. 'enable_permission' => $enablePermissionIdList->toArray()
  356. ], 'EditRoleDebug');
  357. EmailQueue::rPush('角色编辑过程发生异常-权限集不一致', json_encode([
  358. 'role_id' => $roleId,
  359. 'operate_user_id' => $userId,
  360. 'disable_permission' => $disablePermissionList,
  361. 'diff_permission' => $diffPermissionIdList,
  362. 'enable_permission' => $enablePermissionIdList->toArray()
  363. ], 1), ['song.shen@kuxuan-inc.com'], '猎羽');
  364. // return false;
  365. }
  366. // 查找用户创建的角色列表
  367. $roleIdList = Role::query()->where('admin_id', $userId)->where('is_delete', 0)
  368. ->pluck('id')->toArray();
  369. // Log::logInfo('查找用户创建的角色列表', [
  370. // 'user_id' => $userId,
  371. // 'user_role_id_list' => $roleIdList,
  372. // 'already' => $alreadyDealRoleIdList
  373. // ], 'EditRoleDebug');
  374. foreach($roleIdList as $role) {
  375. if(!in_array($role, $alreadyDealRoleIdList)) {
  376. // RolePermission::query()->where('role_id', $role)
  377. // ->whereIn('permission_id', $disablePermissionList)->update(['is_delete' => 1]);
  378. // $alreadyDealRoleIdList[] = $role;
  379. // Log::logInfo('当前角色已删除权限处理完成', [
  380. // 'role_id' => $role,
  381. // 'already' => $alreadyDealRoleIdList
  382. // ], 'EditRoleDebug');
  383. self::dealRole($role, $disablePermissionList, $alreadyDealRoleIdList);
  384. }
  385. }
  386. }
  387. }
  388. return true;
  389. }
  390. /**
  391. * 权限类型
  392. * @param $adminId
  393. * @param $sysGroupId
  394. * @param $isSystemAdmin
  395. * @param $errno
  396. * @return array
  397. */
  398. public static function role_type($adminId, $sysGroupId, $isSystemAdmin, &$errno)
  399. {
  400. $roleTypeIds = [10, 20, 30];
  401. $roleTypeList = [10 => '超级权限', 20 => '管理权限', 30 => '普通权限'];
  402. $userRoleType = 0;
  403. if(!$isSystemAdmin && ($adminId != $sysGroupId)) {
  404. # 获取当前登录用户的角色权限
  405. $userRoleIds = AdminManageRole::select(['role_id'])->where('sys_user_id', $adminId)->where('is_delete', 0)->pluck('role_id');
  406. if(empty($userRoleIds)) {
  407. $errno = 4506;
  408. return [];
  409. }
  410. $userRoleType = Role::whereIn('id', $userRoleIds)->where('is_delete', 0)->min('role_type');
  411. if(!$userRoleType) {
  412. $errno = 4506;
  413. return [];
  414. }
  415. }
  416. $roleTypeIds = array_filter($roleTypeIds, function($v) use($userRoleType) {
  417. return $v >= $userRoleType;
  418. });
  419. $data = [];
  420. foreach ($roleTypeIds as $roleType) {
  421. if(!isset($roleTypeList[$roleType])) continue;
  422. $data[$roleType] = $roleTypeList[$roleType];
  423. }
  424. return $data;
  425. }
  426. /**
  427. * 权限列表
  428. * @param $viewType
  429. * @param $adminId
  430. * @param $sysGroupId
  431. * @param $isSystemAdmin
  432. * @param $page
  433. * @param $page_limit
  434. * @param $errno
  435. * @return array
  436. */
  437. public static function role_list($viewType,$adminId,$sysGroupId,$isSystemAdmin,$page,$page_limit, &$errno)
  438. {
  439. $userRoleType = 0;
  440. if(!$isSystemAdmin && ($adminId != $sysGroupId)) {
  441. # 获取当前登录用户的角色权限
  442. $userRoleIds = AdminManageRole::select(['role_id'])->where('sys_user_id', $adminId)->where('is_delete', 0)->pluck('role_id');
  443. if(empty($userRoleIds)) return [];
  444. $userRoleType = Role::whereIn('id', $userRoleIds)->where('is_delete', 0)->min('role_type');
  445. if(!$userRoleType) return [];
  446. }
  447. $query = Role::query()
  448. ->where(function($query) use($isSystemAdmin, $sysGroupId, $adminId) {
  449. if($isSystemAdmin) {
  450. $query->where('sys_group_id', $adminId);
  451. } else {
  452. $query->where('sys_group_id', $sysGroupId);
  453. }
  454. })
  455. ->where("is_delete",0)->where('role_type', '>=', $userRoleType);
  456. $total = $query->count();
  457. $list = $query->select("id","name","desc","system_role","created_at", "role_type")
  458. ->offset(($page-1)*$page_limit)
  459. ->limit($page_limit)
  460. ->get()->toArray();
  461. foreach ($list as $k=>$item){
  462. $canEdit = 0;
  463. if($userRoleType == 0) {
  464. $canEdit = 1;
  465. }
  466. $list[$k]['can_edit'] = $canEdit;
  467. $list[$k]['is_system'] = $item['system_role'] == 0 ? 0 : 1 ; //是否是系统用户
  468. unset($list[$k]['system_role']);
  469. }
  470. return [$total,$list];
  471. }
  472. /**
  473. * 权限详情
  474. * @param $roleId
  475. * @param $adminId
  476. * @param $sysGroupId
  477. * @param $isSystemAdmin
  478. * @param $errno
  479. * @return array
  480. */
  481. public static function role_info($roleId,$adminId,$sysGroupId,$isSystemAdmin,&$errno)
  482. {
  483. $role_info = Role::query()->where("id",$roleId)
  484. ->where("is_delete",0)
  485. ->where(function($query) use($isSystemAdmin, $sysGroupId, $adminId) {
  486. if($isSystemAdmin) {
  487. $query->where('sys_group_id', $adminId);
  488. } else {
  489. $query->where('sys_group_id', $sysGroupId);
  490. }
  491. })
  492. ->select("id","name","desc","system_role", "role_type")
  493. ->first();
  494. if (empty($role_info)) {
  495. $errno = 4505;
  496. return [];
  497. }
  498. /**权限角色拥有的权限集**/
  499. $permissionIdArr = RolePermission::query()->where("role_id",$roleId)
  500. ->where("is_delete",0)
  501. ->pluck("permission_id")->toArray();
  502. /**补充权限选中**/
  503. $role_info->is_system = $role_info->system_role == 0 ? 0 : 1 ; //是否是系统角色
  504. $role_info->checked_permission = $permissionIdArr;
  505. unset($role_info->system_role); //删除系统角色标识
  506. return $role_info->toArray();
  507. }
  508. }