HelloCSE/app/Http/Controllers/Core/Users.php

70 lignes
2 Kio
PHP
Brut Vue normale Historique

2024-04-10 13:56:22 +02:00
<?php
namespace App\Http\Controllers\Core;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Core\Controller;
use App\Http\Controllers\Core\Exception;
use App\Http\Utility\debug;
use App\Models\Users as UserModel;
class Users extends Controller {
static function getAdminIdList(){
return UserModel::where('isadmin',1)->get()->modelKeys();
}
static function checkForUserExists($login='',$password=''){
$validator = Validator::make(
[
'login' => $login,
'password' => $password,
],
[
'login' => 'required|max:255',
'password' => 'required|max:255',
],
[
'required' => ':attribute est obligatoire.',
'max' => 'La longueur de :attribute est de :size maximum.',
]
);
if($validator->fails())
throw new Exception($validator->errors());
if(!UserModel::where([
'login'=>$login,
'password'=>$password,
])->exists())
return false;
return UserModel::select('id', 'isadmin')
->where('login', $login)
->where('password', $password)
->first()->toArray();
}
static function getUserList($level=''){
if(empty($level) && $level!=='0')
throw new Exception('Niveau obligatoire');
$users=[];
$usersCollection=UserModel::where('isadmin',$level)->get();
foreach($usersCollection as $user){
$users[]=[
'id'=> $user->id,
'login' => $user->login,
'email' => $user->email,
'password' => $user->password,
'token' => $user->token,
'isadmin' => $user->isadmin,
'creation_date' => $user->creation_date,
'modification_date' => $user->modification_date,
];
}
return $users;
}
}