Add new version in repository
This commit is contained in:
@@ -37,16 +37,24 @@ class NewUser extends Notification
|
||||
->markdown('notifications.email')
|
||||
->greeting(__('notifications.greeting', ['firstname' => $notifiable->first_name]))
|
||||
->subject(__('notifications.newuser.subject', ['name' => config('app.name')]))
|
||||
->line(__('notifications.newuser.intro', [
|
||||
'name' => $currentUser->first_name.' '.$currentUser->last_name,
|
||||
]))
|
||||
->line(
|
||||
__(
|
||||
'notifications.newuser.intro', [
|
||||
'name' => $currentUser->first_name.' '.$currentUser->last_name,
|
||||
]
|
||||
)
|
||||
)
|
||||
->action(
|
||||
__('notifications.newuser.button'),
|
||||
route('users.firstlogin', $notifiable->remember_token)
|
||||
)
|
||||
->salutation(__('notifications.salutation', [
|
||||
'name' => $currentUser->first_name.' '.$currentUser->last_name,
|
||||
]))
|
||||
->salutation(
|
||||
__(
|
||||
'notifications.salutation', [
|
||||
'name' => $currentUser->first_name.' '.$currentUser->last_name,
|
||||
]
|
||||
)
|
||||
)
|
||||
->line(__('notifications.newuser.outro'));
|
||||
}
|
||||
|
||||
|
||||
20
app/Repositories/Core/Auth/PasswordSecurities.php
Normal file
20
app/Repositories/Core/Auth/PasswordSecurities.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use App\Models\Core\Auth\PasswordSecurity;
|
||||
|
||||
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(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
36
app/Repositories/Core/Auth/Passwords.php
Normal file
36
app/Repositories/Core/Auth/Passwords.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
use LangleyFoxall\LaravelNISTPasswordRules\PasswordRules;
|
||||
use LangleyFoxall\LaravelNISTPasswordRules\ContextSpecificWords;
|
||||
use LangleyFoxall\LaravelNISTPasswordRules\DerivativesOfContextSpecificWords;
|
||||
use LangleyFoxall\LaravelNISTPasswordRules\RepetitiveCharacters;
|
||||
use LangleyFoxall\LaravelNISTPasswordRules\SequentialCharacters;
|
||||
|
||||
|
||||
|
||||
use App\Repositories\Users;
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -48,7 +47,7 @@ class Permissions
|
||||
|
||||
public static function get($id)
|
||||
{
|
||||
return Permission::find($id);
|
||||
return Permission::findOrFail($id);
|
||||
}
|
||||
|
||||
public static function getTable($id)
|
||||
@@ -70,12 +69,13 @@ class Permissions
|
||||
|
||||
public static function create($data)
|
||||
{
|
||||
return Permission::create($data);
|
||||
$permission = Permission::create($data);
|
||||
return $permission;
|
||||
}
|
||||
|
||||
public static function update($data)
|
||||
{
|
||||
return Permission::find($data['id'])->update($data);
|
||||
return self::get($data['id'])->update($data);
|
||||
}
|
||||
|
||||
public static function count()
|
||||
|
||||
@@ -13,7 +13,7 @@ class ResetPassword extends \Illuminate\Auth\Notifications\ResetPassword
|
||||
*
|
||||
* @return \Illuminate\Notifications\Messages\MailMessage
|
||||
*/
|
||||
/*
|
||||
/*
|
||||
public function toMail($notifiable)
|
||||
{
|
||||
return (new MailMessage())
|
||||
@@ -27,5 +27,5 @@ class ResetPassword extends \Illuminate\Auth\Notifications\ResetPassword
|
||||
)
|
||||
->line(__('notifications.resetpassword.outro'));
|
||||
}
|
||||
*/
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
@@ -14,23 +14,23 @@ class Roles
|
||||
{
|
||||
use LaratrustUserTrait;
|
||||
|
||||
public static function store($input)
|
||||
public static function getListByRights()
|
||||
{
|
||||
if (isset($input['id']) && $input['id']) {
|
||||
return self::update($input);
|
||||
} else {
|
||||
return self::insert($input);
|
||||
}
|
||||
$data = (!Auth::user()->hasRole('admin')) ? Role::whereNotIn('name', ['admin'])->get() : Role::all();
|
||||
return $data->pluck('name', 'id')->toArray();
|
||||
}
|
||||
|
||||
// ajoute une forme juridique
|
||||
public static function insert($input)
|
||||
public static function store($input)
|
||||
{
|
||||
$permissions = array_keys($input['permissions']);
|
||||
$name = $input['name'];
|
||||
$translated = $input['translated'];
|
||||
self::setTranslation($name, $translated);
|
||||
$role = Role::create(['name' => $name, 'display_name' => $name, 'translated' => $translated, 'description' => '', 'active' => true]);
|
||||
return (isset($input['id']) && $input['id']) ? self::update($input) : self::create($input);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -40,11 +40,8 @@ class Roles
|
||||
{
|
||||
$id = ($id) ? $id : $input['id'];
|
||||
$permissions = array_keys($input['permissions']);
|
||||
$name = $input['name'];
|
||||
$translated = $input['translated'];
|
||||
self::setTranslation($name, $translated);
|
||||
$role = Role::find($id);
|
||||
$role->update(['name' => $name, 'translated' => $translated]);
|
||||
$role = self::get($id);
|
||||
$role->update(['name' => $input['name']]);
|
||||
$role->syncPermissions($permissions);
|
||||
return $role;
|
||||
}
|
||||
@@ -52,8 +49,6 @@ class Roles
|
||||
// supprime une forme juridique
|
||||
public static function delete($id)
|
||||
{
|
||||
$old = self::select_by_id($id);
|
||||
self::deleteTranslation($old->translated);
|
||||
return Role::destroy($id);
|
||||
}
|
||||
|
||||
@@ -69,21 +64,10 @@ class Roles
|
||||
return Role::count();
|
||||
}
|
||||
|
||||
// récupère toutes les infos sur les formes juridiques
|
||||
public static function select_all()
|
||||
{
|
||||
return Role::all()->toArray();
|
||||
}
|
||||
|
||||
public static function select_by_id($id)
|
||||
{
|
||||
return Role::find($id)->toArray();
|
||||
}
|
||||
|
||||
public static function getWithPermissions($id)
|
||||
{
|
||||
$role = Role::find($id)->toArray();
|
||||
$role['permissions'] = Role::find($id)->permissions->pluck('id')->toArray();
|
||||
$role = self::get($id)->toArray();
|
||||
$role['permissions'] = self::get($id)->permissions->pluck('id')->toArray();
|
||||
return $role;
|
||||
}
|
||||
|
||||
@@ -99,7 +83,7 @@ class Roles
|
||||
|
||||
public static function get($id)
|
||||
{
|
||||
return Role::find($id);
|
||||
return Role::findOrFail($id);
|
||||
}
|
||||
|
||||
public static function getTable($id)
|
||||
@@ -126,22 +110,6 @@ class Roles
|
||||
|
||||
public static function getOptions()
|
||||
{
|
||||
return Role::orderBy('name', 'asc')->get()->pluck('name', 'id')->toArray();
|
||||
}
|
||||
|
||||
public static function setTranslation($name, $translated)
|
||||
{
|
||||
$lang = Translate::getLang();
|
||||
$appli = 1; // ContractDrive
|
||||
$client = 1; // Client
|
||||
Translations::setTranslation($name, $translated, $lang, $appli, $client);
|
||||
}
|
||||
|
||||
public static function deleteTranslation($translated)
|
||||
{
|
||||
$lang = Translate::getLang();
|
||||
$appli = 1; // ContractDrive
|
||||
$client = 1; // Client
|
||||
Translations::deleteTranslation($translated, $lang, $appli, $client);
|
||||
return Role::orderBy('name', 'asc')->pluck('name', 'id')->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
<?php
|
||||
namespace App\Repositories\Auth;
|
||||
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Laratrust\Traits\LaratrustUserTrait;
|
||||
use Yajra\Datatables\Datatables;
|
||||
use Yajra\DataTables\DataTables;
|
||||
|
||||
use App\Models\Core\Auth\Team;
|
||||
use App\Models\Core\Auth\TeamUser;
|
||||
|
||||
use App\Repositories\Core\User\Users;
|
||||
use App\Repositories\Users;
|
||||
|
||||
class Teams
|
||||
{
|
||||
@@ -38,7 +39,32 @@ class Teams
|
||||
|
||||
public static function getOptions()
|
||||
{
|
||||
return Team::get()->pluck('name', 'id');
|
||||
return Team::orderBy('name', 'asc')->get()->pluck('name', 'id')->toArray();
|
||||
}
|
||||
|
||||
public static function getInfo($id)
|
||||
{
|
||||
return Team::find($id);
|
||||
}
|
||||
|
||||
public static function select_all()
|
||||
{
|
||||
return self::getAll()->toArray();
|
||||
}
|
||||
|
||||
public static function select_by_id($id)
|
||||
{
|
||||
return Team::find($id)->toArray();
|
||||
}
|
||||
|
||||
public static function getAll()
|
||||
{
|
||||
return Team::orderBy('name', 'asc')->get();
|
||||
}
|
||||
|
||||
public static function getByName($name)
|
||||
{
|
||||
return Team::where('name', $name)->first();
|
||||
}
|
||||
|
||||
public static function get($id)
|
||||
@@ -66,15 +92,31 @@ class Teams
|
||||
}
|
||||
}
|
||||
|
||||
// ajoute une équipe/service/direction
|
||||
public static function store($data)
|
||||
{
|
||||
if (isset($data['id']) && $data['id']) {
|
||||
return self::update($data);
|
||||
}
|
||||
return self::create($data);
|
||||
}
|
||||
|
||||
public static function create($data)
|
||||
{
|
||||
return Team::create($data);
|
||||
}
|
||||
|
||||
// met à jour les informations d'une équipe/service/direction
|
||||
public static function update($data)
|
||||
{
|
||||
return Team::find($data['id'])->update($data);
|
||||
}
|
||||
|
||||
public static function count()
|
||||
{
|
||||
return Team::count();
|
||||
}
|
||||
|
||||
public static function toggle_active($id, $active)
|
||||
{
|
||||
return Team::find($id)->update(['active' => $active]);
|
||||
}
|
||||
}
|
||||
|
||||
158
app/Repositories/Core/Auth/UserClients.php
Normal file
158
app/Repositories/Core/Auth/UserClients.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
use Hyn\Tenancy\Environment;
|
||||
use Hyn\Tenancy\Database\Connection;
|
||||
use App\Models\Admin\Website;
|
||||
use Hyn\Tenancy\Contracts\Repositories\WebsiteRepository;
|
||||
|
||||
use App\Models\Core\Auth\UserClient;
|
||||
use App\Models\Core\Auth\User;
|
||||
|
||||
use App\Repositories\Clients;
|
||||
|
||||
class UserClients
|
||||
{
|
||||
public static function associate($user_id, $clients_list)
|
||||
{
|
||||
$clients_existing = self::getClientsByUser($user_id)->toArray();
|
||||
$clients_list = is_array($clients_list) ? $clients_list : array();
|
||||
|
||||
if (is_array($clients_existing)) {
|
||||
$clients_new = array_diff($clients_list, $clients_existing);
|
||||
$clients_to_delete = array_diff($clients_existing, $clients_list);
|
||||
} else {
|
||||
$clients_new = $clients_list;
|
||||
$clients_to_delete = $clients_existing;
|
||||
}
|
||||
|
||||
$history_element_infos = (!empty($clients_new)) ? self::associateClients($user_id, $clients_new) : false;
|
||||
$history_element_infos2 = (!empty($clients_to_delete)) ? self::dissociateClients($user_id, $clients_to_delete) : false;
|
||||
|
||||
// $history_element = $old_translated_name['name'];
|
||||
// $history_element_id = $documentation_category_id;
|
||||
// Histories::insert(190, $history_element_id, $history_element);
|
||||
|
||||
$data['nb'] = self::countByUser($user_id);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function countByUser($id)
|
||||
{
|
||||
return UserClient::byUser($id)->count();
|
||||
}
|
||||
|
||||
public static function associateClients($user_id, $clients)
|
||||
{
|
||||
$history = "";
|
||||
foreach ($clients as $key => $client_id) {
|
||||
$client = Clients::select_by_id($client_id);
|
||||
if ($client) {
|
||||
self::associate_client($user_id, $client_id);
|
||||
$history .= $client['name'] . "| ";
|
||||
}
|
||||
}
|
||||
return $history;
|
||||
}
|
||||
|
||||
public static function associate_client($user_id, $client_id)
|
||||
{
|
||||
self::copyUser($user_id, $client_id);
|
||||
return UserClient::create(['user_id' => $user_id, 'client_id' => $client_id]);
|
||||
}
|
||||
|
||||
public static function changePasswordsByUser($user_id, $password)
|
||||
{
|
||||
try {
|
||||
$username = User::find($user_id)->username;
|
||||
$connection = app(Connection::class);
|
||||
$clients = self::getClientsByUser($user_id);
|
||||
foreach ($clients as $client_id) {
|
||||
Clients::switchClient($client_id);
|
||||
$client_user = User::on($connection->tenantName())->withTrashed()->where('username', $username)->first();
|
||||
if ($client_user) {
|
||||
$client_user->update(['password' => $password]);
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
// Partners::switchPartner();
|
||||
}
|
||||
|
||||
public static function copyUser($user_id, $client_id)
|
||||
{
|
||||
$connection = app(Connection::class);
|
||||
$user = User::on($connection->systemName())->find($user_id);
|
||||
$password = $user->password;
|
||||
Clients::switchClient($client_id);
|
||||
$client_user = User::on($connection->tenantName())->withTrashed()->where('username', $user->username)->first();
|
||||
if (!$client_user) {
|
||||
$user = $user->toArray();
|
||||
$user['password'] = $password;
|
||||
unset($user['id']);
|
||||
unset($user['created_at']);
|
||||
$client_user = User::on($connection->tenantName())->create($user);
|
||||
$client_user->attachRole('superadministrator');
|
||||
} else {
|
||||
if ($client_user->trashed()) {
|
||||
$client_user->restore();
|
||||
}
|
||||
$client_user->attachRole('superadministrator');
|
||||
}
|
||||
// TODO Copy avatar
|
||||
//
|
||||
// dump($client_user->toArray());
|
||||
// exit;
|
||||
// $client_user = User::on($connection->tenantName())->firstOrCreate(['username' => $user->username], $user->toArray());
|
||||
Partners::switchPartner();
|
||||
}
|
||||
|
||||
public static function dissociateClients($user_id, $clients)
|
||||
{
|
||||
$history = "";
|
||||
foreach ($clients as $key => $client_id) {
|
||||
self::dissociate_client($user_id, $client_id);
|
||||
$history .= $client['name'] . "| ";
|
||||
}
|
||||
return $history;
|
||||
}
|
||||
|
||||
public static function dissociate_client($user_id, $client_id)
|
||||
{
|
||||
self::deleteUser($user_id, $client_id);
|
||||
return UserClient::byUser($user_id)->byClient($client_id)->delete();
|
||||
}
|
||||
|
||||
public static function deleteUser($user_id, $client_id)
|
||||
{
|
||||
$connection = app(Connection::class);
|
||||
$user = User::on($connection->systemName())->find($user_id);
|
||||
Clients::switchClient($client_id);
|
||||
$user = User::on($connection->tenantName())->where('username', $user->username)->get();
|
||||
$user->detachRole('superadministrator');
|
||||
$user->delete();
|
||||
Partners::switchPartner();
|
||||
}
|
||||
|
||||
public static function delete_associate_clients($id)
|
||||
{
|
||||
return UserClient::byUser($id)->delete();
|
||||
}
|
||||
|
||||
public static function select_clients_by_id($id)
|
||||
{
|
||||
return UserClient::byUser($id)->get()->pluck('client_id')->toArray();
|
||||
}
|
||||
|
||||
public static function getClientsByUser($id)
|
||||
{
|
||||
return UserClient::byUser($id)->get()->pluck('client_id');
|
||||
}
|
||||
|
||||
public static function getUsersByClient($id)
|
||||
{
|
||||
return UserClient::byClient($id)->get()->pluck('user_id');
|
||||
}
|
||||
}
|
||||
26
app/Repositories/Core/Auth/UserStatusTeams.php
Normal file
26
app/Repositories/Core/Auth/UserStatusTeams.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
use App\Models\Core\Auth\UserStatusTeam;
|
||||
|
||||
class UserStatusTeams
|
||||
{
|
||||
|
||||
// supprime l'association entre les équipes et le statut utilisateur donné
|
||||
public function delete_teams($id)
|
||||
{
|
||||
return UserStatusTeam::byUserStatus($id)->delete();
|
||||
}
|
||||
|
||||
// associe une équipe avec un statut utilisateur
|
||||
public function insert_team($user_status_id, $team_id)
|
||||
{
|
||||
return UserStatusTeam::create(['team_id' => $team_id, 'user_status_id' => $user_status_id]);
|
||||
}
|
||||
|
||||
// récupère les équipes d'un statut utilisateur donné
|
||||
public function select_teams_by_id($id)
|
||||
{
|
||||
return UserStatusTeam::select('team_id')->byUserStatus($id)->get();
|
||||
}
|
||||
}
|
||||
100
app/Repositories/Core/Auth/UserStatuses.php
Normal file
100
app/Repositories/Core/Auth/UserStatuses.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
namespace App\Repositories\Core\Auth;
|
||||
|
||||
use App\Models\Core\Auth\UserStatus;
|
||||
|
||||
class UserStatuses
|
||||
{
|
||||
// compte le nombre de statut utilisateur
|
||||
public static function count()
|
||||
{
|
||||
return UserStatus::count();
|
||||
}
|
||||
|
||||
// supprime un statut utilisateur
|
||||
public static function delete($id)
|
||||
{
|
||||
return UserStatus::destroy($id);
|
||||
}
|
||||
|
||||
// ajoute un statut utilisateur
|
||||
public static function insert($name, $translated, $negociator)
|
||||
{
|
||||
return UserStatus::create(['name' => $name, 'translated' => $translated, 'negociator' => $negociator]);
|
||||
}
|
||||
|
||||
// reset le négociateur parmi les statuts
|
||||
public static function reset_negociator_status()
|
||||
{
|
||||
return UserStatus::update(['negociator' => null]);
|
||||
}
|
||||
|
||||
// récupère toutes les infos sur les statuts utilisateur
|
||||
public static function select_all()
|
||||
{
|
||||
return UserStatus::all()->toArray();
|
||||
}
|
||||
|
||||
// récupère les infos pour un statut utilisateur donné
|
||||
public static function select_by_id($id)
|
||||
{
|
||||
return UserStatus::find($id)->toArray();
|
||||
}
|
||||
|
||||
// récupère les infos pour un statut utilisateur donné
|
||||
public static function select_by_name($name)
|
||||
{
|
||||
return UserStatus::byName($name)->first()->toArray();
|
||||
}
|
||||
|
||||
// récupère les infos du statut considéré comme négociant d'un contrat
|
||||
public static function select_by_negociator()
|
||||
{
|
||||
$status = UserStatus::byNegociator()->first();
|
||||
return $status ? $status->toArray() : null;
|
||||
}
|
||||
|
||||
// met à jour le statut actif/inactif d'un statut utilisateur
|
||||
public static function toggle_active($id, $active)
|
||||
{
|
||||
return UserStatus::find($id)->update(['active' => $active]);
|
||||
}
|
||||
|
||||
// met à jour les informations d'un statut utilisateur
|
||||
public static function update($id, $name, $translated, $negociator)
|
||||
{
|
||||
return UserStatus::find($id)->update(['id' => $id, 'name' => $name, 'translated' => $translated, 'negociator' => $negociator]);
|
||||
}
|
||||
|
||||
// met à jour les informations d'un statut utilisateur
|
||||
public static function update_negociator($id, $negociator)
|
||||
{
|
||||
return UserStatus::find($id)->update(['negociator' => $negociator]);
|
||||
}
|
||||
|
||||
public static function getAllUserStatuses($input)
|
||||
{
|
||||
$data = [];
|
||||
$statuses = self::select_all();
|
||||
foreach ($statuses as $status) {
|
||||
if ($status['active'] <= 0) {
|
||||
continue;
|
||||
}
|
||||
$item = array();
|
||||
$item['id'] = $status['id'];
|
||||
$item['name'] = \App\Repositories\Translate::translateClient($status['translated']);
|
||||
array_push($data, $item);
|
||||
}
|
||||
$data = \App\Repositories\Functions::array_orderby($data, 'name', SORT_ASC);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function getOptions()
|
||||
{
|
||||
return UserStatus::orderBy('name', 'asc')->get()->pluck('name', 'id')->toArray();
|
||||
}
|
||||
|
||||
public static function getNegociatorsOptions()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,17 @@ namespace App\Repositories\Core\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
use Hyn\Tenancy\Database\Connection;
|
||||
use Laratrust\Traits\LaratrustUserTrait;
|
||||
|
||||
use App\Models\Core\Auth\User;
|
||||
use App\Models\Core\Auth\RoleUser;
|
||||
|
||||
use App\Repositories\Clients;
|
||||
use App\Repositories\Partners;
|
||||
|
||||
class Users
|
||||
{
|
||||
use LaratrustUserTrait;
|
||||
@@ -20,54 +26,79 @@ class Users
|
||||
}
|
||||
|
||||
public static function getInfo($id = false)
|
||||
{
|
||||
return self::getWithDetail($id);
|
||||
}
|
||||
|
||||
public static function getWithDetail($id = false)
|
||||
{
|
||||
$id = $id ? $id : self::getId();
|
||||
return User::where('id', $id)->with(['user_detail'])->first();
|
||||
if (!$id) {
|
||||
return false;
|
||||
}
|
||||
$user = self::get($id);
|
||||
$data = $user->toArray();
|
||||
$data['name'] = $user->name;
|
||||
$data['avatar'] = self::getAvatar($id);
|
||||
$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();
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function getEmailsByRole($role) {
|
||||
return User::select('id','email')->whereRoleIs($role)->get()->toArray();
|
||||
}
|
||||
|
||||
public static function getIdsByRole($role) {
|
||||
return User::select('id')->whereRoleIs($role)->get()->pluck('id')->toArray();
|
||||
}
|
||||
|
||||
public static function getListByRole($role)
|
||||
public static function store($data)
|
||||
{
|
||||
return self::selectOptions()->orderBy('nom', 'asc')->whereRoleIs($role)->get();
|
||||
$id = isset($data['id']) ? $data['id'] : false;
|
||||
|
||||
if (!empty($data['password'])) {
|
||||
$data['password'] = Hash::make($data['password']);
|
||||
} else {
|
||||
if ($id) {
|
||||
unset($data['password']);
|
||||
} else {
|
||||
$data['password'] = Hash::make(Str::random(8));
|
||||
}
|
||||
}
|
||||
$data['remember_token'] = Str::random(32);
|
||||
$data['active'] = true;
|
||||
|
||||
$user = $id ? self::update($data, $id) : self::create($data);
|
||||
|
||||
if (isset($data['roles'])) {
|
||||
$user->roles()->sync(array_keys($data['roles']));
|
||||
}
|
||||
// $user->sendNewUserNotification($data['remember_token'], Auth::user());
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public static function getOptions()
|
||||
public static function create($data)
|
||||
{
|
||||
return self::selectOptions()->get();
|
||||
$data['password'] = $data['password'] ? Hash::make($data['password']) : Hash::make(Str::random(8));
|
||||
return User::create($data);
|
||||
}
|
||||
|
||||
public static function selectOptions()
|
||||
public static function update($data, $id = false)
|
||||
{
|
||||
return User::select('id', DB::raw("concat(last_name,' ',first_name) as nom"));
|
||||
}
|
||||
|
||||
public static function hasRole($role)
|
||||
{
|
||||
$user = self::get();
|
||||
return $user ? $user->hasRole($role) : false;
|
||||
}
|
||||
|
||||
public static function getId()
|
||||
{
|
||||
return self::getUser()->id;
|
||||
$id = $id ? $id : $data['id'];
|
||||
$user = self::get($id);
|
||||
$ret = $user->update($data);
|
||||
return $user;
|
||||
}
|
||||
|
||||
public static function get($id = false)
|
||||
{
|
||||
$id = $id ? $id : self::getId();
|
||||
return User::find($id);
|
||||
return User::findOrFail($id);
|
||||
}
|
||||
|
||||
public static function getId()
|
||||
{
|
||||
$user = self::getUser();
|
||||
return $user ? $user->id : false;
|
||||
}
|
||||
|
||||
public static function getName()
|
||||
{
|
||||
$user = self::getUser();
|
||||
return $user->first_name . ' ' . $user->last_name;
|
||||
}
|
||||
|
||||
public static function getUser()
|
||||
@@ -80,12 +111,71 @@ class Users
|
||||
return Auth::check();
|
||||
}
|
||||
|
||||
public static function getOptions()
|
||||
{
|
||||
return User::orderBy('name', 'asc')->get()->pluck('name', 'id')->toArray();
|
||||
}
|
||||
|
||||
public static function delete($id)
|
||||
{
|
||||
$t = RoleUser::byUser($id)->delete();
|
||||
$ret = RoleUser::byUser($id)->delete();
|
||||
return User::destroy($id);
|
||||
}
|
||||
|
||||
public static function getListByRole($role)
|
||||
{
|
||||
return self::selectOptions()->orderBy('name', 'asc')->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;
|
||||
// TODO why is posing problem ???
|
||||
// return $user ? $user->hasPermission($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();
|
||||
@@ -101,36 +191,86 @@ class Users
|
||||
return User::byTeam($id)->byUniqueTeam()->delete();
|
||||
}
|
||||
|
||||
// récupère les champs de la table
|
||||
public static function get_field_table()
|
||||
public static function getAvatar($user_id)
|
||||
{
|
||||
return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());
|
||||
$avatar = self::get($user_id)->avatar;
|
||||
if (!$avatar) {
|
||||
return '/assets/img/no-avatar.png';
|
||||
}
|
||||
$path = Clients::isClient() ? Clients::getPublicPath('/images/avatars/') : Partners::getPublicPath('/images/avatars/');
|
||||
return $path . $avatar;
|
||||
}
|
||||
|
||||
// ajoute un utilisateur
|
||||
public static function insert($data)
|
||||
public static function selectOptions()
|
||||
{
|
||||
return User::create($data);
|
||||
return User::select('id', DB::raw("concat(last_name,' ',first_name) as name"));
|
||||
}
|
||||
|
||||
// récupère tous les utilisateurs
|
||||
public static function select_all()
|
||||
public static function count()
|
||||
{
|
||||
return User::all()->toArray();
|
||||
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();
|
||||
}
|
||||
|
||||
// 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]);
|
||||
}
|
||||
|
||||
public static function uploadAvatar($request)
|
||||
{
|
||||
$targetDir = 'uploads';
|
||||
$file = $request->file('avatar_file');
|
||||
$data = \App\Repositories\Core\Upload::getData($file);
|
||||
$file_uploaded = \App\Repositories\Core\Upload::store($file, $targetDir);
|
||||
$tab = pathinfo($file_uploaded);
|
||||
$response['name'] = $tab['basename'];
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
// met à jour les informations d'un utilisateur
|
||||
public static function update(Request $request)
|
||||
// met à jour l'avatar d'un utilisateur
|
||||
public static function update_avatar($id, $avatar)
|
||||
{
|
||||
return User::find($data['id'])->update($data);
|
||||
return User::find($id)->update(['avatar' => $avatar]);
|
||||
}
|
||||
|
||||
// met à jour le mot de passe d'un utilisateur
|
||||
public static function update_password($id, $password)
|
||||
{
|
||||
$user = User::find($id);
|
||||
$user->password = Hash::make($password);
|
||||
return $user->save();
|
||||
$password = Hash::make($password);
|
||||
UserClients::changePasswordsByUser($id, $password);
|
||||
$connection = app(Connection::class);
|
||||
return User::on($connection->systemName())->find($id)->update(['password' => $password]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user