From 61db3dc551e4a23a21dd902f45eb3675c137b347 Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 28 Dec 2016 18:09:23 +0000 Subject: [PATCH] Updating Auth package --- src/AuthServiceProvider.php | 2 +- src/Http/Controllers/Admin/Controller.php | 4 +- .../Admin/Profile/UpdatePasswordRequest.php | 20 ++------ .../Admin/Roles/CreateRoleRequest.php | 2 +- .../Requests/Admin/Roles/RoleFormRequest.php | 35 ++++++++----- .../Admin/Roles/UpdateRoleRequest.php | 4 +- .../Admin/Users/CreateUserRequest.php | 6 +-- .../Admin/Users/UpdateUserRequest.php | 10 ++-- .../Requests/Admin/Users/UserFormRequest.php | 49 +++++++++++++------ .../Requests/Front/RegisterUserRequest.php | 11 +++-- ...er.php => ViewComposerServiceProvider.php} | 12 +---- 11 files changed, 86 insertions(+), 69 deletions(-) rename src/Providers/{ComposerServiceProvider.php => ViewComposerServiceProvider.php} (92%) diff --git a/src/AuthServiceProvider.php b/src/AuthServiceProvider.php index b2e966f..8f08f49 100644 --- a/src/AuthServiceProvider.php +++ b/src/AuthServiceProvider.php @@ -52,7 +52,7 @@ public function register() Providers\EventServiceProvider::class, Providers\PackagesServiceProvider::class, Providers\AuthorizationServiceProvider::class, - Providers\ComposerServiceProvider::class, + Providers\ViewComposerServiceProvider::class, ]); $this->registerConsoleServiceProvider(Providers\CommandServiceProvider::class); } diff --git a/src/Http/Controllers/Admin/Controller.php b/src/Http/Controllers/Admin/Controller.php index f1e9d58..afc405d 100644 --- a/src/Http/Controllers/Admin/Controller.php +++ b/src/Http/Controllers/Admin/Controller.php @@ -1,6 +1,6 @@ */ -abstract class Controller extends BaseController +abstract class Controller extends AdminController { /* ------------------------------------------------------------------------------------------------ | Traits diff --git a/src/Http/Requests/Admin/Profile/UpdatePasswordRequest.php b/src/Http/Requests/Admin/Profile/UpdatePasswordRequest.php index 832d632..62b5507 100644 --- a/src/Http/Requests/Admin/Profile/UpdatePasswordRequest.php +++ b/src/Http/Requests/Admin/Profile/UpdatePasswordRequest.php @@ -10,17 +10,6 @@ */ class UpdatePasswordRequest extends FormRequest { - /* ------------------------------------------------------------------------------------------------ - | Properties - | ------------------------------------------------------------------------------------------------ - */ - /** - * Authenticated user. - * - * @var \Arcanesoft\Contracts\Auth\Models\User $user - */ - protected $user; - /* ------------------------------------------------------------------------------------------------ | Main Functions | ------------------------------------------------------------------------------------------------ @@ -32,9 +21,10 @@ class UpdatePasswordRequest extends FormRequest */ public function authorize() { - $this->user = $this->route('auth_user'); + /** @var \Arcanesoft\Contracts\Auth\Models\User $user */ + $user = $this->route('auth_user'); - return $this->user->id === auth()->user()->id; + return $user->id === auth()->user()->id; } /** @@ -45,8 +35,8 @@ public function authorize() public function rules() { return [ - 'old_password' => 'required|min:8|different:password|user_password', - 'password' => 'required|min:8|different:old_password|confirmed', + 'old_password' => ['required', 'min:8', 'different:password', 'user_password'], + 'password' => ['required', 'min:8', 'different:old_password', 'confirmed'], ]; } diff --git a/src/Http/Requests/Admin/Roles/CreateRoleRequest.php b/src/Http/Requests/Admin/Roles/CreateRoleRequest.php index 23c4f2d..25caca1 100644 --- a/src/Http/Requests/Admin/Roles/CreateRoleRequest.php +++ b/src/Http/Requests/Admin/Roles/CreateRoleRequest.php @@ -30,7 +30,7 @@ public function authorize() public function rules() { return array_merge(parent::rules(), [ - 'slug' => 'required|min:3|unique:roles,slug', + 'slug' => ['required', 'min:3', $this->getSlugRule()], ]); } } diff --git a/src/Http/Requests/Admin/Roles/RoleFormRequest.php b/src/Http/Requests/Admin/Roles/RoleFormRequest.php index 2959d8e..bd6ce3a 100644 --- a/src/Http/Requests/Admin/Roles/RoleFormRequest.php +++ b/src/Http/Requests/Admin/Roles/RoleFormRequest.php @@ -3,6 +3,7 @@ use Arcanesoft\Auth\Http\Requests\FormRequest; use Arcanesoft\Auth\Models\Permission; use Arcanesoft\Auth\Models\Role; +use Illuminate\Validation\Rule; /** * Class RoleFormRequest @@ -24,26 +25,38 @@ abstract class RoleFormRequest extends FormRequest public function rules() { return [ - 'name' => 'required|min:3', - 'slug' => 'required|min:3|unique:roles,slug', - 'description' => 'required|min:10', - 'permissions' => 'required|array|in:'.Permission::getIds()->implode(','), + 'name' => ['required', 'min:3'], + 'slug' => ['required', 'min:3', $this->getSlugRule()], + 'description' => ['required', 'min:10'], + 'permissions' => ['required', 'array', 'in:'.Permission::getIds()->implode(',')], ]; } + /* ------------------------------------------------------------------------------------------------ + | Other Functions + | ------------------------------------------------------------------------------------------------ + */ /** * Sanitize the inputs. * - * @param array $inputs - * * @return array */ - public function sanitize(array $inputs) + protected function sanitize() { - $inputs['slug'] = (new Role)->makeSlugName( - $this->get($this->has('slug') ? 'slug' : 'name') - ); + return [ + 'slug' => (new Role)->makeSlugName($this->get($this->has('slug') ? 'slug' : 'name')) + ]; + } - return $inputs; + /** + * Get the slug rule. + * + * @param string $column + * + * @return \Illuminate\Validation\Rules\Unique + */ + protected function getSlugRule($column = 'slug') + { + return Rule::unique('roles', $column); } } diff --git a/src/Http/Requests/Admin/Roles/UpdateRoleRequest.php b/src/Http/Requests/Admin/Roles/UpdateRoleRequest.php index f4704c9..7618637 100644 --- a/src/Http/Requests/Admin/Roles/UpdateRoleRequest.php +++ b/src/Http/Requests/Admin/Roles/UpdateRoleRequest.php @@ -29,11 +29,11 @@ public function authorize() */ public function rules() { - /** @var \Arcanesoft\Auth\Models\Role $role */ + /** @var \Arcanesoft\Auth\Models\Role $role */ $role = $this->route('auth_role'); return array_merge(parent::rules(), [ - 'slug' => "required|min:3|unique:roles,slug,{$role->id}", + 'slug' => ['required', 'min:3', $this->getSlugRule()->ignore($role->id)], ]); } } diff --git a/src/Http/Requests/Admin/Users/CreateUserRequest.php b/src/Http/Requests/Admin/Users/CreateUserRequest.php index d9725a0..da398ca 100644 --- a/src/Http/Requests/Admin/Users/CreateUserRequest.php +++ b/src/Http/Requests/Admin/Users/CreateUserRequest.php @@ -30,9 +30,9 @@ public function authorize() public function rules() { return array_merge(parent::rules(), [ - 'username' => 'required|min:3|unique:users,username', - 'email' => 'required|email|unique:users,email', - 'password' => 'required|min:8|confirmed', + 'username' => ['required', 'min:3', $this->getUsernameRule()], + 'email' => ['required', 'email', $this->getEmailRule()], + 'password' => ['required', 'min:8', 'confirmed'], ]); } } diff --git a/src/Http/Requests/Admin/Users/UpdateUserRequest.php b/src/Http/Requests/Admin/Users/UpdateUserRequest.php index b9972ca..51889fd 100644 --- a/src/Http/Requests/Admin/Users/UpdateUserRequest.php +++ b/src/Http/Requests/Admin/Users/UpdateUserRequest.php @@ -29,14 +29,14 @@ public function authorize() */ public function rules() { - /** @var \Arcanesoft\Contracts\Auth\Models\User $user */ + /** @var \Arcanesoft\Contracts\Auth\Models\User $user */ $user = $this->route('auth_user'); return array_merge(parent::rules(), [ - 'username' => "required|min:3|unique:users,username,{$user->id}", - 'email' => "required|email|unique:users,email,{$user->id}", - 'password' => 'required_with:password_confirmation|min:8|confirmed', - 'password_confirmation' => 'required_with:password', + 'username' => ['required', 'min:3', $this->getUsernameRule()->ignore($user->id)], + 'email' => ['required', 'email', $this->getEmailRule()->ignore($user->id)], + 'password' => ['required_with:password_confirmation', 'min:8', 'confirmed'], + 'password_confirmation' => ['required_with:password'], ]); } } diff --git a/src/Http/Requests/Admin/Users/UserFormRequest.php b/src/Http/Requests/Admin/Users/UserFormRequest.php index 8f7468c..1cf7cf3 100644 --- a/src/Http/Requests/Admin/Users/UserFormRequest.php +++ b/src/Http/Requests/Admin/Users/UserFormRequest.php @@ -4,6 +4,7 @@ use Arcanesoft\Contracts\Auth\Models\Role as RoleContract; use Cache; use Illuminate\Support\Str; +use Illuminate\Validation\Rule; /** * Class UserFormRequest @@ -25,8 +26,8 @@ abstract class UserFormRequest extends FormRequest public function rules() { return [ - 'first_name' => 'required|min:2', - 'last_name' => 'required|min:2', + 'first_name' => ['required', 'min:2'], + 'last_name' => ['required', 'min:2'], 'roles' => $this->getRolesRule(), ]; } @@ -34,15 +35,13 @@ public function rules() /** * Sanitize all inputs. * - * @param array $inputs - * * @return array */ - public function sanitize(array $inputs) + protected function sanitize() { - $inputs['username'] = $this->sanitizeUsername($inputs); - - return $inputs; + return [ + 'username' => $this->sanitizeUsername(), + ]; } /* ------------------------------------------------------------------------------------------------ @@ -52,19 +51,41 @@ public function sanitize(array $inputs) /** * Sanitize the username. * - * @param array $inputs - * * @return string */ - protected function sanitizeUsername(array $inputs) + protected function sanitizeUsername() { $username = $this->has('username') - ? $inputs['username'] - : Str::limit($inputs['first_name'], 1, '.') . ' ' . $inputs['last_name']; + ? $this->get('username') + : Str::limit($this->get('first_name'), 1, '.').' '.$this->get('last_name'); return Str::slug($username, config('arcanesoft.auth.slug-separator', '.')); } + /** + * Get the email rule. + * + * @param string $column + * + * @return \Illuminate\Validation\Rules\Unique + */ + protected function getEmailRule($column = 'email') + { + return Rule::unique('users', $column); + } + + /** + * Get the username rule. + * + * @param string $column + * + * @return \Illuminate\Validation\Rules\Unique + */ + protected function getUsernameRule($column = 'username') + { + return Rule::unique('users', $column); + } + /** * Get the roles rule. * @@ -76,6 +97,6 @@ protected function getRolesRule() return app(RoleContract::class)->pluck('id'); }); - return 'required|array|min:1|in:' . $rolesIds->implode(','); + return ['required', 'array', 'min:1', 'in:'.$rolesIds->implode(',')]; } } diff --git a/src/Http/Requests/Front/RegisterUserRequest.php b/src/Http/Requests/Front/RegisterUserRequest.php index 6847aa2..e80f134 100644 --- a/src/Http/Requests/Front/RegisterUserRequest.php +++ b/src/Http/Requests/Front/RegisterUserRequest.php @@ -1,6 +1,7 @@ 'required|email|max:255|unique:users', - 'password' => 'required|confirmed|min:8|max:30', - 'username' => 'required|max:30', - 'first_name' => 'required|max:30', - 'last_name' => 'required|max:30', + 'email' => ['required', 'email', 'max:255', Rule::unique('users', 'email')], + 'password' => ['required', 'confirmed', 'min:8', 'max:30'], + 'username' => ['required', 'max:30'], + 'first_name' => ['required', 'max:30'], + 'last_name' => ['required', 'max:30'], ]; } } diff --git a/src/Providers/ComposerServiceProvider.php b/src/Providers/ViewComposerServiceProvider.php similarity index 92% rename from src/Providers/ComposerServiceProvider.php rename to src/Providers/ViewComposerServiceProvider.php index 0d59f9d..8ea2e09 100644 --- a/src/Providers/ComposerServiceProvider.php +++ b/src/Providers/ViewComposerServiceProvider.php @@ -4,12 +4,12 @@ use \Arcanesoft\Auth\ViewComposers\Dashboard; /** - * Class ComposerServiceProvider + * Class ViewComposerServiceProvider * * @package Arcanesoft\Auth\Providers * @author ARCANEDEV */ -class ComposerServiceProvider extends ServiceProvider +class ViewComposerServiceProvider extends ServiceProvider { /* ------------------------------------------------------------------------------------------------ | Main Functions @@ -24,14 +24,6 @@ public function boot() $this->registerOtherComposers(); } - /** - * {@inheritdoc} - */ - public function register() - { - // - } - /* ------------------------------------------------------------------------------------------------ | Other Functions | ------------------------------------------------------------------------------------------------