Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ady_Gould's avatar

Spate Permissions and dynamic permissions

We are wondering if it is possible to use Spatie Permissions in a way that allows the application to use dynamically created (within the applications main section) roles and permissions to allow a new role or permission to be created and then applied to a part of the application.

For example:

We add a new “writer role” with new permissions “create article”, “edit article”, “publish article” within the admin section of the application.

We then want to apply the permissions to the post/article section of the application.

This needs to be done so that any new role or permission may be applied to any part of the application.

Could anyone provide guidance on how to do this, or even better example code?

Cheers

1 like
3 replies
LaryAI's avatar

Absolutely, Spatie Laravel Permission is designed to handle dynamic roles and permissions, including creating them at runtime via your application’s UI (like an admin panel). Here’s how you can achieve this:

1. Creating Roles and Permissions Dynamically

You can create new roles and permissions using the provided models:

use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

// Create a new permission
$createArticle = Permission::create(['name' => 'create article']);
$editArticle = Permission::create(['name' => 'edit article']);
$publishArticle = Permission::create(['name' => 'publish article']);

// Create a new role and assign permissions
$writerRole = Role::create(['name' => 'writer']);
$writerRole->givePermissionTo(['create article', 'edit article', 'publish article']);

You can wrap this logic in a controller or service and call it from your admin section.


2. Assigning Roles/Permissions to Users

$user = User::find($userId);
$user->assignRole('writer'); // or $user->syncRoles(['writer', ...]);

3. Checking Permissions in Your Application

In your controllers, policies, or Blade templates, you can check for permissions:

// In controller or policy
if ($user->can('publish article')) {
    // Allow publishing
}

// In Blade
@can('publish article')
    <button>Publish</button>
@endcan

4. Dynamic Management in Admin Section

  • Create forms in your admin panel to add new roles and permissions.
  • Assign permissions to roles and roles to users via the UI.
  • Use the above code in your controllers/services to persist these changes.

5. Applying Permissions to Application Sections

For example, in your ArticleController:

public function publish(Article $article)
{
    $this->authorize('publish article');

    // publishing logic...
}

Or, using middleware:

Route::post('/articles/{article}/publish', 'ArticleController@publish')
    ->middleware('permission:publish article');

Summary

  • Spatie Permission fully supports dynamic creation and assignment of roles/permissions.
  • Build admin UI to manage roles/permissions.
  • Use middleware, policies, or Blade directives to enforce permissions.

Official docs for reference:
https://spatie.be/docs/laravel-permission/v5/basic-usage/basic-usage

Let me know if you need a more detailed example for a specific part!

krisi_gjika's avatar

"any new role or permission may be applied to any part of the application" - how would the application know to check for a permission that it does not know it exists?

Snapey's avatar

How could your code know what the arbitrary string "create article" actually allows? How about "replicate legions" or "feed elephant"?

I dont see how you can have user defined permissions without writing some form of LLM that understands your code and how a new permission might affect behaviour.

New roles, absolutely. You can define a role, and assign it a set of existing permissions, and grant that role to a user through your admin, PROVIDED you only use permissions in your code as gates and never Roles.

Please or to participate in this conversation.