RoleValidate.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\validate\auth;
  15. use app\common\validate\BaseValidate;
  16. use app\common\model\auth\{AdminRole, SystemRole, Admin};
  17. /**
  18. * 角色验证器
  19. * Class RoleValidate
  20. * @package app\adminapi\validate\auth
  21. */
  22. class RoleValidate extends BaseValidate
  23. {
  24. protected $rule = [
  25. 'id' => 'require|checkRole',
  26. 'name' => 'require|max:64|unique:' . SystemRole::class . ',name',
  27. 'menu_id' => 'array',
  28. ];
  29. protected $message = [
  30. 'id.require' => '请选择角色',
  31. 'name.require' => '请输入角色名称',
  32. 'name.max' => '角色名称最长为16个字符',
  33. 'name.unique' => '角色名称已存在',
  34. 'menu_id.array' => '权限格式错误'
  35. ];
  36. /**
  37. * @notes 添加场景
  38. * @return RoleValidate
  39. * @author 段誉
  40. * @date 2021/12/29 15:47
  41. */
  42. public function sceneAdd()
  43. {
  44. return $this->only(['name', 'menu_id']);
  45. }
  46. /**
  47. * @notes 详情场景
  48. * @return RoleValidate
  49. * @author 段誉
  50. * @date 2021/12/29 15:47
  51. */
  52. public function sceneDetail()
  53. {
  54. return $this->only(['id']);
  55. }
  56. /**
  57. * @notes 删除场景
  58. * @return RoleValidate
  59. * @author 段誉
  60. * @date 2021/12/29 15:48
  61. */
  62. public function sceneDel()
  63. {
  64. return $this->only(['id'])
  65. ->append('id', 'checkAdmin');
  66. }
  67. /**
  68. * @notes 验证角色是否存在
  69. * @param $value
  70. * @param $rule
  71. * @param $data
  72. * @return bool|string
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. * @author 段誉
  77. * @date 2021/12/29 15:48
  78. */
  79. public function checkRole($value, $rule, $data)
  80. {
  81. if (!SystemRole::find($value)) {
  82. return '角色不存在';
  83. }
  84. return true;
  85. }
  86. /**
  87. * @notes 验证角色是否被使用
  88. * @param $value
  89. * @param $rule
  90. * @param $data
  91. * @return bool|string
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. * @author 段誉
  96. * @date 2021/12/29 15:49
  97. */
  98. public function checkAdmin($value, $rule, $data)
  99. {
  100. if (AdminRole::where(['role_id' => $value])->find()) {
  101. return '有管理员在使用该角色,不允许删除';
  102. }
  103. return true;
  104. }
  105. }