2019-11-27 22:19:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Contracts\Service;
|
|
|
|
use App\Models\Role;
|
|
|
|
use App\Repositories\RoleRepository;
|
|
|
|
|
|
|
|
class RoleService extends Service
|
|
|
|
{
|
|
|
|
private $roleRepo;
|
|
|
|
|
|
|
|
public function __construct(RoleRepository $roleRepo)
|
|
|
|
{
|
|
|
|
$this->roleRepo = $roleRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a role with the given attributes
|
|
|
|
*
|
|
|
|
* @param Role $role
|
|
|
|
* @param array $attrs
|
|
|
|
*
|
|
|
|
* @return Role
|
|
|
|
*/
|
|
|
|
public function updateRole(Role $role, array $attrs)
|
|
|
|
{
|
|
|
|
$role->update($attrs);
|
|
|
|
$role->save();
|
|
|
|
|
|
|
|
return $role;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Role $role
|
|
|
|
* @param array $permissions
|
|
|
|
*/
|
|
|
|
public function setPermissionsForRole(Role $role, array $permissions)
|
|
|
|
{
|
|
|
|
// Update the permissions, filter out null/invalid values
|
|
|
|
$perms = collect($permissions)->filter(static function ($v, $k) {
|
2020-02-25 04:12:36 +08:00
|
|
|
return !empty($v);
|
2019-11-27 22:19:20 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
$role->permissions()->sync($perms);
|
|
|
|
}
|
|
|
|
}
|