Backend: - New tenants table with PSGC-derived code, links to cities_municipalities, optional boundary_polygon override, theme color, contact info, timezone. - Adds nullable tenant_id to users / households / drop_off_points / dumpsites / partner_stores. Foreign-keyed, indexed. - Tenant model with deriveCode() helper + effectiveBoundary() fallback chain. - App\Tenancy\Tenancy — process-level current-tenant register with withTenant() / withoutScope() helpers for jobs + super-admin. - App\Tenancy\TenantScope — global Eloquent scope, no-op when no tenant is set (so seeders + super-admin reads still work). - App\Tenancy\HasTenant trait — applied to Household, DropOffPoint, Dumpsite, PartnerStore. Auto-fills tenant_id on create from Tenancy::current(). - ResolveTenant middleware — reads X-Tenant-Code (or X-Tenant-Id), validates tenant exists + active, sets Tenancy::current(). Falls back to authenticated user's tenant_id when header missing. Registered globally on the api group. - Login + register now require an active tenant (super-admin bypasses). Cross-tenant credential reuse is rejected with a 403 + clear message. - super_admin role added to RoleSeeder + users.role enum. - Public GET /api/v1/tenants/lookup?code= — no auth, returns tenant details for the pre-login screen. Seeders: - SuperAdminSeeder seeds super@verde.local (tenant_id = null). - SanPascualTenantSeeder seeds Region IV-A → Batangas → San Pascual municipality → sample Poblacion barangay → Tenant row with code SAN-PASCUAL-BAT, then backfills every existing tenant-aware row (13 users / 2 households / 5 DOPs / 1 dumpsite / 3 stores) so the dev environment keeps working end-to-end. - Wired into DatabaseSeeder so migrate:fresh --seed bootstraps cleanly. Customer-web: - New /tenant page — text input, calls public lookup, confirms with resolved tenant card, stores code + name in cookies (1 year). "Pilot users: SAN-PASCUAL-BAT" hint as a clickable shortcut. - /login + /register now redirect to /tenant?next= when no cookie, show a verde "signing in to <LGU>" pill with a Switch link, delegate the actual form to client components. - /api/tenant route — POST sets cookie, DELETE clears. - apiServer auto-attaches X-Tenant-Code on every API call when the cookie is present. - Tenant cookies are non-httpOnly so the client can echo them; the session token stays httpOnly. Build: 23 routes (added /tenant), 196 backend tests still pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
179 lines
4.8 KiB
PHP
179 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Str;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
use Spatie\Activitylog\LogOptions;
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
class User extends Authenticatable implements MustVerifyEmail
|
|
{
|
|
/** @use HasFactory<\Database\Factories\UserFactory> */
|
|
use HasApiTokens, HasFactory, HasRoles, LogsActivity, Notifiable, SoftDeletes;
|
|
|
|
public const ROLE_SUPER_ADMIN = 'super_admin';
|
|
public const ROLE_ADMIN = 'admin';
|
|
public const ROLE_RESIDENT = 'resident';
|
|
public const ROLE_DRIVER = 'driver';
|
|
public const ROLE_HELPER = 'helper';
|
|
public const ROLE_SCANNER = 'scanner';
|
|
public const ROLE_STORE_PARTNER = 'store_partner';
|
|
|
|
public const STATUS_ACTIVE = 'active';
|
|
public const STATUS_SUSPENDED = 'suspended';
|
|
public const STATUS_PENDING = 'pending';
|
|
|
|
protected $fillable = [
|
|
'uuid',
|
|
'tenant_id',
|
|
'email',
|
|
'phone',
|
|
'password',
|
|
'role',
|
|
'status',
|
|
'first_name',
|
|
'middle_name',
|
|
'last_name',
|
|
'avatar_path',
|
|
'preferred_language',
|
|
'fcm_token',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $user): void {
|
|
if (empty($user->uuid)) {
|
|
$user->uuid = (string) Str::uuid();
|
|
}
|
|
});
|
|
}
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'phone_verified_at' => 'datetime',
|
|
'last_login_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
];
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logOnly(['email', 'phone', 'role', 'status', 'first_name', 'last_name'])
|
|
->logOnlyDirty()
|
|
->dontSubmitEmptyLogs();
|
|
}
|
|
|
|
public function getFullNameAttribute(): string
|
|
{
|
|
return trim(implode(' ', array_filter([
|
|
$this->first_name,
|
|
$this->middle_name,
|
|
$this->last_name,
|
|
])));
|
|
}
|
|
|
|
public function sendEmailVerificationNotification(): void
|
|
{
|
|
$this->notify(new \App\Notifications\VerifyEmailNotification());
|
|
}
|
|
|
|
public function headedHousehold(): HasOne
|
|
{
|
|
return $this->hasOne(Household::class, 'head_user_id');
|
|
}
|
|
|
|
public function householdMemberships()
|
|
{
|
|
return $this->hasMany(HouseholdMember::class);
|
|
}
|
|
|
|
public function residentProfile(): HasOne
|
|
{
|
|
return $this->hasOne(ResidentProfile::class);
|
|
}
|
|
|
|
public function driverProfile(): HasOne
|
|
{
|
|
return $this->hasOne(DriverProfile::class);
|
|
}
|
|
|
|
public function helperProfile(): HasOne
|
|
{
|
|
return $this->hasOne(HelperProfile::class);
|
|
}
|
|
|
|
public function scannerProfile(): HasOne
|
|
{
|
|
return $this->hasOne(ScannerProfile::class);
|
|
}
|
|
|
|
public function storePartnerProfile(): HasOne
|
|
{
|
|
return $this->hasOne(StorePartnerProfile::class);
|
|
}
|
|
|
|
public function tenant(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Tenant::class);
|
|
}
|
|
|
|
public function isSuperAdmin(): bool
|
|
{
|
|
return $this->role === self::ROLE_SUPER_ADMIN;
|
|
}
|
|
|
|
/**
|
|
* Resolve the role-specific profile relation name. Admins have no profile.
|
|
*/
|
|
public function profileRelation(): ?string
|
|
{
|
|
return match ($this->role) {
|
|
self::ROLE_RESIDENT => 'residentProfile',
|
|
self::ROLE_DRIVER => 'driverProfile',
|
|
self::ROLE_HELPER => 'helperProfile',
|
|
self::ROLE_SCANNER => 'scannerProfile',
|
|
self::ROLE_STORE_PARTNER => 'storePartnerProfile',
|
|
default => null,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Map of role => profile FQCN. Used for create-on-register.
|
|
*/
|
|
public static function profileModelForRole(string $role): ?string
|
|
{
|
|
return match ($role) {
|
|
self::ROLE_RESIDENT => ResidentProfile::class,
|
|
self::ROLE_DRIVER => DriverProfile::class,
|
|
self::ROLE_HELPER => HelperProfile::class,
|
|
self::ROLE_SCANNER => ScannerProfile::class,
|
|
self::ROLE_STORE_PARTNER => StorePartnerProfile::class,
|
|
default => null,
|
|
};
|
|
}
|
|
|
|
public function profile(): ?Model
|
|
{
|
|
$relation = $this->profileRelation();
|
|
|
|
return $relation ? $this->{$relation} : null;
|
|
}
|
|
}
|