Skip to content

Commit

Permalink
Updating Auth package
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Dec 28, 2016
1 parent ef73e27 commit 61db3dc
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/Admin/Controller.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Arcanesoft\Auth\Http\Controllers\Admin;

use Arcanesoft\Core\Bases\AdminController as BaseController;
use Arcanesoft\Core\Bases\AdminController;
use Arcanesoft\Core\Traits\Notifyable;

/**
Expand All @@ -9,7 +9,7 @@
* @package Arcanesoft\Auth\Http\Controllers\Admin
* @author ARCANEDEV <[email protected]>
*/
abstract class Controller extends BaseController
abstract class Controller extends AdminController
{
/* ------------------------------------------------------------------------------------------------
| Traits
Expand Down
20 changes: 5 additions & 15 deletions src/Http/Requests/Admin/Profile/UpdatePasswordRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@
*/
class UpdatePasswordRequest extends FormRequest
{
/* ------------------------------------------------------------------------------------------------
| Properties
| ------------------------------------------------------------------------------------------------
*/
/**
* Authenticated user.
*
* @var \Arcanesoft\Contracts\Auth\Models\User $user
*/
protected $user;

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
Expand All @@ -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;
}

/**
Expand All @@ -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'],
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Requests/Admin/Roles/CreateRoleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
]);
}
}
35 changes: 24 additions & 11 deletions src/Http/Requests/Admin/Roles/RoleFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/Http/Requests/Admin/Roles/UpdateRoleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
]);
}
}
6 changes: 3 additions & 3 deletions src/Http/Requests/Admin/Users/CreateUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
]);
}
}
10 changes: 5 additions & 5 deletions src/Http/Requests/Admin/Users/UpdateUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
]);
}
}
49 changes: 35 additions & 14 deletions src/Http/Requests/Admin/Users/UserFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract;
use Cache;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;

/**
* Class UserFormRequest
Expand All @@ -25,24 +26,22 @@ 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(),
];
}

/**
* 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(),
];
}

/* ------------------------------------------------------------------------------------------------
Expand All @@ -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.
*
Expand All @@ -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(',')];
}
}
11 changes: 6 additions & 5 deletions src/Http/Requests/Front/RegisterUserRequest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Arcanesoft\Auth\Http\Requests\Front;

use Arcanedev\Support\Bases\FormRequest;
use Illuminate\Validation\Rule;

/**
* Class RegisterUserRequest
Expand Down Expand Up @@ -32,11 +33,11 @@ public function authorize()
public function rules()
{
return [
'email' => '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'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
use \Arcanesoft\Auth\ViewComposers\Dashboard;

/**
* Class ComposerServiceProvider
* Class ViewComposerServiceProvider
*
* @package Arcanesoft\Auth\Providers
* @author ARCANEDEV <[email protected]>
*/
class ComposerServiceProvider extends ServiceProvider
class ViewComposerServiceProvider extends ServiceProvider
{
/* ------------------------------------------------------------------------------------------------
| Main Functions
Expand All @@ -24,14 +24,6 @@ public function boot()
$this->registerOtherComposers();
}

/**
* {@inheritdoc}
*/
public function register()
{
//
}

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 61db3dc

Please sign in to comment.