Add per-store managers for Woo Business accounts.
Deploy Ladill Woo Manager / deploy (push) Successful in 39s
Deploy Ladill Woo Manager / deploy (push) Successful in 39s
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>
This commit is contained in:
@@ -7,6 +7,7 @@ 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). */
|
||||
@@ -33,6 +34,34 @@ class User extends Authenticatable
|
||||
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);
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class WooStoreMember extends Model
|
||||
{
|
||||
public const ROLE_MANAGER = 'manager';
|
||||
|
||||
protected $fillable = [
|
||||
'owner_ref',
|
||||
'user_ref',
|
||||
'woo_store_id',
|
||||
'role',
|
||||
];
|
||||
|
||||
public function store(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(WooStore::class, 'woo_store_id');
|
||||
}
|
||||
|
||||
public function hasRole(string ...$roles): bool
|
||||
{
|
||||
return in_array($this->role, $roles, true);
|
||||
}
|
||||
|
||||
public function scopeOwned(Builder $query, string $ownerRef): Builder
|
||||
{
|
||||
return $query->where('owner_ref', $ownerRef);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user