add offers count, & minor fixes code standards
This commit is contained in:
@@ -15,7 +15,9 @@ use App\Models\Core\Auth\RoleUser;
|
||||
|
||||
use App\Repositories\Clients;
|
||||
use App\Repositories\Partners;
|
||||
use App\Repositories\Core\Upload;
|
||||
|
||||
use LangleyFoxall\LaravelNISTPasswordRules\PasswordRules;
|
||||
class Users
|
||||
{
|
||||
use LaratrustUserTrait;
|
||||
@@ -35,16 +37,18 @@ class Users
|
||||
$data = $user->toArray();
|
||||
$data['name'] = $user->name;
|
||||
$data['avatar'] = self::getAvatar($id);
|
||||
$data['last_login'] = $user->previousLoginAt();
|
||||
// $data['last_login'] = $user->previousLoginAt();
|
||||
// $data['roles'] = self::getRoles();
|
||||
// $data['permissions'] = self::getPermissions();
|
||||
$data['roles'] = $user->roles->pluck('id')->toArray();
|
||||
$data['permissions'] = $user->allPermissions()->pluck('id')->toArray();
|
||||
$data['clients'] = $user->clients->pluck('id')->toArray();
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function store($data)
|
||||
{
|
||||
|
||||
$id = isset($data['id']) ? $data['id'] : false;
|
||||
|
||||
if (!empty($data['password'])) {
|
||||
@@ -60,19 +64,20 @@ class Users
|
||||
$data['active'] = true;
|
||||
|
||||
$user = $id ? self::update($data, $id) : self::create($data);
|
||||
$user->roles()->sync(array_keys($data['roles'] ?? []));
|
||||
|
||||
if (isset($data['roles'])) {
|
||||
$user->roles()->sync(array_keys($data['roles']));
|
||||
}
|
||||
UserClients::associate($user->id, $data['clients'] ?? false );
|
||||
// $user->sendNewUserNotification($data['remember_token'], Auth::user());
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public static function create($data)
|
||||
public static function create($data, $copy_password = false)
|
||||
{
|
||||
$data['password'] = $data['password'] ? Hash::make($data['password']) : Hash::make(Str::random(8));
|
||||
return User::create($data);
|
||||
$data['password'] = $copy_password ? $data['password'] : ($data['password'] ? Hash::make($data['password']) : Hash::make(Str::random(8)));
|
||||
$user = User::create($data);
|
||||
PasswordSecurities::create($user->id);
|
||||
return $user;
|
||||
}
|
||||
|
||||
public static function update($data, $id = false)
|
||||
@@ -95,12 +100,17 @@ class Users
|
||||
return $user ? $user->id : false;
|
||||
}
|
||||
|
||||
public static function getName()
|
||||
public static function getName($id = false)
|
||||
{
|
||||
$user = self::getUser();
|
||||
$user = $id ? self::get($id) : self::getUser();
|
||||
return $user->first_name . ' ' . $user->last_name;
|
||||
}
|
||||
|
||||
public static function getUsername($id = false)
|
||||
{
|
||||
return $id ? self::get($id)->username : self::getUser()->username;
|
||||
}
|
||||
|
||||
public static function getUser()
|
||||
{
|
||||
return Auth::user();
|
||||
@@ -113,7 +123,7 @@ class Users
|
||||
|
||||
public static function getOptions()
|
||||
{
|
||||
return User::orderBy('name', 'asc')->get()->pluck('name', 'id')->toArray();
|
||||
return User::orderBy('name')->pluck('name', 'id')->toArray();
|
||||
}
|
||||
|
||||
public static function delete($id)
|
||||
@@ -124,7 +134,7 @@ class Users
|
||||
|
||||
public static function getListByRole($role)
|
||||
{
|
||||
return self::selectOptions()->orderBy('name', 'asc')->whereRoleIs($role)->get();
|
||||
return self::selectOptions()->orderBy('name')->whereRoleIs($role)->get();
|
||||
}
|
||||
|
||||
public static function hasRole($role, $user = false)
|
||||
@@ -211,37 +221,36 @@ class Users
|
||||
return User::count();
|
||||
}
|
||||
|
||||
// récupère tous les utilisateurs pour un statut donné
|
||||
public static function select_all_by_status_id($status_id)
|
||||
{
|
||||
return User::byStatus($status_id);
|
||||
}
|
||||
|
||||
// récupère toutes les informations d'un utilisateur pour un id donné
|
||||
public static function select_by_id($user_id)
|
||||
{
|
||||
return User::with('status')->find($user_id)->toArray();
|
||||
}
|
||||
|
||||
// récupère toutes les informations d'un utilisateur pour un nom donné
|
||||
public static function select_by_name($name)
|
||||
{
|
||||
return User::byName($name)->first()->toArray();
|
||||
return self::getByName($name)->toArray();
|
||||
}
|
||||
|
||||
public static function getByUsername($username)
|
||||
{
|
||||
return User::byUsername($username)->withTrashed()->first();
|
||||
}
|
||||
|
||||
// récupère les utilisateurs actifs d'un statut, d'une équipe et d'une entité donnés
|
||||
public static function select_by_status_and_team_and_entity($status_id, $team_id, $third_party_id)
|
||||
{
|
||||
return User::active()->byStatus($status_id)->byTeam($team_id)->byThirdParty($third_party_id)->get()->toArray();
|
||||
}
|
||||
|
||||
// récupère toutes les informations nécessaires d'un utilisateur pour un id donné
|
||||
public static function select_datas_by_id($user_id)
|
||||
{
|
||||
return User::with('status')->find($user_id)->toArray();
|
||||
}
|
||||
|
||||
// met à jour le statut actif/inactif d'un utilisateur
|
||||
public static function toggle_active($id, $active)
|
||||
{
|
||||
return self::get($id)->update(['active' => $active]);
|
||||
@@ -251,26 +260,29 @@ class Users
|
||||
{
|
||||
$targetDir = 'uploads';
|
||||
$file = $request->file('avatar_file');
|
||||
$data = \App\Repositories\Core\Upload::getData($file);
|
||||
$file_uploaded = \App\Repositories\Core\Upload::store($file, $targetDir);
|
||||
$data = Upload::getData($file);
|
||||
$file_uploaded = Upload::store($file, $targetDir);
|
||||
$tab = pathinfo($file_uploaded);
|
||||
$response['name'] = $tab['basename'];
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
// met à jour l'avatar d'un utilisateur
|
||||
public static function update_avatar($id, $avatar)
|
||||
{
|
||||
return User::find($id)->update(['avatar' => $avatar]);
|
||||
}
|
||||
|
||||
// met à jour le mot de passe d'un utilisateur
|
||||
public static function update_password($id, $password)
|
||||
{
|
||||
$password = Hash::make($password);
|
||||
UserClients::changePasswordsByUser($id, $password);
|
||||
$connection = app(Connection::class);
|
||||
return User::on($connection->systemName())->find($id)->update(['password' => $password]);
|
||||
return User::find($id)->update(['password' => $password]);
|
||||
// $connection = app(Connection::class);
|
||||
// return User::on($connection->systemName())->find($id)->update(['password' => $password]);
|
||||
}
|
||||
|
||||
public static function validate($username, $field = 'current_password')
|
||||
{
|
||||
return PasswordRules::changePassword($username, $field);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user