Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit d4d36e9

Browse files
Merge pull request #91 from pajavyskocil/tests
UES is now updated asynchronously
2 parents b88f3c4 + bc3ec33 commit d4d36e9

File tree

3 files changed

+109
-71
lines changed

3 files changed

+109
-71
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
All notable changes to this project will be documented in this file.
33

44
## [Unreleased]
5+
#### Changed
6+
- UserExtSources are now updated asynchronously
57

68
## [v3.6.0]
79
#### Added

lib/Auth/Process/UpdateUserExtSource.php

Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22

33
namespace SimpleSAML\Module\perun\Auth\Process;
44

5+
use SimpleSAML\Auth\ProcessingFilter;
56
use SimpleSAML\Module\perun\Adapter;
67
use SimpleSAML\Error\Exception;
78
use SimpleSAML\Logger;
9+
use SimpleSAML\Module;
10+
use SimpleSAML\Module\perun\UpdateUESThread;
811

912
/**
1013
* Class sspmod_perun_Auth_Process_UpdateUserExtSource
1114
*
1215
* This filter updates userExtSource attributes when he logs in.
1316
*
1417
* @author Dominik Baránek <[email protected]>
18+
* @author Pavel Vyskočil <[email protected]>
1519
*/
16-
class UpdateUserExtSource extends \SimpleSAML\Auth\ProcessingFilter
20+
class UpdateUserExtSource extends ProcessingFilter
1721
{
1822
private $attrMap;
1923
private $attrsToConversion;
20-
private $adapter;
21-
const UES_ATTR_NMS = 'urn:perun:ues:attribute-def:def:';
2224

2325
public function __construct($config, $reserved)
2426
{
@@ -39,77 +41,19 @@ public function __construct($config, $reserved)
3941
}
4042

4143
$this->attrMap = (array)$config['attrMap'];
42-
$this->adapter = Adapter::getInstance(Adapter::RPC);
4344
}
4445

4546
public function process(&$request)
4647
{
47-
assert(is_array($request));
48-
try {
49-
$userExtSource = $this->adapter->getUserExtSource(
50-
$request['Attributes']['sourceIdPEntityID'][0],
51-
$request['Attributes']['sourceIdPEppn'][0]
52-
);
53-
if ($userExtSource === null) {
54-
throw new Exception(
55-
'sspmod_perun_Auth_Process_UpdateUserExtSource: there is no UserExtSource with ExtSource ' .
56-
$request['Attributes']['sourceIdPEntityID'][0] . " and Login " .
57-
$request['Attributes']['sourceIdPEppn'][0]
58-
);
59-
}
60-
61-
$attributes = $this->adapter->getUserExtSourceAttributes($userExtSource['id'], array_keys($this->attrMap));
62-
63-
if ($attributes === null) {
64-
throw new Exception(
65-
'sspmod_perun_Auth_Process_UpdateUserExtSource: getting attributes was not successful.'
66-
);
67-
}
68-
69-
$attributesToUpdate = [];
70-
foreach ($attributes as $attribute) {
71-
$attrName = self::UES_ATTR_NMS . $attribute['friendlyName'];
72-
if (isset($this->attrMap[$attrName]) && isset($request['Attributes'][$this->attrMap[$attrName]])) {
73-
$attr = $request['Attributes'][$this->attrMap[$attrName]];
74-
75-
if (in_array(self::UES_ATTR_NMS . $attribute['friendlyName'], $this->attrsToConversion)) {
76-
$arrayAsString = [''];
77-
foreach ($attr as $value) {
78-
$arrayAsString[0] .= $value . ';';
79-
}
80-
if (!empty($arrayAsString[0])) {
81-
$arrayAsString[0] = substr($arrayAsString[0], 0, -1);
82-
}
83-
$attr = $arrayAsString;
84-
}
85-
86-
if (strpos($attribute['type'], 'String') ||
87-
strpos($attribute['type'], 'Integer') ||
88-
strpos($attribute['type'], 'Boolean')) {
89-
$valueFromIdP = $attr[0];
90-
} elseif (strpos($attribute['type'], 'Array') || strpos($attribute['type'], 'Map')) {
91-
$valueFromIdP = $attr;
92-
} else {
93-
throw new Exception(
94-
'sspmod_perun_Auth_Process_UpdateUserExtSource: unsupported type of attribute.'
95-
);
96-
}
97-
if ($valueFromIdP !== $attribute['value']) {
98-
$attribute['value'] = $valueFromIdP;
99-
array_push($attributesToUpdate, $attribute);
100-
}
101-
}
102-
}
103-
104-
if (!empty($attributesToUpdate)) {
105-
$this->adapter->setUserExtSourceAttributes($userExtSource['id'], $attributesToUpdate);
106-
}
107-
$this->adapter->updateUserExtSourceLastAccess($userExtSource['id']);
108-
} catch (\Exception $ex) {
109-
Logger::warning(
110-
'sspmod_perun_Auth_Process_UpdateUserExtSource: update was not successful: ' .
111-
$ex->getMessage() . ' Skip to next filter.'
112-
);
113-
}
48+
$data = [
49+
'attributes' => $request['Attributes'],
50+
'attrMap' => $this->attrMap,
51+
'attrsToConversion' => $this->attrsToConversion,
52+
'perunUserId' => $request['perun']['user']->getId()
53+
];
54+
55+
$cmd = 'curl -X POST -H "Content-Type: application/json" -d \'' . json_encode($data) . '\' ' .
56+
Module::getModuleURL('perun/updateUes.php') . ' > /dev/null &';
57+
exec($cmd);
11458
}
11559
}

www/updateUes.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* Script for updating UES in separate thread
5+
*
6+
* @author Pavel Vyskočil <[email protected]>
7+
*/
8+
9+
use SimpleSAML\Logger;
10+
use SimpleSAML\Module\perun\Adapter;
11+
12+
$adapter = Adapter::getInstance(Adapter::RPC);
13+
14+
$entityBody = file_get_contents('php://input');
15+
$body = json_decode($entityBody, true);
16+
17+
$attributes = $body['attributes'];
18+
$attrMap = $body['attrMap'];
19+
$attrsToConversion = $body['attrsToConversion'];
20+
$perunUserId = $body['perunUserId'];
21+
22+
const UES_ATTR_NMS = 'urn:perun:ues:attribute-def:def:';
23+
24+
try {
25+
$userExtSource = $adapter->getUserExtSource(
26+
$attributes['sourceIdPEntityID'][0],
27+
$attributes['sourceIdPEppn'][0]
28+
);
29+
if ($userExtSource === null) {
30+
throw new Exception(
31+
'sspmod_perun_Auth_Process_UpdateUserExtSource: there is no UserExtSource with ExtSource ' .
32+
$attributes['sourceIdPEntityID'][0] . " and Login " .
33+
$attributes['sourceIdPEppn'][0]
34+
);
35+
}
36+
37+
$attributes = $adapter->getUserExtSourceAttributes($userExtSource['id'], array_keys($attrMap));
38+
39+
if ($attributes === null) {
40+
throw new Exception(
41+
'sspmod_perun_Auth_Process_UpdateUserExtSource: getting attributes was not successful.'
42+
);
43+
}
44+
45+
$attributesToUpdate = [];
46+
foreach ($attributes as $attribute) {
47+
$attrName = UES_ATTR_NMS . $attribute['friendlyName'];
48+
if (isset($attrMap[$attrName], $attributes[$attrMap[$attrName]])) {
49+
$attr = $attributes[$attrMap[$attrName]];
50+
51+
if (in_array(UES_ATTR_NMS . $attribute['friendlyName'], $attrsToConversion)) {
52+
$arrayAsString = [''];
53+
foreach ($attr as $value) {
54+
$arrayAsString[0] .= $value . ';';
55+
}
56+
if (!empty($arrayAsString[0])) {
57+
$arrayAsString[0] = substr($arrayAsString[0], 0, -1);
58+
}
59+
$attr = $arrayAsString;
60+
}
61+
62+
if (strpos($attribute['type'], 'String') ||
63+
strpos($attribute['type'], 'Integer') ||
64+
strpos($attribute['type'], 'Boolean')) {
65+
$valueFromIdP = $attr[0];
66+
} elseif (strpos($attribute['type'], 'Array') || strpos($attribute['type'], 'Map')) {
67+
$valueFromIdP = $attr;
68+
} else {
69+
throw new Exception(
70+
'sspmod_perun_updateUes: unsupported type of attribute.'
71+
);
72+
}
73+
if ($valueFromIdP !== $attribute['value']) {
74+
$attribute['value'] = $valueFromIdP;
75+
array_push($attributesToUpdate, $attribute);
76+
}
77+
}
78+
}
79+
80+
if (!empty($attributesToUpdate)) {
81+
$adapter->setUserExtSourceAttributes($userExtSource['id'], $attributesToUpdate);
82+
}
83+
84+
$adapter->updateUserExtSourceLastAccess($userExtSource['id']);
85+
86+
Logger::debug('sspmod_perun_updateUes - Updating UES for user with userId: ' . $perunUserId . ' was successful.');
87+
} catch (\Exception $ex) {
88+
Logger::warning(
89+
'sspmod_perun_updateUes: Updating UES for user with userId: ' . $perunUserId . ' was not successful: ' .
90+
$ex->getMessage()
91+
);
92+
}

0 commit comments

Comments
 (0)