Policy Last updated: 2023-09-30
We know that Policies are important in the roles of administrators
Additionally, in our dash project
We have added groups and roles tables and also created a master policy object
2014_10_11_9999998_create_admin_groups_table.php
2014_10_11_9999999_create_admin_group_roles_table.php
with Models (App\Models\AdminGroup.php,App\Models\AdminGroupRole.php)
also
we have also added the resources AdminGroups
and AdminGroupRoles
in Dash\Resources path
Everything is ready and all you have to do is to add any new resource to the permissions in App\Dash\Resources\AdminGroupRoles
in the option method
select()->make('resource', 'resource')
->rule('required', 'string)->ruleWhenCreate(
Rule::unique('admin_group_roles')->where(function ($q) {
$q->where('resource', request('resource'));
$q->where('admin_group_id', request('admingroup'));
})
)->options([
'users' => 'Users',
'admin_groups' => 'AdminGroups',
'admin_group_role' => 'AdminGroupRoles',
]),
We will see that it is associated with three types of resources Users & AdminGroups & AdminGroupRoles
If we want to add the Articles resource in the permissions , we can do the following
->options([
'users' => 'Users',
'admin_groups' => 'AdminGroups',
'admin_group_role' => 'AdminGroupRoles',
'articles'=>'Articles',
]),
If you want to activate this permission with the articles resource
You need to create a new policy using dash command
php artisan dash:make-policy ArticlePolicy
[App/Policies/ArticlePolicy.php]
if you want to create policy in Modules Path you can use --module=
flag
php artisan dash:make-policy ArticlePolicy --module=Articles
[Modules/Articles/Policies/ArticlePolicy.php]
file created in app/Policies
path
namespace App\Policies;
use Dash\Policies\Policy;
class ArticlePolicy extends Policy {
protected $resource = 'articles';
}
Remember that you want to add the key in $resource
property
protected $resource = 'articles';
Then you can add the policy to the resource Articles
<?php
namespace App\Dash\Resources;
use Dash\Resource;
class Articles extends Resource {
/**
* Policy Permission can handel
* (viewAny,view,create,update,delete,forceDelete,restore) methods
* @param static property as Policy Class
*/
public static $policy = \App\Policies\ArticlePolicy::class ;
Then you can add the value in the database through the example.com/dash/AdminGroupRoles
If you want to know more about how policies work based on the base object , see the abstract class
dash/src/Policies/Policy.php