From c0ddfec9599e80b2cf5b47dfeb6e6a56f19bedda Mon Sep 17 00:00:00 2001 From: merdan Date: Mon, 6 Apr 2020 16:33:51 +0500 Subject: [PATCH] admin users menu --- .../Controllers/Admin/UserCrudController.php | 166 ++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 app/Http/Controllers/Admin/UserCrudController.php diff --git a/app/Http/Controllers/Admin/UserCrudController.php b/app/Http/Controllers/Admin/UserCrudController.php new file mode 100644 index 00000000..9de27df5 --- /dev/null +++ b/app/Http/Controllers/Admin/UserCrudController.php @@ -0,0 +1,166 @@ +crud->setModel(config('backpack.permissionmanager.models.user')); + $this->crud->setEntityNameStrings(trans('backpack::permissionmanager.user'), trans('backpack::permissionmanager.users')); + $this->crud->setRoute(backpack_url('user')); + + // Columns. + $this->crud->setColumns([ + [ + 'name' => 'name', + 'label' => trans('backpack::permissionmanager.name'), + 'type' => 'text', + ], + [ + 'name' => 'email', + 'label' => trans('backpack::permissionmanager.email'), + 'type' => 'email', + ], +// [ // n-n relationship (with pivot table) +// 'label' => trans('backpack::permissionmanager.roles'), // Table column heading +// 'type' => 'select_multiple', +// 'name' => 'roles', // the method that defines the relationship in your Model +// 'entity' => 'roles', // the method that defines the relationship in your Model +// 'attribute' => 'name', // foreign key attribute that is shown to user +// 'model' => config('permission.models.role'), // foreign key model +// ], +// [ // n-n relationship (with pivot table) +// 'label' => trans('backpack::permissionmanager.extra_permissions'), // Table column heading +// 'type' => 'select_multiple', +// 'name' => 'permissions', // the method that defines the relationship in your Model +// 'entity' => 'permissions', // the method that defines the relationship in your Model +// 'attribute' => 'name', // foreign key attribute that is shown to user +// 'model' => config('permission.models.permission'), // foreign key model +// ], + ]); + + // Fields + $this->crud->addFields([ + [ + 'name' => 'name', + 'label' => trans('backpack::permissionmanager.name'), + 'type' => 'text', + ], + [ + 'name' => 'email', + 'label' => trans('backpack::permissionmanager.email'), + 'type' => 'email', + ], + [ + 'name' => 'password', + 'label' => trans('backpack::permissionmanager.password'), + 'type' => 'password', + ], + [ + 'name' => 'password_confirmation', + 'label' => trans('backpack::permissionmanager.password_confirmation'), + 'type' => 'password', + ], + [ + 'label' => 'Account', + 'name' => 'account_id', + 'type' => 'select', + 'entity' => 'account', + 'attribute' => 'email' + ] +// [ +// // two interconnected entities +// 'label' => trans('backpack::permissionmanager.user_role_permission'), +// 'field_unique_name' => 'user_role_permission', +// 'type' => 'checklist_dependency', +// 'name' => 'roles_and_permissions', // the methods that defines the relationship in your Model +// 'subfields' => [ +// 'primary' => [ +// 'label' => trans('backpack::permissionmanager.roles'), +// 'name' => 'roles', // the method that defines the relationship in your Model +// 'entity' => 'roles', // the method that defines the relationship in your Model +// 'entity_secondary' => 'permissions', // the method that defines the relationship in your Model +// 'attribute' => 'name', // foreign key attribute that is shown to user +// 'model' => config('permission.models.role'), // foreign key model +// 'pivot' => true, // on create&update, do you need to add/delete pivot table entries?] +// 'number_columns' => 3, //can be 1,2,3,4,6 +// ], +// 'secondary' => [ +// 'label' => ucfirst(trans('backpack::permissionmanager.permission_singular')), +// 'name' => 'permissions', // the method that defines the relationship in your Model +// 'entity' => 'permissions', // the method that defines the relationship in your Model +// 'entity_primary' => 'roles', // the method that defines the relationship in your Model +// 'attribute' => 'name', // foreign key attribute that is shown to user +// 'model' => config('permission.models.permission'), // foreign key model +// 'pivot' => true, // on create&update, do you need to add/delete pivot table entries?] +// 'number_columns' => 3, //can be 1,2,3,4,6 +// ], +// ], +// ], + ]); + } + + /** + * Store a newly created resource in the database. + * + * @param StoreRequest $request - type injection used for validation using Requests + * + * @return \Illuminate\Http\RedirectResponse + */ + public function store(StoreRequest $request) + { + $this->handlePasswordInput($request); + + $request->request->remove('roles_show'); + $request->request->remove('permissions_show'); + + return parent::storeCrud($request); + } + + /** + * Update the specified resource in the database. + * + * @param UpdateRequest $request - type injection used for validation using Requests + * + * @return \Illuminate\Http\RedirectResponse + */ + public function update(UpdateRequest $request) + { + $this->handlePasswordInput($request); + + $request->request->remove('roles_show'); + $request->request->remove('permissions_show'); + + return parent::updateCrud($request); + } + + /** + * Handle password input fields. + * + * @param Request $request + */ + protected function handlePasswordInput(Request $request) + { + // Remove fields not present on the user. + $request->request->remove('password_confirmation'); + + // Encrypt password if specified. + if ($request->input('password')) { + $request->request->set('password', Hash::make($request->input('password'))); + } else { + $request->request->remove('password'); + } + } +}