Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.24% covered (warning)
88.24%
15 / 17
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GenerateMasqueradeRequest
88.24% covered (warning)
88.24%
15 / 17
50.00% covered (danger)
50.00%
1 / 2
6.06
0.00% covered (danger)
0.00%
0 / 1
 authorize
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
5.12
 rules
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Http\Requests\v2\Masquerade;
4
5use App\Http\Models\Auth\Role;
6use Illuminate\Foundation\Http\FormRequest;
7
8/**
9 * Request for generating a masquerade JWT token.
10 *
11 * Validates the target user and company, and authorizes the requesting admin
12 * based on their role: VENGRESO_ADMIN can masquerade any user, GLOBAL_ADMIN
13 * can only masquerade users within their own company.
14 *
15 * @property string $user_id The ID of the target user to masquerade as
16 * @property string|null $company_id The company ID of the target user (optional for individual users)
17 * @property string|null $admin_refresh_token The admin's current refresh token for session backup
18 */
19class GenerateMasqueradeRequest extends FormRequest
20{
21    /**
22     * Determine if the user is authorized to make this request.
23     *
24     * - VENGRESO_ADMIN: can masquerade any user
25     * - GLOBAL_ADMIN: can only masquerade users in their own company
26     * - All others: denied
27     */
28    public function authorize(): bool
29    {
30        $user = $this->user();
31
32        if (! $user) {
33            return false;
34        }
35
36        $roles = $user->roles();
37
38        if (in_array(Role::VENGRESO_ADMIN, $roles)) {
39            return true;
40        }
41
42        if (in_array(Role::GLOBAL_ADMIN, $roles)) {
43            $companyId = $this->input('company_id');
44
45            // Global Admin without company_id can masquerade individual users (no company)
46            if (! $companyId) {
47                return true;
48            }
49
50            return $user->company_id === $companyId;
51        }
52
53        return false;
54    }
55
56    /**
57     * Get the validation rules that apply to the request.
58     *
59     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
60     */
61    public function rules(): array
62    {
63        return [
64            'user_id' => 'required|string',
65            'company_id' => 'sometimes|nullable|string',
66            'admin_refresh_token' => 'sometimes|nullable|string',
67        ];
68    }
69}