22
33namespace SimpleSAML \Module \perun \Auth \Process ;
44
5+ use SimpleSAML \Auth \ProcessingFilter ;
56use SimpleSAML \Module \perun \Adapter ;
67use SimpleSAML \Error \Exception ;
78use 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}
0 commit comments