-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(policy): CRUD trait added to handle basic crud permissions with …
…policy
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace CustomD\LaravelHelpers\Models\Policies; | ||
|
||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Str; | ||
|
||
trait CrudPermissions | ||
{ | ||
|
||
public function can(Authenticatable $user, string $action, ?Model $model = null): bool | ||
{ | ||
$permission = collect([ | ||
$this->permission_name ?? self::parsePermissionNameFromPolicy(), | ||
$action | ||
])->filter()->implode("."); | ||
|
||
if (method_exists($model, 'userHasPermission') && ! $model->userHasPermission($user)) { | ||
return false; | ||
} | ||
|
||
return $user->can($permission); | ||
} | ||
|
||
public static function parsePermissionNameFromPolicy() | ||
{ | ||
$class = class_basename(get_called_class()); | ||
$class = Str::replaceLast('Policy', '', $class); | ||
$class = Str::lower($class); | ||
return Str::plural($class); | ||
} | ||
|
||
/** | ||
* Determine whether the user can view any models. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Authenticatable $user | ||
* @return mixed | ||
*/ | ||
public function viewAny(Authenticatable $user) | ||
{ | ||
return $this->can($user, 'list'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can view the model. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Authenticatable $user | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return mixed | ||
*/ | ||
public function view(Authenticatable $user, Model $model) | ||
{ | ||
return $this->can($user, 'view'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can create models. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Authenticatable $user | ||
* @return mixed | ||
*/ | ||
public function create(Authenticatable $user) | ||
{ | ||
return $this->can($user, 'create'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can update the model. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Authenticatable $user | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return mixed | ||
*/ | ||
public function update(Authenticatable $user, Model $model) | ||
{ | ||
return $this->can($user, 'update'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can delete the model. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Authenticatable $user | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return mixed | ||
*/ | ||
public function delete(Authenticatable $user, Model $model) | ||
{ | ||
return $this->can($user, 'delete'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can restore the model. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Authenticatable $user | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return mixed | ||
*/ | ||
public function restore(Authenticatable $user, Model $model) | ||
{ | ||
return $this->can($user, 'restore'); | ||
} | ||
} |