Added a new authorizer method for checking a user has a permission (for use in sprinkle level access checks)
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
|
||||
namespace UserFrosting\Sprinkle\UFTweaks\ServicesProvider;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
@@ -151,6 +152,25 @@ class ServicesProvider
|
||||
}
|
||||
);
|
||||
|
||||
/*
|
||||
* Check if the specified user (by user_id) has a particular permission.
|
||||
*
|
||||
* @param int $user_id the id of the user.
|
||||
* @param int $permission_slug slug of the permission to check.
|
||||
* @return bool true if the user has the permission, false otherwise.
|
||||
*/
|
||||
$authorizer->addCallback(
|
||||
'has_permission',
|
||||
function ($user_id, $permission_slug) {
|
||||
return Capsule::table('role_users')
|
||||
->join('permission_roles', 'role_users.role_id', '=', 'permission_roles.role_id')
|
||||
->join('permissions', 'permission_roles.permission_id', '=', 'permissions.id')
|
||||
->where('user_id', $user_id)
|
||||
->where('slug', $permission_slug)
|
||||
->count() > 0;
|
||||
}
|
||||
);
|
||||
|
||||
return $authorizer;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user