Skip to content

Commit 54c0445

Browse files
authored
Merge pull request #322 from IFRCGo/WN-367
Update user rules after creating application
2 parents aabef12 + 8c85d44 commit 54c0445

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)