fixing up the "is setup" check in the installer
[gwvp.git] / gwvplib / gwvprepoadmin.php
index 53cf4b1..cb533bf 100644 (file)
@@ -6,16 +6,31 @@ $MENU_ITEMS["20repos"]["link"] = "$BASE_URL/admin/repos";
 
 function gwvp_RepoAdminCallMe()
 {
+       
+       error_log("in repoadmin callme");
        if(isset($_REQUEST["q"])) {
                $query = $_REQUEST["q"];
                $qspl = explode("/", $query);
                if(isset($qspl[0]) && isset($qspl[1])) {
                        if($qspl[0] == "admin" && $qspl[1] == "repos") {
+                               error_log("i got here, where next?");
                                if(isset($qspl[2])) {
                                        switch($qspl[2]) {
                                                case "create":
                                                        return "gwvp_CreateRepoPage";
                                                        break;
+                                               case "docreate":
+                                                       return "gwvp_DoCreateRepoPage";
+                                                       break;
+                                               case "manage":
+                                                       return "gwvp_ManageRepoPage";
+                                                       break;
+                                               case "updateperms":
+                                                       return "gwvp_UpdateRepoPerms";
+                                                       break;
+                                               case "details":
+                                                       return "gwvp_RepoDetailsPage";
+                                                       break;
                                                default:
                                                        return "gwvp_RepoAdminPage";
                                        }                                       
@@ -30,11 +45,181 @@ function gwvp_RepoAdminCallMe()
        return false;
 }
 
+function gwvp_ManageRepoPage()
+{
+       gwvp_goMainPage("gwvp_ManageRepoPageBody");
+}
+
 function gwvp_RepoAdminPage()
 {
        gwvp_goMainPage("gwvp_RepoAdminPageBody");
 }
 
+function gwvp_CreateRepoPage()
+{
+       gwvp_goMainPage("gwvp_CreateRepoPageBody");
+}
+
+function gwvp_RepoDetailsPage()
+{
+       gwvp_goMainPage("gwvp_RepoDetailsPageBody");
+}
+
+function gwvp_RepoDetailsPageBody()
+{
+       echo "I am a repo details page";
+}
+
+function gwvp_UpdateRepoPerms()
+{
+       global $LOGIN_TYPE, $BASE_URL;
+       
+       $rid = -1;
+       if(isset($_REQUEST["q"])) {
+               $query = $_REQUEST["q"];
+               $qspl = explode("/", $query);
+               $rid = $qspl[3];
+       }
+
+       // a visible addition
+       if(isset($_REQUEST["visadd"])) {
+               foreach($_REQUEST["vismembersout"] as $mems) {
+                       //gwvp_addRepoPermission($repoid, $permtype, $permref);
+                       gwvp_addRepoPermission($rid, "visible", $mems);
+               }
+       }
+       
+       // a visible removal
+       if(isset($_REQUEST["visremove"])) {
+               foreach($_REQUEST["vismembersin"] as $rems) {
+                       error_log("Remove permid, $rems");
+                       gwvp_removeRepoPermission($rems);
+               }
+       }
+
+       // a read addition
+       if(isset($_REQUEST["readadd"])) {
+               foreach($_REQUEST["readmembersout"] as $mems) {
+                       //gwvp_addRepoPermission($repoid, $permtype, $permref);
+                       gwvp_addRepoPermission($rid, "read", $mems);
+               }
+       }
+       
+       // a read removal
+       if(isset($_REQUEST["readremove"])) {
+               foreach($_REQUEST["readmembersin"] as $rems) {
+                       error_log("Remove permid, $rems");
+                       gwvp_removeRepoPermission($rems);
+               }
+       }
+
+       // a write addition
+       if(isset($_REQUEST["writeadd"])) {
+               foreach($_REQUEST["writemembersout"] as $mems) {
+                       //gwvp_addRepoPermission($repoid, $permtype, $permref);
+                       gwvp_addRepoPermission($rid, "write", $mems);
+               }
+       }
+       
+       // a write removal
+       if(isset($_REQUEST["writeremove"])) {
+               foreach($_REQUEST["writemembersin"] as $rems) {
+                       error_log("Remove permid, $rems");
+                       gwvp_removeRepoPermission($rems);
+               }
+       }
+       
+       gwvp_SendMessage("info", "Permissions Updated");
+       header("Location: $BASE_URL/admin/repos/manage/$rid");
+       return;
+}
+
+//function gwvp_createGitRepo($name, $ownerid, $desc, $defaultperms=0, $bundle=null)
+
+
+function gwvp_DoCreateRepoPage()
+{
+       global $BASE_URL;
+       
+       $reponame = $_REQUEST["reponame"];
+       $repodesc = $_REQUEST["repodesc"];
+       
+       $defperms = 0;
+       
+       if(isset($_REQUEST["defperms"])) {
+               switch($_REQUEST["defperms"]) {
+                       case "permsall":
+                               $defperms = 0;
+                               break;
+                       case "permsmeonly":
+                               $defperms = 1;
+                               break;
+                       case "permsinvisible":
+                               $defperms = 2;
+                               break;
+                       default:
+                               $defperms = 2;
+               }
+       }
+       
+       // TODO: this code is bollocks, need to redo
+       if(gwvp_repoExists($reponame)) {
+               gwvp_SendMessage("error", "a repository with the name <b>\"$reponame\"</b> already exists");
+               //header("Location: $BASE_URL/admin/repos/create?reponameobv=$reponame&repodescobv=$repodesc");
+       } else if($_FILES["bundlefile"]["size"] > 0) { //               if(isset($_FILES["bundlefile"]["size"]))  <--- this needs to happen here TODO
+               error_log("bundle file tmpname is ".$_FILES["bundlefile"]["tmp_name"]);
+               // function gwvp_createGitRepo($name, $ownerid, $desc, $defaultperms=0, $bundle=null)
+               // TODO: deal with default perms
+               gwvp_createGitRepo($reponame, $_SESSION["id"], $repodesc, $_FILES["bundlefile"]["tmp_name"], $defperms);
+               gwvp_SendMessage("info", "Repo, $reponame, created");
+       } else if(gwvp_createGitRepo($reponame, $_SESSION["id"], $repodesc, null, $defperms)) {
+               gwvp_SendMessage("info", "Repo, $reponame, created");
+       }
+       header("Location: $BASE_URL/admin/repos");
+       
+       
+}
+
+function gwvp_CreateRepoPageBody()
+{
+       global $BASE_URL;
+       
+       $repo_base = gwvp_getConfigVal("repodir");
+       
+       $reponameobv = "";
+       $repodescobv = "";
+       
+       if(isset($_REQUEST["reponameobv"])) $reponameobv = $_REQUEST["reponameobv"];
+       if(isset($_REQUEST["repodescobv"])) $repodescobv = $_REQUEST["repodescobv"];
+       
+       echo "<h2>Create a Repo</h2>";
+       echo "<form method=\"post\" enctype=\"multipart/form-data\" action=\"$BASE_URL/admin/repos/docreate\">";
+       echo "<table>";
+       echo "<tr><td>Repository Name</td><td bgcolor=\"#eeeeee\"><input type=\"text\" name=\"reponame\" value=\"$reponameobv\"></td></tr>";
+       echo "<tr><td>Repository Description</td><td bgcolor=\"#eeeeee\"><input type=\"text\" name=\"repodesc\" value=\"$repodescobv\"></td></tr>";
+       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>";
+       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>";
+       
+       echo "<tr><td>Default Permisison Set</td><td bgcolor=\"#eeeeee\">";
+       
+       echo "<table>";
+       echo "<tr><td><input type=\"radio\" name=\"defperms\" value=\"permsall\" checked></td><td>Anyone Can Read, Only you can write</td></tr>";
+       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>";
+       echo "<tr><td><input type=\"radio\" name=\"defperms\" value=\"permsinvisible\"></td><td>Repository only visible to you</td></tr>";
+       echo "</table>";
+       
+       
+       echo "</td><td><i>Permissions can be changed in repository management later</i></td></tr>";
+       
+       echo "</table>";
+       
+       
+       
+       
+       
+       echo "<input type=\"submit\" name=\"create\" value=\"Create\"><br>";
+       echo "</form>";
+}
 
 function gwvp_RepoAdminPageBody()
 {
@@ -44,21 +229,301 @@ function gwvp_RepoAdminPageBody()
        echo "<h2>Repo Management</h2>";
        echo "<a href=\"$BASE_URL/admin/repos/create\">Create a Repo</a><br>";
        
+       if(isset($_SESSION["isloggedin"])) {
+               echo "<h3>Your Repo's</h3>";
+               $ownreps = gwvp_getOwnedRepos($_SESSION["id"]);
+               if($ownreps != false) {
+                       echo "<table border=\"1\">";
+                       echo "<tr><th>Repo Name</th><th>Repo description</th></tr>";
+                       foreach($ownreps as $repos) {
+                               $mjay = print_r($repos, true);
+                               error_log("snafu: $mjay");
+                               $reponame = $repos["name"];
+                               $repodesc = $repos["description"];
+                               $rid = $repos["id"];
+                               echo "<tr><td>$reponame</td><td>$repodesc</td><td><a href=\"$BASE_URL/admin/repos/manage/$rid\">Manage</a></td></tr>";
+                       }
+                       echo "</table>";
+               } else {
+                       echo "You own no repositories";
+               }
+               echo "<hr>";
+       }
+       
        
        // next we need a repo list - with perms checking - ug
        // i must also remember that the home page will also contain a list of repos and that this page is solely for maintance
        // and creation of repos - so i dont need to get over-worked about the info stored on this page outside of those activities
+       echo "<h3>All Repositories</h3>";
        $rlist = gwvp_GetRepoList();
        echo "<table border=\"1\"><tr><th>Repo Name</th><th>Repo Description</th><th>Repo Owner</th></tr>";
        foreach($rlist as $u_res) {
+               $rid = $u_res["id"];
                $rname = $u_res["name"];
                $rdesc = $u_res["description"];
-               $rown = $u_res["owner"];
-               echo "<tr><td>$rname</td><td>$rdesc</td><td>$rown</td></tr>";
+               $rown = gwvp_getUserName($u_res["owner"]);
+               $manordetslink = "details";
+               $manordets = "Details";
+               if(isset($_SESSION["id"])) {
+                       if(gwvp_resolvRepoPerms($_SESSION["id"], $rid)>3) {
+                               $manordetslink = "manage";
+                               $manordets = "Manage";
+                       }
+               }
+               echo "<tr><td>$rname</td><td>$rdesc</td><td>$rown</td><td><a href=\"$BASE_URL/admin/repos/$manordetslink/$rid\">$manordets</a></td></tr>";
        }
        echo "</table>";
        
        return;
 }
 
+function gwvp_DisemableRefId($who)
+{
+       if($who == "anon") return "Everyone";
+       if($who == "authed") return "All Authenticated User";
+       
+       $epl = explode(":", $who);
+       
+       if($epl[0] == "user") {
+               $username = gwvp_getUserName($epl[1]);
+               return "User: $username";
+       } else if ($epl[0] == "group") {
+               $grdent = gwvp_getGroup($epl[1]);
+               $groupname = $grdent["name"];
+               return "Group: $groupname";
+       } else return "unknown";
+}
+
+function gwvp_ManageRepoPageBody()
+{
+       global $LOGIN_TYPE, $BASE_URL;
+       
+       $rid = -1;
+       if(isset($_REQUEST["q"])) {
+               $query = $_REQUEST["q"];
+               $qspl = explode("/", $query);
+               $rid = $qspl[3];
+       }
+       
+       $repodets = gwvp_GetRepo($rid);
+       $reponame = $repodets["name"];
+       $repodesc = $repodets["description"];
+       $repoownid = $repodets["owner"];
+       $owndby = gwvp_getUserName($repoownid);
+       $users = gwvp_getUsers();
+       $groups = gwvp_getGroups();
+       $repoperms = gwvp_getRepoPermissions($rid);
+       
+       
+       echo "<h2>Repository Management</h2>";
+       echo "<b>$owndby's</b> Repository <i>$reponame</i><br>";
+       
+       echo "<form method=\"post\" action=\"$BASE_URL/admin/repos/update/$rid\">";
+       
+       echo "<table>";
+       echo "<tr><td>Description</td><td><input type=\"text\" name=\"desc\" value=\"$repodesc\"></td></tr>";
+       echo "</table>";
+       echo "<input type=\"submit\" name=\"update\" value=\"Update\"><br>";
+       echo "</form>";
+       
+       
+       echo "<h3>Repository Permissions</h3>";
+       echo "<form method=\"post\" action=\"$BASE_URL/admin/repos/updateperms/$rid\">";
+       echo "<table>";
+       echo "<tr bgcolor=\"#eeeee0\"><th>Visibility</th><th>Read/Clone</th><th>Write/Push</th></tr>";
+       echo "<tr><td bgcolor=\"#eeeeff\">";
+       
+       
+       
+       
+       // visibility section
+       $visin[0] = 0;
+       echo "<table>";
+       echo "<tr><th>Allowed</th><td></td><th>All</th></tr>";
+       echo "<tr><td>";
+       // list allowed users
+       echo "<select name=\"vismembersin[]\" size=\"20\" multiple=\"true\">";
+       $ninvis = 0;
+       foreach($repoperms as $v_perms) {
+               if($v_perms["type"] == "visible") {
+                       $who = $v_perms["ref"]; // now we need to disemble ref
+                       $pid = $v_perms["id"];
+                       $refid = gwvp_DisemableRefId($who);
+                       
+                       $visin[$who] = true;
+                       
+                       echo "<option value=\"$pid\">$refid</option>";
+                       $ninvis++;
+               }
+       }
+       if($ninvis == 0) echo "<option>--------------</option>";
+
+       echo "</select>";
+       
+       echo "</td><td>";
+       // buttons
+       echo "<input type=\"submit\" name=\"visadd\" value=\"<<\"><br>";
+       echo "<input type=\"submit\" name=\"visremove\" value=\">>\"><br>";
+       
+       echo "</td><td>";
+       // list all users/groups/specials
+       $noutvis = 0;
+       echo "<select name=\"vismembersout[]\" size=\"20\" multiple=\"true\">";
+       if(!isset($visin["anon"])) echo "<option value=\"anon\">Everyone</option>";
+       if(!isset($visin["authed"])) echo "<option value=\"authed\">All Authenticated User</option>";
+       foreach($groups as $u_groups) {
+               $gname = $u_groups["name"];
+               $gid = $u_groups["id"];
+               if(!gwvp_IsGroupAdmin(null, $gid)) if(!isset($visin["group:$gid"])) {
+                       echo "<option value=\"group:$gid\">Group: $gname</option>";
+                       $noutvis++;
+               }
+       }
+       foreach($users as $u_users) {
+               $uid = $u_users["id"];
+               $email = $u_users["email"];
+               $username = $u_users["username"];
+               $fullname = $u_users["fullname"];
+               if(!gwvp_IsUserAdmin(null, null, $uid)) if(!isset($visin["user:$uid"])) {
+                       echo "<option value=\"user:$uid\">User: $username</option>";
+                       $noutvis++;
+               }
+       }
+       if($noutvis == 0) echo "<option>-------------------</option>";
+       echo "</select>";
+       
+       echo "</td></tr>";
+       echo "</table>";
+       // end visibility section
+       
+       
+       echo "</td><td bgcolor=\"#eeffee\">";
+       // Read/clone/pull section
+       $readin[0] = 0;
+       echo "<table>";
+       echo "<tr><th>Allowed</th><td></td><th>All</th></tr>";
+       echo "<tr><td>";
+       // list allowed users
+       echo "<select name=\"readmembersin[]\" size=\"20\" multiple=\"true\">";
+       $ninread = 0;
+       foreach($repoperms as $v_perms) {
+               if($v_perms["type"] == "read") {
+                       $who = $v_perms["ref"]; // now we need to disemble ref
+                       $pid = $v_perms["id"];
+                       $refid = gwvp_DisemableRefId($who);
+                       
+                       $readin[$who] = true;
+                       
+                       echo "<option value=\"$pid\">$refid</option>";
+                       $ninread++;
+               }
+       }
+       if($ninread == 0) echo "<option>-------------------</option>";
+
+       echo "</select>";
+       
+       echo "</td><td>";
+       // buttons
+       echo "<input type=\"submit\" name=\"readadd\" value=\"<<\"><br>";
+       echo "<input type=\"submit\" name=\"readremove\" value=\">>\"><br>";
+       
+       echo "</td><td>";
+       // list all users/groups/specials
+       echo "<select name=\"readmembersout[]\" size=\"20\" multiple=\"true\">";
+       $noutread = 0;
+       if(!isset($readin["anon"])) echo "<option value=\"anon\">Everyone</option>";
+       if(!isset($readin["authed"])) echo "<option value=\"authed\">All Authenticated User</option>";
+       foreach($groups as $u_groups) {
+               $gname = $u_groups["name"];
+               $gid = $u_groups["id"];
+               if(!gwvp_IsGroupAdmin(null, $gid)) if(!isset($readin["group:$gid"])) {
+                       echo "<option value=\"group:$gid\">Group: $gname</option>";
+                       $noutread++;
+               }
+       }
+       foreach($users as $u_users) {
+               $uid = $u_users["id"];
+               $email = $u_users["email"];
+               $username = $u_users["username"];
+               $fullname = $u_users["fullname"];
+               if(!gwvp_IsUserAdmin(null, null, $uid)) if(!isset($readin["user:$uid"])) {
+                       echo "<option value=\"user:$uid\">User: $username</option>";
+                       $noutread++;
+               }
+       }
+       if($noutread == 0) echo "<option>-------------------</option>";
+       echo "</select>";
+       
+       echo "</td></tr>";
+       echo "</table>";
+       // end read/clone/pull section
+       
+       
+       echo "</td><td bgcolor=\"#ffeeee\">";
+       // write/push section
+       $writein[0] = 0;
+       echo "<table>";
+       echo "<tr><th>Allowed</th><td></td><th>All</th></tr>";
+       echo "<tr><td>";
+       // list allowed users
+       echo "<select name=\"writemembersin[]\" size=\"20\" multiple=\"true\">";
+       $ninwrite = 0;
+       foreach($repoperms as $v_perms) {
+               if($v_perms["type"] == "write") {
+                       $who = $v_perms["ref"]; // now we need to disemble ref
+                       $pid = $v_perms["id"];
+                       $refid = gwvp_DisemableRefId($who);
+                       
+                       $writein[$who] = true;
+                       
+                       echo "<option value=\"$pid\">$refid</option>";
+                       $ninwrite++;
+               }
+       }
+       if($ninwrite == 0) echo "<option>-------------------</option>";
+       echo "</select>";
+       
+       echo "</td><td>";
+       // buttons
+       echo "<input type=\"submit\" name=\"writeadd\" value=\"<<\"><br>";
+       echo "<input type=\"submit\" name=\"writeremove\" value=\">>\"><br>";
+       
+       echo "</td><td>";
+       // list all users/groups/specials
+       echo "<select name=\"writemembersout[]\" size=\"20\" multiple=\"true\">";
+       $noutwrite = 0;
+       // cant have anon for writes if(!isset($writein["anon"])) echo "<option value=\"anon\">Everyone</option>";
+       if(!isset($writein["authed"])) echo "<option value=\"authed\">All Authenticated User</option>";
+       foreach($groups as $u_groups) {
+               $gname = $u_groups["name"];
+               $gid = $u_groups["id"];
+               if(!gwvp_IsGroupAdmin(null, $gid)) if(!isset($writein["group:$gid"])) {
+                       $noutwrite++;
+                       echo "<option value=\"group:$gid\">Group: $gname</option>";
+               }
+       }
+       foreach($users as $u_users) {
+               $uid = $u_users["id"];
+               $email = $u_users["email"];
+               $username = $u_users["username"];
+               $fullname = $u_users["fullname"];
+               if(!gwvp_IsUserAdmin(null, null, $uid)) if(!isset($writein["user:$uid"])) {
+                       $noutwrite++;
+                       echo "<option value=\"user:$uid\">User: $username</option>";
+               }
+       }
+       if($noutwrite == 0) echo "<option>-------------------</option>";
+       echo "</select>";
+       
+       echo "</td></tr>";
+       echo "</table>";
+       // end write/push section
+       
+       echo "</td></tr>";
+       
+       echo "</table>";
+       echo "</form>";
+}
+
+
 ?>
\ No newline at end of file