Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
5e1513f6 | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
0337d704 | 4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
ef0c3d8b | 22 | class XnetSession extends XorgSession |
0337d704 | 23 | { |
6b79b8ef FB |
24 | public function __construct() |
25 | { | |
ab694eb5 | 26 | parent::__construct(); |
6b79b8ef FB |
27 | } |
28 | ||
ab694eb5 | 29 | public function startAvailableAuth() |
1490093c | 30 | { |
6b79b8ef | 31 | if (!S::logged() && Get::has('auth')) { |
ab694eb5 FB |
32 | if (!$this->start(AUTH_MDP)) { |
33 | return false; | |
34 | } | |
6b79b8ef FB |
35 | } |
36 | ||
30439e34 | 37 | if (!S::logged() && Post::has('auth_type') && Post::v('auth_type') == 'xnet' && !Post::has('wait')) { |
8a0908db | 38 | $email = Post::v('username'); |
3c64137d SJ |
39 | $type = XDB::fetchOneCell('SELECT type |
40 | FROM accounts | |
8a0908db SJ |
41 | WHERE email = {?}', |
42 | $email); | |
43 | if ((!is_null($type) && $type != 'xnet') || !User::isForeignEmailAddress($email)) { | |
3c64137d SJ |
44 | Platal::page()->trigErrorRedirect('Ce formulaire d\'authentification est réservé aux extérieurs à la communauté polytechnicienne.', ''); |
45 | } | |
46 | ||
47 | $user = parent::doAuth(AUTH_MDP); | |
48 | if (is_null($user)) { | |
49 | return false; | |
50 | } | |
51 | if (!parent::checkAuth(AUTH_MDP) || !parent::startSessionAs($user, AUTH_MDP)) { | |
52 | $this->destroy(); | |
53 | return false; | |
54 | } | |
55 | } | |
56 | ||
ab694eb5 | 57 | global $globals; |
7a05c64d | 58 | if (!S::logged() && $globals->xnet->auth_baseurl) { |
ab694eb5 | 59 | // prevent connection to be linked to disconnection |
71fe935c | 60 | if (($i = strpos($_SERVER['REQUEST_URI'], 'exit')) !== false) |
0337d704 | 61 | $returl = "http://{$_SERVER['SERVER_NAME']}".substr($_SERVER['REQUEST_URI'], 0, $i); |
62 | else | |
63 | $returl = "http://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}"; | |
7a05c64d | 64 | $url = $globals->xnet->auth_baseurl; |
0337d704 | 65 | $url .= "?session=" . session_id(); |
cab08090 | 66 | $url .= "&challenge=" . S::v('challenge'); |
67 | $url .= "&pass=" . md5(S::v('challenge') . $globals->xnet->secret); | |
0337d704 | 68 | $url .= "&url=".urlencode($returl); |
ab694eb5 | 69 | S::set('loginX', $url); |
0337d704 | 70 | } |
bf517daf | 71 | |
72 | if (S::logged() && $globals->asso()) { | |
73 | $perms = S::v('perms'); | |
74 | $perms->rmFlag('groupadmin'); | |
75 | $perms->rmFlag('groupmember'); | |
35fa92e8 | 76 | $perms->rmFlag('groupannu'); |
bf517daf | 77 | if (may_update()) { |
78 | $perms->addFlag('groupadmin'); | |
79 | $perms->addFlag('groupmember'); | |
35fa92e8 | 80 | $perms->addFlag('groupannu'); |
bf517daf | 81 | } |
82 | if (is_member()) { | |
83 | $perms->addFlag('groupmember'); | |
2c86d368 | 84 | if ($globals->asso('pub') != 'private') { |
35fa92e8 | 85 | $perms->addFlag('groupannu'); |
86 | } | |
e5539fd7 PC |
87 | } else if ($globals->asso('pub') == 'public') { |
88 | $perms->addFlag('groupannu'); | |
35fa92e8 | 89 | } |
90 | if ($globals->asso('cat') == 'Promotions') { | |
91 | $perms->addFlag('groupannu'); | |
bf517daf | 92 | } |
ab694eb5 | 93 | S::set('perms', $perms); |
bf517daf | 94 | } |
ab694eb5 | 95 | return true; |
0337d704 | 96 | } |
cab08090 | 97 | |
ab694eb5 | 98 | protected function doAuth($level) |
0337d704 | 99 | { |
ab694eb5 | 100 | if (S::identified()) { // ok, c'est bon, on n'a rien à faire |
1bf36cd1 | 101 | return User::getSilentWithValues(null, array('uid' => S::i('uid'))); |
0337d704 | 102 | } |
ab694eb5 FB |
103 | if (!Get::has('auth')) { |
104 | return null; | |
105 | } | |
106 | global $globals; | |
107 | if (md5('1' . S::v('challenge') . $globals->xnet->secret . Get::i('uid') . '1') != Get::v('auth')) { | |
108 | return null; | |
109 | } | |
110 | Get::kill('auth'); | |
111 | S::set('auth', AUTH_MDP); | |
1bf36cd1 | 112 | return User::getSilentWithValues(null, array('uid' => Get::i('uid'))); |
c0d6753f | 113 | } |
114 | ||
ab694eb5 | 115 | protected function startSessionAs($user, $level) |
bf517daf | 116 | { |
7fd6dbb3 FB |
117 | // The user must have 'groups' permission to access X.net |
118 | if (!$user->checkPerms('groups')) { | |
119 | return false; | |
120 | } | |
6d1e6661 | 121 | if ($level == AUTH_SUID) { |
ab694eb5 | 122 | S::set('auth', AUTH_MDP); |
0337d704 | 123 | } |
7fd6dbb3 FB |
124 | |
125 | S::set('uid', $user->uid); | |
126 | S::set('hruid', $user->hruid); | |
127 | ||
128 | // XXX: Transition code, should not be in session anymore | |
129 | S::set('display_name', $user->display_name); | |
130 | S::set('full_name', $user->full_name); | |
131 | S::set('femme', $user->isFemale()); | |
132 | S::set('email_format', $user->email_format); | |
133 | S::set('token', $user->token); | |
134 | S::set('perms', $user->perms); | |
135 | S::set('is_admin', $user->is_admin); | |
136 | ||
137 | ||
138 | $this->makePerms($user->perms, $user->is_admin); | |
cab08090 | 139 | S::kill('challenge'); |
140 | S::kill('loginX'); | |
0afc7e1e | 141 | S::kill('may_update'); |
142 | S::kill('is_member'); | |
0337d704 | 143 | Get::kill('uid'); |
63528107 | 144 | Get::kill('PHPSESSID'); |
b0b937fd | 145 | |
fd834b4b | 146 | $args = array(); |
b0b937fd | 147 | foreach($_GET as $key => $val) { |
ab694eb5 | 148 | $args[] = urlencode($key). '=' .urlencode($val); |
0337d704 | 149 | } |
ab694eb5 | 150 | return true; |
0337d704 | 151 | } |
152 | ||
ab694eb5 | 153 | public function doSelfSuid() |
0deaff1d | 154 | { |
866bd535 FB |
155 | $user =& S::user(); |
156 | if (!$this->startSUID($user)) { | |
ab694eb5 | 157 | return false; |
0deaff1d | 158 | } |
7fd6dbb3 | 159 | S::set('perms', User::makePerms(PERMS_USER)); |
ab694eb5 | 160 | return true; |
b8e265bf | 161 | } |
b8e265bf | 162 | |
ab694eb5 | 163 | public function stopSUID() |
eaf30d86 | 164 | { |
0c02607e | 165 | $perms = S::suid('perms'); |
ab694eb5 FB |
166 | if (!parent::stopSUID()) { |
167 | return false; | |
168 | } | |
0deaff1d | 169 | S::kill('may_update'); |
170 | S::kill('is_member'); | |
0c02607e | 171 | S::set('perms', $perms); |
ab694eb5 | 172 | return true; |
b8e265bf | 173 | } |
b8e265bf | 174 | } |
175 | ||
0deaff1d | 176 | // {{{ function may_update |
0337d704 | 177 | |
0deaff1d | 178 | /** Return administration rights for the current asso |
179 | * @param force Force administration rights to be read from database | |
180 | * @param lose Force administration rights to be false | |
181 | */ | |
b8e265bf | 182 | function may_update($force = false, $lose = false) |
183 | { | |
184 | if (!isset($_SESSION['may_update'])) { | |
185 | $_SESSION['may_update'] = array(); | |
186 | } | |
187 | $may_update =& $_SESSION['may_update']; | |
188 | ||
0337d704 | 189 | global $globals; |
b8e265bf | 190 | $asso_id = $globals->asso('id'); |
0deaff1d | 191 | if (!$asso_id) { |
192 | return false; | |
193 | } elseif ($lose) { | |
194 | $may_update[$asso_id] = false; | |
0c02607e | 195 | } elseif (S::admin() || (S::suid() && $force)) { |
0deaff1d | 196 | $may_update[$asso_id] = true; |
197 | } elseif (!isset($may_update[$asso_id]) || $force) { | |
b8e265bf | 198 | $res = XDB::query("SELECT perms |
eb41eda9 | 199 | FROM group_members |
b8e265bf | 200 | WHERE uid={?} AND asso_id={?}", |
a14159bf | 201 | S::v('uid'), $asso_id); |
b8e265bf | 202 | $may_update[$asso_id] = ($res->fetchOneCell() == 'admin'); |
b8e265bf | 203 | } |
204 | return $may_update[$asso_id]; | |
0337d704 | 205 | } |
206 | ||
207 | // }}} | |
0deaff1d | 208 | // {{{ function is_member |
0337d704 | 209 | |
0deaff1d | 210 | /** Get membership informations for the current asso |
211 | * @param force Force membership to be read from database | |
212 | * @param lose Force membership to be false | |
eaf30d86 | 213 | */ |
b8e265bf | 214 | function is_member($force = false, $lose = false) |
c1863ee9 | 215 | { |
b8e265bf | 216 | if (!isset($_SESSION['is_member'])) { |
217 | $_SESSION['is_member'] = array(); | |
218 | } | |
219 | $is_member =& $_SESSION['is_member']; | |
220 | ||
0337d704 | 221 | global $globals; |
258b9710 | 222 | $asso_id = $globals->asso('id'); |
0deaff1d | 223 | if (!$asso_id) { |
224 | return false; | |
b8e265bf | 225 | } elseif ($lose) { |
226 | $is_member[$asso_id] = false; | |
0c02607e | 227 | } elseif (S::suid() && $force) { |
0deaff1d | 228 | $is_member[$asso_id] = true; |
229 | } elseif (!isset($is_member[$asso_id]) || $force) { | |
230 | $res = XDB::query("SELECT COUNT(*) | |
eb41eda9 | 231 | FROM group_members |
0deaff1d | 232 | WHERE uid={?} AND asso_id={?}", |
233 | S::v('uid'), $asso_id); | |
234 | $is_member[$asso_id] = ($res->fetchOneCell() == 1); | |
258b9710 | 235 | } |
236 | return $is_member[$asso_id]; | |
0337d704 | 237 | } |
238 | ||
239 | // }}} | |
a7de4ef7 | 240 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 241 | ?> |