| 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/Models/ |
Upload File : |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Builder;
class User extends BaseModel
{
/**
* The table associated with the model.
*/
protected $table = 'repo_user';
/**
* The primary key for the model.
*/
protected $primaryKey = 'user_id';
/**
* The attributes that are mass assignable.
*/
protected $fillable = [
'company_id',
'email',
'password_hash',
'first_name',
'last_name',
'is_active',
'is_verified'
];
/**
* The attributes that should be hidden for serialization.
* Extended from BaseModel to include password and other sensitive data
*/
protected $hidden = [
'password_hash',
'id',
'user_id',
'company_id',
'role_id',
'report_id',
'module_id'
];
/**
* The attributes that should be cast to native types.
*/
protected $casts = [
'is_active' => 'boolean',
'is_verified' => 'boolean',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* Get the company that owns the user.
*/
public function company(): BelongsTo
{
return $this->belongsTo(Company::class, 'company_id', 'company_id');
}
/**
* Get the user's security information.
*/
public function security(): HasOne
{
return $this->hasOne(UserSecurity::class, 'user_id', 'user_id');
}
/**
* Get the user's flags.
*/
public function flags(): HasMany
{
return $this->hasMany(UserFlag::class, 'user_id', 'user_id');
}
/**
* Get the roles for the user.
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(
Role::class,
'repo_user_role',
'user_id',
'role_id',
'user_id',
'role_id'
)->withPivot('assigned_at');
}
/**
* Get all reports created by the user.
*/
public function reports(): HasMany
{
return $this->hasMany(Report::class, 'created_by', 'user_id');
}
/**
* Get user's full name.
*/
public function getFullNameAttribute(): string
{
return trim($this->first_name . ' ' . $this->last_name);
}
/**
* Get user's initials.
*/
public function getInitialsAttribute(): string
{
return strtoupper(substr($this->first_name, 0, 1) . substr($this->last_name, 0, 1));
}
/**
* Check if user has a specific role.
*/
public function hasRole(string $roleName): bool
{
return $this->roles()->where('role_name', $roleName)->exists();
}
/**
* Check if user has any of the given roles.
*/
public function hasAnyRole(array $roleNames): bool
{
return $this->roles()->whereIn('role_name', $roleNames)->exists();
}
/**
* Check if user has permission for a module.
*/
public function hasModulePermission(string $moduleName): bool
{
return $this->roles()
->whereHas('modules', function (Builder $query) use ($moduleName) {
$query->where('module_name', $moduleName);
})
->exists();
}
/**
* Get all modules user has access to.
*/
public function getAccessibleModules(): array
{
$modules = [];
foreach ($this->roles as $role) {
foreach ($role->modules as $module) {
$modules[] = $module->module_name;
}
}
return array_unique($modules);
}
/**
* Get specific flag value.
*/
public function getFlagValue(string $flagKey): bool
{
$flag = $this->flags()->where('flag_key', $flagKey)->first();
return $flag ? $flag->flag_value : false;
}
/**
* Set or update a flag.
*/
public function setFlag(string $flagKey, bool $value): void
{
$this->flags()->updateOrCreate(
['flag_key' => $flagKey],
['flag_value' => $value]
);
}
/**
* Check if user is monetized.
*/
public function getIsMonetizedAttribute(): bool
{
return $this->getFlagValue('is_monetized');
}
/**
* Check if user has lifetime access.
*/
public function getLifetimeEnabledAttribute(): bool
{
return $this->getFlagValue('lifetime_enabled');
}
/**
* Scope a query to only include verified users.
*/
public function scopeVerified(Builder $query): Builder
{
return $query->where('is_verified', true);
}
/**
* Scope a query to only include unverified users.
*/
public function scopeUnverified(Builder $query): Builder
{
return $query->where('is_verified', false);
}
/**
* Scope a query to filter by company.
*/
public function scopeByCompany(Builder $query, int $companyId): Builder
{
return $query->where('company_id', $companyId);
}
/**
* Scope a query to filter by role.
*/
public function scopeByRole(Builder $query, string $roleName): Builder
{
return $query->whereHas('roles', function (Builder $q) use ($roleName) {
$q->where('role_name', $roleName);
});
}
/**
* Scope a query to search users.
*/
public function scopeSearch(Builder $query, string $term): Builder
{
return $query->where(function (Builder $q) use ($term) {
$q->where('first_name', 'ILIKE', "%{$term}%")
->orWhere('last_name', 'ILIKE', "%{$term}%")
->orWhere('email', 'ILIKE', "%{$term}%");
});
}
/**
* Create a new user with default settings.
*/
public static function createWithDefaults(array $attributes): self
{
$user = static::create($attributes);
// Assign default customer role
$customerRole = Role::where('role_name', 'customer')->first();
if ($customerRole) {
$user->roles()->attach($customerRole->role_id);
}
return $user;
}
/**
* Verify the user's email.
*/
public function verify(): bool
{
$this->is_verified = true;
return $this->save();
}
/**
* Activate the user.
*/
public function activate(): bool
{
$this->is_active = true;
return $this->save();
}
/**
* Deactivate the user.
*/
public function deactivate(): bool
{
$this->is_active = false;
return $this->save();
}
/**
* Check if user is active.
*/
public function isActive(): bool
{
return $this->is_active;
}
}