root/elgg/trunk/auth/sso/lib.php

Revision 55, 3.6 kB (checked in by bettse, 2 years ago)

re #463

change from providing SSO with a 'url' to providing a 'bounce'

Line 
1 <?php
2 ini_set('display_errors','1');
3 ini_set('display_startup_errors','1');
4 error_reporting (E_ALL);
5
6 /**
7  * SSO authentication for elgg.
8  *
9  * sso/
10  *    lib.php   <- file with authentication callback functions used by elgg
11  *    sso_config.php   <- sso configuration
12  *    sso.php      <- sso library
13  *
14  * To use sso authentication in elgg in your
15  * config.php file add the two lines below
16  *   $CFG->auth = 'sso';            // tells elgg to use sso
17  *   $CFG->sso_user_create = true;  // tells elgg to create a user for sso authenticated visitors
18  *
19  */
20
21 // sso library
22 require_once('sso.php');
23
24 // sso configuration
25 require_once('sso_config.php');
26
27
28 /**
29  * This method is a callback function used by elgg.
30  * This function is called by  authenticate_account
31  * in lib/elgglib.php.
32  */
33 function sso_authenticate_user_login()
34 {
35     global $CFG;
36     $redirect = "/social/index.php";
37
38     sso_authenticate(true, true, array('bounce'=>$redirect));
39     $data = sso_session_userinfo();
40
41     if (array_key_exists('userinfo', $data) && array_key_exists('username', $data['userinfo']) && $data['userinfo']['username'] != '') {
42         // Onid login successful
43         $username = $data['userinfo']['username'];
44
45         if ($CFG->sso_user_create == true) {
46             // create elgg user automatically
47             sso_create_elgg_user($username, $data['userinfo']);
48         }
49         // Return the user object
50         return get_record_select('users', "username = ? AND active = ? AND user_type = ? ", array($username,'yes','person'));
51     }
52     return false;
53 }
54
55 /**
56  * creates an entry in the elgg database for the given onid username.
57  */
58 function sso_create_elgg_user($username, $user_info) {
59     if(!validate_username($username)) {
60         $messages[] = __gettext("Error! ONID Username does not meet Elgg requirements");
61     } else {
62         // Does the user already exist?
63         $username = strtolower($username);
64
65         if (record_exists('users','username',$username)) {
66             // onid usernames are unique, so if it's already in the db, we're good to go
67             return true;
68         } else {
69             // Everythink OK, create user
70             $user = new StdClass;
71             $user->email = $user_info["email"];
72             $user->name  = $user_info["firstname"];
73             $user->name  = $user->name . " " . $user_info["lastname"];
74             $user->username = $username;
75             $user->password = md5($password);
76             $user->user_type = 'person';
77             $user->owner = -1;
78
79             $user_id = insert_record('users',$user);
80
81             if (!empty($user_id)) {
82                 $rssresult = run("weblogs:rss:publish", array($user_id, false));
83                 $rssresult = run("files:rss:publish", array($user_id, false));
84                 $rssresult = run("profile:rss:publish", array($user_id, false));
85             } else {
86                 // User creation failed
87                 $messages[] = sprintf(__gettext("User addition %d failed: Unknown reason, please contact you system administrator."), $username);
88             }
89         }
90     }
91 }
92
93 /**
94  * This function logs the user out of sso.
95  * It is is called by login/logout.php
96  */
97 function sso_authenticate_user_logout()
98 {
99     sso_logout();
100     unset($_SESSION['sso']);
101     unset($_SESSION['ldap']);
102
103 }
104
105 /**
106  * This function checks whether or not we have
107  * sso information in session. If we do have sso
108  * information we proceed to authenticate user.
109  *
110  * This function is called by isloggedin() in
111  * lib/elgglib.php
112  */
113 function check_sso_session ()
114 {
115     global $USER;
116     $data = sso_session_userinfo();
117     if ($data != null) {
118         authenticate_account(null, null);
119     }
120 }
121 ?>
122
Note: See TracBrowser for help on using the browser.