first commit
This commit is contained in:
32
app/Traits/ExposePermissions.php
Normal file
32
app/Traits/ExposePermissions.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait ExposePermissions
|
||||
{
|
||||
protected ?User $permissionsUser = null;
|
||||
|
||||
/** Overrides the user for which the list of permissions is shown in the can attribute. */
|
||||
public function setPermissionsUser(?User $user): self
|
||||
{
|
||||
$this->permissionsUser = $user;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function can(): Attribute
|
||||
{
|
||||
$model = $this;
|
||||
return Attribute::make(
|
||||
get: static function () use ($model) {
|
||||
$user = $model->permissionsUser ?? request()->user(); // if called from queue - use existing user
|
||||
return collect($model::PERMISSIONS)->mapWithKeys(static fn ($item) => [
|
||||
$item => $user?->can(Str::camel($item), $model)
|
||||
]);
|
||||
}
|
||||
)->shouldCache();
|
||||
}
|
||||
}
|
||||
30
app/Traits/HasAttachments.php
Normal file
30
app/Traits/HasAttachments.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\Attachment;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
|
||||
trait HasAttachments
|
||||
{
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function attachments()
|
||||
{
|
||||
return $this->attachmentsRelation;
|
||||
}
|
||||
|
||||
/**
|
||||
* It's important to name the relationship the same as the method because otherwise
|
||||
* eager loading of the polymorphic relationship will fail on queued jobs.
|
||||
*
|
||||
* @see https://github.com/laravelio/laravel.io/issues/350
|
||||
*/
|
||||
public function attachmentsRelation(): MorphMany
|
||||
{
|
||||
return $this->morphMany(Attachment::class, 'attachmentsRelation', 'attachmentable_type', 'attachmentable_id');
|
||||
}
|
||||
}
|
||||
128
app/Traits/HasRole.php
Normal file
128
app/Traits/HasRole.php
Normal file
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Enums\Role;
|
||||
use Cache;
|
||||
|
||||
trait HasRole
|
||||
{
|
||||
/**
|
||||
* Determine if the user has admin role.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
final public function isAdmin(): bool
|
||||
{
|
||||
return $this->hasRole(Role::ADMIN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user has role.
|
||||
*
|
||||
* @param Role $role
|
||||
* @return bool
|
||||
*/
|
||||
final public function hasRole(Role|array $role): bool
|
||||
{
|
||||
if (is_array($role)) {
|
||||
foreach ($role as $e) {
|
||||
if ($this->role_id === $e->value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->role_id === $role->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user role in the project.
|
||||
*
|
||||
* @param $projectId
|
||||
* @return int|null
|
||||
*/
|
||||
final public function getProjectRole(int $projectId): ?int
|
||||
{
|
||||
$project = self::projects()
|
||||
->where(['project_id' => $projectId])
|
||||
->first();
|
||||
|
||||
return optional(optional($project)->pivot)->role_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user has a role in the project.
|
||||
*
|
||||
* @param Role|array $role
|
||||
* @param int $projectId
|
||||
* @return bool
|
||||
*/
|
||||
final public function hasProjectRole(Role|array $role, int $projectId): bool
|
||||
{
|
||||
$self = $this;
|
||||
$roles = Cache::store('octane')->remember(
|
||||
"role_project_$self->id",
|
||||
config('cache.role_caching_ttl'),
|
||||
static fn() => $self->projectsRelation()
|
||||
->get(['role_id', 'project_id'])
|
||||
->keyBy('project_id')
|
||||
->map(static fn($el) => $el->role_id)
|
||||
->all(),
|
||||
);
|
||||
|
||||
if (!isset($roles[$projectId])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_array($role)) {
|
||||
foreach ($role as $e) {
|
||||
if ($roles[$projectId] === $e->value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} elseif ($roles[$projectId] === $role->value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($role === Role::ANY) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user has a role in any project.
|
||||
*
|
||||
* @param Role|array $role
|
||||
* @return bool
|
||||
*/
|
||||
final public function hasRoleInAnyProject(Role|array $role): bool
|
||||
{
|
||||
$self = $this;
|
||||
$roles = Cache::store('octane')->remember(
|
||||
"role_any_project_$self->id",
|
||||
config('cache.role_caching_ttl'),
|
||||
static fn() => $self->projectsRelation()
|
||||
->get(['role_id'])
|
||||
->keyBy('role_id')
|
||||
->map(static fn($el) => $el->role_id)
|
||||
->all(),
|
||||
);
|
||||
|
||||
if (is_array($role)) {
|
||||
foreach ($role as $e) {
|
||||
if (isset($roles[$e->value])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} elseif (isset($roles[$role->value])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user