116 lines
2.9 KiB
PHP
116 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Core\Auth;
|
|
|
|
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 getListByRights()
|
|
{
|
|
$data = (!Auth::user()->hasRole('admin')) ? Role::whereNotIn('name', ['admin'])->get() : Role::all();
|
|
return $data->pluck('name', 'id')->toArray();
|
|
}
|
|
|
|
public static function store($input)
|
|
{
|
|
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;
|
|
}
|
|
|
|
// 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']);
|
|
$role = self::get($id);
|
|
$role->update(['name' => $input['name']]);
|
|
$role->syncPermissions($permissions);
|
|
return $role;
|
|
}
|
|
|
|
// supprime une forme juridique
|
|
public static function delete($id)
|
|
{
|
|
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();
|
|
}
|
|
|
|
public static function getWithPermissions($id)
|
|
{
|
|
$role = self::get($id)->toArray();
|
|
$role['permissions'] = self::get($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::findOrFail($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')->pluck('name', 'id')->toArray();
|
|
}
|
|
}
|