-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
109 changed files
with
1,568 additions
and
1,452 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Concerns; | ||
|
||
use App\Models\Organization; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
trait BelongsToOrganization | ||
{ | ||
public function initializeBelongsToOrganization(): void | ||
{ | ||
$this->fillable[] = 'organization_id'; | ||
} | ||
|
||
public function organization(): BelongsTo | ||
{ | ||
return $this->belongsTo(Organization::class); | ||
} | ||
|
||
public function belongsToOrganization(?Organization $organization = null): bool | ||
{ | ||
if ($organization === null) { | ||
return $this->organization_id !== null; | ||
} | ||
|
||
return $this->organization_id === $organization->getKey(); | ||
} | ||
|
||
public function scopeWhereBelongsToOrganization(Builder $query, ?Organization $organization = null): Builder | ||
{ | ||
if ($organization === null) { | ||
return $query->whereNotNull('organization_id'); | ||
} | ||
|
||
return $query->whereBelongsTo($organization); | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Concerns\Enums; | ||
|
||
trait Comparable | ||
{ | ||
/** | ||
* Check if this enum matches the given enum instance or value. | ||
*/ | ||
public function is(mixed $enum): bool | ||
{ | ||
if ($enum instanceof static) { | ||
return $this->value === $enum->value; | ||
} | ||
|
||
return $this->value === $enum; | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Concerns\Enums; | ||
|
||
trait HasLabel | ||
{ | ||
protected function labelKeyPrefix(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function label(): string | ||
{ | ||
$label = collect([$this->labelKeyPrefix(), $this->value]) | ||
->filter() | ||
->implode('.'); | ||
|
||
return __($label); | ||
} | ||
} |
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,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Concerns; | ||
|
||
use App\Enums\UserRole; | ||
use App\Models\Organization; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
trait HasRole | ||
{ | ||
public function initializeHasRole(): void | ||
{ | ||
$this->casts['role'] = UserRole::class; | ||
} | ||
|
||
private function hasRole(UserRole $role): bool | ||
{ | ||
return $this->role === $role; | ||
} | ||
|
||
public function isSuperUser(): bool | ||
{ | ||
return $this->isSuperAdmin() || $this->isSuperManager(); | ||
} | ||
|
||
public function isSuperAdmin(): bool | ||
{ | ||
return $this->hasRole(UserRole::SUPERADMIN); | ||
} | ||
|
||
public function isSuperManager(): bool | ||
{ | ||
return $this->hasRole(UserRole::SUPERMANAGER); | ||
} | ||
|
||
public function isOrganizationAdmin(?Organization $organization = null): bool | ||
{ | ||
return $this->hasRole(UserRole::ADMIN) && $this->belongsToOrganization($organization); | ||
} | ||
|
||
public function isOrganizationManager(?Organization $organization = null): bool | ||
{ | ||
return $this->hasRole(UserRole::USER) && $this->belongsToOrganization($organization); | ||
} | ||
|
||
// ========================================== // | ||
public function scopeOnlySuperUsers(Builder $query): Builder | ||
{ | ||
return $query->whereIn('role', [UserRole::SUPERADMIN, UserRole::SUPERMANAGER]); | ||
} | ||
|
||
public function scopeOnlySuperAdmins(Builder $query): Builder | ||
{ | ||
return $query->where('role', UserRole::SUPERADMIN); | ||
} | ||
|
||
public function scopeOnlySuperManagers(Builder $query): Builder | ||
{ | ||
return $query->where('role', UserRole::SUPERMANAGER); | ||
} | ||
|
||
public function scopeWithoutSuperUsers(Builder $query): Builder | ||
{ | ||
return $query->whereNotIn('role', [UserRole::SUPERADMIN, UserRole::SUPERMANAGER]); | ||
} | ||
|
||
// ========================================== // | ||
|
||
public function scopeOnlyOrganizationAdmins(Builder $query, ?Organization $organization = null): Builder | ||
{ | ||
return $query->where('role', UserRole::ADMIN) | ||
->whereBelongsToOrganization($organization); | ||
} | ||
|
||
// ========================================== // | ||
|
||
public function isDonor(): bool | ||
{ | ||
return $this->role === UserRole::donor; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.