123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace Dcat\Admin\Controllers;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Auth\Permission;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Models\Repositories\Role;
- use Dcat\Admin\Models\Role as RoleModel;
- use Dcat\Admin\Show;
- use Dcat\Admin\SimpleGrid;
- use Dcat\Admin\Support\Helper;
- use Illuminate\Routing\Controller;
- class RoleController extends Controller
- {
- use HasResourceActions {
- destroy as delete;
- }
- /**
- * Index interface.
- *
- * @param Content $content
- *
- * @return Content
- */
- public function index(Content $content)
- {
- return $content
- ->title(trans('admin.roles'))
- ->description(trans('admin.list'))
- ->body($this->grid());
- }
- /**
- * Show interface.
- *
- * @param mixed $id
- * @param Content $content
- *
- * @return Content
- */
- public function show($id, Content $content)
- {
- return $content
- ->title(trans('admin.roles'))
- ->description(trans('admin.detail'))
- ->body($this->detail($id));
- }
- /**
- * Edit interface.
- *
- * @param mixed $id
- * @param Content $content
- *
- * @return Content
- */
- public function edit($id, Content $content)
- {
- return $content
- ->title(trans('admin.roles'))
- ->description(trans('admin.edit'))
- ->body($this->form()->edit($id));
- }
- /**
- * Create interface.
- *
- * @param Content $content
- *
- * @return Content
- */
- public function create(Content $content)
- {
- return $content
- ->title(trans('admin.roles'))
- ->description(trans('admin.create'))
- ->body($this->form());
- }
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- if ($mini = request(SimpleGrid::QUERY_NAME)) {
- $grid = new SimpleGrid(new Role());
- } else {
- $grid = new Grid(new Role());
- }
- $grid->id('ID')->bold()->sortable();
- $grid->slug->label('primary');
- $grid->name;
- if (! $mini) {
- $grid->created_at;
- $grid->updated_at->sortable();
- }
- $grid->disableBatchDelete();
- $grid->disableEditButton();
- $grid->showQuickEditButton();
- $grid->disableFilterButton();
- $grid->quickSearch(['id', 'name', 'slug']);
- $grid->createMode(Grid::CREATE_MODE_DIALOG);
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $roleModel = config('admin.database.roles_model');
- if ($roleModel::isAdministrator($actions->row->slug)) {
- $actions->disableDelete();
- }
- });
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- *
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make(new Role('permissions'), function (Show $show) use ($id) {
- $show->id;
- $show->slug;
- $show->name;
- $show->permissions->width(12)->as(function ($permission) {
- return collect($permission)->pluck('name');
- })->label('primary');
- $show->divider();
- $show->created_at;
- $show->updated_at;
- if ($id == RoleModel::ADMINISTRATOR_ID) {
- $show->disableDeleteButton();
- }
- })->key($id);
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- public function form()
- {
- return Form::make(new Role('permissions'), function (Form $form) {
- $roleTable = config('admin.database.roles_table');
- $connection = config('admin.database.connection');
- $id = $form->key();
- $form->display('id', 'ID');
- $form->text('slug', trans('admin.slug'))
- ->required()
- ->creationRules(['required', "unique:{$connection}.{$roleTable}"])
- ->updateRules(['required', "unique:{$connection}.{$roleTable},slug,$id"]);
- $form->text('name', trans('admin.name'))->required();
- $form->tree('permissions')
- ->nodes(function () {
- $permissionModel = config('admin.database.permissions_model');
- $permissionModel = new $permissionModel();
- return $permissionModel->allNodes();
- })
- ->customFormat(function ($v) {
- if (! $v) {
- return [];
- }
- return array_column($v, 'id');
- });
- $form->display('created_at', trans('admin.created_at'));
- $form->display('updated_at', trans('admin.updated_at'));
- if ($id == RoleModel::ADMINISTRATOR_ID) {
- $form->disableDeleteButton();
- }
- });
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- *
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- if (in_array(RoleModel::ADMINISTRATOR_ID, Helper::array($id))) {
- Permission::error();
- }
- return $this->delete($id);
- }
- }
|