Commit | Line | Data |
---|---|---|
3f6bc75f FB |
1 | <?php |
2 | ||
3f6bc75f | 3 | class xorgAuth extends dcAuth { |
1759942c FB |
4 | public $xorg_infos = array('forlife' => null, |
5 | 'prenom' => null, | |
f525ba66 FB |
6 | 'nom' => null, |
7 | 'grpauth' => null, | |
8 | 'perms' => null); | |
9 | static public function behavior_coreBlogConstruct(&$blog) { | |
10 | global $core; | |
11 | $core->auth->sudo(array($core->auth, 'updateUserPerms'), $blog); | |
12 | } | |
13 | ||
1759942c | 14 | |
e105d162 FB |
15 | public function __construct(&$core) { |
16 | parent::__construct($core); | |
f525ba66 | 17 | $core->addBehavior('coreBlogConstruct', array('xorgAuth', 'behavior_coreBlogConstruct')); |
e105d162 FB |
18 | } |
19 | ||
f33c2acf | 20 | public function buildFromSession() { |
e105d162 | 21 | global $core; |
001b27af | 22 | @header('Last-Modified:'); |
e105d162 FB |
23 | if (!isset($core) || !isset($core->session)) { |
24 | return; | |
25 | } | |
5a64dfac FB |
26 | if (!session_id()) { |
27 | $core->session->start(); | |
28 | } | |
001b27af FB |
29 | $user = @$_SESSION['auth-xorg']; |
30 | if ($user && is_null($this->xorg_infos['forlife'])) { | |
1759942c FB |
31 | foreach ($this->xorg_infos as $key => $val) { |
32 | $this->xorg_infos[$key] = $_SESSION['auth-xorg-' . $key]; | |
33 | } | |
001b27af | 34 | $this->user_id = $user; |
e3770a7c | 35 | $this->user_admin = ($_SESSION['auth-xorg-perms'] == 'admin'); |
16237aee | 36 | parent::checkUser($this->user_id); |
f4aeade7 | 37 | $core->getUserBlogs(); |
1759942c FB |
38 | } |
39 | } | |
40 | ||
f33c2acf FB |
41 | public function createUser() { |
42 | global $core; | |
43 | if (!$core->userExists($_SESSION['auth-xorg'])) { | |
44 | $cur = new cursor($this->con, 'dc_user'); | |
45 | $cur->user_id = $_SESSION['auth-xorg']; | |
46 | $cur->user_pwd = md5(rand()); | |
e3770a7c | 47 | $cur->user_super = ($_SESSION['auth-xorg-perms'] == 'admin'); |
f33c2acf FB |
48 | $cur->user_lang = 'fr'; |
49 | $cur->user_name = $_SESSION['auth-xorg-nom']; | |
50 | $cur->user_firstname = $_SESSION['auth-xorg-prenom']; | |
51 | $cur->user_displayname = $cur->user_firstname . ' ' . $cur->user_name; | |
52 | $cur->user_email = $_SESSION['auth-xorg'] . '@polytechnique.org'; | |
53 | $cur->user_options = $core->userDefaults(); | |
54 | $cur->user_options['post_xorg_perms'] = 'public'; | |
55 | $cur->user_default_blog = 'default'; // FIXME | |
56 | $core->addUser($cur); | |
57 | } | |
58 | } | |
59 | ||
f525ba66 | 60 | public function updateUserPerms(&$blog) { |
f33c2acf | 61 | global $core; |
f525ba66 FB |
62 | $this->buildFromSession(); |
63 | if (!isset($_SESSION['auth-xorg'])) { | |
64 | return; | |
65 | } | |
66 | $type = $blog->settings->get('xorg_blog_type'); | |
67 | $owner = $blog->settings->get('xorg_blog_owner'); | |
68 | $level = $this->xorg_infos['grpauth']; | |
e3770a7c FB |
69 | $rec = $core->getUser($this->userID()); |
70 | $wasAdmin = $rec->f('user_super'); | |
71 | $isAdmin = $this->xorg_infos['perms'] == 'admin'; | |
72 | if (($wasAdmin && !$isAdmin) || (!$wasAdmin && $isAdmin)) { | |
73 | $cur = new cursor($this->con, 'dc_user'); | |
74 | $cur->user_super = $isAdmin ? '1' : '0'; | |
75 | $core->updUser($this->userID(), $cur); | |
76 | } | |
f525ba66 FB |
77 | if (($type == 'group-admin' || $type == 'group-member') && $level == 'admin') { |
78 | if ($owner != $_SESSION['xorg-group']) { | |
79 | return; | |
80 | } | |
81 | $perms = array('usage' => true, | |
82 | 'contentadmin' => true, | |
83 | 'admin' => true); | |
84 | } else if ($type == 'group-member' && $level == 'membre') { | |
85 | if ($owner != $_SESSION['xorg-group']) { | |
86 | return; | |
87 | } | |
88 | $perms = array('usage' => true); | |
89 | } else if ($type == 'user' && $owner == $this->xorg_infos['forlife']) { | |
90 | $perms = array('usage' => true, | |
91 | 'contentadmin' => true, | |
92 | 'admin' => true); | |
070afbd1 | 93 | } else if ($type != 'user') { |
f525ba66 | 94 | $perms = array(); |
070afbd1 FB |
95 | } else { |
96 | return; | |
f525ba66 | 97 | } |
f33c2acf | 98 | $core->setUserBlogPermissions($_SESSION['auth-xorg'], |
f525ba66 FB |
99 | $blog->id, |
100 | $perms); | |
f33c2acf FB |
101 | } |
102 | ||
103 | ||
104 | /** Xorg SSO API */ | |
105 | ||
e105d162 FB |
106 | public function callXorg($path = null) { |
107 | if (is_null($path)) { | |
108 | $path = $_SERVER['REQUEST_URI']; | |
109 | } | |
110 | $this->buildFromSession(); | |
be74d9bd | 111 | if (@$_SESSION['auth-xorg']) { |
e105d162 | 112 | return true; |
be74d9bd | 113 | } |
9d447124 | 114 | global $core; |
5a64dfac FB |
115 | if (!session_id()) { |
116 | $core->session->start(); | |
001b27af | 117 | } |
be74d9bd FB |
118 | $_SESSION["auth-x-challenge"] = md5(uniqid(rand(), 1)); |
119 | $url = "https://www.polytechnique.org/auth-groupex/utf8"; | |
120 | $url .= "?session=" . session_id(); | |
121 | $url .= "&challenge=" . $_SESSION["auth-x-challenge"]; | |
122 | $url .= "&pass=" . md5($_SESSION["auth-x-challenge"] . XORG_AUTH_KEY); | |
f525ba66 FB |
123 | $type = $core->blog->settings->get('xorg_blog_type'); |
124 | if ($type == 'group-member' || $type == 'group-admin') { | |
125 | $_SESSION['xorg-group'] = $core->blog->settings->get('xorg_blog_owner'); | |
126 | $url .= '&group=' . $core->blog->settings->get('xorg_blog_owner'); | |
127 | } else { | |
128 | unset($_SESSION['xorg-group']); | |
129 | } | |
9d447124 | 130 | $url .= "&url=" . urlencode($core->blog->url . "auth/XorgReturn?path=" . $path); |
be74d9bd FB |
131 | session_write_close(); |
132 | header("Location: $url"); | |
133 | exit; | |
134 | } | |
135 | ||
136 | public function returnXorg() { | |
137 | if (!isset($_GET['auth'])) { | |
138 | return false; | |
139 | } | |
140 | $params = ''; | |
e105d162 | 141 | global $core; |
001b27af FB |
142 | $_COOKIE[DC_SESSION_NAME] = $_GET['PHPSESSID']; |
143 | unset($_GET['PHPSESSID']); | |
fc802c03 | 144 | if (!session_id()) { |
5a64dfac FB |
145 | $core->session->start(); |
146 | } | |
be74d9bd FB |
147 | foreach($this->xorg_infos as $key => $val) { |
148 | if(!isset($_GET[$key])) { | |
149 | return false; | |
150 | } | |
151 | $_SESSION['auth-xorg-' . $key] = $_GET[$key]; | |
be74d9bd FB |
152 | $params .= $_GET[$key]; |
153 | } | |
154 | if (md5('1' . $_SESSION['auth-x-challenge'] . XORG_AUTH_KEY . $params . '1') == $_GET['auth']) { | |
155 | unset($_GET['auth']); | |
a1a69528 FB |
156 | $_SESSION['sess_user_id'] = $_SESSION['auth-xorg'] = $_GET['forlife']; |
157 | $_SESSION['sess_browser_uid'] = http::browserUID(DC_MASTER_KEY); | |
158 | $_SESSION['sess_blog_id'] = 'default'; | |
f33c2acf | 159 | $this->sudo(array($this, 'createUser')); |
001b27af | 160 | $path = $_GET['path']; |
c0556a51 | 161 | header("Location: http://murphy.m4x.org" . $_GET['path']); |
e105d162 | 162 | exit; |
be74d9bd | 163 | } |
a1a69528 FB |
164 | unset($_SESSION['auth-xorg']); |
165 | unset($_SESSION['sess_user_id']); | |
be74d9bd | 166 | unset($_GET['auth']); |
e105d162 | 167 | echo "Failed !!!"; |
be74d9bd FB |
168 | return false; |
169 | } | |
170 | ||
171 | public function killSession() { | |
e105d162 | 172 | global $core; |
5a64dfac FB |
173 | if (!session_id()) { |
174 | $core->session->start(); | |
175 | } | |
e105d162 | 176 | $core->session->destroy(); |
9d447124 | 177 | header('Location: ' . $core->blog->url); |
be74d9bd FB |
178 | exit; |
179 | } | |
a1a69528 | 180 | |
f33c2acf FB |
181 | |
182 | /** Dotclear dcAuth API */ | |
183 | ||
ccfabbd3 | 184 | public function checkUser($user_id, $pwd = null, $user_key = null) { |
16237aee | 185 | return $this->callXorg(); |
ccfabbd3 FB |
186 | } |
187 | ||
188 | public function check($permissions, $blog_id) { | |
189 | $this->buildFromSession(); | |
e3770a7c | 190 | return parent::check($permissions, $blog_id); |
ccfabbd3 FB |
191 | } |
192 | ||
0ff09dcb FB |
193 | public function checkPassword($pwd) { |
194 | $this->buildFromSession(); | |
195 | return !empty($this->user_id); | |
196 | } | |
197 | ||
a1a69528 FB |
198 | public function allowPassChange() { |
199 | return false; | |
200 | } | |
201 | ||
202 | public function userID() { | |
203 | $this->buildFromSession(); | |
ccfabbd3 | 204 | return parent::userID(); |
a1a69528 FB |
205 | } |
206 | ||
207 | public function getPermissions() { | |
ccfabbd3 FB |
208 | $this->buildFromSession(); |
209 | return parent::getPermissions(); | |
a1a69528 FB |
210 | } |
211 | ||
212 | public function getInfo($n) { | |
ccfabbd3 | 213 | $this->buildFromSession(); |
dc51645d FB |
214 | if ($n == 'xorg_group_member') { |
215 | global $core; | |
216 | if ($core->blog->settings('xorg_blog_owner') != $_SESSION['xorg-group']) { | |
217 | return false; | |
218 | } | |
219 | $perm = $this->xorg_infos['grpauth']; | |
220 | return $this->isSuperAdmin() || $perm == 'admin' || $perm == 'membre'; | |
221 | } | |
ccfabbd3 | 222 | return parent::getInfo($n); |
abb4dd42 FB |
223 | } |
224 | ||
225 | public function getOption($n) { | |
ccfabbd3 FB |
226 | $this->buildFromSession(); |
227 | return parent::getOption($n); | |
a1a69528 | 228 | } |
7d26c37d | 229 | |
abb4dd42 | 230 | public function getOptions() { |
ccfabbd3 FB |
231 | $this->buildFromSession(); |
232 | return parent::getOptions(); | |
a1a69528 | 233 | } |
001b27af FB |
234 | |
235 | public function authForm() { | |
236 | global $core; | |
237 | $path = "http://murphy.m4x.org/~x2003bruneau/dotclear/"; | |
238 | return '<fieldset>'. | |
239 | '<p><a href="' . $path . 'auth/Xorg?path=/~x2003bruneau/dotclear/admin/index.php">Via Polytechnique.org</a></p>' . | |
001b27af FB |
240 | '</fieldset>'. |
241 | '<p>'.__('You must accept cookies in order to use the private area.').'</p>'; | |
242 | } | |
3f6bc75f FB |
243 | } |
244 | ||
245 | ?> |