UserController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace Dcat\Admin\Controllers;
  3. use Dcat\Admin\Models\Repositories\Administrator;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Layout\Content;
  7. use Dcat\Admin\MiniGrid;
  8. use Dcat\Admin\Show;
  9. use Dcat\Admin\Widgets\Tree;
  10. use Illuminate\Routing\Controller;
  11. class UserController extends Controller
  12. {
  13. use HasResourceActions;
  14. /**
  15. * Index interface.
  16. *
  17. * @return Content
  18. */
  19. public function index(Content $content)
  20. {
  21. if (request('_mini')) {
  22. return $content->body($this->miniGrid());
  23. }
  24. return $content
  25. ->header(trans('admin.administrator'))
  26. ->description(trans('admin.list'))
  27. ->body($this->grid());
  28. }
  29. /**
  30. * Show interface.
  31. *
  32. * @param mixed $id
  33. * @param Content $content
  34. *
  35. * @return Content
  36. */
  37. public function show($id, Content $content)
  38. {
  39. return $content
  40. ->header(trans('admin.administrator'))
  41. ->description(trans('admin.detail'))
  42. ->body($this->detail($id));
  43. }
  44. /**
  45. * Edit interface.
  46. *
  47. * @param $id
  48. *
  49. * @return Content
  50. */
  51. public function edit($id, Content $content)
  52. {
  53. return $content
  54. ->header(trans('admin.administrator'))
  55. ->description(trans('admin.edit'))
  56. ->body($this->form($id)->edit($id));
  57. }
  58. /**
  59. * Create interface.
  60. *
  61. * @return Content
  62. */
  63. public function create(Content $content)
  64. {
  65. return $content
  66. ->header(trans('admin.administrator'))
  67. ->description(trans('admin.create'))
  68. ->body($this->form());
  69. }
  70. /**
  71. * Make a grid builder.
  72. *
  73. * @return Grid
  74. */
  75. protected function grid()
  76. {
  77. $grid = new Grid(new Administrator());
  78. $grid->disableBatchDelete();
  79. $grid->model()->with('roles');
  80. $grid->id->bold()->sortable();
  81. $grid->username;
  82. $grid->name;
  83. $grid->roles->pluck('name')->label('primary');
  84. $permissionModel = config('admin.database.permissions_model');
  85. $roleModel = config('admin.database.roles_model');
  86. $nodes = (new $permissionModel)->allNodes();
  87. $grid->permissions->display(function ($v, $column) use (&$nodes, $roleModel) {
  88. if (empty($this->roles)) {
  89. return;
  90. }
  91. return $column->tree(function (Grid\Displayers\Tree $tree) use (&$nodes, $roleModel) {
  92. $tree->nodes($nodes);
  93. foreach (array_column($this->roles, 'slug') as $slug) {
  94. if ($roleModel::isAdministrator($slug)) {
  95. $tree->checkedAll();
  96. }
  97. }
  98. });
  99. });
  100. $grid->created_at;
  101. $grid->updated_at->sortable();
  102. $grid->actions(function (Grid\Displayers\Actions $actions) {
  103. if ($actions->getKey() == 1) {
  104. $actions->disableDelete();
  105. }
  106. });
  107. $grid->filter(function (Grid\Filter $filter) {
  108. $filter->equal('id');
  109. $filter->like('username');
  110. $filter->like('name');
  111. });
  112. return $grid;
  113. }
  114. protected function miniGrid()
  115. {
  116. $grid = new MiniGrid(new Administrator());
  117. $grid->id->bold()->sortable();
  118. $grid->username;
  119. $grid->name;
  120. $grid->filter(function (Grid\Filter $filter) {
  121. $filter->equal('id')->width('270px');
  122. $filter->like('username')->width('270px');
  123. $filter->like('name')->width('270px');
  124. });
  125. return $grid;
  126. }
  127. /**
  128. * Make a show builder.
  129. *
  130. * @param mixed $id
  131. *
  132. * @return Show
  133. */
  134. protected function detail($id)
  135. {
  136. $show = new Show(new Administrator());
  137. $show->setId($id);
  138. $show->id;
  139. $show->username;
  140. $show->name;
  141. $show->newline();
  142. $show->created_at;
  143. $show->updated_at;
  144. $show->divider();
  145. $show->roles->width(6)->as(function ($roles) {
  146. return collect($roles)->pluck('name');
  147. })->label('primary');
  148. $show->permissions->width(6)->unescape()->as(function () {
  149. $permissionModel = config('admin.database.permissions_model');
  150. $roleModel = config('admin.database.roles_model');
  151. $permissionModel = new $permissionModel;
  152. $nodes = $permissionModel->allNodes();
  153. $tree = Tree::make($nodes);
  154. $isAdministrator = false;
  155. foreach (array_column($this->roles, 'slug') as $slug) {
  156. if ($roleModel::isAdministrator($slug)) {
  157. $tree->checkedAll();
  158. $isAdministrator = true;
  159. }
  160. }
  161. if (!$isAdministrator) {
  162. $keyName = $permissionModel->getKeyName();
  163. $tree->checked(
  164. $roleModel::getPermissionId(array_column($this->roles, $keyName))->flatten()
  165. );
  166. }
  167. return $tree->render();
  168. });
  169. return $show;
  170. }
  171. /**
  172. * Make a form builder.
  173. *
  174. * @return Form
  175. */
  176. public function form($id = null)
  177. {
  178. $roleModel = config('admin.database.roles_model');
  179. $form = new Form(new Administrator());
  180. $form->display('id', 'ID');
  181. if (request()->isMethod('POST')) {
  182. $userTable = config('admin.database.users_table');
  183. $userNameRules = "required|unique:{$userTable}";
  184. } else {
  185. $userNameRules = 'required';
  186. }
  187. $form->text('username', trans('admin.username'))->rules($userNameRules);
  188. $form->text('name', trans('admin.name'))->rules('required');
  189. $form->image('avatar', trans('admin.avatar'));
  190. $form->password('password', trans('admin.password'))->rules('required|confirmed');
  191. $form->password('password_confirmation', trans('admin.password_confirmation'))
  192. ->rules('required')
  193. ->default(function ($form) {
  194. return $form->model()->get('password');
  195. });
  196. $form->ignore(['password_confirmation']);
  197. $form->multipleSelect('roles', trans('admin.roles'))
  198. ->options($roleModel::all()->pluck('name', 'id'))
  199. ->customFormat(function ($v) {
  200. return array_column($v, 'id');
  201. });
  202. if ($id) {
  203. $form->display('created_at', trans('admin.created_at'));
  204. $form->display('updated_at', trans('admin.updated_at'));
  205. }
  206. $form->saving(function (Form $form) {
  207. if ($form->password && $form->model()->get('password') != $form->password) {
  208. $form->password = bcrypt($form->password);
  209. }
  210. });
  211. return $form;
  212. }
  213. }