Files
ladill-woo-manager/app/Models/User.php
T
isaaccladandCursor 09359ca86a
Deploy Ladill Woo Manager / deploy (push) Successful in 39s
Add per-store managers for Woo Business accounts.
Agencies can invite store-scoped managers via Identity, with access limited to one WooCommerce store while owners retain full multi-store control.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-08 07:51:08 +00:00

72 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
use Laravel\Sanctum\HasApiTokens;
/** Thin local mirror of the platform identity (auth.ladill.com owns users). */
class User extends Authenticatable
{
/** @use HasFactory<UserFactory> */
use HasApiTokens, HasFactory, Notifiable;
protected $fillable = ['public_id', 'name', 'email', 'avatar_url', 'password', 'last_app_active_at'];
protected $hidden = ['password', 'remember_token'];
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'last_app_active_at' => 'datetime',
'password' => 'hashed',
];
}
public function wooStores(): HasMany
{
return $this->hasMany(WooStore::class);
}
public function wooMemberships(): HasMany
{
return $this->hasMany(WooStoreMember::class, 'user_ref', 'public_id');
}
public function canAccessAccount(int $accountId): bool
{
if ($accountId === $this->id) {
return true;
}
$account = self::find($accountId);
return $account !== null
&& $this->wooMemberships()->where('owner_ref', $account->public_id)->exists();
}
/** @return Collection<int, User> */
public function accessibleAccounts(): Collection
{
$ownerRefs = $this->wooMemberships()->pluck('owner_ref')->all();
return collect([$this])
->merge(self::whereIn('public_id', $ownerRefs)->get())
->unique('id')
->values();
}
public function avatarUrl(): ?string
{
$url = trim((string) $this->avatar_url);
return $url !== '' ? $url : null;
}
}