| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/project-slim/app/Repositories/ |
Upload File : |
<?php
namespace App\Repositories;
use App\Models\User;
use App\Models\Role;
use App\Models\UserSecurity;
use App\Repositories\Interfaces\UserRepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
class EloquentUserRepository implements UserRepositoryInterface
{
/**
* Find user by ID
*/
public function findById(int $id): ?User
{
return User::with(['company', 'roles', 'security', 'flags'])->find($id);
}
/**
* Find user by email
*/
public function findByEmail(string $email): ?User
{
return User::with(['company', 'roles', 'security', 'flags'])
->where('email', $email)
->first();
}
/**
* Create a new user
*/
public function create(array $data): User
{
$user = User::create([
'company_id' => $data['company_id'] ?? null,
'email' => $data['email'],
'password_hash' => $data['password_hash'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'is_active' => $data['is_active'] ?? true,
'is_verified' => $data['is_verified'] ?? false
]);
// Create security record
$user->security()->create([
'user_id' => $user->user_id
]);
// Assign default role if specified
if (isset($data['role_name'])) {
$role = Role::where('role_name', $data['role_name'])->first();
if ($role) {
$user->roles()->attach($role->role_id);
}
} else {
// Assign default customer role
$customerRole = Role::where('role_name', 'customer')->first();
if ($customerRole) {
$user->roles()->attach($customerRole->role_id);
}
}
return $user->load(['company', 'roles', 'security', 'flags']);
}
/**
* Update user
*/
public function update(int $id, array $data): ?User
{
$user = $this->findById($id);
if (!$user) {
return null;
}
$user->update(array_filter([
'company_id' => $data['company_id'] ?? $user->company_id,
'email' => $data['email'] ?? $user->email,
'password_hash' => $data['password_hash'] ?? $user->password_hash,
'first_name' => $data['first_name'] ?? $user->first_name,
'last_name' => $data['last_name'] ?? $user->last_name,
'is_active' => $data['is_active'] ?? $user->is_active,
'is_verified' => $data['is_verified'] ?? $user->is_verified
]));
return $user->refresh();
}
/**
* Delete user
*/
public function delete(int $id): bool
{
$user = $this->findById($id);
if (!$user) {
return false;
}
return $user->delete();
}
/**
* Get all users with pagination
*/
public function getAll(int $page = 1, int $limit = 20, array $filters = []): LengthAwarePaginator
{
$query = User::with(['company', 'roles'])
->orderBy('created_at', 'desc');
// Apply filters
if (isset($filters['company_id'])) {
$query->where('company_id', $filters['company_id']);
}
if (isset($filters['is_active'])) {
$query->where('is_active', $filters['is_active']);
}
if (isset($filters['is_verified'])) {
$query->where('is_verified', $filters['is_verified']);
}
if (isset($filters['role'])) {
$query->byRole($filters['role']);
}
if (isset($filters['search'])) {
$query->search($filters['search']);
}
return $query->paginate($limit, ['*'], 'page', $page);
}
/**
* Get users by company
*/
public function getByCompany(int $companyId, int $page = 1, int $limit = 20): LengthAwarePaginator
{
return User::with(['roles'])
->where('company_id', $companyId)
->orderBy('created_at', 'desc')
->paginate($limit, ['*'], 'page', $page);
}
/**
* Search users
*/
public function search(string $term, int $page = 1, int $limit = 20): LengthAwarePaginator
{
return User::with(['company', 'roles'])
->search($term)
->orderBy('created_at', 'desc')
->paginate($limit, ['*'], 'page', $page);
}
/**
* Activate user
*/
public function activate(int $id): bool
{
$user = $this->findById($id);
if (!$user) {
return false;
}
return $user->activate();
}
/**
* Deactivate user
*/
public function deactivate(int $id): bool
{
$user = $this->findById($id);
if (!$user) {
return false;
}
return $user->deactivate();
}
/**
* Verify user email
*/
public function verify(int $id): bool
{
$user = $this->findById($id);
if (!$user) {
return false;
}
return $user->verify();
}
/**
* Update user password
*/
public function updatePassword(int $id, string $passwordHash): bool
{
$user = $this->findById($id);
if (!$user) {
return false;
}
$user->password_hash = $passwordHash;
return $user->save();
}
/**
* Get user by verification token
*/
public function findByVerificationToken(string $token): ?User
{
return User::with(['company', 'roles', 'security', 'flags'])
->whereHas('security', function ($query) use ($token) {
$query->where('verification_token', $token);
})
->first();
}
/**
* Get user by password reset token
*/
public function findByPasswordResetToken(string $token): ?User
{
return User::with(['company', 'roles', 'security', 'flags'])
->whereHas('security', function ($query) use ($token) {
$query->where('password_reset_token', $token)
->where('password_reset_expires_at', '>', now());
})
->first();
}
/**
* Assign role to user
*/
public function assignRole(int $userId, string $roleName): bool
{
$user = $this->findById($userId);
$role = Role::where('role_name', $roleName)->first();
if (!$user || !$role) {
return false;
}
$user->roles()->syncWithoutDetaching([$role->role_id]);
return true;
}
/**
* Remove role from user
*/
public function removeRole(int $userId, string $roleName): bool
{
$user = $this->findById($userId);
$role = Role::where('role_name', $roleName)->first();
if (!$user || !$role) {
return false;
}
$user->roles()->detach($role->role_id);
return true;
}
/**
* Bulk activate users
*/
public function bulkActivate(array $userIds): int
{
return User::whereIn('user_id', $userIds)->update(['is_active' => true]);
}
/**
* Bulk deactivate users
*/
public function bulkDeactivate(array $userIds): int
{
return User::whereIn('user_id', $userIds)->update(['is_active' => false]);
}
/**
* Bulk delete users
*/
public function bulkDelete(array $userIds): int
{
return User::whereIn('user_id', $userIds)->delete();
}
/**
* Get user statistics
*/
public function getStatistics(): array
{
return [
'total' => User::count(),
'active' => User::where('is_active', true)->count(),
'verified' => User::where('is_verified', true)->count(),
'recent' => User::recent(7)->count(),
'by_role' => Role::withCount('users')->get()->pluck('users_count', 'role_name')->toArray()
];
}
}