RoleController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Dcat\Admin\Controllers;
  3. use Dcat\Admin\Auth\Permission;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Layout\Content;
  7. use Dcat\Admin\Models\Repositories\Role;
  8. use Dcat\Admin\Models\Role as RoleModel;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\SimpleGrid;
  11. use Dcat\Admin\Support\Helper;
  12. use Illuminate\Routing\Controller;
  13. class RoleController extends Controller
  14. {
  15. use HasResourceActions {
  16. destroy as delete;
  17. }
  18. /**
  19. * Index interface.
  20. *
  21. * @param Content $content
  22. *
  23. * @return Content
  24. */
  25. public function index(Content $content)
  26. {
  27. return $content
  28. ->title(trans('admin.roles'))
  29. ->description(trans('admin.list'))
  30. ->body($this->grid());
  31. }
  32. /**
  33. * Show interface.
  34. *
  35. * @param mixed $id
  36. * @param Content $content
  37. *
  38. * @return Content
  39. */
  40. public function show($id, Content $content)
  41. {
  42. return $content
  43. ->title(trans('admin.roles'))
  44. ->description(trans('admin.detail'))
  45. ->body($this->detail($id));
  46. }
  47. /**
  48. * Edit interface.
  49. *
  50. * @param mixed $id
  51. * @param Content $content
  52. *
  53. * @return Content
  54. */
  55. public function edit($id, Content $content)
  56. {
  57. return $content
  58. ->title(trans('admin.roles'))
  59. ->description(trans('admin.edit'))
  60. ->body($this->form()->edit($id));
  61. }
  62. /**
  63. * Create interface.
  64. *
  65. * @param Content $content
  66. *
  67. * @return Content
  68. */
  69. public function create(Content $content)
  70. {
  71. return $content
  72. ->title(trans('admin.roles'))
  73. ->description(trans('admin.create'))
  74. ->body($this->form());
  75. }
  76. /**
  77. * Make a grid builder.
  78. *
  79. * @return Grid
  80. */
  81. protected function grid()
  82. {
  83. if ($mini = request(SimpleGrid::QUERY_NAME)) {
  84. $grid = new SimpleGrid(new Role());
  85. } else {
  86. $grid = new Grid(new Role());
  87. }
  88. $grid->id('ID')->bold()->sortable();
  89. $grid->slug->label('primary');
  90. $grid->name;
  91. if (! $mini) {
  92. $grid->created_at;
  93. $grid->updated_at->sortable();
  94. }
  95. $grid->disableBatchDelete();
  96. $grid->disableEditButton();
  97. $grid->showQuickEditButton();
  98. $grid->disableFilterButton();
  99. $grid->quickSearch(['id', 'name', 'slug']);
  100. $grid->createMode(Grid::CREATE_MODE_DIALOG);
  101. $grid->actions(function (Grid\Displayers\Actions $actions) {
  102. $roleModel = config('admin.database.roles_model');
  103. if ($roleModel::isAdministrator($actions->row->slug)) {
  104. $actions->disableDelete();
  105. }
  106. });
  107. return $grid;
  108. }
  109. /**
  110. * Make a show builder.
  111. *
  112. * @param mixed $id
  113. *
  114. * @return Show
  115. */
  116. protected function detail($id)
  117. {
  118. return Show::make(new Role('permissions'), function (Show $show) use ($id) {
  119. $show->id;
  120. $show->slug;
  121. $show->name;
  122. $show->permissions->width(12)->as(function ($permission) {
  123. return collect($permission)->pluck('name');
  124. })->label('primary');
  125. $show->divider();
  126. $show->created_at;
  127. $show->updated_at;
  128. if ($id == RoleModel::ADMINISTRATOR_ID) {
  129. $show->disableDeleteButton();
  130. }
  131. })->key($id);
  132. }
  133. /**
  134. * Make a form builder.
  135. *
  136. * @return Form
  137. */
  138. public function form()
  139. {
  140. return Form::make(new Role('permissions'), function (Form $form) {
  141. $roleTable = config('admin.database.roles_table');
  142. $connection = config('admin.database.connection');
  143. $id = $form->key();
  144. $form->display('id', 'ID');
  145. $form->text('slug', trans('admin.slug'))
  146. ->required()
  147. ->creationRules(['required', "unique:{$connection}.{$roleTable}"])
  148. ->updateRules(['required', "unique:{$connection}.{$roleTable},slug,$id"]);
  149. $form->text('name', trans('admin.name'))->required();
  150. $form->tree('permissions')
  151. ->nodes(function () {
  152. $permissionModel = config('admin.database.permissions_model');
  153. $permissionModel = new $permissionModel();
  154. return $permissionModel->allNodes();
  155. })
  156. ->customFormat(function ($v) {
  157. if (! $v) {
  158. return [];
  159. }
  160. return array_column($v, 'id');
  161. });
  162. $form->display('created_at', trans('admin.created_at'));
  163. $form->display('updated_at', trans('admin.updated_at'));
  164. if ($id == RoleModel::ADMINISTRATOR_ID) {
  165. $form->disableDeleteButton();
  166. }
  167. });
  168. }
  169. /**
  170. * Remove the specified resource from storage.
  171. *
  172. * @param int $id
  173. *
  174. * @return \Illuminate\Http\Response
  175. */
  176. public function destroy($id)
  177. {
  178. if (in_array(RoleModel::ADMINISTRATOR_ID, Helper::array($id))) {
  179. Permission::error();
  180. }
  181. return $this->delete($id);
  182. }
  183. }