|
11 | 11 | use Illuminate\Http\JsonResponse; |
12 | 12 | use Illuminate\Http\Request; |
13 | 13 | use Illuminate\Http\Resources\Json\AnonymousResourceCollection; |
| 14 | +use Illuminate\Support\Facades\Log; |
14 | 15 |
|
15 | 16 |
|
16 | 17 | /** |
@@ -142,11 +143,28 @@ public function create(Request $request) |
142 | 143 | $description = $request->get('description'); |
143 | 144 | $estimatedUsers = $request->get('estimatedUsers', 0); |
144 | 145 |
|
145 | | - $userId = $request->user()->id; |
| 146 | + $user = $request->user()->loadMissing('roles', 'organisations'); |
| 147 | + $userId = $user->id; |
146 | 148 |
|
147 | 149 | try { |
148 | 150 | $application = $this->client->createApplication($name, $description, $estimatedUsers, $userId); |
149 | 151 |
|
| 152 | + $userRules = [ |
| 153 | + 'can_access_legacy_whatnow' => (bool) $user->can_access_legacy_whatnow, |
| 154 | + 'can_access_preparedness_v2' => (bool) $user->can_access_preparedness_v2, |
| 155 | + ]; |
| 156 | + |
| 157 | + $currentRole = $user->roles->first(); |
| 158 | + if ($currentRole && ! $currentRole->api_full_access) { |
| 159 | + $userRules['allowed_country_code'] = $user->organisations->pluck('organisation_code')->values()->all(); |
| 160 | + } |
| 161 | + |
| 162 | + try { |
| 163 | + $this->client->updateRules($userId, $userRules); |
| 164 | + } catch (\Exception $e) { |
| 165 | + Log::error('Failed to update rules for user ' . $userId . ': ' . $e->getMessage()); |
| 166 | + } |
| 167 | + |
150 | 168 | return ApplicationResource::make($application); |
151 | 169 | } catch (RcnApiResourceNotFoundException $e) { |
152 | 170 | return $this->respondWithNotFound($e); |
|
0 commit comments