Global forms permission for new forms #4755
-
We have a dynamic formbuilder where the user can create his own forms. At the moment every new form creates a new line in the permissions. If a no super-admin creates a new from he has to ask a super-admin to give him the permission to edit the form. Is there a possibility to give forms permission on default for each form even for new onces? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This can be done with a little bit of extra code, here's what I did earlier:
<?php
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Statamic\Events\FormSaved;
use Statamic\Facades\Role;
class AddFormPermissionsToAdminRole
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(FormSaved $event)
{
$adminRole = Role::find('site_admin');
if (! $adminRole->hasPermission("view {$event->form->handle()} form submissions")) {
$adminRole
->addPermission("view {$event->form->handle()} form submissions")
->addPermission("delete {$event->form->handle()} form submissions")
->save();
}
}
} Make sure to replace
\Statamic\Events\FormSaved::class => [
\App\Listeners\AddFormPermissionsToAdminRole::class,
], Then when any new forms are created, your user should have access to edit them. |
Beta Was this translation helpful? Give feedback.
This can be done with a little bit of extra code, here's what I did earlier:
app/Listeners/AddFormPermissionsToAdminRole.php