GlobalUserController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\GlobalUser;
  4. use App\Models\Tenant;
  5. use Encore\Admin\Controllers\AdminController;
  6. use Encore\Admin\Form;
  7. use Encore\Admin\Grid;
  8. use Encore\Admin\Show;
  9. use Encore\Admin\Widgets\Table;
  10. use Illuminate\Support\Facades\Hash;
  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('is_admin')->using([0 => 'NO', 1 => 'YES']);
  27. $grid->column('tenant.name')->expand(function ($model) {
  28. $tenants = $model->tenant()->get()->map(function ($tenant) {
  29. return $tenant->only(['id', 'name', 'tenancy_db_connection', 'expired_at']);
  30. });
  31. return new Table(['ID', 'Name', 'Db_connection', 'expired_at'], $tenants->toArray());
  32. });
  33. $grid->column('created_at')->datetime();
  34. $grid->column('updated_at')->datetime();
  35. $grid->disableActions();
  36. $grid->filter(function (Grid\Filter $filter) {
  37. $filter->like('username');
  38. $filter->equal('tenant_id')->select(Tenant::query()->pluck('name', 'id'));
  39. });
  40. return $grid;
  41. }
  42. /**
  43. * Make a show builder.
  44. *
  45. * @param mixed $id
  46. *
  47. * @return Show
  48. */
  49. protected function detail($id)
  50. {
  51. $show = new Show(GlobalUser::query()->findOrFail($id));
  52. $show->field('id');
  53. $show->field('username');
  54. $show->field('is_admin')->using([0 => 'NO', 1 => 'YES']);
  55. $show->field('created_at');
  56. $show->field('updated_at');
  57. $show->tenant('tenant', function ($tenant) {
  58. $tenant->id();
  59. $tenant->name();
  60. $tenant->tenancy_db_connection();
  61. $tenant->expired_at();
  62. $tenant->panel()->tools(function ($tools) {
  63. $tools->disableList();
  64. $tools->disableEdit();
  65. $tools->disableDelete();
  66. });
  67. });
  68. return $show;
  69. }
  70. protected function form()
  71. {
  72. $form = new Form(new GlobalUser());
  73. $form->select('tenant_id', 'Tenant ID')
  74. ->options(Tenant::all()->pluck('name','id'))->rules('required')
  75. ->default(request()->get('tenant_id'));
  76. $form->text('username')->placeholder('unique email or phone')->rules('required');
  77. $form->text('password')->rules('required');
  78. $form->radio('is_admin')->options([0 => 'NO', 1 => 'YES'])->default(1)->required();
  79. $form->saving(function (Form $form) {
  80. $form->password = Hash::make($form->password);
  81. });
  82. return $form;
  83. }
  84. }