diff --git a/app/Repositories/Core/Auth/PasswordSecurities.php b/app/Repositories/Core/Auth/PasswordSecurities.php new file mode 100644 index 00000000..bdf1662c --- /dev/null +++ b/app/Repositories/Core/Auth/PasswordSecurities.php @@ -0,0 +1,28 @@ + $user_id, + 'password_expiry_days' => $delay, + 'password_updated_at' => Carbon::now(), + ]); + } + + public static function getUserName($id) + { + return self::getUser($id)->username; + } + + public static function getUser($id) + { + return PasswordSecurity::with('user')->find($id)->user; + } +} diff --git a/app/Repositories/Core/Auth/Passwords.php b/app/Repositories/Core/Auth/Passwords.php new file mode 100644 index 00000000..5ce753e0 --- /dev/null +++ b/app/Repositories/Core/Auth/Passwords.php @@ -0,0 +1,23 @@ +setMinLength(5); + $validator->setMinLowerCaseLetters(2); + $validator->setMinUpperCaseLetters(1); + $validator->setMinNumbers(1); + $validator->setMinSymbols(3); + + if ($validator->isValid($password)) { + printf('password %s is valid'.PHP_EOL, $password); + } else { + printf('password %s is invalid'.PHP_EOL, $password); + var_dump($validator->getErrors()); + } + } +} diff --git a/app/Repositories/Core/Auth/Permissions.php b/app/Repositories/Core/Auth/Permissions.php new file mode 100644 index 00000000..828e609f --- /dev/null +++ b/app/Repositories/Core/Auth/Permissions.php @@ -0,0 +1,26 @@ +distinct('module')->get()->pluck('module'); + } + + public static function getByName($name) + { + return Permission::where('name', $name)->first(); + } + + public static function getModel() + { + return Permission::query(); + } +} diff --git a/app/Repositories/Core/Auth/Roles.php b/app/Repositories/Core/Auth/Roles.php new file mode 100644 index 00000000..83f00f79 --- /dev/null +++ b/app/Repositories/Core/Auth/Roles.php @@ -0,0 +1,83 @@ +hasRole('admin') ? Role::whereNotIn('name', ['admin'])->get() : Role::all(); + + return $data->pluck('name', 'id')->toArray(); + } + + public static function create($data) + { + $permissions = array_keys($data['permissions']); + unset($data['permissions']); + $data['active'] = true; + $role = Role::create($data); + $role->attachPermissions($permissions); + + return $role; + } + + public static function update($input, $id = false) + { + $id = $id ? $id : $input['id']; + $permissions = array_keys($input['permissions']); + $role = self::get($id); + $role->update(['name' => $input['name']]); + $role->syncPermissions($permissions); + + return $role; + } + + public static function toggleActive($id, $active) + { + return Role::find($id)->update(['active' => $active]); + } + + public static function getWithPermissions($id) + { + $role = self::get($id)->toArray(); + $role['permissions'] = self::get($id)->permissions->pluck('id')->toArray(); + + return $role; + } + + public static function getByName($name) + { + return Role::where('name', $name)->first(); + } + + public static function getRolesByUser($user_id = false) + { + $user_id = $user_id ? $user_id : Users::getId(); + + return RoleUser::byUser($user_id); + } + + public static function getUsersByRole($id) + { + return RoleUser::byTeam($id)->get(); + } + + public static function getUsersIdByRole($id) + { + return self::getUsersByRole($id)->pluck('user_id'); + } + + public static function getModel() + { + return RoleUser::query(); + } +} diff --git a/app/Repositories/Core/Auth/Teams.php b/app/Repositories/Core/Auth/Teams.php new file mode 100644 index 00000000..b8495429 --- /dev/null +++ b/app/Repositories/Core/Auth/Teams.php @@ -0,0 +1,61 @@ +get(); + } + + public static function getUsersIdByTeam($id) + { + return self::getUsersByTeam($id)->pluck('user_id'); + } + + public static function getUsersByTeam2($id) + { + return Team::find($id)->users(); + } + + public static function getByName($name) + { + return Team::where('name', $name)->first(); + } + + public static function delete($id) + { + Users::destroyByUniqueTeam($id); + + return Team::destroy($id); + } + + public static function destroyBySociete($id) + { + $teams = Team::bySociete($id)->get(); + foreach ($teams as $team) { + self::delete($team->id); + } + } + + public static function getModel() + { + return Team::query(); + } +} diff --git a/app/Repositories/Core/Auth/Users.php b/app/Repositories/Core/Auth/Users.php index 3ff8d06a..8941a0c4 100644 --- a/app/Repositories/Core/Auth/Users.php +++ b/app/Repositories/Core/Auth/Users.php @@ -9,12 +9,57 @@ use App\Traits\Model\Basic; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Str; +use LangleyFoxall\LaravelNISTPasswordRules\PasswordRules; use Laratrust\Traits\LaratrustUserTrait; class Users { use Basic, LaratrustUserTrait; + public static function isAdmin() + { + return self::hasRole('admin'); + } + + public static function getInfo($id = false) + { + $id = $id ? $id : self::getId(); + if (! $id) { + return false; + } + $user = self::get($id); + $data = $user->toArray(); + $data['name'] = $user->name; + $data['avatar'] = self::getAvatar($id); + $data['roles'] = $user->roles->pluck('id')->toArray(); + $data['permissions'] = $user->allPermissions()->pluck('id')->toArray(); + + return $data; + } + + public static function store($data) + { + if ($data['id'] ?? false) { + unset($data['password']); + } + $user = $data['id'] ?? false ? self::update($data) : self::create($data); + $user->roles()->sync(array_keys($data['roles'] ?? [])); + + return $user; + } + + public static function create($data) + { + $data['password'] = $data['password'] ?? Hash::make($data['password']); + $data['remember_token'] = Str::random(32); + $data['active'] = true; + $user = User::create($data); + PasswordSecurities::create($user->id); + + return $user; + } + public static function getId() { $user = self::getUser(); @@ -49,6 +94,96 @@ class Users return User::orderBy('name')->pluck('name', 'id')->toArray(); } + public static function delete($id) + { + $ret = RoleUser::byUser($id)->delete(); + + return User::destroy($id); + } + + public static function getListByRole($role) + { + return self::selectOptions()->orderBy('name')->whereRoleIs($role)->get(); + } + + public static function hasRole($role, $user = false) + { + $user = $user ? $user : self::getUser(); + + return $user ? $user->hasRole($role) : false; + } + + public static function hasPermission($permission, $user = false) + { + if (self::isAdmin()) { + return true; + } + $user = $user ? $user : self::getUser(); + $permissions = self::getPermissions($user); + + return $user ? self::checkPermission($permissions, $permission) : false; + } + + public static function checkPermission($permissions, $permission) + { + if (! strpos($permission, '*')) { + return in_array($permission, $permissions); + } + $permission = str_replace('*', '', $permission); + foreach ($permissions as $item) { + if (stripos($item, $permission) !== false) { + return true; + } + } + + return false; + } + + public static function getRoles($user = false) + { + $user = $user ? $user : self::getUser(); + + return $user ? $user->roles->pluck('name')->toArray() : false; + } + + public static function getRolesToEdit() + { + return Roles::getListByRights(); + } + + public static function getPermissions($user = false) + { + $user = $user ? $user : self::getUser(); + + return $user ? $user->allPermissions()->pluck('name')->toArray() : false; + } + + public static function getByTeam($id) + { + return User::byTeam($id)->get(); + } + + public static function getByUniqueTeam($id) + { + return User::byTeam($id)->byUniqueTeam()->get(); + } + + public static function destroyByUniqueTeam($id) + { + return User::byTeam($id)->byUniqueTeam()->delete(); + } + + public static function getAvatar($user_id) + { + $avatar = self::get($user_id)->avatar; + if (! $avatar) { + return '/assets/img/no-avatar.png'; + } + $path = '/images/avatars/'; + + return $path.$avatar; + } + public static function selectOptions() { return User::select('id', DB::raw("concat(last_name,' ',first_name) as name")); @@ -64,6 +199,24 @@ class Users return self::get($id)->update(['active' => $active]); } + public static function uploadAvatar($request) + { + $targetDir = 'uploads'; + $file = $request->file('avatar_file'); + $data = Upload::getData($file); + $file_uploaded = Upload::store($file, $targetDir); + $tab = pathinfo($file_uploaded); + + return [ + 'name' => $tab['basename'], + ]; + } + + public static function updateAvatar($id, $avatar) + { + return User::find($id)->update(['avatar' => $avatar]); + } + public static function updatePassword($id, $password) { $password = Hash::make($password); @@ -71,6 +224,11 @@ class Users return User::find($id)->update(['password' => $password]); } + public static function validate($username, $field = 'current_password') + { + return PasswordRules::changePassword($username, $field); + } + public static function getModel() { return User::query(); diff --git a/app/Repositories/Core/Images.php b/app/Repositories/Core/Images.php new file mode 100644 index 00000000..bb121725 --- /dev/null +++ b/app/Repositories/Core/Images.php @@ -0,0 +1,10 @@ +toArray() : false; + + return $data; + } +} +