php8
[web.mtrack] / admin / auth.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3 include '../../inc/common.php';
4
5 MTrackACL::requireAnyRights('User', 'modify');
6 $plugins = MTrackConfig::getSection('plugins');
7
8 function get_openid_admins()
9 {
10   $admins = array();
11   $regadmins = array();
12   foreach (MTrackConfig::getSection('user_classes') as $id => $role) {
13     if ($role == 'admin') {
14       if (preg_match('@^https?://@', $id)) {
15         $admins[] = $id;
16       } else {
17         $regadmins[$id] = $id;
18       }
19     }
20   }
21   if (count($regadmins)) {
22     /* look at aliases to see if there are any that look like OpenIDs */
23     foreach (MTrackDB::q('select alias, userid from useraliases')->fetchAll()
24         as $row) {
25       if (!preg_match('@^https?://@', $row[0])) {
26         continue;
27       }
28       if (isset($regadmins[$row[1]])) {
29         $admins[] = $row[0];
30       }
31     }
32   }
33   return $admins;
34 }
35
36 function get_admins()
37 {
38   $admins = array();
39   foreach (MTrackConfig::getSection('user_classes') as $id => $role) {
40     if ($role == 'admin' && !preg_match('@^https?://@', $id)) {
41       $admins[] = $id;
42     }
43   }
44   return $admins;
45 }
46
47 $message = null;
48
49 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
50   if (isset($_POST['setuppublic'])) {
51     $admins = get_openid_admins();
52     $add_admin = isset($_POST['adminopenid']) ?
53                     trim($_POST['adminopenid']) : '';
54     $localid = isset($_POST['adminuserid']) ?
55                     trim($_POST['adminuserid']) : '';
56     if (count($admins) == 0 && (!strlen($add_admin) || !strlen($localid))) {
57       $message = "You MUST add an OpenID for the administrator";
58     } else {
59       if (strlen($localid)) {
60         MTrackConfig::set('user_classes', $localid, 'admin');
61       }
62       $new = true;
63       foreach (MTrackDB::q('select userid from userinfo where userid = ?',
64           $localid)->fetchAll() as $row) {
65         $new = false;
66         break;
67       }
68       if ($new) {
69         MTrackDB::q('insert into userinfo (userid, active) values (?, 1)', $localid);
70       }
71       $new = true;
72       foreach (MTrackDB::q('select userid from useraliases where alias = ?', $add_admin)->fetchAll() as $row) {
73         if ($row[0] != $localid) {
74           throw new Exception("$add_admin is already associated with $row[0]");
75         }
76         $new = false;
77       }
78       if ($new) {
79         MTrackDB::q('insert into useraliases (userid, alias) values (?,?)',
80           $localid, $add_admin);
81       }
82
83       MTrackConfig::set('plugins', 'MTrackAuth_OpenID', '');
84       if (isset($plugins['MTrackAuth_HTTP'])) {
85         MTrackConfig::remove('plugins', 'MTrackAuth_HTTP');
86         // Reset anonymous for public access
87         MTrackConfig::remove('user_class_roles', 'anonymous');
88       }
89
90       MTrackConfig::save();
91       header("Location: {$ABSWEB}admin/auth.php");
92       exit;
93     }
94   } elseif (isset($_POST['setupprivate'])) {
95     $admins = get_admins();
96     $add_admin = isset($_POST['adminuser']) ?
97                     trim($_POST['adminuser']) : '';
98     if (count($admins) == 0 && !strlen($add_admin)) {
99       $message = "You MUST add a user with admin rights";
100     } else {
101       $vardir = MTrackConfig::get('core', 'vardir');
102       $pfile = "$vardir/http.user";
103
104       if (strlen($add_admin)) {
105         if (!isset($_SERVER['REMOTE_USER'])) {
106           // validate the password
107           if ($_POST['adminpass1'] != $_POST['adminpass2']) {
108             $message = "Passwords don't match";
109           } else {
110             $http_auth = new MTrackAuth_HTTP(null, "digest:$pfile");
111             $http_auth->setUserPassword($add_admin, $_POST['adminpass1']);
112           }
113         }
114         MTrackConfig::set('user_classes', $add_admin, 'admin');
115       }
116       if ($message == null) {
117         if (!isset($plugins['MTrackAuth_HTTP'])) {
118           MTrackConfig::set('plugins', 'MTrackAuth_HTTP',
119               "$vardir/http.group, digest:$pfile");
120         }
121         if (isset($plugins['MTrackAuth_OpenID'])) {
122           MTrackConfig::remove('plugins', 'MTrackAuth_OpenID');
123           // Set up the roles for private access
124           // Use default authenticated permissions
125           MTrackConfig::remove('user_class_roles', 'authenticated');
126           // Make anonymous have no rights
127           MTrackConfig::set('user_class_roles', 'anonymous', '');
128         }
129         MTrackConfig::save();
130         header("Location: {$ABSWEB}admin/auth.php");
131         exit;
132       }
133     }
134   }
135 }
136
137 mtrack_head("Administration - Authentication");
138
139 $plugins = MTrackConfig::getSection('plugins');
140 $http_configd = isset($plugins['MTrackAuth_HTTP']) ?  " (Active)" : '';
141 $openid_configd = isset($plugins['MTrackAuth_OpenID']) ?  " (Active)" : '';
142
143
144 ?>
145 <h1>Authentication</h1>
146 <?php
147 if ($message) {
148   $message = htmlentities($message, ENT_QUOTES, 'utf-8');
149   echo <<<HTML
150 <div class='ui-state-error ui-corner-all'>
151     <span class='ui-icon ui-icon-alert'></span>
152     $message
153 </div>
154 HTML;
155 }
156
157
158 ?>
159 <p>
160 Select one of the following, depending
161 on which one best matches your intended mtrack deployment:
162 </p>
163
164 <form method='post'>
165 <div id="authaccordion">
166 <h2><a href='#'>Private (HTTP authentication)<?php echo $http_configd ?></a></h2>
167 <div>
168 <p>
169   I want to strictly control who has access to mtrack, and prevent
170   anonymous users from having any rights.
171 </p>
172 <?php
173 if (isset($_SERVER['REMOTE_USER'])) {
174 ?>
175 <p>
176   It looks like your web server is configured to use HTTP authentication
177   (you're authenticated as <?php
178     echo htmlentities($_SERVER['REMOTE_USER'], ENT_QUOTES, 'utf-8') ?>)
179   mtrack will defer to your web server configuration for authentication.
180   Contact your system administrator to add or remove users, or to change
181   their passwords.  You may still use the mtrack user management screens
182   to change rights assignments for the users.
183 </p>
184 <?php
185 } else {
186 ?>
187 <p>
188   mtrack will use HTTP authentication and store the password and group
189   files in the <em>vardir</em>.
190 </p>
191 <?php
192 }
193 echo "<h3>Administrators</h3>";
194 $admins = get_admins();
195 if (count($admins)) {
196   echo "<p>The following users are configured with admin rights:</p>";
197   echo "<p>";
198   foreach ($admins as $id) {
199     echo mtrack_username($id) . " ";
200   }
201   echo "</p>";
202 } else {
203   echo <<<HTML
204 <p>You <em>MUST</em> add at least one user as an administrator,
205 otherwise no one will be able to administer the system without editing
206 the config.ini file.
207 </p>
208 HTML;
209
210   echo <<<HTML
211 <table>
212 <tr>
213 <td><b>Add Admin User</b>:</td>
214 <td><input type="text" name="adminuser"></td>
215 </tr>
216 HTML;
217
218   if (!isset($_SERVER['REMOTE_USER'])) {
219     echo <<<HTML
220 <tr>
221   <td><b>Set Password</b>:</td>
222   <td><input type="password" name="adminpass1"></td>
223 </tr>
224 <tr>
225   <td><b>Confirm Password</b>:</td>
226   <td><input type="password" name="adminpass2"></td>
227 </tr>
228 </table>
229 HTML;
230   } else {
231     echo <<<HTML
232 </table>
233 <p>
234 <em>You can't set the password here, because mtrack has no way to automatically
235 find out how to do that.  Contact your system administrator to ensure that
236 you have a username and password configured for mtrack</em></p>
237 HTML;
238   }
239 }
240 ?>
241   <input type='submit' name='setupprivate'
242     value='Configure Private Authentication'>
243
244 </div>
245 <h2><a href='#'>Public (OpenID)<?php echo $openid_configd ?></a></h2>
246 <div>
247 <p>
248   I want to allow the public access to mtrack, but only allow people that
249   I trust to make certain kinds of changes.
250 </p>
251 <p>
252   mtrack will use OpenID to manage authentication.
253 </p>
254 <h3>Administrators</h3>
255 <?php
256 $admins = get_openid_admins();
257 if (count($admins)) {
258   echo "<p>The following OpenID users are configured with admin rights:</p>";
259   echo "<p>";
260   foreach ($admins as $id) {
261     echo mtrack_username($id) . " ($id) ";
262   }
263   echo "</p>";
264 } else {
265   echo <<<HTML
266 <p>You <em>MUST</em> add at least one OpenID as an administrator,
267 otherwise no one will be able to administer the system without editing
268 the config.ini file.
269 </p>
270 HTML;
271 }
272 ?>
273 <b>Add Admin OpenID</b>: <input type="text" name="adminopenid"><br>
274 <b>Local Username</b>: <input type="text" name="adminuserid"><br>
275   <input type='submit' name='setuppublic'
276     value='Configure Public Authentication'>
277 </div>
278 </div>
279 </form>
280 <script>
281 $(document).ready(function () {
282   $('#authaccordion').accordion({
283     active: <?php
284   if (isset($plugins['MTrackAuth_OpenID'])) {
285     echo "1";
286   } else {
287     echo "0";
288   }
289 ?>});
290 });
291 </script>
292 <?php
293 mtrack_foot();
294