Core classes removed by error
This commit is contained in:
28
app/Repositories/Core/Auth/PasswordSecurities.php
Normal file
28
app/Repositories/Core/Auth/PasswordSecurities.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
use App\Models\Core\Auth\PasswordSecurity;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class PasswordSecurities
|
||||
{
|
||||
public static function create($user_id, $delay = 90)
|
||||
{
|
||||
return PasswordSecurity::create([
|
||||
'user_id' => $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;
|
||||
}
|
||||
}
|
||||
23
app/Repositories/Core/Auth/Passwords.php
Normal file
23
app/Repositories/Core/Auth/Passwords.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
class Passwords
|
||||
{
|
||||
public static function validator()
|
||||
{
|
||||
$validator = new \Password\Validator(new \Password\StringHelper());
|
||||
$validator->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());
|
||||
}
|
||||
}
|
||||
}
|
||||
26
app/Repositories/Core/Auth/Permissions.php
Normal file
26
app/Repositories/Core/Auth/Permissions.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
use App\Models\Core\Auth\Permission;
|
||||
use App\Traits\Model\Basic;
|
||||
|
||||
class Permissions
|
||||
{
|
||||
use Basic;
|
||||
|
||||
public static function getModules()
|
||||
{
|
||||
return Permission::select('module')->distinct('module')->get()->pluck('module');
|
||||
}
|
||||
|
||||
public static function getByName($name)
|
||||
{
|
||||
return Permission::where('name', $name)->first();
|
||||
}
|
||||
|
||||
public static function getModel()
|
||||
{
|
||||
return Permission::query();
|
||||
}
|
||||
}
|
||||
83
app/Repositories/Core/Auth/Roles.php
Normal file
83
app/Repositories/Core/Auth/Roles.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
use App\Models\Core\Auth\Role;
|
||||
use App\Models\Core\Auth\RoleUser;
|
||||
use App\Traits\Model\Basic;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Laratrust\Traits\LaratrustUserTrait;
|
||||
|
||||
class Roles
|
||||
{
|
||||
use Basic, LaratrustUserTrait;
|
||||
|
||||
public static function getListByRights()
|
||||
{
|
||||
$data = ! Auth::user()->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();
|
||||
}
|
||||
}
|
||||
61
app/Repositories/Core/Auth/Teams.php
Normal file
61
app/Repositories/Core/Auth/Teams.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
use App\Models\Core\Auth\Team;
|
||||
use App\Models\Core\Auth\TeamUser;
|
||||
use App\Repositories\Users;
|
||||
use App\Traits\Model\Basic;
|
||||
use Laratrust\Traits\LaratrustUserTrait;
|
||||
|
||||
class Teams
|
||||
{
|
||||
use Basic, LaratrustUserTrait;
|
||||
|
||||
public static function getTeamsByUser($user_id = false)
|
||||
{
|
||||
$user_id = $user_id ? $user_id : Users::getId();
|
||||
|
||||
return TeamUser::byUser($user_id);
|
||||
}
|
||||
|
||||
public static function getUsersByTeam($id)
|
||||
{
|
||||
return TeamUser::byTeam($id)->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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
10
app/Repositories/Core/Images.php
Normal file
10
app/Repositories/Core/Images.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core;
|
||||
|
||||
use App\Traits\Repository\Imageable;
|
||||
|
||||
class Images
|
||||
{
|
||||
use Imageable;
|
||||
}
|
||||
19
app/Repositories/Users.php
Normal file
19
app/Repositories/Users.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Repositories\Core\Auth\Users as parentUsers;
|
||||
use App\Repositories\Shop\SaleChannels;
|
||||
|
||||
class Users extends parentUsers
|
||||
{
|
||||
public static function getInfo($id = false)
|
||||
{
|
||||
$data = parent::getInfo($id);
|
||||
$sale_channel_default = SaleChannels::getDefault();
|
||||
$data['sale_channel'] = $sale_channel_default ? $sale_channel_default->toArray() : false;
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user