[WIP] Fix ergonomics rules
This commit is contained in:
147
app/Repositories/Core/Auth/Roles.php
Normal file
147
app/Repositories/Core/Auth/Roles.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Laratrust\Traits\LaratrustUserTrait;
|
||||
use Yajra\DataTables\DataTables;
|
||||
|
||||
use App\Models\Core\Auth\Role;
|
||||
use App\Models\Core\Auth\RoleUser;
|
||||
|
||||
class Roles
|
||||
{
|
||||
use LaratrustUserTrait;
|
||||
|
||||
public static function store($input)
|
||||
{
|
||||
if (isset($input['id']) && $input['id']) {
|
||||
return self::update($input);
|
||||
} else {
|
||||
return self::insert($input);
|
||||
}
|
||||
}
|
||||
|
||||
// ajoute une forme juridique
|
||||
public static function insert($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]);
|
||||
$role->attachPermissions($permissions);
|
||||
return $role;
|
||||
}
|
||||
|
||||
// met à jour les informations d'une forme juridique
|
||||
public static function update($input, $id = false)
|
||||
{
|
||||
$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->syncPermissions($permissions);
|
||||
return $role;
|
||||
}
|
||||
|
||||
// supprime une forme juridique
|
||||
public static function delete($id)
|
||||
{
|
||||
$old = self::select_by_id($id);
|
||||
self::deleteTranslation($old->translated);
|
||||
return Role::destroy($id);
|
||||
}
|
||||
|
||||
// met à jour le statut actif/inactif d'une forme juridique
|
||||
public static function toggle_active($id, $active)
|
||||
{
|
||||
return Role::find($id)->update(['active' => $active]);
|
||||
}
|
||||
|
||||
// compte le nombre de formes juridiques
|
||||
public static function count()
|
||||
{
|
||||
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();
|
||||
return $role;
|
||||
}
|
||||
|
||||
public static function getAll()
|
||||
{
|
||||
return Role::orderBy('name', 'asc')->get();
|
||||
}
|
||||
|
||||
public static function getByName($name)
|
||||
{
|
||||
return Role::where('name', $name)->first();
|
||||
}
|
||||
|
||||
public static function get($id)
|
||||
{
|
||||
return Role::find($id);
|
||||
}
|
||||
|
||||
public static function getTable($id)
|
||||
{
|
||||
$datas = Role::orderBy('name', 'asc');
|
||||
return Datatables::of($datas)->make(true);
|
||||
}
|
||||
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user