File: /var/www/api_matriculas/app/Repositories/UserRepository.php
<?php
namespace App\Repositories;
use App\Models\Profile;
use App\Models\User;
use Exception;
use Illuminate\Support\Facades\Log;
class UserRepository
{
public function getAll($profileId = null, $symbolSearch = '=')
{
$registers = User::where('deleted', false);
$registers->where('profile_id', '!=', 1);
if ($profileId > 0) {
$registers->where('profile_id', $symbolSearch, $profileId);
}
return $registers->orderBy('id', 'desc')->get();
}
public function getById($id, $profileId = null)
{
$query = User::where('id', $id)->where('deleted', false);
if ($profileId > 0) {
$query->where('profile_id', $profileId);
}
$register = $query->first();
if (!$register) {
throw new Exception("Usuario no existe o fue eliminado", 404);
}
return $register;
}
public function getByTokenAccount($token)
{
$register = User::where('activation_token', $token)->where('deleted', false)->first();
if (!$register) {
throw new Exception("Usuario no existe o fue eliminado", 404);
}
return $register;
}
public function getByUsername($username, $id = null, $profileId = null, $symbolSearch = '=')
{
$query = User::where('username', $username)->where('deleted', false);
if ($id > 0) {
$query->where('id', '!=', $id);
}
if ($profileId > 0) {
$query->where('profile_id', $symbolSearch, $profileId);
}
$register = $query->first();
if (!$register) {
return false;
}
return $register;
}
public function getByRut($rut, $id = null)
{
$query = User::where('rut', $rut)->where('deleted', false);
if ($id > 0) {
$query->where('id', '!=', $id);
}
return $query->first() ?: false;
}
public function getByEmail($email, $id = null, $profileId = null, $symbolSearch = '=')
{
$query = User::where('email', $email)->where('deleted', false);
if ($id > 0) {
$query->where('id', '!=', $id);
}
if ($profileId > 0) {
$query->where('profile_id', $symbolSearch, $profileId);
}
$register = $query->first();
if (!$register) {
return false;
}
return $register;
}
public function create($data)
{
// Determinar el ID de perfil
$profileId = $data->profile_id ?? 2;
// Si hay un usuario autenticado y está intentando crear un admin siendo no-admin
if (auth()->check() && auth()->user()->profile->id != 1 && isset($data->profile_id) && $data->profile_id == 1) {
$profileId = auth()->user()->profile->id;
}
$createData = [
'name' => strUpper($data->name),
'username' => formatterRut($data->rut, false),
'rut' => !empty($data->rut) ? formatterRut($data->rut, false) : null,
'email' => strLower($data->email),
'password' => bcrypt($data->password),
'activation_token' => !empty($data->activation_token) ? ($data->activation_token) : null,
'account_confirmed' => $data->account_confirmed ?? false,
'account_confirmed_at' => !empty($data->account_confirmed_at) ? $data->account_confirmed_at : null,
'status' => true,
'profile_id' => $profileId,
'created_at' => now(),
'updated_at' => now(),
];
// Agregar usuario creador/actualizador si está autenticado
if (auth()->check()) {
$createData['user_created'] = auth()->user()->id;
$createData['user_updated'] = auth()->user()->id;
}
return User::create($createData);
}
public function update($registerData, $data)
{
$register = $this->getById($registerData->id);
// Determinar el ID de perfil
$profileId = $data->profile_id ?? 2;
// Si hay un usuario autenticado y está intentando crear un admin siendo no-admin
if (auth()->check() && auth()->user()->profile->id != 1 && isset($data->profile_id) && $data->profile_id == 1) {
$profileId = auth()->user()->profile->id;
}
$register->name = strCapital($data->name);
$register->profile_id = $profileId;
$register->rut = !empty($data->rut) ? formatterRut($data->rut, false) : null;
$register->username = !empty($data->rut) ? formatterRut($data->rut, false) : $register->username;
$register->email = strLower($data->email);
$register->status = $data->status == 0 || !$data->status ? false : true;
$register->updated_at = now();
$register->user_updated = auth()->user()->id;
if ($register->save()) {
return true;
}
return false;
}
public function activationAccount($register, $user_confirmed)
{
$register->user_confirmed = $user_confirmed;
$register->account_confirmed = true;
$register->account_confirmed_at = now();
$register->updated_at = now();
$register->user_updated = $user_confirmed;
if ($register->save()) {
return true;
}
return false;
}
}