UserController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace Dcat\Admin\Controllers;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Auth\Permission;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Layout\Content;
  8. use Dcat\Admin\Models\Administrator as AdministratorModel;
  9. use Dcat\Admin\Models\Repositories\Administrator;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\SimpleGrid;
  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(SimpleGrid::QUERY_NAME)) {
  28. return $content->body($this->simpleGrid());
  29. }
  30. return $content
  31. ->title(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. ->title(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. ->title(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. ->title(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->showQuickEditButton();
  109. $grid->disableFilterButton();
  110. $grid->quickSearch(['id', 'name', 'username']);
  111. $grid->createMode(Grid::CREATE_MODE_DIALOG);
  112. $grid->actions(function (Grid\Displayers\Actions $actions) {
  113. if ($actions->key() == AdministratorModel::DEFAULT_ID) {
  114. $actions->disableDelete();
  115. }
  116. });
  117. });
  118. }
  119. /**
  120. * @return SimpleGrid
  121. */
  122. protected function simpleGrid()
  123. {
  124. $grid = new SimpleGrid(new Administrator());
  125. $grid->quickSearch(['id', 'name', 'username']);
  126. $grid->id->bold()->sortable();
  127. $grid->username;
  128. $grid->name;
  129. $grid->created_at;
  130. return $grid;
  131. }
  132. /**
  133. * Make a show builder.
  134. *
  135. * @param mixed $id
  136. *
  137. * @return Show
  138. */
  139. protected function detail($id)
  140. {
  141. return Admin::show($id, new Administrator('roles'), function (Show $show) {
  142. $show->id;
  143. $show->username;
  144. $show->name;
  145. $show->avatar->image();
  146. $show->newline();
  147. $show->created_at;
  148. $show->updated_at;
  149. $show->divider();
  150. $show->roles->width(6)->as(function ($roles) {
  151. if (! $roles) {
  152. return;
  153. }
  154. return collect($roles)->pluck('name');
  155. })->label('primary');
  156. $show->permissions->width(6)->unescape()->as(function () {
  157. $roles = (array) $this->roles;
  158. $permissionModel = config('admin.database.permissions_model');
  159. $roleModel = config('admin.database.roles_model');
  160. $permissionModel = new $permissionModel();
  161. $nodes = $permissionModel->allNodes();
  162. $tree = Tree::make($nodes);
  163. $isAdministrator = false;
  164. foreach (array_column($roles, 'slug') as $slug) {
  165. if ($roleModel::isAdministrator($slug)) {
  166. $tree->checkedAll();
  167. $isAdministrator = true;
  168. }
  169. }
  170. if (! $isAdministrator) {
  171. $keyName = $permissionModel->getKeyName();
  172. $tree->checked(
  173. $roleModel::getPermissionId(array_column($roles, $keyName))->flatten()
  174. );
  175. }
  176. return $tree->render();
  177. });
  178. if ($show->key() == AdministratorModel::DEFAULT_ID) {
  179. $show->disableDeleteButton();
  180. }
  181. });
  182. }
  183. /**
  184. * Make a form builder.
  185. *
  186. * @return Form
  187. */
  188. public function form()
  189. {
  190. return Admin::form(new Administrator('roles'), function (Form $form) {
  191. $userTable = config('admin.database.users_table');
  192. $connection = config('admin.database.connection');
  193. $id = $form->key();
  194. $form->display('id', 'ID');
  195. $form->text('username', trans('admin.username'))
  196. ->required()
  197. ->creationRules(['required', "unique:{$connection}.{$userTable}"])
  198. ->updateRules(['required', "unique:{$connection}.{$userTable},username,$id"]);
  199. $form->text('name', trans('admin.name'))->required();
  200. $form->image('avatar', trans('admin.avatar'));
  201. if ($id) {
  202. $form->password('password', trans('admin.password'))
  203. ->minLength(5)
  204. ->maxLength(20)
  205. ->customFormat(function ($v) {
  206. if ($v == $this->password) {
  207. return;
  208. }
  209. return $v;
  210. });
  211. } else {
  212. $form->password('password', trans('admin.password'))
  213. ->required()
  214. ->minLength(5)
  215. ->maxLength(20);
  216. }
  217. $form->password('password_confirmation', trans('admin.password_confirmation'))->same('password');
  218. $form->ignore(['password_confirmation']);
  219. $form->multipleSelect('roles', trans('admin.roles'))
  220. ->options(function () {
  221. $roleModel = config('admin.database.roles_model');
  222. return $roleModel::all()->pluck('name', 'id');
  223. })
  224. ->customFormat(function ($v) {
  225. return array_column($v, 'id');
  226. });
  227. $form->display('created_at', trans('admin.created_at'));
  228. $form->display('updated_at', trans('admin.updated_at'));
  229. if ($id == AdministratorModel::DEFAULT_ID) {
  230. $form->disableDeleteButton();
  231. }
  232. })->saving(function (Form $form) {
  233. if ($form->password && $form->model()->get('password') != $form->password) {
  234. $form->password = bcrypt($form->password);
  235. }
  236. if (! $form->password) {
  237. $form->deleteInput('password');
  238. }
  239. });
  240. }
  241. /**
  242. * Remove the specified resource from storage.
  243. *
  244. * @param int $id
  245. *
  246. * @return \Illuminate\Http\Response
  247. */
  248. public function destroy($id)
  249. {
  250. if (in_array(AdministratorModel::DEFAULT_ID, Helper::array($id))) {
  251. Permission::error();
  252. }
  253. return $this->delete($id);
  254. }
  255. }