Skip to content

Commit

Permalink
adding configuration to enable/disable policy
Browse files Browse the repository at this point in the history
  • Loading branch information
rupadana committed Feb 8, 2024
1 parent eca3a8e commit c598295
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ To Generate Token, you just need create it from admin panel. It will be Token Re

![Image](https://res.cloudinary.com/rupadana/image/upload/v1704958748/Screenshot_2024-01-11_at_15.37.55_ncpg8n.png)

By default, Token Resource is protected by TokenPolicy. You can disable it by publishing the config and change this line.

```php
'models' => [
'token' => [
'enable_policy' => false // default: true
]
],
```

## TODO

- [ ] Test Plugin for Tenancy purpose
Expand Down
8 changes: 4 additions & 4 deletions config/api-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
'token' => 'User',
],
],
'can_access' => [
'role' => [
'super_admin',
],
'models' => [
'token' => [
'enable_policy' => true
]
],
];
53 changes: 53 additions & 0 deletions src/Policies/TokenPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ class TokenPolicy
{
use HandlesAuthorization;

public function isPolicyEnabled() : bool
{
return config('api-service.models.token.enable_policy', true);
}

/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
if(!$this->isPolicyEnabled()) {
return true;
}

return $user->can('view_any_token');
}

Expand All @@ -23,6 +32,10 @@ public function viewAny(User $user): bool
*/
public function view(User $user, Token $token): bool
{
if (!$this->isPolicyEnabled()) {
return true;
}

return $user->can('view_token');
}

Expand All @@ -31,6 +44,10 @@ public function view(User $user, Token $token): bool
*/
public function create(User $user): bool
{
if (!$this->isPolicyEnabled()) {
return true;
}

return $user->can('create_token');
}

Expand All @@ -39,6 +56,10 @@ public function create(User $user): bool
*/
public function update(User $user, Token $token): bool
{
if (!$this->isPolicyEnabled()) {
return true;
}

return $user->can('update_token');
}

Expand All @@ -47,6 +68,10 @@ public function update(User $user, Token $token): bool
*/
public function delete(User $user, Token $token): bool
{
if (!$this->isPolicyEnabled()) {
return true;
}

return $user->can('delete_token');
}

Expand All @@ -55,6 +80,10 @@ public function delete(User $user, Token $token): bool
*/
public function deleteAny(User $user): bool
{
if (!$this->isPolicyEnabled()) {
return true;
}

return $user->can('delete_any_token');
}

Expand All @@ -63,6 +92,10 @@ public function deleteAny(User $user): bool
*/
public function forceDelete(User $user, Token $token): bool
{
if (!$this->isPolicyEnabled()) {
return true;
}

return $user->can('force_delete_token');
}

Expand All @@ -71,6 +104,10 @@ public function forceDelete(User $user, Token $token): bool
*/
public function forceDeleteAny(User $user): bool
{
if (!$this->isPolicyEnabled()) {
return true;
}

return $user->can('force_delete_any_token');
}

Expand All @@ -79,6 +116,10 @@ public function forceDeleteAny(User $user): bool
*/
public function restore(User $user, Token $token): bool
{
if (!$this->isPolicyEnabled()) {
return true;
}

return $user->can('restore_token');
}

Expand All @@ -87,6 +128,10 @@ public function restore(User $user, Token $token): bool
*/
public function restoreAny(User $user): bool
{
if (!$this->isPolicyEnabled()) {
return true;
}

return $user->can('restore_any_token');
}

Expand All @@ -95,6 +140,10 @@ public function restoreAny(User $user): bool
*/
public function replicate(User $user, Token $token): bool
{
if (!$this->isPolicyEnabled()) {
return true;
}

return $user->can('replicate_token');
}

Expand All @@ -103,6 +152,10 @@ public function replicate(User $user, Token $token): bool
*/
public function reorder(User $user): bool
{
if (!$this->isPolicyEnabled()) {
return true;
}

return $user->can('reorder_token');
}
}

0 comments on commit c598295

Please sign in to comment.