hooks.php
5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* ownCloud - user_saml
*
* @author Sixto Martin <smartin@yaco.es>
* @copyright 2012 Yaco Sistemas // CONFIA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* This class contains all hooks.
*/
class OC_USER_SAML_Hooks {
static public function post_login($parameters) {
$userid = $parameters['uid'];
$samlBackend = new OC_USER_SAML();
if ($samlBackend->auth->isAuthenticated()) {
$attributes = $samlBackend->auth->getAttributes();
$usernameFound = false;
foreach($samlBackend->usernameMapping as $usernameMapping) {
if (array_key_exists($usernameMapping, $attributes) && !empty($attributes[$usernameMapping][0])) {
$usernameFound = true;
$uid = $attributes[$usernameMapping][0];
OC_Log::write('saml','Authenticated user '.$uid,OC_Log::DEBUG);
break;
}
}
if ($usernameFound && $uid == $userid) {
$attributes = $samlBackend->auth->getAttributes();
$saml_email = '';
foreach ($samlBackend->mailMapping as $mailMapping) {
if (array_key_exists($mailMapping, $attributes) && !empty($attributes[$mailMapping][0])) {
$saml_email = $attributes[$mailMapping][0];
break;
}
}
$saml_display_name = '';
foreach ($samlBackend->displayNameMapping as $displayNameMapping) {
if (array_key_exists($displayNameMapping, $attributes) && !empty($attributes[$displayNameMapping][0])) {
$saml_display_name = $attributes[$displayNameMapping][0];
break;
}
}
$saml_groups = array();
foreach ($samlBackend->groupMapping as $groupMapping) {
if (array_key_exists($groupMapping, $attributes) && !empty($attributes[$groupMapping])) {
$saml_groups = array_merge($saml_groups, $attributes[$groupMapping]);
}
}
if (empty($saml_groups) && !empty($samlBackend->defaultGroup)) {
$saml_groups = array($samlBackend->defaultGroup);
OC_Log::write('saml','Using default group "'.$samlBackend->defaultGroup.'" for the user: '.$uid, OC_Log::DEBUG);
}
if (!OC_User::userExists($uid)) {
if (preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $uid)) {
OC_Log::write('saml','Invalid username "'.$uid.'", allowed chars "a-zA-Z0-9" and "_.@-" ',OC_Log::DEBUG);
return false;
}
else {
$random_password = OC_Util::generate_random_bytes(20);
OC_Log::write('saml','Creating new user: '.$uid, OC_Log::DEBUG);
OC_User::createUser($uid, $random_password);
if(OC_User::userExists($uid)) {
OC_Util::setupFS($uid);
if (isset($saml_email)) {
update_mail($uid, $saml_email);
}
if (isset($saml_groups)) {
update_groups($uid, $saml_groups, $samlBackend->protectedGroups, true);
}
if (isset($saml_display_name)) {
update_display_name($uid, $saml_display_name);
}
}
}
}
else {
if ($samlBackend->updateUserData) {
OC_Util::setupFS($uid);
OC_Log::write('saml','Updating data of the user: '.$uid,OC_Log::DEBUG);
if(isset($saml_email)) {
update_mail($uid, $saml_email);
}
if (isset($saml_groups)) {
update_groups($uid, $saml_groups, $samlBackend->protectedGroups, false);
}
if (isset($saml_display_name)) {
update_display_name($uid, $saml_display_name);
}
}
}
return true;
}
}
return false;
}
static public function logout($parameters) {
$samlBackend = new OC_USER_SAML();
if ($samlBackend->auth->isAuthenticated()) {
OC_Log::write('saml', 'Executing SAML logout', OC_Log::DEBUG);
$samlBackend->auth->logout();
}
return true;
}
}
function update_mail($uid, $email) {
if ($email != OC_Preferences::getValue($uid, 'settings', 'email', '')) {
OC_Preferences::setValue($uid, 'settings', 'email', $email);
OC_Log::write('saml','Set email "'.$email.'" for the user: '.$uid, OC_Log::DEBUG);
}
}
function update_groups($uid, $groups, $protectedGroups=array(), $just_created=false) {
if(!$just_created) {
$old_groups = OC_Group::getUserGroups($uid);
foreach($old_groups as $group) {
if(!in_array($group, $protectedGroups) && !in_array($group, $groups)) {
OC_Group::removeFromGroup($uid,$group);
OC_Log::write('saml','Removed "'.$uid.'" from the group "'.$group.'"', OC_Log::DEBUG);
}
}
}
foreach($groups as $group) {
if (preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $group)) {
OC_Log::write('saml','Invalid group "'.$group.'", allowed chars "a-zA-Z0-9" and "_.@-" ',OC_Log::DEBUG);
}
else {
if (!OC_Group::inGroup($uid, $group)) {
if (!OC_Group::groupExists($group)) {
OC_Group::createGroup($group);
OC_Log::write('saml','New group created: '.$group, OC_Log::DEBUG);
}
OC_Group::addToGroup($uid, $group);
OC_Log::write('saml','Added "'.$uid.'" to the group "'.$group.'"', OC_Log::DEBUG);
}
}
}
}
function update_display_name($uid, $displayName) {
OC_User::setDisplayName($uid, $displayName);
}