Skip to content

Commit

Permalink
review users (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Sep 21, 2023
1 parent 0762f80 commit accb8eb
Show file tree
Hide file tree
Showing 109 changed files with 1,568 additions and 1,452 deletions.
40 changes: 40 additions & 0 deletions app/Concerns/BelongsToOrganization.php
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace App\Concerns;
namespace App\Concerns\Enums;

trait ArrayableEnum
trait Arrayable
{
public static function names(): array
{
Expand All @@ -28,18 +28,4 @@ public static function options(): array
])
->all();
}

public function label(): string
{
$label = collect([$this->translationKeyPrefix(), $this->value])
->filter()
->implode('.');

return __($label);
}

protected function translationKeyPrefix(): ?string
{
return null;
}
}
20 changes: 20 additions & 0 deletions app/Concerns/Enums/Comparable.php
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;
}
}
22 changes: 22 additions & 0 deletions app/Concerns/Enums/HasLabel.php
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);
}
}
83 changes: 83 additions & 0 deletions app/Concerns/HasRole.php
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;
}
}
2 changes: 1 addition & 1 deletion app/Concerns/LogsActivityForApproval.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function tapActivity(Activity $activity, string $eventName)
return;
}

if (auth()->user()->isBbAdmin() || auth()->user()->isBbManager()) {
if (auth()->user()->isSuperUser()) {
activity()->disableLogging();

if (! $activity->description) {
Expand Down
22 changes: 9 additions & 13 deletions app/Concerns/MustSetInitialPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace App\Concerns;

use App\Enums\UserRole;
use App\Notifications\Admin\WelcomeNotification as AdminWelcomeNotification;
use App\Notifications\Ngo\WelcomeNotification;
use App\Notifications\Admin;
use App\Notifications\Ngo;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

Expand All @@ -21,10 +20,8 @@ protected static function bootMustSetInitialPassword(): void
});

static::created(function (self $user) {
if (! app()->runningInConsole()) {
if (! empty($user->created_by)) {
$user->sendWelcomeNotification();
}
if (filled($user->created_by)) {
$user->sendWelcomeNotification();
}
});
}
Expand All @@ -45,11 +42,10 @@ public function setPassword(string $password): bool

public function sendWelcomeNotification(): void
{
if ($this->role === UserRole::ngo_admin) {
$this->notify(new WelcomeNotification());

return;
}
$this->notify(new AdminWelcomeNotification());
$this->notify(
$this->isSuperUser()
? new Admin\WelcomeNotification
: new Ngo\WelcomeNotification
);
}
}
5 changes: 3 additions & 2 deletions app/Enums/ActivityDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace App\Enums;

use App\Concerns\ArrayableEnum;
use App\Concerns\Enums\Arrayable;

enum ActivityDomain: string
{
use ArrayableEnum;
use Arrayable;

case environmental_protection = 'Protecția mediului';
case education = 'Educație';
case healthcare = 'Sănătate';
Expand Down
11 changes: 8 additions & 3 deletions app/Enums/OrganizationStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@

namespace App\Enums;

use App\Concerns\ArrayableEnum;
use App\Concerns\Enums\Arrayable;
use App\Concerns\Enums\Comparable;
use App\Concerns\Enums\HasLabel;

enum OrganizationStatus: string
{
use ArrayableEnum;
use Arrayable;
use Comparable;
use HasLabel;

case draft = 'draft';
case pending = 'pending';
case approved = 'active';
case rejected = 'disabled';
case pending_changes = 'pending_changes';

protected function translationKeyPrefix(): ?string
protected function labelKeyPrefix(): ?string
{
return 'organization.status_arr';
}
Expand Down
11 changes: 8 additions & 3 deletions app/Enums/ProjectStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

namespace App\Enums;

use App\Concerns\ArrayableEnum;
use App\Concerns\Enums\Arrayable;
use App\Concerns\Enums\Comparable;
use App\Concerns\Enums\HasLabel;

enum ProjectStatus: string
{
use ArrayableEnum;
use Arrayable;
use Comparable;
use HasLabel;

case draft = 'draft';
case pending = 'pending';
case change_request = 'change_request';
Expand All @@ -17,7 +22,7 @@ enum ProjectStatus: string
case active = 'active';
case disabled = 'disabled';

protected function translationKeyPrefix(): ?string
protected function labelKeyPrefix(): ?string
{
return 'project.status_arr';
}
Expand Down
19 changes: 13 additions & 6 deletions app/Enums/UserRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@

namespace App\Enums;

use App\Concerns\ArrayableEnum;
use App\Concerns\Enums\Arrayable;
use App\Concerns\Enums\Comparable;
use App\Concerns\Enums\HasLabel;

enum UserRole: string
{
use ArrayableEnum;
use Arrayable;
use Comparable;
use HasLabel;

case SUPERADMIN = 'superadmin';
case SUPERMANAGER = 'supermanager';
case ADMIN = 'admin';
case USER = 'user';

case donor = 'donor';
case ngo_admin = 'ngo-admin';
case bb_manager = 'bb-manager';
case bb_admin = 'bb-admin';

public function translationKeyPrefix(): string
public function labelKeyPrefix(): string
{
return 'user.roles';
}
Expand Down
12 changes: 9 additions & 3 deletions app/Enums/VolunteerStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@

namespace App\Enums;

use App\Concerns\ArrayableEnum;
use App\Concerns\Enums\Arrayable;
use App\Concerns\Enums\Comparable;
use App\Concerns\Enums\HasLabel;

enum VolunteerStatus: string
{
use ArrayableEnum;
use Arrayable;
use Comparable;
use HasLabel;

case PENDING = 'pending';
case APPROVED = 'approved';
case REJECTED = 'rejected';
public function translationKeyPrefix(): string

public function labelKeyPrefix(): string
{
return 'volunteer.statuses';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ protected function setUp(): void
->with([
'activityDomains',
'counties',
'users' => fn ($q) => $q->onlyNGOAdmins(),
'projects' => fn ($q) => $q->select('id', 'organization_id', 'status')
->withCount('donations'),
])
Expand Down
Loading

0 comments on commit accb8eb

Please sign in to comment.