UserController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace Dcat\Admin\Controllers;
  3. use Dcat\Admin\Auth\Permission;
  4. use Dcat\Admin\Models\Repositories\Administrator;
  5. use Dcat\Admin\Models\Administrator as AdministratorModel;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Layout\Content;
  9. use Dcat\Admin\MiniGrid;
  10. use Dcat\Admin\Show;
  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('_mini')) {
  27. return $content->body($this->miniGrid());
  28. }
  29. return $content
  30. ->header(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. ->header(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. ->header(trans('admin.administrator'))
  60. ->description(trans('admin.edit'))
  61. ->body($this->form($id)->edit($id));
  62. }
  63. /**
  64. * Create interface.
  65. *
  66. * @return Content
  67. */
  68. public function create(Content $content)
  69. {
  70. return $content
  71. ->header(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. $grid = new Grid(new Administrator());
  83. $grid->disableBatchDelete();
  84. $grid->disableCreateButton();
  85. $grid->model()->with('roles');
  86. $grid->id->bold()->sortable();
  87. $grid->username;
  88. $grid->name;
  89. $grid->roles->pluck('name')->label('primary');
  90. $permissionModel = config('admin.database.permissions_model');
  91. $roleModel = config('admin.database.roles_model');
  92. $nodes = (new $permissionModel)->allNodes();
  93. $grid->permissions->display(function ($v, $column) use (&$nodes, $roleModel) {
  94. if (empty($this->roles)) {
  95. return;
  96. }
  97. return $column->tree(function (Grid\Displayers\Tree $tree) use (&$nodes, $roleModel) {
  98. $tree->nodes($nodes);
  99. foreach (array_column($this->roles, 'slug') as $slug) {
  100. if ($roleModel::isAdministrator($slug)) {
  101. $tree->checkedAll();
  102. }
  103. }
  104. });
  105. });
  106. $grid->created_at;
  107. $grid->updated_at->sortable();
  108. $grid->actions(function (Grid\Displayers\Actions $actions) {
  109. if ($actions->getKey() == AdministratorModel::DEFAULT_ID) {
  110. $actions->disableDelete();
  111. }
  112. });
  113. $grid->filter(function (Grid\Filter $filter) {
  114. $filter->equal('id');
  115. $filter->like('username');
  116. $filter->like('name');
  117. });
  118. return $grid;
  119. }
  120. /**
  121. * @return MiniGrid
  122. */
  123. protected function miniGrid()
  124. {
  125. $grid = new MiniGrid(new Administrator());
  126. $grid->id->bold()->sortable()->filter(
  127. Grid\Column\Filter\Equal::make('ID')
  128. );
  129. $grid->username->filter(
  130. Grid\Column\Filter\StartWith::make(__('admin.username'))
  131. );
  132. $grid->name->filter(
  133. Grid\Column\Filter\StartWith::make(__('admin.name'))
  134. );
  135. $grid->created_at;
  136. return $grid;
  137. }
  138. /**
  139. * Make a show builder.
  140. *
  141. * @param mixed $id
  142. *
  143. * @return Show
  144. */
  145. protected function detail($id)
  146. {
  147. $show = new Show(new Administrator());
  148. $show->setId($id);
  149. $show->id;
  150. $show->username;
  151. $show->name;
  152. $show->avatar->image();
  153. $show->newline();
  154. $show->created_at;
  155. $show->updated_at;
  156. $show->divider();
  157. $show->roles->width(6)->as(function ($roles) {
  158. return collect($roles)->pluck('name');
  159. })->label('primary');
  160. $show->permissions->width(6)->unescape()->as(function () {
  161. $permissionModel = config('admin.database.permissions_model');
  162. $roleModel = config('admin.database.roles_model');
  163. $permissionModel = new $permissionModel;
  164. $nodes = $permissionModel->allNodes();
  165. $tree = Tree::make($nodes);
  166. $isAdministrator = false;
  167. foreach (array_column($this->roles, 'slug') as $slug) {
  168. if ($roleModel::isAdministrator($slug)) {
  169. $tree->checkedAll();
  170. $isAdministrator = true;
  171. }
  172. }
  173. if (!$isAdministrator) {
  174. $keyName = $permissionModel->getKeyName();
  175. $tree->checked(
  176. $roleModel::getPermissionId(array_column($this->roles, $keyName))->flatten()
  177. );
  178. }
  179. return $tree->render();
  180. });
  181. if ($id == AdministratorModel::DEFAULT_ID) {
  182. $show->disableDeleteButton();
  183. }
  184. return $show;
  185. }
  186. /**
  187. * Make a form builder.
  188. *
  189. * @return Form
  190. */
  191. public function form($id = null)
  192. {
  193. $userTable = config('admin.database.users_table');
  194. $connection = config('admin.database.connection');
  195. $form = new Form(new Administrator());
  196. $form->display('id', 'ID');
  197. $form->text('username', trans('admin.username'))
  198. ->required()
  199. ->creationRules(['required', "unique:{$connection}.{$userTable}"])
  200. ->updateRules(['required', "unique:{$connection}.{$userTable},username,$id"]);
  201. $form->text('name', trans('admin.name'))->required();
  202. $form->image('avatar', trans('admin.avatar'));
  203. if ($id) {
  204. $form->password('password', trans('admin.password'))
  205. ->rules('confirmed')
  206. ->customFormat(function ($v) {
  207. if ($v == $this->password) {
  208. return;
  209. }
  210. return $v;
  211. });
  212. $form->password('password_confirmation', trans('admin.password_confirmation'));
  213. } else {
  214. $form->password('password', trans('admin.password'))
  215. ->required()
  216. ->rules('confirmed');
  217. $form->password('password_confirmation', trans('admin.password_confirmation'));
  218. }
  219. $form->ignore(['password_confirmation']);
  220. $form->multipleSelect('roles', trans('admin.roles'))
  221. ->options(function () {
  222. $roleModel = config('admin.database.roles_model');
  223. return $roleModel::all()->pluck('name', 'id');
  224. })
  225. ->customFormat(function ($v) {
  226. return array_column($v, 'id');
  227. });
  228. if ($id) {
  229. $form->display('created_at', trans('admin.created_at'));
  230. $form->display('updated_at', trans('admin.updated_at'));
  231. }
  232. $form->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. if ($id == AdministratorModel::DEFAULT_ID) {
  241. $form->disableDeleteButton();
  242. }
  243. return $form;
  244. }
  245. /**
  246. * Remove the specified resource from storage.
  247. *
  248. * @param int $id
  249. *
  250. * @return \Illuminate\Http\Response
  251. */
  252. public function destroy($id)
  253. {
  254. if (in_array(AdministratorModel::DEFAULT_ID, Helper::array($id))) {
  255. Permission::error();
  256. }
  257. return $this->delete($id);
  258. }
  259. }