diff --git a/packages/Webkul/Admin/src/Resources/lang/ar/app.php b/packages/Webkul/Admin/src/Resources/lang/ar/app.php index 9e25c1489..580d7e3b8 100644 --- a/packages/Webkul/Admin/src/Resources/lang/ar/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/ar/app.php @@ -1337,10 +1337,10 @@ return [ 'response' => [ 'being-used' => ':source في :name يتم استخدام هذا المورد', - 'single-admin-present' => 'Cannot change the role if only one admin with all access is present.', 'product-copied' => 'تم نسخ المنتج', 'error-while-copying' => 'خطأ في نسخ المنتج', 'product-can-not-be-copied' => 'لا يمكن نسخ منتجات الحجز', + 'cannot-change' => 'Cannot change the :name.', 'cannot-delete-default' => 'لا يمكن حذف القناة الافتراضية', 'create-success' => 'إنشاء الاسم بنجاح:name', 'update-success' => 'تحديث الاسم بنجاح :name ', diff --git a/packages/Webkul/Admin/src/Resources/lang/de/app.php b/packages/Webkul/Admin/src/Resources/lang/de/app.php index 6b772c2e4..b4f73a226 100755 --- a/packages/Webkul/Admin/src/Resources/lang/de/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/de/app.php @@ -1344,10 +1344,10 @@ return [ 'response' => [ 'being-used' => 'Diese Ressource :name wird verwendet in :source', - 'single-admin-present' => 'Cannot change the role if only one admin with all access is present.', 'product-copied' => 'Das Produkt wurde kopiert', 'error-while-copying' => 'Fehler beim Kopieren des Produkts', 'product-can-not-be-copied' => 'Produkte vom Typ :type können nicht kopiert werden.', + 'cannot-change' => 'Cannot change the :name.', 'cannot-delete-default' => 'Der Standardkanal kann nicht gelöscht werden', 'create-success' => ':name erfolgreich erstellt.', 'update-success' => ':name erfolgreich aktualisiert.', diff --git a/packages/Webkul/Admin/src/Resources/lang/en/app.php b/packages/Webkul/Admin/src/Resources/lang/en/app.php index 4a3fbbe9d..a5bd6fc93 100755 --- a/packages/Webkul/Admin/src/Resources/lang/en/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/en/app.php @@ -1353,10 +1353,10 @@ 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', + 'cannot-change' => 'Cannot change the :name.', 'cannot-delete-default' => 'Cannot delete the default channel', 'create-success' => ':name created successfully.', 'update-success' => ':name updated successfully.', diff --git a/packages/Webkul/Admin/src/Resources/lang/es/app.php b/packages/Webkul/Admin/src/Resources/lang/es/app.php index 43e7f0095..2181e00f0 100644 --- a/packages/Webkul/Admin/src/Resources/lang/es/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/es/app.php @@ -1346,10 +1346,10 @@ return [ 'response' => [ 'being-used' => 'Este recurso: :name está siendo usado en :source', - 'single-admin-present' => 'Cannot change the role if only one admin with all access is present.', 'product-copied' => 'El Producto ha sido copiado', 'error-while-copying' => 'Se produjo un error al intentar copiar el producto', 'product-can-not-be-copied' => 'Productos del tipo :type no se pueden copiar', + 'cannot-change' => 'Cannot change the :name.', 'cannot-delete-default' => 'No puede borrar el canal predeterminado', 'create-success' => ':name :name creado con éxito.', 'update-success' => ':name actualizado con éxito.', diff --git a/packages/Webkul/Admin/src/Resources/lang/fa/app.php b/packages/Webkul/Admin/src/Resources/lang/fa/app.php index 9fc2e4776..6b6f79ac4 100644 --- a/packages/Webkul/Admin/src/Resources/lang/fa/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/fa/app.php @@ -1337,7 +1337,7 @@ return [ 'response' => [ 'being-used' => ' مورد استفاده قرار می گیرد :source در :name این منبع', - 'single-admin-present' => 'Cannot change the role if only one admin with all access is present.', + 'cannot-change' => 'Cannot change the :name.', 'cannot-delete-default' => 'کانال پیش فرض حذف نمی شود', 'create-success' => ' با موفقیت ایجاد شد :name', 'update-success' => ' با موفقیت به روز شد :name', diff --git a/packages/Webkul/Admin/src/Resources/lang/fr/app.php b/packages/Webkul/Admin/src/Resources/lang/fr/app.php index 09d56b0e7..031d957e4 100644 --- a/packages/Webkul/Admin/src/Resources/lang/fr/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/fr/app.php @@ -1360,10 +1360,10 @@ return [ 'response' => [ 'being-used' => 'Cette ressource :name est utilisée dans :source', - 'single-admin-present' => 'Cannot change the role if only one admin with all access is present.', 'product-copied' => 'Le produit a été copié', 'error-while-copying' => 'Une erreur s\'est produite lors de la tentative de copie du produit', 'product-can-not-be-copied' => 'Les produits de type :type ne peuvent pas être copiés', + 'cannot-change' => 'Cannot change the :name.', 'cannot-delete-default' => 'Impossible de supprimer la chaîne par défaut', 'create-success' => ':name créé avec succès.', 'update-success' => ':name mis à jour avec succès.', diff --git a/packages/Webkul/Admin/src/Resources/lang/it/app.php b/packages/Webkul/Admin/src/Resources/lang/it/app.php index 0343d217c..a1e6c1353 100644 --- a/packages/Webkul/Admin/src/Resources/lang/it/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/it/app.php @@ -1340,7 +1340,7 @@ return [ 'response' => [ 'being-used' => 'Questo resource :name is getting used in :source', - 'single-admin-present' => 'Cannot change the role if only one admin with all access is present.', + 'cannot-change' => 'Cannot change the :name.', 'cannot-delete-default' => 'Non è possibile eliminare il canale di default', 'create-success' => ':name creato con successo.', 'update-success' => ':name aggiornato con successo.', diff --git a/packages/Webkul/Admin/src/Resources/lang/nl/app.php b/packages/Webkul/Admin/src/Resources/lang/nl/app.php index d25072c36..5d3ef5ff4 100644 --- a/packages/Webkul/Admin/src/Resources/lang/nl/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/nl/app.php @@ -1334,7 +1334,7 @@ return [ 'response' => [ 'being-used' => 'Deze bron :name is wennen :source', - 'single-admin-present' => 'Cannot change the role if only one admin with all access is present.', + 'cannot-change' => 'Cannot change the :name.', 'cannot-delete-default' => 'Kan het standaardkanaal niet verwijderen', 'create-success' => ':name succesvol gemaakt.', 'update-success' => ':name succesvol geupdatet.', diff --git a/packages/Webkul/Admin/src/Resources/lang/pl/app.php b/packages/Webkul/Admin/src/Resources/lang/pl/app.php index 4112d8c66..1bb09a780 100644 --- a/packages/Webkul/Admin/src/Resources/lang/pl/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/pl/app.php @@ -1337,7 +1337,7 @@ return [ 'response' => [ 'being-used' => 'Ten zasób :name est używana w :source', - 'single-admin-present' => 'Cannot change the role if only one admin with all access is present.', + 'cannot-change' => 'Cannot change the :name.', 'cannot-delete-default' => 'Nie można usunąć domyślnego kanału', 'create-success' => ':name została utworzona pomyślnie.', 'update-success' => ':name została zaktualizowana pomyślnie.', diff --git a/packages/Webkul/Admin/src/Resources/lang/pt_BR/app.php b/packages/Webkul/Admin/src/Resources/lang/pt_BR/app.php index 718f20f19..6aa9f5397 100755 --- a/packages/Webkul/Admin/src/Resources/lang/pt_BR/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/pt_BR/app.php @@ -1336,7 +1336,7 @@ return [ 'response' => [ 'being-used' => 'Este recurso :name está sendo usado em :source', - 'single-admin-present' => 'Cannot change the role if only one admin with all access is present.', + 'cannot-change' => 'Cannot change the :name.', 'cannot-delete-default' => 'Não é possível excluir o canal padrão', 'create-success' => ':name criado com sucesso.', 'update-success' => ':name atualizaco com sucesso.', diff --git a/packages/Webkul/Admin/src/Resources/lang/tr/app.php b/packages/Webkul/Admin/src/Resources/lang/tr/app.php index 5e0569e00..305526fd1 100644 --- a/packages/Webkul/Admin/src/Resources/lang/tr/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/tr/app.php @@ -1324,7 +1324,7 @@ return [ 'response' => [ 'being-used' => ':name isimli kaynak :source isimli kaynakta kullanılıyor.', - 'single-admin-present' => 'Cannot change the role if only one admin with all access is present.', + 'cannot-change' => 'Cannot change the :name.', 'cannot-delete-default' => 'Varsayılan kanal silinemez.', 'create-success' => ':name başarıyla oluşturuldu.', 'update-success' => ':name başarıyla güncellendi.', diff --git a/packages/Webkul/User/src/Http/Controllers/UserController.php b/packages/Webkul/User/src/Http/Controllers/UserController.php index e74204319..901da33f6 100755 --- a/packages/Webkul/User/src/Http/Controllers/UserController.php +++ b/packages/Webkul/User/src/Http/Controllers/UserController.php @@ -124,46 +124,17 @@ class UserController extends Controller */ public function update(UserForm $request, $id) { - $data = $request->all(); + $data = $this->prepareUserData($request, $id); - $user = $this->adminRepository->find($id); - - /** - * Is password changed. - */ - $isPasswordChanged = false; - - if (! $data['password']) { - unset($data['password']); - } else { - $isPasswordChanged = true; - - $data['password'] = bcrypt($data['password']); - } - - /** - * 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']); + if ($data instanceof \Illuminate\Http\RedirectResponse) { + return $data; } Event::dispatch('user.admin.update.before', $id); $admin = $this->adminRepository->update($data, $id); - if ($isPasswordChanged) { + if (isset($data['password']) && $data['password']) { Event::dispatch('user.admin.update-password', $admin); } @@ -182,7 +153,7 @@ class UserController extends Controller */ public function destroy($id) { - $user = $this->adminRepository->findOrFail($id); + $this->adminRepository->findOrFail($id); if ($this->adminRepository->count() == 1) { session()->flash('error', trans('admin::app.response.last-delete-error', ['name' => 'Admin'])); @@ -255,4 +226,66 @@ class UserController extends Controller return redirect()->route($this->_config['redirect']); } } + + /** + * Validate user data. + * + * @param \Webkul\User\Http\Requests\UserForm $request + * @param int $id + * @return array|\Illuminate\Http\RedirectResponse + */ + private function prepareUserData(UserForm $request, $id) + { + $data = $request->validated(); + + $user = $this->adminRepository->find($id); + + /** + * Password check. + */ + if (! $data['password']) { + unset($data['password']); + } else { + $data['password'] = bcrypt($data['password']); + } + + /** + * Is user with `permission_type` all changed status. + */ + $data['status'] = isset($data['status']) ? 1 : 0; + + $isStatusChangedToInactive = (int) $data['status'] === 0 && (int) $user->status === 1; + + if ($isStatusChangedToInactive && $this->adminRepository->countAdminsWithAllAccessAndActiveStatus() === 1) { + return $this->cannotChangeRedirectResponse('status'); + } + + /** + * 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) { + return $this->cannotChangeRedirectResponse('role'); + } + + return $data; + } + + /** + * Cannot change redirect response. + * + * @param string $columnName + * @return \Illuminate\Http\RedirectResponse + */ + private function cannotChangeRedirectResponse(string $columnName): \Illuminate\Http\RedirectResponse + { + session()->flash('error', trans('admin::app.response.cannot-change', [ + 'name' => $columnName + ])); + + return redirect()->route($this->_config['redirect']); + } } diff --git a/packages/Webkul/User/src/Http/Requests/UserForm.php b/packages/Webkul/User/src/Http/Requests/UserForm.php index dd200020b..2aa5bcb14 100755 --- a/packages/Webkul/User/src/Http/Requests/UserForm.php +++ b/packages/Webkul/User/src/Http/Requests/UserForm.php @@ -6,8 +6,6 @@ use Illuminate\Foundation\Http\FormRequest; class UserForm extends FormRequest { - protected $rules; - /** * Determine if the user is authorized to make this request. * @@ -25,7 +23,7 @@ class UserForm extends FormRequest */ public function rules() { - $this->rules = [ + $rules = [ 'name' => 'required', 'email' => 'email|unique:admins,email', 'password' => 'nullable', @@ -35,9 +33,9 @@ class UserForm extends FormRequest ]; if ($this->method() == 'PUT') { - $this->rules['email'] = 'email|unique:admins,email,' . $this->route('id'); + $rules['email'] = 'email|unique:admins,email,' . $this->route('id'); } - return $this->rules; + return $rules; } } diff --git a/packages/Webkul/User/src/Repositories/AdminRepository.php b/packages/Webkul/User/src/Repositories/AdminRepository.php index 061130d1d..db0cb3008 100755 --- a/packages/Webkul/User/src/Repositories/AdminRepository.php +++ b/packages/Webkul/User/src/Repositories/AdminRepository.php @@ -25,7 +25,22 @@ class AdminRepository extends Repository { return $this->getModel() ->leftJoin('roles', 'admins.role_id', '=', 'roles.id') - ->where(["roles.permission_type" => "all"]) + ->where('roles.permission_type', 'all') + ->get() + ->count(); + } + + /** + * Count admins with all access and active status. + * + * @return int + */ + public function countAdminsWithAllAccessAndActiveStatus(): int + { + return $this->getModel() + ->leftJoin('roles', 'admins.role_id', '=', 'roles.id') + ->where('admins.status', 1) + ->where('roles.permission_type', 'all') ->get() ->count(); }