Merge branch 'xorg/maint' into xorg/master
[platal.git] / classes / xnetsession.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
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
22 class XnetSession extends XorgSession
23 {
24 public function __construct()
25 {
26 parent::__construct();
27 }
28
29 public function startAvailableAuth()
30 {
31 if (!S::logged() && Get::has('auth')) {
32 if (!$this->start(AUTH_MDP)) {
33 return false;
34 }
35 }
36
37 if (!S::logged() && Post::has('auth_type') && Post::v('auth_type') == 'xnet' && !Post::has('wait')) {
38 $type = XDB::fetchOneCell('SELECT type
39 FROM accounts
40 WHERE hruid = {?}',
41 Post::v('username'));
42 if (!is_null($type) && $type != 'xnet') {
43 Platal::page()->trigErrorRedirect('Ce formulaire d\'authentification est réservé aux extérieurs à la communauté polytechnicienne.', '');
44 }
45
46 $user = parent::doAuth(AUTH_MDP);
47 if (is_null($user)) {
48 return false;
49 }
50 if (!parent::checkAuth(AUTH_MDP) || !parent::startSessionAs($user, AUTH_MDP)) {
51 $this->destroy();
52 return false;
53 }
54 }
55
56 global $globals;
57 if (!S::logged() && $globals->xnet->auth_baseurl) {
58 // prevent connection to be linked to disconnection
59 if (($i = strpos($_SERVER['REQUEST_URI'], 'exit')) !== false)
60 $returl = "http://{$_SERVER['SERVER_NAME']}".substr($_SERVER['REQUEST_URI'], 0, $i);
61 else
62 $returl = "http://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}";
63 $url = $globals->xnet->auth_baseurl;
64 $url .= "?session=" . session_id();
65 $url .= "&challenge=" . S::v('challenge');
66 $url .= "&pass=" . md5(S::v('challenge') . $globals->xnet->secret);
67 $url .= "&url=".urlencode($returl);
68 S::set('loginX', $url);
69 }
70
71 if (S::logged() && $globals->asso()) {
72 $perms = S::v('perms');
73 $perms->rmFlag('groupadmin');
74 $perms->rmFlag('groupmember');
75 $perms->rmFlag('groupannu');
76 if (may_update()) {
77 $perms->addFlag('groupadmin');
78 $perms->addFlag('groupmember');
79 $perms->addFlag('groupannu');
80 }
81 if (is_member()) {
82 $perms->addFlag('groupmember');
83 if ($globals->asso('pub') != 'private') {
84 $perms->addFlag('groupannu');
85 }
86 } else if ($globals->asso('pub') == 'public') {
87 $perms->addFlag('groupannu');
88 }
89 if ($globals->asso('cat') == 'Promotions') {
90 $perms->addFlag('groupannu');
91 }
92 S::set('perms', $perms);
93 }
94 return true;
95 }
96
97 protected function doAuth($level)
98 {
99 if (S::identified()) { // ok, c'est bon, on n'a rien à faire
100 return User::getSilentWithValues(null, array('uid' => S::i('uid')));
101 }
102 if (!Get::has('auth')) {
103 return null;
104 }
105 global $globals;
106 if (md5('1' . S::v('challenge') . $globals->xnet->secret . Get::i('uid') . '1') != Get::v('auth')) {
107 return null;
108 }
109 Get::kill('auth');
110 S::set('auth', AUTH_MDP);
111 return User::getSilentWithValues(null, array('uid' => Get::i('uid')));
112 }
113
114 protected function startSessionAs($user, $level)
115 {
116 // The user must have 'groups' permission to access X.net
117 if (!$user->checkPerms('groups')) {
118 return false;
119 }
120 if ($level == AUTH_SUID) {
121 S::set('auth', AUTH_MDP);
122 }
123
124 S::set('uid', $user->uid);
125 S::set('hruid', $user->hruid);
126
127 // XXX: Transition code, should not be in session anymore
128 S::set('display_name', $user->display_name);
129 S::set('full_name', $user->full_name);
130 S::set('femme', $user->isFemale());
131 S::set('email_format', $user->email_format);
132 S::set('token', $user->token);
133 S::set('perms', $user->perms);
134 S::set('is_admin', $user->is_admin);
135
136
137 $this->makePerms($user->perms, $user->is_admin);
138 S::kill('challenge');
139 S::kill('loginX');
140 S::kill('may_update');
141 S::kill('is_member');
142 Get::kill('uid');
143 Get::kill('PHPSESSID');
144
145 $args = array();
146 foreach($_GET as $key => $val) {
147 $args[] = urlencode($key). '=' .urlencode($val);
148 }
149 return true;
150 }
151
152 public function doSelfSuid()
153 {
154 $user =& S::user();
155 if (!$this->startSUID($user)) {
156 return false;
157 }
158 S::set('perms', User::makePerms(PERMS_USER));
159 return true;
160 }
161
162 public function stopSUID()
163 {
164 $perms = S::suid('perms');
165 if (!parent::stopSUID()) {
166 return false;
167 }
168 S::kill('may_update');
169 S::kill('is_member');
170 S::set('perms', $perms);
171 return true;
172 }
173 }
174
175 // {{{ function may_update
176
177 /** Return administration rights for the current asso
178 * @param force Force administration rights to be read from database
179 * @param lose Force administration rights to be false
180 */
181 function may_update($force = false, $lose = false)
182 {
183 if (!isset($_SESSION['may_update'])) {
184 $_SESSION['may_update'] = array();
185 }
186 $may_update =& $_SESSION['may_update'];
187
188 global $globals;
189 $asso_id = $globals->asso('id');
190 if (!$asso_id) {
191 return false;
192 } elseif ($lose) {
193 $may_update[$asso_id] = false;
194 } elseif (S::admin() || (S::suid() && $force)) {
195 $may_update[$asso_id] = true;
196 } elseif (!isset($may_update[$asso_id]) || $force) {
197 $res = XDB::query("SELECT perms
198 FROM group_members
199 WHERE uid={?} AND asso_id={?}",
200 S::v('uid'), $asso_id);
201 $may_update[$asso_id] = ($res->fetchOneCell() == 'admin');
202 }
203 return $may_update[$asso_id];
204 }
205
206 // }}}
207 // {{{ function is_member
208
209 /** Get membership informations for the current asso
210 * @param force Force membership to be read from database
211 * @param lose Force membership to be false
212 */
213 function is_member($force = false, $lose = false)
214 {
215 if (!isset($_SESSION['is_member'])) {
216 $_SESSION['is_member'] = array();
217 }
218 $is_member =& $_SESSION['is_member'];
219
220 global $globals;
221 $asso_id = $globals->asso('id');
222 if (!$asso_id) {
223 return false;
224 } elseif ($lose) {
225 $is_member[$asso_id] = false;
226 } elseif (S::suid() && $force) {
227 $is_member[$asso_id] = true;
228 } elseif (!isset($is_member[$asso_id]) || $force) {
229 $res = XDB::query("SELECT COUNT(*)
230 FROM group_members
231 WHERE uid={?} AND asso_id={?}",
232 S::v('uid'), $asso_id);
233 $is_member[$asso_id] = ($res->fetchOneCell() == 1);
234 }
235 return $is_member[$asso_id];
236 }
237
238 // }}}
239 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
240 ?>