Permissions.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. declare(strict_types=1);
  3. namespace Modules\Permissions\Models;
  4. use Catch\Base\CatchModel as Model;
  5. use Catch\CatchAdmin;
  6. use Catch\Enums\Status;
  7. use Illuminate\Database\Eloquent\Casts\Attribute;
  8. use Illuminate\Database\Eloquent\Relations\HasMany;
  9. use Modules\Permissions\Enums\MenuStatus;
  10. use Modules\Permissions\Enums\MenuType;
  11. /**
  12. * @property $id
  13. * @property $parent_id
  14. * @property $permission_name
  15. * @property $route
  16. * @property $icon
  17. * @property $module
  18. * @property $permission_mark
  19. * @property $component
  20. * @property $redirect
  21. * @property $keepalive
  22. * @property $type
  23. * @property $hidden
  24. * @property $sort
  25. * @property $active_menu
  26. * @property $creator_id
  27. * @property $created_at
  28. * @property $updated_at
  29. * @property $deleted_at
  30. */
  31. class Permissions extends Model
  32. {
  33. protected $table = 'permissions';
  34. protected $fillable = ['id', 'parent_id', 'permission_name', 'route', 'icon', 'module', 'permission_mark', 'component', 'redirect', 'keepalive', 'type', 'hidden', 'active_menu', 'sort', 'creator_id', 'created_at', 'updated_at', 'deleted_at'];
  35. /**
  36. * @var array
  37. */
  38. protected array $fields = ['id','parent_id','permission_name','route','icon','module','permission_mark','component','redirect','keepalive','type','hidden','active_menu','sort','created_at','updated_at'];
  39. protected bool $isPaginate = false;
  40. /**
  41. * @var array
  42. */
  43. protected array $form = ['parent_id','permission_name','route','icon','module','permission_mark','component','redirect','keepalive','type','active_menu', 'hidden','sort'];
  44. /**
  45. * @var array
  46. */
  47. public array $searchable = [
  48. 'permission_name' => 'like',
  49. 'role_id' => '='
  50. ];
  51. protected $hidden = ['pivot'];
  52. /**
  53. * default permission actions
  54. *
  55. * @var array|string[]
  56. */
  57. protected array $defaultActions = [
  58. 'index' => '列表',
  59. 'store' => '新增',
  60. 'show' => '读取',
  61. 'update' => '更新',
  62. 'destroy' => '删除',
  63. 'enable' => '禁用/启用',
  64. 'import' => '导入',
  65. 'export' => '导出',
  66. ];
  67. /**
  68. * @var bool
  69. */
  70. protected bool $asTree = true;
  71. /**
  72. * @var string[]
  73. */
  74. protected $casts = [
  75. 'type' => MenuType::class,
  76. 'status' => MenuStatus::class
  77. ];
  78. /**
  79. * is inner
  80. *
  81. * @return Attribute
  82. */
  83. public function isInner(): Attribute
  84. {
  85. return Attribute::make(
  86. get: fn($value) => $value == 1
  87. );
  88. }
  89. /**
  90. * is hidden
  91. *
  92. * @return bool
  93. */
  94. public function isHidden(): bool
  95. {
  96. return $this->hidden === Status::Disable;
  97. }
  98. /**
  99. * action type
  100. *
  101. * @return bool
  102. */
  103. public function isAction(): bool
  104. {
  105. return $this->type == MenuType::Action;
  106. }
  107. /**
  108. * is top menu
  109. *
  110. * @return bool
  111. */
  112. public function isTopMenu(): bool
  113. {
  114. return $this->type == MenuType::Top;
  115. }
  116. /**
  117. * is menu
  118. *
  119. * @return bool
  120. */
  121. public function isMenu(): bool
  122. {
  123. return $this->type == MenuType::Menu;
  124. }
  125. /**
  126. * actions
  127. *
  128. * @return HasMany
  129. */
  130. public function actions(): HasMany
  131. {
  132. return $this->hasMany(self::class, 'parent_id', 'id')->where('type', MenuType::Action);
  133. }
  134. /**
  135. *
  136. * @param array $data
  137. * @return bool
  138. * @throws \ReflectionException
  139. */
  140. public function storeBy(array $data): bool
  141. {
  142. if ($data['actions'] ?? false) {
  143. /* @var static $parentMenu */
  144. $parentMenu = $this->firstBy(value: $data['parent_id'], field: 'id');
  145. if (! $parentMenu->isMenu()) {
  146. return false;
  147. }
  148. $actions = CatchAdmin::getControllerActions($parentMenu->module, $parentMenu->permission_mark);
  149. foreach ($actions as $k => $action) {
  150. if (! isset($this->defaultActions[$action])) {
  151. continue;
  152. }
  153. $this->addAction($this->newInstance([
  154. 'type' => MenuType::Action->value(),
  155. 'parent_id' => $data['parent_id'],
  156. 'permission_name' => $this->defaultActions[$action],
  157. 'permission_mark' => $action,
  158. 'sort' => $k + 1
  159. ]), $parentMenu);
  160. }
  161. return true;
  162. }
  163. $model = $this->fill($data);
  164. if ($model->isAction()) {
  165. $parentMenu = $this->firstBy($model->parent_id, 'id');
  166. return $this->addAction($model, $parentMenu);
  167. }
  168. if ($model->isTopMenu()) {
  169. $data['route'] = '/'.trim($data['route'], '/');
  170. }
  171. if (parent::storeBy($data)) {
  172. return true;
  173. }
  174. return false;
  175. }
  176. /**
  177. * add action
  178. *
  179. * @param $model
  180. * @param Permissions $parent
  181. * @return mixed
  182. */
  183. protected function addAction($model, mixed $parent): mixed
  184. {
  185. $model->setAttribute('module', $parent->module);
  186. $model->setAttribute('permission_mark', $parent->permission_mark. '@'. $model->permission_mark);
  187. $model->setAttribute('route', '');
  188. $model->setAttribute('icon', '');
  189. $model->setAttribute('component', '');
  190. $model->setAttribute('redirect', '');
  191. if ($this->where('module', $model->getAttribute('module'))->where('permission_mark', $model->getAttribute('permission_mark'))->first()) {
  192. return false;
  193. }
  194. return $model->setCreatorId()->save();
  195. }
  196. /**
  197. * update data
  198. *
  199. * @param $id
  200. * @param array $data
  201. * @return mixed
  202. */
  203. public function updateBy($id, array $data): mixed
  204. {
  205. $model = $this->fill($data);
  206. if ($model->isAction()) {
  207. /* @var Permissions $parentMenu */
  208. $parentMenu = $this->firstBy($model->parent_id, 'id');
  209. $data['permission_mark'] = $parentMenu->permission_mark.'@'.$data['permission_mark'];
  210. }
  211. return parent::updateBy($id, $data);
  212. }
  213. }