authorizeAbility($request, 'admin.members.view'); if ($upgrade = $this->proFeatureUpgradeView( $request, self::FEATURE, 'Team', 'Invite colleagues, assign roles, and scope access by branch with Frontdesk Pro.', )) { return $upgrade; } $organization = $this->organization($request); $members = Member::owned($this->ownerRef($request)) ->where('organization_id', $organization->id) ->with('branch') ->orderBy('created_at') ->get(); $adminRoles = ['super_admin', 'org_admin', 'branch_admin']; $heroStats = [ 'total' => $members->count(), 'admins' => $members->whereIn('role', $adminRoles)->count(), 'branch_scoped' => $members->whereNotNull('branch_id')->count(), ]; return view('frontdesk.admin.members.index', [ 'members' => $members, 'organization' => $organization, 'roles' => config('frontdesk.roles'), 'heroStats' => $heroStats, ]); } public function create(Request $request, IdentityTeamClient $identity): View { $this->authorizeAbility($request, 'admin.members.manage'); if ($upgrade = $this->proFeatureUpgradeView( $request, self::FEATURE, 'Team', 'Invite team members with Frontdesk Pro.', )) { return $upgrade; } $organization = $this->organization($request); $branches = Branch::owned($this->ownerRef($request)) ->where('organization_id', $organization->id) ->where('is_active', true) ->orderBy('name') ->get(); $mailboxOptions = []; try { $mailboxOptions = $identity->mailboxOptions($this->ownerRef($request)); } catch (\Throwable) { // Identity optional for form rendering. } return view('frontdesk.admin.members.create', [ 'organization' => $organization, 'branches' => $branches, 'roles' => config('frontdesk.roles'), 'mailboxOptions' => $mailboxOptions, ]); } public function store(Request $request, IdentityTeamClient $identity): RedirectResponse { $this->authorizeAbility($request, 'admin.members.manage'); if ($deny = $this->denyWithoutFeature($request, self::FEATURE, self::UPGRADE_MESSAGE)) { return $deny; } $organization = $this->organization($request); $owner = $this->ownerRef($request); $validated = $request->validate([ 'email' => ['required', 'email', 'max:255'], 'role' => ['required', 'string', 'in:'.implode(',', array_keys(config('frontdesk.roles')))], 'branch_id' => ['nullable', 'integer', 'exists:frontdesk_branches,id'], ]); $email = strtolower(trim($validated['email'])); if ($email === strtolower((string) $request->user()->email)) { return back()->withErrors(['email' => 'You cannot invite yourself.']); } $identity->inviteAppMember( $owner, $email, ['frontdesk'], [ 'frontdesk' => [ 'organization_id' => $organization->id, 'role' => $validated['role'], 'branch_id' => $validated['branch_id'] ?? null, ], ], ); Member::updateOrCreate( [ 'organization_id' => $organization->id, 'user_ref' => $email, ], [ 'owner_ref' => $owner, 'role' => $validated['role'], 'branch_id' => $validated['branch_id'] ?? null, ], ); return redirect()->route('frontdesk.members.index')->with('success', 'Invitation sent to '.$email.'.'); } public function destroy(Request $request, Member $member): RedirectResponse { $this->authorizeAbility($request, 'admin.members.manage'); $this->authorizeOwner($request, $member); if ($deny = $this->denyWithoutFeature($request, self::FEATURE, self::UPGRADE_MESSAGE)) { return $deny; } abort_if($member->user_ref === $this->ownerRef($request), 422, 'You cannot remove yourself.'); $member->delete(); return redirect()->route('frontdesk.members.index')->with('success', 'Member removed.'); } }