Skip to content

Commit 8c85d44

Browse files
committed
Update user rules after creating application
After creating an application, load the user's roles and organisations and send updated access rules to the API. Add feature flags for legacy WhatNow and Preparedness V2, and restrict allowed_country_code to the user's organisations when the current role lacks api_full_access. Wrap the rules update in a try/catch and log failures. Also import Illuminate\Support\Facades\Log.
1 parent aabef12 commit 8c85d44

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

app/Http/Controllers/WhatNow/ApplicationController.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Http\JsonResponse;
1212
use Illuminate\Http\Request;
1313
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
14+
use Illuminate\Support\Facades\Log;
1415

1516

1617
/**
@@ -142,11 +143,28 @@ public function create(Request $request)
142143
$description = $request->get('description');
143144
$estimatedUsers = $request->get('estimatedUsers', 0);
144145

145-
$userId = $request->user()->id;
146+
$user = $request->user()->loadMissing('roles', 'organisations');
147+
$userId = $user->id;
146148

147149
try {
148150
$application = $this->client->createApplication($name, $description, $estimatedUsers, $userId);
149151

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+
150168
return ApplicationResource::make($application);
151169
} catch (RcnApiResourceNotFoundException $e) {
152170
return $this->respondWithNotFound($e);

0 commit comments

Comments
 (0)