añadir funicionalidades de permisos y grupos
This commit is contained in:
176
app/Models/Group.php
Normal file
176
app/Models/Group.php
Normal file
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Spatie\Permission\Traits\HasPermissions;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class Group extends Model
|
||||
{
|
||||
use SoftDeletes, HasPermissions;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'description'
|
||||
];
|
||||
|
||||
/**
|
||||
* The accessors to append to the model's array form.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $appends = ['permission_names'];
|
||||
|
||||
/**
|
||||
* Relationship: Users belonging to this group
|
||||
*/
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class)
|
||||
->withTimestamps()
|
||||
->using(GroupUser::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship: Permissions assigned to this group
|
||||
*/
|
||||
public function permissions(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
config('permission.models.permission'),
|
||||
config('permission.table_names.group_has_permissions'),
|
||||
'group_id',
|
||||
'permission_id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope: Groups with specific permission
|
||||
*/
|
||||
public function scopeWithPermission(Builder $query, $permission): Builder
|
||||
{
|
||||
return $query->whereHas('permissions', function ($q) use ($permission) {
|
||||
$q->where('name', $permission);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope: Groups with permissions on specific resource
|
||||
*/
|
||||
public function scopeWithResourcePermissions(Builder $query, $resourceId, $resourceType): Builder
|
||||
{
|
||||
return $query->whereHas('permissions', function ($q) use ($resourceId, $resourceType) {
|
||||
$q->where('name', 'like', "{$resourceType}-{$resourceId}-%");
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign permission to group
|
||||
*/
|
||||
public function assignPermission($permission): self
|
||||
{
|
||||
if (is_string($permission)) {
|
||||
$permission = Permission::findByName($permission);
|
||||
}
|
||||
|
||||
$this->permissions()->syncWithoutDetaching($permission);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke permission from group
|
||||
*/
|
||||
public function revokePermission($permission): self
|
||||
{
|
||||
if (is_string($permission)) {
|
||||
$permission = Permission::findByName($permission);
|
||||
}
|
||||
|
||||
$this->permissions()->detach($permission);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync all permissions
|
||||
*/
|
||||
public function syncPermissions($permissions): self
|
||||
{
|
||||
$permissionIds = collect($permissions)->map(function ($perm) {
|
||||
return is_string($perm) ? Permission::findByName($perm)->id : $perm->id;
|
||||
});
|
||||
|
||||
$this->permissions()->sync($permissionIds);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if group has permission
|
||||
*/
|
||||
public function hasPermission($permission): bool
|
||||
{
|
||||
if (is_string($permission)) {
|
||||
return $this->permissions->contains('name', $permission);
|
||||
}
|
||||
|
||||
return $this->permissions->contains('id', $permission->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all users with permissions through this group
|
||||
*/
|
||||
public function getUsersWithPermissionsAttribute()
|
||||
{
|
||||
return User::whereHas('groups', function ($query) {
|
||||
$query->where('groups.id', $this->id);
|
||||
})->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get permission names attribute
|
||||
*/
|
||||
public function getPermissionNamesAttribute()
|
||||
{
|
||||
return $this->permissions->pluck('name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Validation rules
|
||||
*/
|
||||
public static function validationRules($groupId = null): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|string|max:255|unique:groups,name,'.$groupId,
|
||||
'description' => 'nullable|string|max:500',
|
||||
'permissions' => 'array',
|
||||
'permissions.*' => 'exists:permissions,id',
|
||||
'users' => 'array',
|
||||
'users.*' => 'exists:users,id'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The "booting" method of the model
|
||||
*/
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::deleting(function ($group) {
|
||||
if ($group->isForceDeleting()) {
|
||||
$group->users()->detach();
|
||||
$group->permissions()->detach();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,11 @@ class Project extends Model
|
||||
return $this->hasMany(Folder::class);
|
||||
}
|
||||
|
||||
public function currentFolder()
|
||||
{
|
||||
return $this->belongsTo(Folder::class);
|
||||
}
|
||||
|
||||
public function documents() {
|
||||
return $this->hasMany(Document::class);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -12,8 +13,7 @@ use Spatie\Permission\Traits\HasRoles;
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
use HasRoles;
|
||||
use HasFactory, Notifiable, HasRoles, SoftDeletes;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
@@ -60,4 +60,34 @@ class User extends Authenticatable
|
||||
->map(fn (string $name) => Str::of($name)->substr(0, 1))
|
||||
->implode('');
|
||||
}
|
||||
|
||||
public function groups()
|
||||
{
|
||||
return $this->belongsToMany(Group::class)
|
||||
->withTimestamps()
|
||||
->using(GroupUser::class);
|
||||
}
|
||||
|
||||
public function hasPermissionThroughGroup($permission)
|
||||
{
|
||||
return $this->groups->flatMap(function ($group) {
|
||||
return $group->permissions;
|
||||
})->contains('name', $permission);
|
||||
}
|
||||
|
||||
public function getAllPermissionsAttribute()
|
||||
{
|
||||
return $this->getAllPermissions()
|
||||
->merge($this->groups->flatMap->permissions)
|
||||
->unique('id');
|
||||
}
|
||||
|
||||
public function hasAnyPermission($permissions): bool
|
||||
{
|
||||
return $this->hasPermissionTo($permissions) ||
|
||||
$this->groups->contains(function ($group) use ($permissions) {
|
||||
return $group->hasAnyPermission($permissions);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user