Prettied up the permissions page a bit
[gwvp.git] / gwvplib / gwvprepoadmin.php
1 <?php
2 $CALL_ME_FUNCTIONS["repoadmin"] = "gwvp_RepoAdminCallMe";
3
4 $MENU_ITEMS["20repos"]["text"] = "Repo Admin";
5 $MENU_ITEMS["20repos"]["link"] = "$BASE_URL/admin/repos";
6
7 function gwvp_RepoAdminCallMe()
8 {
9         
10         error_log("in repoadmin callme");
11         if(isset($_REQUEST["q"])) {
12                 $query = $_REQUEST["q"];
13                 $qspl = explode("/", $query);
14                 if(isset($qspl[0]) && isset($qspl[1])) {
15                         if($qspl[0] == "admin" && $qspl[1] == "repos") {
16                                 error_log("i got here, where next?");
17                                 if(isset($qspl[2])) {
18                                         switch($qspl[2]) {
19                                                 case "create":
20                                                         return "gwvp_CreateRepoPage";
21                                                         break;
22                                                 case "docreate":
23                                                         return "gwvp_DoCreateRepoPage";
24                                                         break;
25                                                 case "manage":
26                                                         return "gwvp_ManageRepoPage";
27                                                         break;
28                                                 case "updateperms":
29                                                         return "gwvp_UpdateRepoPerms";
30                                                         break;
31                                                 default:
32                                                         return "gwvp_RepoAdminPage";
33                                         }                                       
34                                 } else {
35                                         return "gwvp_RepoAdminPage";
36                                 }
37                         }
38                 } 
39                 else return false;
40         }
41         
42         return false;
43 }
44
45 function gwvp_ManageRepoPage()
46 {
47         gwvp_goMainPage("gwvp_ManageRepoPageBody");
48 }
49
50 function gwvp_RepoAdminPage()
51 {
52         gwvp_goMainPage("gwvp_RepoAdminPageBody");
53 }
54
55 function gwvp_CreateRepoPage()
56 {
57         gwvp_goMainPage("gwvp_CreateRepoPageBody");
58 }
59
60 function gwvp_UpdateRepoPerms()
61 {
62         global $LOGIN_TYPE, $BASE_URL;
63         
64         $rid = -1;
65         if(isset($_REQUEST["q"])) {
66                 $query = $_REQUEST["q"];
67                 $qspl = explode("/", $query);
68                 $rid = $qspl[3];
69         }
70
71         // a visible addition
72         if(isset($_REQUEST["visadd"])) {
73                 foreach($_REQUEST["vismembersout"] as $mems) {
74                         //gwvp_addRepoPermission($repoid, $permtype, $permref);
75                         gwvp_addRepoPermission($rid, "visible", $mems);
76                 }
77         }
78         
79         // a visible removal
80         if(isset($_REQUEST["visremove"])) {
81                 foreach($_REQUEST["vismembersin"] as $rems) {
82                         error_log("Remove permid, $rems");
83                         gwvp_removeRepoPermission($rems);
84                 }
85         }
86
87         // a read addition
88         if(isset($_REQUEST["readadd"])) {
89                 foreach($_REQUEST["readmembersout"] as $mems) {
90                         //gwvp_addRepoPermission($repoid, $permtype, $permref);
91                         gwvp_addRepoPermission($rid, "read", $mems);
92                 }
93         }
94         
95         // a read removal
96         if(isset($_REQUEST["readremove"])) {
97                 foreach($_REQUEST["readmembersin"] as $rems) {
98                         error_log("Remove permid, $rems");
99                         gwvp_removeRepoPermission($rems);
100                 }
101         }
102
103         // a write addition
104         if(isset($_REQUEST["writeadd"])) {
105                 foreach($_REQUEST["writemembersout"] as $mems) {
106                         //gwvp_addRepoPermission($repoid, $permtype, $permref);
107                         gwvp_addRepoPermission($rid, "write", $mems);
108                 }
109         }
110         
111         // a write removal
112         if(isset($_REQUEST["writeremove"])) {
113                 foreach($_REQUEST["writemembersin"] as $rems) {
114                         error_log("Remove permid, $rems");
115                         gwvp_removeRepoPermission($rems);
116                 }
117         }
118         
119         gwvp_SendMessage("info", "Permissions Updated");
120         header("Location: $BASE_URL/admin/repos/manage/$rid");
121         return;
122 }
123
124 //function gwvp_createGitRepo($name, $ownerid, $desc, $defaultperms=0, $bundle=null)
125
126
127 function gwvp_DoCreateRepoPage()
128 {
129         global $BASE_URL;
130         
131         $reponame = $_REQUEST["reponame"];
132         $repodesc = $_REQUEST["repodesc"];
133         
134         $defperms = 0;
135         
136         if(isset($_REQUEST["defperms"])) {
137                 switch($_REQUEST["defperms"]) {
138                         case "permsall":
139                                 $defperms = 0;
140                                 break;
141                         case "permsmeonly":
142                                 $defperms = 1;
143                                 break;
144                         case "permsinvisible":
145                                 $defperms = 2;
146                                 break;
147                         default:
148                                 $defperms = 2;
149                 }
150         }
151         
152         // TODO: this code is bollocks, need to redo
153         if(gwvp_repoExists($reponame)) {
154                 gwvp_SendMessage("error", "a repository with the name <b>\"$reponame\"</b> already exists");
155                 //header("Location: $BASE_URL/admin/repos/create?reponameobv=$reponame&repodescobv=$repodesc");
156         } else if($_FILES["bundlefile"]["size"] > 0) { //               if(isset($_FILES["bundlefile"]["size"]))  <--- this needs to happen here TODO
157                 error_log("bundle file tmpname is ".$_FILES["bundlefile"]["tmp_name"]);
158                 // function gwvp_createGitRepo($name, $ownerid, $desc, $defaultperms=0, $bundle=null)
159                 // TODO: deal with default perms
160                 gwvp_createGitRepo($reponame, $_SESSION["id"], $repodesc, $_FILES["bundlefile"]["tmp_name"], $defperms);
161                 gwvp_SendMessage("info", "Repo, $reponame, created");
162         } else if(gwvp_createGitRepo($reponame, $_SESSION["id"], $repodesc, null, $defperms)) {
163                 gwvp_SendMessage("info", "Repo, $reponame, created");
164         }
165         header("Location: $BASE_URL/admin/repos");
166         
167         
168 }
169
170 function gwvp_CreateRepoPageBody()
171 {
172         global $BASE_URL;
173         
174         $repo_base = gwvp_getConfigVal("repodir");
175         
176         $reponameobv = "";
177         $repodescobv = "";
178         
179         if(isset($_REQUEST["reponameobv"])) $reponameobv = $_REQUEST["reponameobv"];
180         if(isset($_REQUEST["repodescobv"])) $repodescobv = $_REQUEST["repodescobv"];
181         
182         echo "<h2>Create a Repo</h2>";
183         echo "<form method=\"post\" enctype=\"multipart/form-data\" action=\"$BASE_URL/admin/repos/docreate\">";
184         echo "<table>";
185         echo "<tr><td>Repository Name</td><td bgcolor=\"#eeeeee\"><input type=\"text\" name=\"reponame\" value=\"$reponameobv\"></td></tr>";
186         echo "<tr><td>Repository Description</td><td bgcolor=\"#eeeeee\"><input type=\"text\" name=\"repodesc\" value=\"$repodescobv\"></td></tr>";
187         echo "<tr><td>Repository Bundle</td><td bgcolor=\"#eeeeee\"><input type=\"file\" name=\"bundlefile\"></td><td><i>Create a bundle for pro-creating the git repository (export your git bundle with \"git bundle create /tmp/filename --branches\")<br>";
188         echo "Typically you wouldn't use this as its easier to \"push\" to the repo after its created by adding it as a remote and pushing your local master branch</i></td></tr>";
189         
190         echo "<tr><td>Default Permisison Set</td><td bgcolor=\"#eeeeee\">";
191         
192         echo "<table>";
193         echo "<tr><td><input type=\"radio\" name=\"defperms\" value=\"permsall\" checked></td><td>Anyone Can Read, Only you can write</td></tr>";
194         echo "<tr><td><input type=\"radio\" name=\"defperms\" value=\"permsmeonly\"></td><td>Anyone can see the repository exists, but only you can read or write to it</td></tr>";
195         echo "<tr><td><input type=\"radio\" name=\"defperms\" value=\"permsinvisible\"></td><td>Repository only visible to you</td></tr>";
196         echo "</table>";
197         
198         
199         echo "</td><td><i>Permissions can be changed in repository management later</i></td></tr>";
200         
201         echo "</table>";
202         
203         
204         
205         
206         
207         echo "<input type=\"submit\" name=\"create\" value=\"Create\"><br>";
208         echo "</form>";
209 }
210
211 function gwvp_RepoAdminPageBody()
212 {
213         // first we need a menu
214         global $BASE_URL;
215         
216         echo "<h2>Repo Management</h2>";
217         echo "<a href=\"$BASE_URL/admin/repos/create\">Create a Repo</a><br>";
218         
219         if(isset($_SESSION["isloggedin"])) {
220                 echo "<h3>Your Repo's</h3>";
221                 $ownreps = gwvp_getOwnedRepos($_SESSION["id"]);
222                 if($ownreps != false) {
223                         echo "<table border=\"1\">";
224                         echo "<tr><th>Repo Name</th><th>Repo description</th></tr>";
225                         foreach($ownreps as $repos) {
226                                 $mjay = print_r($repos, true);
227                                 error_log("snafu: $mjay");
228                                 $reponame = $repos["name"];
229                                 $repodesc = $repos["description"];
230                                 $rid = $repos["id"];
231                                 echo "<tr><td>$reponame</td><td>$repodesc</td><td><a href=\"$BASE_URL/admin/repos/manage/$rid\">Manage</a></td></tr>";
232                         }
233                         echo "</table>";
234                 } else {
235                         echo "You own no repositories";
236                 }
237                 echo "<hr>";
238         }
239         
240         
241         // next we need a repo list - with perms checking - ug
242         // i must also remember that the home page will also contain a list of repos and that this page is solely for maintance
243         // and creation of repos - so i dont need to get over-worked about the info stored on this page outside of those activities
244         $rlist = gwvp_GetRepoList();
245         echo "<table border=\"1\"><tr><th>Repo Name</th><th>Repo Description</th><th>Repo Owner</th></tr>";
246         foreach($rlist as $u_res) {
247                 $rid = $u_res["id"];
248                 $rname = $u_res["name"];
249                 $rdesc = $u_res["description"];
250                 $rown = gwvp_getUserName($u_res["owner"]);
251                 echo "<tr><td>$rname</td><td>$rdesc</td><td>$rown</td><td><a href=\"$BASE_URL/admin/repos/manage/$rid\">Details</a></td></tr>";
252         }
253         echo "</table>";
254         
255         return;
256 }
257
258 function gwvp_DisemableRefId($who)
259 {
260         if($who == "anon") return "Everyone";
261         if($who == "authed") return "All Authenticated User";
262         
263         $epl = explode(":", $who);
264         
265         if($epl[0] == "user") {
266                 $username = gwvp_getUserName($epl[1]);
267                 return "User: $username";
268         } else if ($epl[0] == "group") {
269                 $grdent = gwvp_getGroup($epl[1]);
270                 $groupname = $grdent["name"];
271                 return "Group: $groupname";
272         } else return "unknown";
273 }
274
275 function gwvp_ManageRepoPageBody()
276 {
277         global $LOGIN_TYPE, $BASE_URL;
278         
279         $rid = -1;
280         if(isset($_REQUEST["q"])) {
281                 $query = $_REQUEST["q"];
282                 $qspl = explode("/", $query);
283                 $rid = $qspl[3];
284         }
285         
286         $repodets = gwvp_GetRepo($rid);
287         $reponame = $repodets["name"];
288         $repodesc = $repodets["description"];
289         $repoownid = $repodets["owner"];
290         $owndby = gwvp_getUserName($repoownid);
291         $users = gwvp_getUsers();
292         $groups = gwvp_getGroups();
293         $repoperms = gwvp_getRepoPermissions($rid);
294         
295         
296         echo "<h2>Repository Management</h2>";
297         echo "<b>$owndby's</b> Repository <i>$reponame</i><br>";
298         
299         echo "<form method=\"post\" action=\"$BASE_URL/admin/repos/update/$rid\">";
300         
301         echo "<table>";
302         echo "<tr><td>Description</td><td><input type=\"text\" name=\"desc\" value=\"$repodesc\"></td></tr>";
303         echo "</table>";
304         echo "<input type=\"submit\" name=\"update\" value=\"Update\"><br>";
305         echo "</form>";
306         
307         echo "<form method=\"post\" action=\"$BASE_URL/admin/repos/updateperms/$rid\">";
308         echo "<table>";
309         echo "<tr bgcolor=\"#eeeee0\"><th>Visibility</th><th>Read/Clone</th><th>Write/Push</th></tr>";
310         echo "<tr><td bgcolor=\"#eeeeff\">";
311         
312         
313         
314         
315         // visibility section
316         $visin[0] = 0;
317         echo "<table>";
318         echo "<tr><th>Allowed</th><td></td><th>All</th></tr>";
319         echo "<tr><td>";
320         // list allowed users
321         echo "<select name=\"vismembersin[]\" size=\"20\" multiple=\"true\">";
322         $ninvis = 0;
323         foreach($repoperms as $v_perms) {
324                 if($v_perms["type"] == "visible") {
325                         $who = $v_perms["ref"]; // now we need to disemble ref
326                         $pid = $v_perms["id"];
327                         $refid = gwvp_DisemableRefId($who);
328                         
329                         $visin[$who] = true;
330                         
331                         echo "<option value=\"$pid\">$refid</option>";
332                         $ninvis++;
333                 }
334         }
335         if($ninvis == 0) echo "<option>--------------</option>";
336
337         echo "</select>";
338         
339         echo "</td><td>";
340         // buttons
341         echo "<input type=\"submit\" name=\"visadd\" value=\"<<\"><br>";
342         echo "<input type=\"submit\" name=\"visremove\" value=\">>\"><br>";
343         
344         echo "</td><td>";
345         // list all users/groups/specials
346         $noutvis = 0;
347         echo "<select name=\"vismembersout[]\" size=\"20\" multiple=\"true\">";
348         if(!isset($visin["anon"])) echo "<option value=\"anon\">Everyone</option>";
349         if(!isset($visin["authed"])) echo "<option value=\"authed\">All Authenticated User</option>";
350         foreach($groups as $u_groups) {
351                 $gname = $u_groups["name"];
352                 $gid = $u_groups["id"];
353                 if(!gwvp_IsGroupAdmin(null, $gid)) if(!isset($visin["group:$gid"])) {
354                         echo "<option value=\"group:$gid\">Group: $gname</option>";
355                         $noutvis++;
356                 }
357         }
358         foreach($users as $u_users) {
359                 $uid = $u_users["id"];
360                 $email = $u_users["email"];
361                 $username = $u_users["username"];
362                 $fullname = $u_users["fullname"];
363                 if(!gwvp_IsUserAdmin(null, null, $uid)) if(!isset($visin["user:$uid"])) {
364                         echo "<option value=\"user:$uid\">User: $username</option>";
365                         $noutvis++;
366                 }
367         }
368         if($noutvis == 0) echo "<option>-------------------</option>";
369         echo "</select>";
370         
371         echo "</td></tr>";
372         echo "</table>";
373         // end visibility section
374         
375         
376         echo "</td><td bgcolor=\"#eeffee\">";
377         // Read/clone/pull section
378         $readin[0] = 0;
379         echo "<table>";
380         echo "<tr><th>Allowed</th><td></td><th>All</th></tr>";
381         echo "<tr><td>";
382         // list allowed users
383         echo "<select name=\"readmembersin[]\" size=\"20\" multiple=\"true\">";
384         $ninread = 0;
385         foreach($repoperms as $v_perms) {
386                 if($v_perms["type"] == "read") {
387                         $who = $v_perms["ref"]; // now we need to disemble ref
388                         $pid = $v_perms["id"];
389                         $refid = gwvp_DisemableRefId($who);
390                         
391                         $readin[$who] = true;
392                         
393                         echo "<option value=\"$pid\">$refid</option>";
394                         $ninread++;
395                 }
396         }
397         if($ninread == 0) echo "<option>-------------------</option>";
398
399         echo "</select>";
400         
401         echo "</td><td>";
402         // buttons
403         echo "<input type=\"submit\" name=\"readadd\" value=\"<<\"><br>";
404         echo "<input type=\"submit\" name=\"readremove\" value=\">>\"><br>";
405         
406         echo "</td><td>";
407         // list all users/groups/specials
408         echo "<select name=\"readmembersout[]\" size=\"20\" multiple=\"true\">";
409         $noutread = 0;
410         if(!isset($readin["anon"])) echo "<option value=\"anon\">Everyone</option>";
411         if(!isset($readin["authed"])) echo "<option value=\"authed\">All Authenticated User</option>";
412         foreach($groups as $u_groups) {
413                 $gname = $u_groups["name"];
414                 $gid = $u_groups["id"];
415                 if(!gwvp_IsGroupAdmin(null, $gid)) if(!isset($readin["group:$gid"])) {
416                         echo "<option value=\"group:$gid\">Group: $gname</option>";
417                         $noutread++;
418                 }
419         }
420         foreach($users as $u_users) {
421                 $uid = $u_users["id"];
422                 $email = $u_users["email"];
423                 $username = $u_users["username"];
424                 $fullname = $u_users["fullname"];
425                 if(!gwvp_IsUserAdmin(null, null, $uid)) if(!isset($readin["user:$uid"])) {
426                         echo "<option value=\"user:$uid\">User: $username</option>";
427                         $noutread++;
428                 }
429         }
430         if($noutread == 0) echo "<option>-------------------</option>";
431         echo "</select>";
432         
433         echo "</td></tr>";
434         echo "</table>";
435         // end read/clone/pull section
436         
437         
438         echo "</td><td bgcolor=\"#ffeeee\">";
439         // write/push section
440         $writein[0] = 0;
441         echo "<table>";
442         echo "<tr><th>Allowed</th><td></td><th>All</th></tr>";
443         echo "<tr><td>";
444         // list allowed users
445         echo "<select name=\"writemembersin[]\" size=\"20\" multiple=\"true\">";
446         $ninwrite = 0;
447         foreach($repoperms as $v_perms) {
448                 if($v_perms["type"] == "write") {
449                         $who = $v_perms["ref"]; // now we need to disemble ref
450                         $pid = $v_perms["id"];
451                         $refid = gwvp_DisemableRefId($who);
452                         
453                         $writein[$who] = true;
454                         
455                         echo "<option value=\"$pid\">$refid</option>";
456                         $ninwrite++;
457                 }
458         }
459         if($ninwrite == 0) echo "<option>-------------------</option>";
460         echo "</select>";
461         
462         echo "</td><td>";
463         // buttons
464         echo "<input type=\"submit\" name=\"writeadd\" value=\"<<\"><br>";
465         echo "<input type=\"submit\" name=\"writeremove\" value=\">>\"><br>";
466         
467         echo "</td><td>";
468         // list all users/groups/specials
469         echo "<select name=\"writemembersout[]\" size=\"20\" multiple=\"true\">";
470         $noutwrite = 0;
471         // cant have anon for writes if(!isset($writein["anon"])) echo "<option value=\"anon\">Everyone</option>";
472         if(!isset($writein["authed"])) echo "<option value=\"authed\">All Authenticated User</option>";
473         foreach($groups as $u_groups) {
474                 $gname = $u_groups["name"];
475                 $gid = $u_groups["id"];
476                 if(!gwvp_IsGroupAdmin(null, $gid)) if(!isset($writein["group:$gid"])) {
477                         $noutwrite++;
478                         echo "<option value=\"group:$gid\">Group: $gname</option>";
479                 }
480         }
481         foreach($users as $u_users) {
482                 $uid = $u_users["id"];
483                 $email = $u_users["email"];
484                 $username = $u_users["username"];
485                 $fullname = $u_users["fullname"];
486                 if(!gwvp_IsUserAdmin(null, null, $uid)) if(!isset($writein["user:$uid"])) {
487                         $noutwrite++;
488                         echo "<option value=\"user:$uid\">User: $username</option>";
489                 }
490         }
491         if($noutwrite == 0) echo "<option>-------------------</option>";
492         echo "</select>";
493         
494         echo "</td></tr>";
495         echo "</table>";
496         // end write/push section
497         
498         echo "</td></tr>";
499         
500         echo "</table>";
501         echo "</form>";
502 }
503
504
505 ?>