diff --git a/packages/Webkul/Admin/src/Resources/lang/en/app.php b/packages/Webkul/Admin/src/Resources/lang/en/app.php index 013fe690b..4a3fbbe9d 100755 --- a/packages/Webkul/Admin/src/Resources/lang/en/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/en/app.php @@ -1353,6 +1353,7 @@ return [ 'response' => [ 'being-used' => 'This resource :name is getting used in :source', + 'single-admin-present' => 'Cannot change the role if only one admin with all access is present.', 'product-copied' => 'The Product has been copied', 'error-while-copying' => 'Something went wrong while trying to copy the product', 'product-can-not-be-copied' => 'Products of type :type can not be copied', diff --git a/packages/Webkul/User/src/Http/Controllers/RoleController.php b/packages/Webkul/User/src/Http/Controllers/RoleController.php index 7e35b6d7f..d8ef1c6ab 100755 --- a/packages/Webkul/User/src/Http/Controllers/RoleController.php +++ b/packages/Webkul/User/src/Http/Controllers/RoleController.php @@ -125,18 +125,10 @@ class RoleController extends Controller */ $isChangedFromAll = $params['permission_type'] == "custom" && $this->roleRepository->find($id)->permission_type == 'all'; - if ($isChangedFromAll) { - $adminCountWithAllAccess = $this->adminRepository->getModel() - ->leftJoin('roles', 'admins.role_id', '=', 'roles.id') - ->where(["roles.permission_type" => "all"]) - ->get() - ->count(); + if ($isChangedFromAll && $this->adminRepository->countAdminsWithAllAccess() === 1) { + session()->flash('error', trans('admin::app.response.being-used', ['name' => 'Role', 'source' => 'Admin User'])); - if ($adminCountWithAllAccess == 1) { - session()->flash('error', trans('admin::app.response.being-used', ['name' => 'Role', 'source' => 'Admin User'])); - - return redirect()->route($this->_config['redirect']); - } + return redirect()->route($this->_config['redirect']); } Event::dispatch('user.role.update.before', $id); diff --git a/packages/Webkul/User/src/Http/Controllers/UserController.php b/packages/Webkul/User/src/Http/Controllers/UserController.php index 06d58e228..e74204319 100755 --- a/packages/Webkul/User/src/Http/Controllers/UserController.php +++ b/packages/Webkul/User/src/Http/Controllers/UserController.php @@ -3,30 +3,30 @@ namespace Webkul\User\Http\Controllers; use Hash; -use Illuminate\Support\Str; use Illuminate\Support\Facades\Event; +use Illuminate\Support\Str; use Webkul\User\Http\Requests\UserForm; -use Webkul\User\Repositories\RoleRepository; use Webkul\User\Repositories\AdminRepository; +use Webkul\User\Repositories\RoleRepository; class UserController extends Controller { /** - * Contains route related configuration + * Contains route related configuration. * * @var array */ protected $_config; /** - * AdminRepository object + * Admin repository instance. * * @var \Webkul\User\Repositories\AdminRepository */ protected $adminRepository; /** - * RoleRepository object + * Role repository instance. * * @var \Webkul\User\Repositories\RoleRepository */ @@ -42,8 +42,7 @@ class UserController extends Controller public function __construct( AdminRepository $adminRepository, RoleRepository $roleRepository - ) - { + ) { $this->adminRepository = $adminRepository; $this->roleRepository = $roleRepository; @@ -125,21 +124,39 @@ class UserController extends Controller */ public function update(UserForm $request, $id) { - $isPasswordChanged = false; - $data = $request->all(); + $user = $this->adminRepository->find($id); + + /** + * Is password changed. + */ + $isPasswordChanged = false; + if (! $data['password']) { unset($data['password']); } else { $isPasswordChanged = true; + $data['password'] = bcrypt($data['password']); } - if (isset($data['status'])) { - $data['status'] = 1; - } else { - $data['status'] = 0; + /** + * Status update. + */ + $data['status'] = isset($data['status']) ? 1 : 0; + + /** + * Is user with `permission_type` all role changed. + */ + $isRoleChanged = $user->role->permission_type === 'all' + && isset($data['role_id']) + && (int) $data['role_id'] !== $user->role_id; + + if ($isRoleChanged && $this->adminRepository->countAdminsWithAllAccess() === 1) { + session()->flash('error', trans('admin::app.response.single-admin-present')); + + return redirect()->route($this->_config['redirect']); } Event::dispatch('user.admin.update.before', $id); @@ -186,7 +203,7 @@ class UserController extends Controller Event::dispatch('user.admin.delete.after', $id); return response()->json(['message' => true], 200); - } catch (Exception $e) { + } catch (\Exception $e) { session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Admin'])); } } @@ -208,7 +225,7 @@ class UserController extends Controller } /** - * destroy current after confirming + * Destroy current after confirming. * * @return \Illuminate\Http\Response */ diff --git a/packages/Webkul/User/src/Repositories/AdminRepository.php b/packages/Webkul/User/src/Repositories/AdminRepository.php index c8cfcf24b..061130d1d 100755 --- a/packages/Webkul/User/src/Repositories/AdminRepository.php +++ b/packages/Webkul/User/src/Repositories/AdminRepository.php @@ -7,12 +7,26 @@ use Webkul\Core\Eloquent\Repository; class AdminRepository extends Repository { /** - * Specify Model class name + * Specify model class name. * * @return mixed */ - function model() + function model(): string { - return 'Webkul\User\Contracts\Admin'; + return \Webkul\User\Contracts\Admin::class; } -} \ No newline at end of file + + /** + * Count admins with all access. + * + * @return int + */ + public function countAdminsWithAllAccess(): int + { + return $this->getModel() + ->leftJoin('roles', 'admins.role_id', '=', 'roles.id') + ->where(["roles.permission_type" => "all"]) + ->get() + ->count(); + } +}