usermanagement/app/Models/PermissionGroup.php

65 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2024-08-07 01:47:07 +00:00
<?php
namespace Modules\Usermanagement\Models;
use Spatie\Permission\Models\Role;
class PermissionGroup extends Base
{
protected $fillable = [
'name',
'slug'
];
/**
* Retrieves all permissions associated with a given permission group ID.
*
* @param int $id The ID of the permission group.
*
* @return \Illuminate\Database\Eloquent\Collection The collection of permissions.
*/
public static function getpermissionsByGroupId($id)
{
return Permission::where('permission_group_id', $id)->get();
}
/**
* Returns a relationship instance for the Permission model.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function permission()
{
return $this->hasMany(Permission::class);
}
/**
* Retrieves the roles associated with a given permission group.
*
* @param object $group The permission group object.
*
* @return array The array of roles associated with the permission group.
*/
public function roles($group)
{
$permission = Permission::where('permission_group_id', $group->id)->first();
2024-08-08 14:35:30 +00:00
$data = [];
if ($permission) {
$roles = Role::all();
foreach ($roles as $role) {
if ($role->hasPermissionTo($permission->name)) {
array_push($data, $role);
}
2024-08-07 01:47:07 +00:00
}
2024-08-08 14:35:30 +00:00
} else {
$data = Role::all();
2024-08-07 01:47:07 +00:00
}
return $data;
}
}