2
2
3
3
namespace SimpleSAML \Module \cesnet \Auth \Process ;
4
4
5
+ use SimpleSAML \Auth \ProcessingFilter ;
5
6
use SimpleSAML \Module \perun \LdapConnector ;
6
7
use SimpleSAML \Module \perun \RpcConnector ;
7
8
use SimpleSAML \Module \perun \AdapterLdap ;
17
18
*
18
19
* @author Pavel Vyskocil <vyskocilpavel@muni.cz>
19
20
*/
20
- class IsCesnetEligible extends \ SimpleSAML \ Auth \ ProcessingFilter
21
+ class IsCesnetEligible extends ProcessingFilter
21
22
{
22
23
const CONFIG_FILE_NAME = 'module_cesnet_IsCesnetEligible.php ' ;
23
24
const ORGANIZATION_LDAP_BASE = 'ou=Organizations,o=eduID.cz,o=apps,dc=cesnet,dc=cz ' ;
24
25
25
26
const HOSTEL_ENTITY_ID = "https://idp.hostel.eduid.cz/idp/shibboleth " ;
26
27
27
28
const INTERFACE_PROPNAME = "interface " ;
28
- const CESNET_ELIGIBLE_LAST_SEEN_ATTR = "cesnetEligibleLastSeenAttr " ;
29
+ const ATTR_NAME = "attrName " ;
30
+ const RPC_ATTRIBUTE_NAME = "RPC.attributeName " ;
31
+ const LDAP_ATTRIBUTE_NAME = 'LDAP.attributeName ' ;
29
32
const DEFAULT_ATTR_NAME = 'isCesnetEligibleLastSeen ' ;
33
+ const LDAP = 'LDAP ' ;
34
+ const RPC = 'RPC ' ;
30
35
31
- private $ cesnetEligibleLastSeen ;
32
- private $ cesnetEligibleLastSeenAttr ;
36
+ private $ cesnetEligibleLastSeenValue ;
37
+ private $ cesnetEligibleLastSeenAttribute ;
38
+ private $ interface = self ::RPC ;
39
+ private $ rpcAttrName ;
40
+ private $ ldapAttrName ;
41
+ private $ returnAttrName = self ::DEFAULT_ATTR_NAME ;
33
42
34
43
private $ spEntityId ;
35
44
private $ idpEntityId ;
@@ -40,6 +49,11 @@ class IsCesnetEligible extends \SimpleSAML\Auth\ProcessingFilter
40
49
*/
41
50
private $ cesnetLdapConnector ;
42
51
52
+ /**
53
+ * @var AdapterLdap
54
+ */
55
+ private $ ldapAdapter ;
56
+
43
57
/**
44
58
* @var RpcConnector
45
59
*/
@@ -49,23 +63,33 @@ public function __construct($config, $reserved)
49
63
{
50
64
parent ::__construct ($ config , $ reserved );
51
65
52
- if (!isset ($ config [self ::CESNET_ELIGIBLE_LAST_SEEN_ATTR ])) {
66
+ if (!isset ($ config [self ::RPC_ATTRIBUTE_NAME ]) || empty ( $ config [ self :: RPC_ATTRIBUTE_NAME ])) {
53
67
throw new Exception (
54
68
"cesnet:IsCesnetEligible - missing mandatory configuration option ' " .
55
- self ::CESNET_ELIGIBLE_LAST_SEEN_ATTR . "'. "
69
+ self ::RPC_ATTRIBUTE_NAME . "'. "
56
70
);
57
71
}
58
72
59
- if (isset ($ config ['attrName ' ])) {
60
- $ this ->attrName = $ config ['attrName ' ];
61
- } else {
62
- $ this ->attrName = self ::DEFAULT_ATTR_NAME ;
63
- }
64
-
65
- $ this ->cesnetEligibleLastSeenAttr = $ config [self ::CESNET_ELIGIBLE_LAST_SEEN_ATTR ];
73
+ $ this ->rpcAttrName = $ config [self ::RPC_ATTRIBUTE_NAME ];
66
74
67
- $ this ->cesnetLdapConnector = (new AdapterLdap (self ::CONFIG_FILE_NAME ))->getConnector ();
68
75
$ this ->rpcConnector = (new AdapterRpc ())->getConnector ();
76
+ $ this ->cesnetLdapConnector = (new AdapterLdap (self ::CONFIG_FILE_NAME ))->getConnector ();
77
+
78
+ if (isset ($ config [self ::ATTR_NAME ]) && !empty ($ config [self ::ATTR_NAME ])) {
79
+ $ this ->returnAttrName = $ config ['attrName ' ];
80
+ }
81
+
82
+ if (isset ($ config [self ::INTERFACE_PROPNAME ], $ config [self ::LDAP_ATTRIBUTE_NAME ]) &&
83
+ $ config [self ::INTERFACE_PROPNAME ] === self ::LDAP && !empty ($ config [self ::LDAP_ATTRIBUTE_NAME ])) {
84
+ $ this ->interface = $ config [self ::INTERFACE_PROPNAME ];
85
+ $ this ->ldapAttrName = $ config [self ::LDAP_ATTRIBUTE_NAME ];
86
+ $ this ->ldapAdapter = new AdapterLdap ();
87
+ } else {
88
+ Logger::warning (
89
+ "cesnet:IsCesnetEligible - One of " . self ::INTERFACE_PROPNAME . self ::LDAP_ATTRIBUTE_NAME .
90
+ " is missing or empty. RPC interface will be used "
91
+ );
92
+ }
69
93
}
70
94
71
95
public function process (&$ request )
@@ -104,32 +128,54 @@ public function process(&$request)
104
128
105
129
try {
106
130
if (!empty ($ user )) {
107
- $ this ->cesnetEligibleLastSeen = $ this ->rpcConnector ->get (
108
- 'attributesManager ' ,
109
- 'getAttribute ' ,
110
- ['user ' => $ user ->getId (), 'attributeName ' => $ this ->cesnetEligibleLastSeenAttr ,]
111
- );
112
- }
131
+ if ($ this ->interface === self ::LDAP ) {
132
+ $ attrs = $ this ->ldapAdapter ->getUserAttributes ($ user , [$ this ->ldapAttrName ]);
133
+ if (isset ($ attrs [$ this ->ldapAttrName ][0 ])) {
134
+ $ this ->cesnetEligibleLastSeenValue = $ attrs [$ this ->ldapAttrName ][0 ];
135
+ }
136
+ } else {
137
+ $ this ->cesnetEligibleLastSeenAttribute = $ this ->rpcConnector ->get (
138
+ 'attributesManager ' ,
139
+ 'getAttribute ' ,
140
+ ['user ' => $ user ->getId (), 'attributeName ' => $ this ->rpcAttrName ]
141
+ );
142
+ $ this ->cesnetEligibleLastSeenValue = $ this ->cesnetEligibleLastSeenAttribute ['value ' ];
143
+ }
113
144
114
- if ((!empty ($ this ->eduPersonScopedAffiliation ) && $ this ->isCesnetEligible ())
115
- || $ isHostelVerified
116
- ) {
117
- $ this ->cesnetEligibleLastSeen ['value ' ] = date ("Y-m-d H:i:s " );
145
+ if ($ isHostelVerified || (!empty ($ this ->eduPersonScopedAffiliation ) && $ this ->isCesnetEligible ())) {
146
+ $ this ->cesnetEligibleLastSeenValue = date ("Y-m-d H:i:s " );
147
+
148
+ if ($ this ->cesnetEligibleLastSeenAttribute === null ) {
149
+ $ this ->cesnetEligibleLastSeenAttribute = $ this ->rpcConnector ->get (
150
+ 'attributesManager ' ,
151
+ 'getAttribute ' ,
152
+ ['user ' => $ user ->getId (), 'attributeName ' => $ this ->rpcAttrName ,]
153
+ );
154
+ }
155
+ $ this ->cesnetEligibleLastSeenAttribute ['value ' ] = $ this ->cesnetEligibleLastSeenValue ;
118
156
119
- if (!empty ($ user )) {
120
157
$ this ->rpcConnector ->post (
121
158
'attributesManager ' ,
122
159
'setAttribute ' ,
123
- ['user ' => $ user ->getId (), 'attribute ' => $ this ->cesnetEligibleLastSeen ,]
160
+ ['user ' => $ user ->getId (), 'attribute ' => $ this ->cesnetEligibleLastSeenAttribute ,]
161
+ );
162
+
163
+ Logger::debug (
164
+ "cesnet:IsCesnetEligible - Value of attribute isCesnetEligibleLastSeen was updated to " .
165
+ $ this ->cesnetEligibleLastSeenValue . "in Perun system. "
124
166
);
125
167
}
126
168
}
127
169
} catch (Exception $ ex ) {
128
170
Logger::warning ("cesnet:IsCesnetEligible - " . $ ex ->getMessage ());
129
171
}
130
172
131
- if ($ this ->cesnetEligibleLastSeen ['value ' ] != null ) {
132
- $ request ['Attributes ' ][$ this ->attrName ] = [$ this ->cesnetEligibleLastSeen ['value ' ]];
173
+ if ($ this ->cesnetEligibleLastSeenValue !== null ) {
174
+ $ request ['Attributes ' ][$ this ->returnAttrName ] = [$ this ->cesnetEligibleLastSeenValue ];
175
+ Logger::debug (
176
+ "cesnet:IsCesnetEligible - Attribute " . $ this ->returnAttrName . " was set to value " .
177
+ $ this ->cesnetEligibleLastSeenValue
178
+ );
133
179
}
134
180
}
135
181
0 commit comments