Blame view
sources/apps/user_cas/lib/hooks.php
4.06 KB
|
42e4f8d60
|
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 |
<?php
/**
* ownCloud - user_cas
*
* @author Sixto Martin <sixto.martin.garcia@gmail.com>
* @copyright Sixto Martin Garcia. 2012
*
* 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_CAS_Hooks {
static public function post_login($parameters) {
$uid = $parameters['uid'];
$casBackend = new OC_USER_CAS();
if (phpCAS::isAuthenticated()) {
$attributes = phpCAS::getAttributes();
$cas_uid = phpCAS::getUser();
if ($cas_uid == $uid) {
if (array_key_exists($casBackend->mailMapping, $attributes)) {
$cas_email = $attributes[$casBackend->mailMapping][0];
}
if (array_key_exists($casBackend->groupMapping, $attributes)) {
$cas_groups = $attributes[$casBackend->groupMapping];
}
else if (!empty($casBackend->defaultGroup)) {
$cas_groups = array($casBackend->defaultGroup);
OC_Log::write('cas','Using default group "'.$casBackend->defaultGroup.'" for the user: '.$uid, OC_Log::DEBUG);
}
if (!OC_User::userExists($uid)) {
if (preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $uid)) {
OC_Log::write('cas','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('cas','Creating new user: '.$uid, OC_Log::DEBUG);
OC_User::createUser($uid, $random_password);
if(OC_User::userExists($uid)) {
if (isset($cas_email)) {
update_mail($uid, $cas_email);
}
if (isset($cas_groups)) {
update_groups($uid, $cas_groups, $casBackend->protectedGroups, true);
}
}
}
}
else {
if ($casBackend->updateUserData) {
OC_Log::write('cas','Updating data of the user: '.$uid,OC_Log::DEBUG);
if(isset($cas_email)) {
update_mail($uid, $cas_email);
}
if (isset($cas_groups)) {
update_groups($uid, $cas_groups, $casBackend->protectedGroups, false);
}
}
}
return true;
}
}
return false;
}
static public function logout($parameters) {
$casBackend = new OC_USER_CAS();
if (phpCAS::isAuthenticated()) {
OC_Log::write('cas','Executing CAS logout: '.$parameters['uid'],OC_Log::DEBUG);
phpCAS::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('cas','Set email "'.$email.'" for the user: '.$uid, OC_Log::DEBUG);
}
}
function update_groups($uid, $groups, $protected_groups=array(), $just_created=false) {
if(!$just_created) {
$old_groups = OC_Group::getUserGroups($uid);
foreach($old_groups as $group) {
if(!in_array($group, $protected_groups) && !in_array($group, $groups)) {
OC_Group::removeFromGroup($uid,$group);
OC_Log::write('cas','Removed "'.$uid.'" from the group "'.$group.'"', OC_Log::DEBUG);
}
}
}
foreach($groups as $group) {
if (preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $group)) {
OC_Log::write('cas','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('cas','New group created: '.$group, OC_Log::DEBUG);
}
OC_Group::addToGroup($uid, $group);
OC_Log::write('cas','Added "'.$uid.'" to the group "'.$group.'"', OC_Log::DEBUG);
}
}
}
}
|