|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\User; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
|
|
|
|
|
7
|
|
|
use Illuminate\Http\Response; |
|
|
|
|
|
|
8
|
|
|
use Scriptotek\Alma\Client; |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class WebhooksController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Alma webhooks router. |
|
14
|
|
|
* |
|
15
|
|
|
* @param Request $request |
|
16
|
|
|
* @param Client $almaClient |
|
17
|
|
|
* @return Response |
|
18
|
|
|
*/ |
|
19
|
|
|
public function handle(Request $request, Client $almaClient) |
|
20
|
|
|
{ |
|
21
|
|
|
$secret = config('alma.webhook_secret'); |
|
|
|
|
|
|
22
|
|
|
$eventType = $request->input('action'); |
|
23
|
|
|
$hash = $request->header('X-Exl-Signature'); |
|
24
|
|
|
$expectedHash = 'sha256=' . hash_hmac('sha256', $request->getContent(), $secret); |
|
25
|
|
|
|
|
26
|
|
|
if ($hash !== $expectedHash) { |
|
27
|
|
|
\Log::warning( |
|
28
|
|
|
"Ignoring Alma '{$eventType}' event due to invalid signature. " . |
|
29
|
|
|
"Expected '{$expectedHash}', got '{$hash}'." |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
return response()->json(['errorMessage' => 'Invalid Signature'], 401); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
switch ($eventType) { |
|
36
|
|
|
case 'USER': |
|
37
|
|
|
return $this->handleUserUpdate($almaClient, $request->input('webhook_user')); |
|
38
|
|
|
|
|
39
|
|
|
default: |
|
40
|
|
|
return response('No handler for this webhook event type.', 202); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Handler for the Alma user webhook. |
|
46
|
|
|
* |
|
47
|
|
|
* @param Client $almaClient |
|
48
|
|
|
* @param $data |
|
49
|
|
|
* @return Response |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function handleUserUpdate(Client $almaClient, $data) |
|
52
|
|
|
{ |
|
53
|
|
|
\Log::info("Received user update notification from Alma. Type {$data->method}, caused by {$data->cause}."); |
|
54
|
|
|
|
|
55
|
|
|
$almaClientUser = \Scriptotek\Alma\Users\User::make($almaClient, $data->user->user_id) |
|
|
|
|
|
|
56
|
|
|
->setData($data->user); |
|
57
|
|
|
|
|
58
|
|
|
$almaUser = new \App\Alma\User($almaClientUser); |
|
59
|
|
|
|
|
60
|
|
|
$localUser = User::where('alma_primary_id', '=', $almaUser->primaryId) |
|
61
|
|
|
->orWhere('university_id', '=', $almaUser->getUniversityId()) |
|
62
|
|
|
->first(); |
|
63
|
|
|
|
|
64
|
|
|
if (is_null($localUser)) { |
|
65
|
|
|
\Log::debug('User not in Bibrex'); |
|
66
|
|
|
} else { |
|
67
|
|
|
$localUser->mergeFromAlmaResponse($almaUser); |
|
68
|
|
|
$localUser->save(); |
|
69
|
|
|
\Log::debug('Updated user with changes from Alma: ' . $localUser->id); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Say yo to Alma |
|
73
|
|
|
return response('Yo!', 200); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths