First Commit
This commit is contained in:
Executable
+385
@@ -0,0 +1,385 @@
|
||||
<?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 App\Helpers\Log;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
|
||||
class ActivityLog extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'activity_logs';
|
||||
|
||||
/**
|
||||
* The primary key associated with table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'activity_logs_id';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'activity_logs_user_id',
|
||||
'activity_logs_bus_id',
|
||||
'activity_logs_action',
|
||||
'activity_logs_resource_type',
|
||||
'activity_logs_resource_id',
|
||||
'activity_logs_old_values',
|
||||
'activity_logs_new_values',
|
||||
'activity_logs_ip_address',
|
||||
'activity_logs_user_agent'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'activity_logs_old_values' => 'array',
|
||||
'activity_logs_new_values' => 'array',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be mutated to dates.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dates = [
|
||||
'created_at',
|
||||
'updated_at'
|
||||
];
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
/**
|
||||
* Get the user that performed the activity.
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\Modules\Auth\Models\User::class, 'activity_logs_user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the company where the activity occurred.
|
||||
*/
|
||||
public function company(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Company::class, 'activity_logs_bus_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get logs for a specific user.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param int|null $userId
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeForUser($query, ?int $userId)
|
||||
{
|
||||
return $query->where('activity_logs_user_id', $userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get logs for a specific company.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param int|null $companyId
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeForCompany($query, ?int $companyId)
|
||||
{
|
||||
return $query->where('activity_logs_bus_id', $companyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get logs for a specific action.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $action
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeForAction($query, string $action)
|
||||
{
|
||||
return $query->where('activity_logs_action', $action);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get logs for a specific resource type.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $resourceType
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeForResourceType($query, string $resourceType)
|
||||
{
|
||||
return $query->where('activity_logs_resource_type', $resourceType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get logs for a specific resource.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $resourceType
|
||||
* @param int $resourceId
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeForResource($query, string $resourceType, int $resourceId)
|
||||
{
|
||||
return $query->where('activity_logs_resource_type', $resourceType)
|
||||
->where('activity_logs_resource_id', $resourceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get logs within a date range.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param \DateTime|string $startDate
|
||||
* @param \DateTime|string $endDate
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeBetweenDates($query, $startDate, $endDate)
|
||||
{
|
||||
return $query->whereBetween('created_at', [$startDate, $endDate]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to order logs by creation date.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $direction
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeOrderByDate($query, string $direction = 'desc')
|
||||
{
|
||||
return $query->orderBy('created_at', $direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an activity.
|
||||
*
|
||||
* @param int|null $userId
|
||||
* @param int|null $companyId
|
||||
* @param string $action
|
||||
* @param string $resourceType
|
||||
* @param int|null $resourceId
|
||||
* @param array|null $oldValues
|
||||
* @param array|null $newValues
|
||||
* @param string|null $ipAddress
|
||||
* @param string|null $userAgent
|
||||
* @return static
|
||||
*/
|
||||
public static function log(
|
||||
?int $userId,
|
||||
?int $companyId,
|
||||
string $action,
|
||||
string $resourceType,
|
||||
?int $resourceId = null,
|
||||
?array $oldValues = null,
|
||||
?array $newValues = null,
|
||||
?string $ipAddress = null,
|
||||
?string $userAgent = null
|
||||
): self {
|
||||
// Also log to file for monitoring
|
||||
Log::info("Activity: {$action}", [
|
||||
'user_id' => $userId,
|
||||
'company_id' => $companyId,
|
||||
'resource' => "{$resourceType}:{$resourceId}",
|
||||
'ip' => $ipAddress ?? ($_SERVER['REMOTE_ADDR'] ?? '127.0.0.1')
|
||||
]);
|
||||
$resolvedCompanyId = $companyId ?? ($_SESSION['current_company_id'] ?? ($_SESSION['user_idbus'] ?? null));
|
||||
|
||||
// Validate company id exists to satisfy FK constraints
|
||||
if ($resolvedCompanyId !== null) {
|
||||
$exists = Capsule::table('bus')->where('bus_id', $resolvedCompanyId)->exists();
|
||||
if (!$exists) {
|
||||
$resolvedCompanyId = null;
|
||||
}
|
||||
}
|
||||
|
||||
$attributes = [
|
||||
'activity_logs_user_id' => $userId,
|
||||
'activity_logs_bus_id' => $resolvedCompanyId,
|
||||
'activity_logs_action' => $action,
|
||||
'activity_logs_resource_type' => $resourceType,
|
||||
'activity_logs_resource_id' => $resourceId,
|
||||
'activity_logs_old_values' => $oldValues,
|
||||
'activity_logs_new_values' => $newValues,
|
||||
'activity_logs_ip_address' => $ipAddress ?? ($_SERVER['REMOTE_ADDR'] ?? '127.0.0.1'),
|
||||
'activity_logs_user_agent' => $userAgent ?? ($_SERVER['HTTP_USER_AGENT'] ?? 'Console')
|
||||
];
|
||||
|
||||
// If company id is invalid/null and DB enforces NOT NULL or FK, avoid DB insert
|
||||
if ($resolvedCompanyId === null) {
|
||||
return new self($attributes);
|
||||
}
|
||||
|
||||
return self::create($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the action attribute.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getActionAttribute(): string
|
||||
{
|
||||
return $this->activity_logs_action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resource type attribute.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getResourceTypeAttribute(): string
|
||||
{
|
||||
return $this->activity_logs_resource_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resource id attribute.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getResourceIdAttribute(): ?int
|
||||
{
|
||||
return $this->activity_logs_resource_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user name attribute.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUserNameAttribute(): string
|
||||
{
|
||||
return $this->user ? ($this->user->users_name ?: $this->user->users_email) : __('activity.unknown_user');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the action color attribute.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getActionColorAttribute(): string
|
||||
{
|
||||
$colors = [
|
||||
'created' => 'success',
|
||||
'updated' => 'primary',
|
||||
'deleted' => 'danger',
|
||||
'login' => 'success',
|
||||
'logout' => 'warning',
|
||||
'password_changed' => 'info',
|
||||
'profile_updated' => 'info',
|
||||
'module_enabled' => 'success',
|
||||
'module_disabled' => 'secondary',
|
||||
'permission_granted' => 'success',
|
||||
'permission_revoked' => 'danger'
|
||||
];
|
||||
|
||||
return $colors[$this->activity_logs_action] ?? 'secondary';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the formatted action description.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFormattedActionAttribute(): string
|
||||
{
|
||||
$actions = [
|
||||
'created' => __('activity.created'),
|
||||
'updated' => __('activity.updated'),
|
||||
'deleted' => __('activity.deleted'),
|
||||
'login' => __('activity.login'),
|
||||
'logout' => __('activity.logout'),
|
||||
'password_changed' => __('activity.password_changed'),
|
||||
'profile_updated' => __('activity.profile_updated'),
|
||||
'module_enabled' => __('activity.module_enabled'),
|
||||
'module_disabled' => __('activity.module_disabled'),
|
||||
'permission_granted' => __('activity.permission_granted'),
|
||||
'permission_revoked' => __('activity.permission_revoked')
|
||||
];
|
||||
|
||||
return $actions[$this->activity_logs_action] ?? $this->activity_logs_action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the formatted resource description.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFormattedResourceAttribute(): string
|
||||
{
|
||||
if ($this->activity_logs_resource_type && $this->activity_logs_resource_id) {
|
||||
return "{$this->activity_logs_resource_type} #{$this->activity_logs_resource_id}";
|
||||
}
|
||||
|
||||
return $this->activity_logs_resource_type ?: __('activity.unknown_resource');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the formatted changes description.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFormattedChangesAttribute(): string
|
||||
{
|
||||
if (!$this->activity_logs_old_values && !$this->activity_logs_new_values) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$changes = [];
|
||||
|
||||
if ($this->activity_logs_old_values && $this->activity_logs_new_values) {
|
||||
foreach ($this->activity_logs_old_values as $key => $oldValue) {
|
||||
if (isset($this->activity_logs_new_values[$key])) {
|
||||
$newValue = $this->activity_logs_new_values[$key];
|
||||
$changes[] = "{$key}: '{$oldValue}' → '{$newValue}'";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return implode(', ', $changes);
|
||||
}
|
||||
}
|
||||
Executable
+283
@@ -0,0 +1,283 @@
|
||||
<?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\BelongsToMany;
|
||||
use Modules\Company\Models\Company;
|
||||
|
||||
class Module extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'modules';
|
||||
|
||||
/**
|
||||
* The primary key associated with table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'modules_id';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'modules_name',
|
||||
'modules_display_name',
|
||||
'modules_description',
|
||||
'modules_version',
|
||||
'modules_author',
|
||||
'modules_ative',
|
||||
'modules_public',
|
||||
'modules_blocked',
|
||||
'modules_dependencies',
|
||||
'modules_permissions',
|
||||
'modules_settings',
|
||||
'modules_path',
|
||||
'modules_namespace',
|
||||
'modules_icon',
|
||||
'menu_side_bar',
|
||||
'menu_nav_bar',
|
||||
'modules_order'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'modules_ative' => 'boolean',
|
||||
'modules_public' => 'boolean',
|
||||
'modules_blocked' => 'boolean',
|
||||
'menu_side_bar' => 'boolean',
|
||||
'menu_nav_bar' => 'boolean',
|
||||
'modules_dependencies' => 'array',
|
||||
'modules_permissions' => 'array',
|
||||
'modules_settings' => 'array',
|
||||
'modules_install_date' => 'datetime',
|
||||
'modules_update_date' => 'datetime',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be mutated to dates.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dates = [
|
||||
'modules_install_date',
|
||||
'modules_update_date',
|
||||
'created_at',
|
||||
'updated_at'
|
||||
];
|
||||
|
||||
/**
|
||||
* Get companies that use this module.
|
||||
*/
|
||||
public function companies(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Company::class, 'bus_modules', 'bus_modules_module_id', 'bus_modules_bus_id')
|
||||
->withPivot(['bus_modules_is_enabled', 'bus_modules_settings', 'bus_modules_enabled_by', 'bus_modules_enabled_at', 'bus_modules_show_public_nav', 'bus_modules_show_dashboard_nav'])
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get only active modules.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeActive($query)
|
||||
{
|
||||
return $query->where('modules_ative', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get only public modules.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopePublic($query)
|
||||
{
|
||||
return $query->where('modules_public', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get only blocked modules.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeBlocked($query)
|
||||
{
|
||||
return $query->where('modules_blocked', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get modules by name.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $name
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeByName($query, string $name)
|
||||
{
|
||||
return $query->where('modules_name', $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to order modules by order field.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeOrdered($query)
|
||||
{
|
||||
return $query->orderBy('modules_order', 'asc');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if module is active.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isActive(): bool
|
||||
{
|
||||
return $this->modules_ative;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if module is public.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isPublic(): bool
|
||||
{
|
||||
return $this->modules_public;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if module is blocked.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isBlocked(): bool
|
||||
{
|
||||
return $this->modules_blocked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get module icon with Bootstrap Icons classes.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getIconAttribute(): string
|
||||
{
|
||||
return $this->attributes['modules_icon'] ?? 'bi bi-gear';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get module display name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDisplayNameAttribute(): string
|
||||
{
|
||||
return $this->modules_display_name ?: $this->modules_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get module dependencies.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDependenciesAttribute(): array
|
||||
{
|
||||
return $this->modules_dependencies ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get module permissions.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPermissionsAttribute(): array
|
||||
{
|
||||
return $this->modules_permissions ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get module settings.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSettingsAttribute(): array
|
||||
{
|
||||
return $this->modules_settings ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get setting value.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSetting(string $key, $default = null)
|
||||
{
|
||||
$settings = $this->getSettingsAttribute();
|
||||
return $settings[$key] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if module has specific permission.
|
||||
*
|
||||
* @param string $permission
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPermission(string $permission): bool
|
||||
{
|
||||
return in_array($permission, $this->getPermissionsAttribute());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if module depends on another module.
|
||||
*
|
||||
* @param string $dependency
|
||||
* @return bool
|
||||
*/
|
||||
public function hasDependency(string $dependency): bool
|
||||
{
|
||||
return in_array($dependency, $this->getDependenciesAttribute());
|
||||
}
|
||||
}
|
||||
Executable
+129
@@ -0,0 +1,129 @@
|
||||
<?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;
|
||||
|
||||
class RolePermission extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'role_permissions';
|
||||
|
||||
/**
|
||||
* The primary key associated with table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'role_permissions_id';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'role_permissions_role',
|
||||
'role_permissions_bus_id',
|
||||
'role_permissions_permission',
|
||||
'role_permissions_created_by'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime'
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be mutated to dates.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dates = [
|
||||
'created_at',
|
||||
'updated_at'
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the company that owns the role permission.
|
||||
*/
|
||||
public function company(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Company::class, 'role_permissions_bus_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user who created the role permission.
|
||||
*/
|
||||
public function createdByUser(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\Modules\Auth\Models\User::class, 'role_permissions_created_by');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get permissions for a specific role.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $role
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeForRole($query, string $role)
|
||||
{
|
||||
return $query->where('role_permissions_role', $role);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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('role_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('role_permissions_permission', $permission);
|
||||
}
|
||||
}
|
||||
Executable
+178
@@ -0,0 +1,178 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user