Getting Started
Quick Start
On this page
This walk-through takes a fresh install to a working permission check. It assumes you finished Installation, including making yourself the super admin.
1. Create a role#
- Open
/systemroles/admin/roles. - Click Add Role.
- Enter Role Name
Editor. Leave Guard Name empty: it becomeseditor(the name in snake case). - Save.
The new role gets every permission that already exists, switched off.
2. Add a permission#
- On the roles list, click Manage on the
Editorrow. - Click Add Permission.
- Fill in:
- Permission Name:
article.publish - Guard Name:
article.publish - Permission: Yes
- Permission Name:
- Save.
Editor now has article.publish switched on. Every other role also gets an article.publish row, switched off, so the matrix stays complete. The super admin always passes every check, whatever its row says.
3. Give a user the role#
- Open
/systemroles/admin/roles/users. - Find the user and click Assign Permission (this button assigns a role).
- Choose
Editorand save.
The change is stored in user_roles and logged in user_roles_history.
4. Check the permission in code#
PHP
use BabeRuka\SystemRoles\Services\SystemRolesService;
public function publish(Article $article)
{
$roleId = SystemRolesService::roleIdForUser(auth()->id());
abort_unless(SystemRolesService::roleHas($roleId, 'article.publish'), 403);
$article->publish();
return back()->with('success', 'Published.');
}5. Or protect a whole route#
Create a small middleware once (full version in Protecting Routes with Middleware):
PHP
// app/Http/Middleware/HasPermission.php
namespace App\Http\Middleware;
use BabeRuka\SystemRoles\Services\SystemRolesService;
use Closure;
use Illuminate\Http\Request;
class HasPermission
{
public function handle(Request $request, Closure $next, string $permission)
{
$roleId = SystemRolesService::roleIdForUser(optional($request->user())->getKey());
abort_unless(SystemRolesService::roleHas($roleId, $permission), 403);
return $next($request);
}
}Register the alias (Laravel 11/12, bootstrap/app.php):
PHP
->withMiddleware(function (Middleware $middleware) {
$middleware->alias(['permission' => \App\Http\Middleware\HasPermission::class]);
})Use it:
PHP
Route::post('/articles/{article}/publish', [ArticleController::class, 'publish'])
->middleware(['auth', 'permission:article.publish']);That is the full loop: roles and permissions are managed on screen, and your code checks them.