GlobalUserController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Events\GlobalUserCreated;
  4. use App\Models\GlobalUser;
  5. use App\Models\Tenant;
  6. use Encore\Admin\Controllers\AdminController;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Show;
  10. use Encore\Admin\Widgets\Table;
  11. class GlobalUserController extends AdminController
  12. {
  13. protected $title = '全局用户';
  14. protected $description = '';
  15. /**
  16. * Make a grid builder.
  17. *
  18. * @return Grid
  19. */
  20. protected function grid()
  21. {
  22. $grid = new Grid(new GlobalUser(['tenant']));
  23. $grid->model()->orderByDesc('created_at');
  24. $grid->column('id');
  25. $grid->column('username')->copyable();
  26. $grid->column('tenant.name')->expand(function ($model) {
  27. $tenants = $model->tenant()->get()->map(function ($tenant) {
  28. return $tenant->only(['id', 'name', 'tenancy_db_connection', 'expired_at']);
  29. });
  30. return new Table(['ID', 'Name', 'Db_connection', 'expired_at'], $tenants->toArray());
  31. });
  32. $grid->column('created_at')->datetime();
  33. $grid->column('updated_at')->datetime();
  34. $grid->disableActions();
  35. $grid->filter(function (Grid\Filter $filter) {
  36. $filter->like('username');
  37. $filter->equal('tenant_id')->select(Tenant::query()->pluck('name', 'id'));
  38. });
  39. return $grid;
  40. }
  41. /**
  42. * Make a show builder.
  43. *
  44. * @param mixed $id
  45. *
  46. * @return Show
  47. */
  48. protected function detail($id)
  49. {
  50. $show = new Show(GlobalUser::query()->findOrFail($id));
  51. $show->field('id');
  52. $show->field('username');
  53. $show->field('created_at');
  54. $show->field('updated_at');
  55. $show->tenant('tenant', function ($tenant) {
  56. $tenant->id();
  57. $tenant->name();
  58. $tenant->tenancy_db_connection();
  59. $tenant->expired_at();
  60. $tenant->panel()->tools(function ($tools) {
  61. $tools->disableList();
  62. $tools->disableEdit();
  63. $tools->disableDelete();
  64. });
  65. });
  66. return $show;
  67. }
  68. protected function form()
  69. {
  70. $form = new Form(new GlobalUser());
  71. $form->select('tenant_id', 'Tenant ID')
  72. ->options(Tenant::all()->pluck('name','id'))->rules('required')
  73. ->default(request()->get('tenant_id'));
  74. $form->text('username')->placeholder('unique email or phone')->rules('required');
  75. $form->text('password')->rules('required');
  76. $form->display('role')->default("超级管理员");
  77. $user = [
  78. 'username' => $form->username,
  79. 'password' => $form->password,
  80. 'tenant_id' => $form->tenant_id,
  81. ];
  82. // callback before save
  83. $form->saving(function (Form $form) {
  84. $form->ignore('password');
  85. $form->ignore('role');
  86. });
  87. // callback after save
  88. $form->saved(function (Form $form) use ($user) {
  89. event(new GlobalUserCreated($user));
  90. });
  91. return $form;
  92. }
  93. }