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:
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Woo;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\Woo\Concerns\ResolvesWooContext;
|
||||
use App\Models\WooStore;
|
||||
use App\Models\WooStoreMember;
|
||||
use App\Services\Identity\IdentityTeamClient;
|
||||
use App\Services\Woo\SubscriptionService;
|
||||
use App\Services\Woo\WooStoreMemberResolver;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreMemberController extends Controller
|
||||
{
|
||||
use ResolvesWooContext;
|
||||
|
||||
public function __construct(
|
||||
private SubscriptionService $subscriptions,
|
||||
private WooStoreMemberResolver $members,
|
||||
) {}
|
||||
|
||||
public function store(Request $request, IdentityTeamClient $identity): RedirectResponse
|
||||
{
|
||||
if ($redirect = $this->guardManage($request)) {
|
||||
return $redirect;
|
||||
}
|
||||
|
||||
$owner = (string) $this->accountUser($request)->public_id;
|
||||
|
||||
$validated = $request->validate([
|
||||
'email' => ['required', 'email', 'max:255'],
|
||||
'woo_store_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists('woo_stores', 'id')->where(fn ($q) => $q
|
||||
->where('user_id', $this->accountUser($request)->id)
|
||||
->where('status', WooStore::STATUS_ACTIVE)),
|
||||
],
|
||||
]);
|
||||
|
||||
$email = strtolower(trim($validated['email']));
|
||||
$actor = $this->actor($request);
|
||||
|
||||
if ($email === strtolower((string) $actor->email)) {
|
||||
return back()->withErrors(['email' => 'You cannot invite yourself.']);
|
||||
}
|
||||
|
||||
try {
|
||||
$identity->inviteAppMember(
|
||||
$owner,
|
||||
$email,
|
||||
['woo'],
|
||||
[
|
||||
'woo' => [
|
||||
'role' => WooStoreMember::ROLE_MANAGER,
|
||||
'store_id' => (int) $validated['woo_store_id'],
|
||||
],
|
||||
],
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
return back()->withInput()->with('error', $e->getMessage());
|
||||
}
|
||||
|
||||
WooStoreMember::updateOrCreate(
|
||||
[
|
||||
'owner_ref' => $owner,
|
||||
'user_ref' => $email,
|
||||
'woo_store_id' => (int) $validated['woo_store_id'],
|
||||
],
|
||||
[
|
||||
'role' => WooStoreMember::ROLE_MANAGER,
|
||||
],
|
||||
);
|
||||
|
||||
return redirect()
|
||||
->route('woo.settings')
|
||||
->withFragment('store-team')
|
||||
->with('success', 'Invitation sent to '.$email.'.');
|
||||
}
|
||||
|
||||
public function destroy(Request $request, WooStoreMember $member): RedirectResponse
|
||||
{
|
||||
if ($redirect = $this->guardManage($request)) {
|
||||
return $redirect;
|
||||
}
|
||||
|
||||
abort_unless($member->owner_ref === (string) $this->accountUser($request)->public_id, 404);
|
||||
|
||||
$actor = $this->actor($request);
|
||||
if ($member->user_ref === $actor->public_id || $member->user_ref === strtolower((string) $actor->email)) {
|
||||
return redirect()->route('woo.settings')->withFragment('store-team')->with('error', 'You cannot remove yourself.');
|
||||
}
|
||||
|
||||
$member->delete();
|
||||
|
||||
return redirect()
|
||||
->route('woo.settings')
|
||||
->withFragment('store-team')
|
||||
->with('success', 'Store manager removed.');
|
||||
}
|
||||
|
||||
private function guardManage(Request $request): ?RedirectResponse
|
||||
{
|
||||
if (! $this->members->canManageStoreTeam($this->wooMember($request), (string) $this->accountUser($request)->public_id, $this->actor($request))) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$account = $this->accountUser($request);
|
||||
if (! $this->subscriptions->canManageStoreTeam($account)) {
|
||||
return redirect()->route('woo.pro.index')
|
||||
->with('upsell', 'Per-store managers require Ladill Woo Manager Business.');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user