178 lines
4.5 KiB
PHP
Executable File
178 lines
4.5 KiB
PHP
Executable File
<?php
|
|
|
|
/**
|
|
* SysCMS - Content Management System
|
|
*
|
|
* Copyright (c) 2025 Marco Costa
|
|
* All rights reserved.
|
|
*
|
|
* Author: Marco Costa
|
|
* Email: contato@syspanel.com.br
|
|
* Project: SysCMS
|
|
*
|
|
* This software is licensed, not sold.
|
|
* Permission is granted to use this software
|
|
* subject to the terms of the SysCMS Commercial License.
|
|
*
|
|
* Redistribution, sublicensing, resale, or
|
|
* disclosure of this software, in whole or in part,
|
|
* is strictly prohibited without prior written permission.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
|
|
*/
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Modules\Company\Models\Company;
|
|
use Modules\Auth\Models\User;
|
|
use Carbon\Carbon;
|
|
|
|
class UserPermission extends Model
|
|
{
|
|
/**
|
|
* The table associated with the model.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = 'user_permissions';
|
|
|
|
/**
|
|
* The primary key associated with table.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $primaryKey = 'user_permissions_id';
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'user_permissions_user_id',
|
|
'user_permissions_bus_id',
|
|
'user_permissions_permission',
|
|
'user_permissions_granted_by',
|
|
'user_permissions_expires_at'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'user_permissions_granted_at' => 'datetime',
|
|
'user_permissions_expires_at' => 'datetime',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be mutated to dates.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $dates = [
|
|
'user_permissions_granted_at',
|
|
'user_permissions_expires_at',
|
|
'created_at',
|
|
'updated_at'
|
|
];
|
|
|
|
/**
|
|
* Get the user that owns the permission.
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_permissions_user_id');
|
|
}
|
|
|
|
/**
|
|
* Get the company that owns the permission.
|
|
*/
|
|
public function company(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Company::class, 'user_permissions_bus_id');
|
|
}
|
|
|
|
/**
|
|
* Get the user who granted the permission.
|
|
*/
|
|
public function grantedByUser(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_permissions_granted_by');
|
|
}
|
|
|
|
/**
|
|
* Check if permission is expired.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isExpired(): bool
|
|
{
|
|
return $this->user_permissions_expires_at && $this->user_permissions_expires_at->isPast();
|
|
}
|
|
|
|
/**
|
|
* Check if permission is active.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isActive(): bool
|
|
{
|
|
return !$this->isExpired();
|
|
}
|
|
|
|
/**
|
|
* Scope to get only active permissions.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where(function ($q) {
|
|
$q->whereNull('user_permissions_expires_at')
|
|
->orWhere('user_permissions_expires_at', '>', Carbon::now());
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Scope to get permissions for a specific user.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @param int $userId
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public function scopeForUser($query, int $userId)
|
|
{
|
|
return $query->where('user_permissions_user_id', $userId);
|
|
}
|
|
|
|
/**
|
|
* Scope to get permissions for a specific company.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @param int $companyId
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public function scopeForCompany($query, int $companyId)
|
|
{
|
|
return $query->where('user_permissions_bus_id', $companyId);
|
|
}
|
|
|
|
/**
|
|
* Scope to get permissions with specific permission.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
* @param string $permission
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public function scopeWithPermission($query, string $permission)
|
|
{
|
|
return $query->where('user_permissions_permission', $permission);
|
|
}
|
|
} |