UserController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace Dcat\Admin\Controllers;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Auth\Permission;
  5. use Dcat\Admin\Models\Repositories\Administrator;
  6. use Dcat\Admin\Models\Administrator as AdministratorModel;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Layout\Content;
  10. use Dcat\Admin\MiniGrid;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Support\Helper;
  13. use Dcat\Admin\Widgets\Tree;
  14. use Illuminate\Routing\Controller;
  15. class UserController extends Controller
  16. {
  17. use HasResourceActions {
  18. destroy as delete;
  19. }
  20. /**
  21. * Index interface.
  22. *
  23. * @return Content
  24. */
  25. public function index(Content $content)
  26. {
  27. if (request('_mini')) {
  28. return $content->body($this->miniGrid());
  29. }
  30. return $content
  31. ->header(trans('admin.administrator'))
  32. ->description(trans('admin.list'))
  33. ->body($this->grid());
  34. }
  35. /**
  36. * Show interface.
  37. *
  38. * @param mixed $id
  39. * @param Content $content
  40. *
  41. * @return Content
  42. */
  43. public function show($id, Content $content)
  44. {
  45. return $content
  46. ->header(trans('admin.administrator'))
  47. ->description(trans('admin.detail'))
  48. ->body($this->detail($id));
  49. }
  50. /**
  51. * Edit interface.
  52. *
  53. * @param $id
  54. *
  55. * @return Content
  56. */
  57. public function edit($id, Content $content)
  58. {
  59. return $content
  60. ->header(trans('admin.administrator'))
  61. ->description(trans('admin.edit'))
  62. ->body($this->form()->edit($id));
  63. }
  64. /**
  65. * Create interface.
  66. *
  67. * @return Content
  68. */
  69. public function create(Content $content)
  70. {
  71. return $content
  72. ->header(trans('admin.administrator'))
  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. return Admin::grid(new Administrator('roles'), function (Grid $grid) {
  84. $grid->id('ID')->bold()->sortable();
  85. $grid->username;
  86. $grid->name;
  87. $grid->roles->pluck('name')->label('primary');
  88. $permissionModel = config('admin.database.permissions_model');
  89. $roleModel = config('admin.database.roles_model');
  90. $nodes = (new $permissionModel)->allNodes();
  91. $grid->permissions
  92. ->if(function () {
  93. return ! empty($this->roles);
  94. })
  95. ->tree(function (Grid\Displayers\Tree $tree) use (&$nodes, $roleModel) {
  96. $tree->nodes($nodes);
  97. foreach (array_column($this->roles, 'slug') as $slug) {
  98. if ($roleModel::isAdministrator($slug)) {
  99. $tree->checkedAll();
  100. }
  101. }
  102. })
  103. ->else()
  104. ->showEmpty();
  105. $grid->created_at;
  106. $grid->updated_at->sortable();
  107. $grid->disableBatchDelete();
  108. $grid->disableCreateButton();
  109. $grid->showQuickCreateButton();
  110. $grid->showQuickEditButton();
  111. $grid->disableFilterButton();
  112. $grid->quickSearch(['id', 'name', 'username']);
  113. $grid->actions(function (Grid\Displayers\Actions $actions) {
  114. if ($actions->getKey() == AdministratorModel::DEFAULT_ID) {
  115. $actions->disableDelete();
  116. }
  117. });
  118. });
  119. }
  120. /**
  121. * @return MiniGrid
  122. */
  123. protected function miniGrid()
  124. {
  125. $grid = new MiniGrid(new Administrator());
  126. $grid->quickSearch(['id', 'name', 'username']);
  127. $grid->id->bold()->sortable();
  128. $grid->username;
  129. $grid->name;
  130. $grid->created_at;
  131. return $grid;
  132. }
  133. /**
  134. * Make a show builder.
  135. *
  136. * @param mixed $id
  137. *
  138. * @return Show
  139. */
  140. protected function detail($id)
  141. {
  142. return Admin::show($id, new Administrator('roles'), function (Show $show) {
  143. $show->id;
  144. $show->username;
  145. $show->name;
  146. $show->avatar->image();
  147. $show->newline();
  148. $show->created_at;
  149. $show->updated_at;
  150. $show->divider();
  151. $show->roles->width(6)->as(function ($roles) {
  152. if (! $roles) return;
  153. return collect($roles)->pluck('name');
  154. })->label('primary');
  155. $show->permissions->width(6)->unescape()->as(function () {
  156. $roles = (array) $this->roles;
  157. $permissionModel = config('admin.database.permissions_model');
  158. $roleModel = config('admin.database.roles_model');
  159. $permissionModel = new $permissionModel;
  160. $nodes = $permissionModel->allNodes();
  161. $tree = Tree::make($nodes);
  162. $isAdministrator = false;
  163. foreach (array_column($roles, 'slug') as $slug) {
  164. if ($roleModel::isAdministrator($slug)) {
  165. $tree->checkedAll();
  166. $isAdministrator = true;
  167. }
  168. }
  169. if (!$isAdministrator) {
  170. $keyName = $permissionModel->getKeyName();
  171. $tree->checked(
  172. $roleModel::getPermissionId(array_column($roles, $keyName))->flatten()
  173. );
  174. }
  175. return $tree->render();
  176. });
  177. if ($show->getId() == AdministratorModel::DEFAULT_ID) {
  178. $show->disableDeleteButton();
  179. }
  180. });
  181. }
  182. /**
  183. * Make a form builder.
  184. *
  185. * @return Form
  186. */
  187. public function form()
  188. {
  189. return Admin::form(new Administrator('roles'), function (Form $form) {
  190. $userTable = config('admin.database.users_table');
  191. $connection = config('admin.database.connection');
  192. $id = $form->getKey();
  193. $form->display('id', 'ID');
  194. $form->text('username', trans('admin.username'))
  195. ->required()
  196. ->creationRules(['required', "unique:{$connection}.{$userTable}"])
  197. ->updateRules(['required', "unique:{$connection}.{$userTable},username,$id"]);
  198. $form->text('name', trans('admin.name'))->required();
  199. $form->image('avatar', trans('admin.avatar'));
  200. if ($id) {
  201. $form->password('password', trans('admin.password'))
  202. ->rules('confirmed')
  203. ->customFormat(function ($v) {
  204. if ($v == $this->password) {
  205. return;
  206. }
  207. return $v;
  208. });
  209. $form->password('password_confirmation', trans('admin.password_confirmation'));
  210. } else {
  211. $form->password('password', trans('admin.password'))
  212. ->required()
  213. ->rules('confirmed');
  214. $form->password('password_confirmation', trans('admin.password_confirmation'));
  215. }
  216. $form->ignore(['password_confirmation']);
  217. $form->multipleSelect('roles', trans('admin.roles'))
  218. ->options(function () {
  219. $roleModel = config('admin.database.roles_model');
  220. return $roleModel::all()->pluck('name', 'id');
  221. })
  222. ->customFormat(function ($v) {
  223. return array_column($v, 'id');
  224. });
  225. $form->display('created_at', trans('admin.created_at'));
  226. $form->display('updated_at', trans('admin.updated_at'));
  227. if ($id == AdministratorModel::DEFAULT_ID) {
  228. $form->disableDeleteButton();
  229. }
  230. })->saving(function (Form $form) {
  231. if ($form->password && $form->model()->get('password') != $form->password) {
  232. $form->password = bcrypt($form->password);
  233. }
  234. if (! $form->password) {
  235. $form->deleteInput('password');
  236. }
  237. });
  238. }
  239. /**
  240. * Remove the specified resource from storage.
  241. *
  242. * @param int $id
  243. *
  244. * @return \Illuminate\Http\Response
  245. */
  246. public function destroy($id)
  247. {
  248. if (in_array(AdministratorModel::DEFAULT_ID, Helper::array($id))) {
  249. Permission::error();
  250. }
  251. return $this->delete($id);
  252. }
  253. }