authorize('viewAny', User::class); $perPage = $request->has('limit') ? $request->limit : 10; $viewer = $request->user(); $members = User::whereCompany() ->applyFilters($request->all()) ->where('id', '<>', $viewer->id) ->latest() ->paginate($perPage); return UserResource::collection($members)->additional([ 'meta' => ['user_total_count' => User::whereCompany()->count()], ]); } /** * Open a staff account and place it in the companies the form listed. * * Note the gate: only the active company is weighed, so an owner may file * an account into any company whose id they care to submit. * * @return JsonResponse */ public function store(MemberRequest $request) { $this->authorize('create', User::class); $member = $this->memberService->create( $request->getUserPayload(), $request->validated('companies'), ); return new UserResource($member); } /** * One colleague, provided they share the active company with the caller. * * @return JsonResponse */ public function show(User $member) { $this->authorize('view', $member); return new UserResource($member); } /** * Overwrite a colleague's account and re-point their memberships. * * @return JsonResponse */ public function update(MemberRequest $request, User $member) { $this->authorize('update', $member); $this->memberService->update( $member, $request->getUserPayload(), $request->validated('companies'), ); return new UserResource($member); } /** * Erase a batch of accounts. * * The submitted ids were checked against the users table installation-wide, * then narrowed to members of the active company here, so an id belonging * to somebody else's tenant clears validation and is quietly dropped from * the batch — the call still answers success. Kept as it stands. * * The gate is the bulk ability rather than the per-account policy, so it * asks nothing about the individual targets; the narrowing above is what * keeps one company out of another's accounts. * * @param Request $request * @return JsonResponse */ public function delete(DeleteMemberRequest $request) { $this->authorize('delete multiple users', User::class); $submitted = $request->users; if ($submitted) { $targets = User::whereCompany() ->whereIn('id', $submitted) ->pluck('id') ->toArray(); if ($targets) { $this->memberService->delete($targets); } } return response()->json([ 'success' => true, ]); } }