UserController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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\Administrator as AdministratorModel;
  8. use Dcat\Admin\Models\Repositories\Administrator;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\SimpleGrid;
  11. use Dcat\Admin\Support\Helper;
  12. use Dcat\Admin\Widgets\Tree;
  13. use Illuminate\Routing\Controller;
  14. class UserController extends Controller
  15. {
  16. use HasResourceActions {
  17. destroy as delete;
  18. }
  19. /**
  20. * Index interface.
  21. *
  22. * @return Content
  23. */
  24. public function index(Content $content)
  25. {
  26. if (request(SimpleGrid::QUERY_NAME)) {
  27. return $content->body($this->simpleGrid());
  28. }
  29. return $content
  30. ->title(trans('admin.administrator'))
  31. ->description(trans('admin.list'))
  32. ->body($this->grid());
  33. }
  34. /**
  35. * Show interface.
  36. *
  37. * @param mixed $id
  38. * @param Content $content
  39. *
  40. * @return Content
  41. */
  42. public function show($id, Content $content)
  43. {
  44. return $content
  45. ->title(trans('admin.administrator'))
  46. ->description(trans('admin.detail'))
  47. ->body($this->detail($id));
  48. }
  49. /**
  50. * Edit interface.
  51. *
  52. * @param $id
  53. *
  54. * @return Content
  55. */
  56. public function edit($id, Content $content)
  57. {
  58. return $content
  59. ->title(trans('admin.administrator'))
  60. ->description(trans('admin.edit'))
  61. ->body($this->form()->edit($id));
  62. }
  63. /**
  64. * Create interface.
  65. *
  66. * @return Content
  67. */
  68. public function create(Content $content)
  69. {
  70. return $content
  71. ->title(trans('admin.administrator'))
  72. ->description(trans('admin.create'))
  73. ->body($this->form());
  74. }
  75. /**
  76. * Make a grid builder.
  77. *
  78. * @return Grid
  79. */
  80. protected function grid()
  81. {
  82. return Grid::make(new Administrator('roles'), function (Grid $grid) {
  83. $grid->id('ID')->bold()->sortable();
  84. $grid->username;
  85. $grid->name;
  86. $grid->roles->pluck('name')->label('primary');
  87. $permissionModel = config('admin.database.permissions_model');
  88. $roleModel = config('admin.database.roles_model');
  89. $nodes = (new $permissionModel())->allNodes();
  90. $grid->permissions
  91. ->if(function () {
  92. return ! empty($this->roles);
  93. })
  94. ->tree(function (Grid\Displayers\Tree $tree) use (&$nodes, $roleModel) {
  95. $tree->nodes($nodes);
  96. foreach (array_column($this->roles, 'slug') as $slug) {
  97. if ($roleModel::isAdministrator($slug)) {
  98. $tree->checkedAll();
  99. }
  100. }
  101. })
  102. ->else()
  103. ->asEmpty();
  104. $grid->created_at;
  105. $grid->updated_at->sortable();
  106. $grid->disableBatchDelete();
  107. $grid->showQuickEditButton();
  108. $grid->disableFilterButton();
  109. $grid->quickSearch(['id', 'name', 'username']);
  110. $grid->enableDialogCreate();
  111. $grid->actions(function (Grid\Displayers\Actions $actions) {
  112. if ($actions->key() == AdministratorModel::DEFAULT_ID) {
  113. $actions->disableDelete();
  114. }
  115. });
  116. });
  117. }
  118. /**
  119. * @return SimpleGrid
  120. */
  121. protected function simpleGrid()
  122. {
  123. $grid = new SimpleGrid(new Administrator());
  124. $grid->quickSearch(['id', 'name', 'username']);
  125. $grid->id->bold()->sortable();
  126. $grid->username;
  127. $grid->name;
  128. $grid->created_at;
  129. return $grid;
  130. }
  131. /**
  132. * Make a show builder.
  133. *
  134. * @param mixed $id
  135. *
  136. * @return Show
  137. */
  138. protected function detail($id)
  139. {
  140. return Show::make($id, new Administrator('roles'), function (Show $show) {
  141. $show->id;
  142. $show->username;
  143. $show->name;
  144. $show->avatar->image();
  145. $show->newline();
  146. $show->created_at;
  147. $show->updated_at;
  148. $show->divider();
  149. $show->roles->width(6)->as(function ($roles) {
  150. if (! $roles) {
  151. return;
  152. }
  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->key() == 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 Form::make(new Administrator('roles'), function (Form $form) {
  190. $userTable = config('admin.database.users_table');
  191. $connection = config('admin.database.connection');
  192. $id = $form->key();
  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. ->minLength(5)
  203. ->maxLength(20)
  204. ->customFormat(function ($v) {
  205. if ($v == $this->password) {
  206. return;
  207. }
  208. return $v;
  209. });
  210. } else {
  211. $form->password('password', trans('admin.password'))
  212. ->required()
  213. ->minLength(5)
  214. ->maxLength(20);
  215. }
  216. $form->password('password_confirmation', trans('admin.password_confirmation'))->same('password');
  217. $form->ignore(['password_confirmation']);
  218. $form->multipleSelect('roles', trans('admin.roles'))
  219. ->options(function () {
  220. $roleModel = config('admin.database.roles_model');
  221. return $roleModel::all()->pluck('name', 'id');
  222. })
  223. ->customFormat(function ($v) {
  224. return array_column($v, 'id');
  225. });
  226. $form->display('created_at', trans('admin.created_at'));
  227. $form->display('updated_at', trans('admin.updated_at'));
  228. if ($id == AdministratorModel::DEFAULT_ID) {
  229. $form->disableDeleteButton();
  230. }
  231. })->saving(function (Form $form) {
  232. if ($form->password && $form->model()->get('password') != $form->password) {
  233. $form->password = bcrypt($form->password);
  234. }
  235. if (! $form->password) {
  236. $form->deleteInput('password');
  237. }
  238. });
  239. }
  240. /**
  241. * Remove the specified resource from storage.
  242. *
  243. * @param int $id
  244. *
  245. * @return \Illuminate\Http\Response
  246. */
  247. public function destroy($id)
  248. {
  249. if (in_array(AdministratorModel::DEFAULT_ID, Helper::array($id))) {
  250. Permission::error();
  251. }
  252. return $this->delete($id);
  253. }
  254. }