repo permissions management complete (though pretty ugly code)
authorPaul J R <me@pjr.cc>
Fri, 21 Sep 2012 01:30:13 +0000 (11:30 +1000)
committerPaul J R <me@pjr.cc>
Fri, 21 Sep 2012 01:30:13 +0000 (11:30 +1000)
gwvpmini/gwvpmini_db.php
gwvpmini/gwvpmini_view.php

index 63b8f84..c3ac22b 100644 (file)
@@ -122,7 +122,11 @@ function gwvpmini_ChangeRepoPerm($rid, $uid, $acc)
                $permsarray[$uid] = $acc;
        } else {
                $permsarray = unserialize(base64_decode($cperms_t));
-               $permsarray[$uid] = $acc; 
+               $permsarray[$uid] = $acc;
+               if($acc == 0) {
+                       error_log("PERMSUPDATE: REMOVE $uid");
+                       unset($permsarray[$uid]);
+               }
        }
        
        // check if base is now r or a, we can drop any 1's
@@ -155,15 +159,11 @@ function gwvpmini_GetRepoPerms($rid)
                $cperms_t = $row[0];\r
        }\r
        \r
-       if($cperms_t === false) return 0;\r
-       \r
-       error_log("PERMSCHECK $rid, $uid:".print_r($dets, true));\r
-       \r
-       if($dets === false) return 0;\r
-       \r
-       if($dets["ownerid"] == $uid) return 2;\r
+       if($cperms_t === false) return false;\r
        \r
        $permsarray = unserialize(base64_decode($cperms_t));
+       
+       return $permsarray;
 }
 
 //returns 0 for none, 1 for read, 2 for writes
index 5862068..d1b7eaa 100644 (file)
@@ -24,6 +24,18 @@ function gwvpmini_RepoViewCallMe()
                        if($qspl[0] == "updaterepobaseperms") {
                                return "gwvpmini_UpdateRepoBasePerms";
                        } 
+                       if($qspl[0] == "repoaddreader") {
+                               return "gwvpmini_AddRepoReader";
+                       }
+                       if($qspl[0] == "repoaddcontrib") {
+                               return "gwvpmini_AddRepoContributor";
+                       }
+                       if($qspl[0] == "reporemovereaders") {
+                               return "gwvpmini_RemoveRepoReader";
+                       }
+                       if($qspl[0] == "reporemovecontribs") {
+                               return "gwvpmini_RemoveRepoContributor";
+                       }
                        return false;\r
                }\r
                else return false;\r
@@ -80,7 +92,9 @@ function gwvpmini_RepoViewPageBody()
        echo "<b>$desc</b><br>";
        
        if($owner_view) {
-               $bperms = gwvpmini_GetRepoPerm(gwvpmini_GetRepoId($repo_view_call), "b");
+               $bperms_f = gwvpmini_GetRepoPerms(gwvpmini_GetRepoId($repo_view_call));
+               
+               $bperms = $bperms_f["b"];
                
                $anyo = "";
                $regd = "";
@@ -104,13 +118,36 @@ function gwvpmini_RepoViewPageBody()
                echo "<input type=\"submit\" name=\"Set\" value=\"Set\">";
                echo "</form>";
                if($bperms == "x") {
-                       echo "</td><td>Readers<br>";
+                       echo "</td><td><b>Readers</b><br>";
+                       echo "<form method=\"post\" action=\"$BASE_URL/reporemovereaders/$repo_view_call\">";
+                       $nl = 0;
+                       foreach($bperms_f as $key=>$val) {
+                               if($val == 1) {
+                                       $dets = gwvpmini_getUser(null, null, $key);
+                                       echo get_gravatar($dets["email"], 18, 'mm', 'g', true)." <input type=\"checkbox\" name=\"$key\"> ".$dets["username"]."<br>";
+                                       $nl = 1;
+                               }
+                       }
+                       if($nl==1) echo "<input type=\"submit\" name=\"remove\" value=\"Remove Selected\">";
+                       echo "</form>";
                        echo "<form method=\"post\" action=\"$BASE_URL/repoaddreader/$repo_view_call\">";
                        echo "<input type=\"text\" name=\"readerusername\"> <input type=\"submit\" name=\"Add\" value=\"Add\">";
                        echo "</form><br>";
                }
                
-               echo "</td><td>Contributors";
+               echo "</td><td><b>Contributors</b><br>";
+               echo "<form method=\"post\" action=\"$BASE_URL/reporemovecontribs/$repo_view_call\">";
+               $nl = 0;
+               foreach($bperms_f as $key=>$val) {\r
+                       if($val == 2) {\r
+                               $dets = gwvpmini_getUser(null, null, $key);\r
+                               echo get_gravatar($dets["email"], 18, 'mm', 'g', true)." <input type=\"checkbox\" name=\"$key\"> ".$dets["username"]."<br>";
+                               $nl = 1;\r
+                       }\r
+               }\r
+               if($nl==1) echo "<input type=\"submit\" name=\"remove\" value=\"Remove Selected\">";\r
+               echo "</form>";
+               
                echo "<form method=\"post\" action=\"$BASE_URL/repoaddcontrib/$repo_view_call\">";
                echo "<input type=\"text\" name=\"contribusername\"> <input type=\"submit\" name=\"Add\" value=\"Add\">";
                echo "</form><br>";
@@ -202,6 +239,241 @@ function gwvpmini_UpdateRepoBasePerms()
        }\r
        
        header("Location: $BASE_URL/view/$repo_view_call");
+}
+
+function gwvpmini_AddRepoReader()
+{
+       global $BASE_URL, $repo_view_call;\r
+       \r
+       if(isset($_REQUEST["q"])) {\r
+               $query = $_REQUEST["q"];\r
+               $qspl = explode("/", $query);\r
+               error_log("PLOOP:qview".print_r($qspl, true));\r
+       }\r
+       \r
+       if(isset($qspl[1])) $repo_view_call = $qspl[1];\r
+       else {\r
+               error_log("PLOOP: no repo name");
+               // TODO: btw, this makes no sense\r
+               header("Location: $BASE_URL/view/$repo_view_call");\r
+               return;\r
+       }\r
+       \r
+       $newperms = $_REQUEST["base_perms"];\r
+       \r
+       $owner = gwvpmini_GetRepoOwnerDetailsFromName($repo_view_call);\r
+       $desc = gwvpmini_GetRepoDescFromName($repo_view_call);\r
+       \r
+       $owner_name = $owner["username"];\r
+       \r
+       $owner_view = false;\r
+       if(isset($_SESSION["id"])) {\r
+               if($owner["id"] == $_SESSION["id"]) {\r
+                       $owner_view = true;\r
+               }\r
+       }
+       if(!$owner_view) {
+               gwvpmini_SendMessage("error", "failure updating permission for repo");\r
+               error_log("PLOOP: attempt to update from non-owner");
+               header("Location: $BASE_URL/view/$repo_view_call");
+               return;
+       }\r
+       
+       $auid = gwvpmini_GetUserId($_REQUEST["readerusername"]);
+       
+       if($auid == $_SESSION["id"]) {
+               gwvpmini_SendMessage("error", "You cannot add yourself as a reader as you already own the repo");
+               header("Location: $BASE_URL/view/$repo_view_call");\r
+               return;
+       }
+       
+       if($auid > 0)  {\r
+               $rid = gwvpmini_GetRepoId($repo_view_call);
+               
+               gwvpmini_ChangeRepoPerm($rid, $auid, 1);
+               gwvpmini_SendMessage("info", "Added user ".$_REQUEST["readerusername"]." as a reader");
+               header("Location: $BASE_URL/view/$repo_view_call");\r
+               return;
+       } else {
+               gwvpmini_SendMessage("error", "Couldnt find user with username of ".$_REQUEST["readerusername"]);
+               header("Location: $BASE_URL/view/$repo_view_call");
+               return;
+       }
+       
+       
+}
+
+function gwvpmini_AddRepoContributor()
+{
+       global $BASE_URL, $repo_view_call;\r
+       \r
+       if(isset($_REQUEST["q"])) {\r
+               $query = $_REQUEST["q"];\r
+               $qspl = explode("/", $query);\r
+               error_log("PLOOP:qview".print_r($qspl, true));\r
+       }\r
+       \r
+       if(isset($qspl[1])) $repo_view_call = $qspl[1];\r
+       else {\r
+               error_log("PLOOP: no repo name");\r
+               // TODO: btw, this makes no sense\r
+               header("Location: $BASE_URL/view/$repo_view_call");\r
+               return;\r
+       }\r
+       \r
+       $newperms = $_REQUEST["base_perms"];\r
+       \r
+       $owner = gwvpmini_GetRepoOwnerDetailsFromName($repo_view_call);\r
+       $desc = gwvpmini_GetRepoDescFromName($repo_view_call);\r
+       \r
+       $owner_name = $owner["username"];\r
+       \r
+       $owner_view = false;\r
+       if(isset($_SESSION["id"])) {\r
+               if($owner["id"] == $_SESSION["id"]) {\r
+                       $owner_view = true;\r
+               }\r
+       }\r
+       if(!$owner_view) {\r
+               gwvpmini_SendMessage("error", "failure updating permission for repo");\r
+               error_log("PLOOP: attempt to update from non-owner");\r
+               header("Location: $BASE_URL/view/$repo_view_call");\r
+               return;\r
+       }\r
+       \r
+       $auid = gwvpmini_GetUserId($_REQUEST["contribusername"]);\r
+       \r
+       if($auid == $_SESSION["id"]) {\r
+               gwvpmini_SendMessage("error", "You cannot add yourself as a contributor as you already own the repo");\r
+               header("Location: $BASE_URL/view/$repo_view_call");\r
+               return;\r
+       }\r
+       \r
+       if($auid > 0)  {\r
+               $rid = gwvpmini_GetRepoId($repo_view_call);\r
+       \r
+               gwvpmini_ChangeRepoPerm($rid, $auid, 2);\r
+               gwvpmini_SendMessage("info", "Added user ".$_REQUEST["contribusername"]." as a contributor");\r
+               header("Location: $BASE_URL/view/$repo_view_call");\r
+               return;\r
+       } else {\r
+               gwvpmini_SendMessage("error", "Couldnt find user with username of ".$_REQUEST["contribusername"]);\r
+               header("Location: $BASE_URL/view/$repo_view_call");\r
+               return;\r
+       }
+}
+
+function gwvpmini_RemoveRepoContributor()
+{
+       
+       global $BASE_URL, $repo_view_call;\r
+       \r
+       if(isset($_REQUEST["q"])) {\r
+               $query = $_REQUEST["q"];\r
+               $qspl = explode("/", $query);\r
+               error_log("PLOOP:qview".print_r($qspl, true));\r
+       }\r
+       \r
+       if(isset($qspl[1])) $repo_view_call = $qspl[1];\r
+       else {\r
+               error_log("PLOOP: no repo name");\r
+               // TODO: btw, this makes no sense\r
+               header("Location: $BASE_URL/view/$repo_view_call");\r
+               return;\r
+       }\r
+       \r
+       \r
+       $owner = gwvpmini_GetRepoOwnerDetailsFromName($repo_view_call);\r
+       $desc = gwvpmini_GetRepoDescFromName($repo_view_call);\r
+       \r
+       $owner_name = $owner["username"];\r
+       \r
+       $owner_view = false;\r
+       if(isset($_SESSION["id"])) {\r
+               if($owner["id"] == $_SESSION["id"]) {\r
+                       $owner_view = true;\r
+               }\r
+       }\r
+       if(!$owner_view) {\r
+               gwvpmini_SendMessage("error", "failure updating permission for repo");\r
+               error_log("PLOOP: attempt to update from non-owner");\r
+               header("Location: $BASE_URL/view/$repo_view_call");\r
+               return;\r
+       }
+       
+       $rid = gwvpmini_GetRepoId($repo_view_call);
+       
+       $bperms_f = gwvpmini_GetRepoPerms($rid);
+       
+       foreach($bperms_f as $key=>$val) {
+               if($val == 2) {
+                       if(isset($_REQUEST["$key"])) {
+                               gwvpmini_ChangeRepoPerm($rid, $key, 0);
+                       }
+               }
+       }
+       
+       gwvpmini_SendMessage("info", "Repo permissions updated");
+       header("Location: $BASE_URL/view/$repo_view_call");\r
+       return;\r
+       
+}\r
+
+\r
+function gwvpmini_RemoveRepoReader()\r
+{\r
+\r
+       global $BASE_URL, $repo_view_call;\r
+\r
+       if(isset($_REQUEST["q"])) {\r
+               $query = $_REQUEST["q"];\r
+               $qspl = explode("/", $query);\r
+               error_log("PLOOP:qview".print_r($qspl, true));\r
+       }\r
+\r
+       if(isset($qspl[1])) $repo_view_call = $qspl[1];\r
+       else {\r
+               error_log("PLOOP: no repo name");\r
+               // TODO: btw, this makes no sense\r
+               header("Location: $BASE_URL/view/$repo_view_call");\r
+               return;\r
+       }\r
+\r
+\r
+       $owner = gwvpmini_GetRepoOwnerDetailsFromName($repo_view_call);\r
+       $desc = gwvpmini_GetRepoDescFromName($repo_view_call);\r
+\r
+       $owner_name = $owner["username"];\r
+\r
+       $owner_view = false;\r
+       if(isset($_SESSION["id"])) {\r
+               if($owner["id"] == $_SESSION["id"]) {\r
+                       $owner_view = true;\r
+               }\r
+       }\r
+       if(!$owner_view) {\r
+               gwvpmini_SendMessage("error", "failure updating permission for repo");\r
+               error_log("PLOOP: attempt to update from non-owner");\r
+               header("Location: $BASE_URL/view/$repo_view_call");\r
+               return;\r
+       }\r
+\r
+       $rid = gwvpmini_GetRepoId($repo_view_call);\r
+\r
+       $bperms_f = gwvpmini_GetRepoPerms($rid);\r
+\r
+       foreach($bperms_f as $key=>$val) {\r
+               if($val == 1) {\r
+                       if(isset($_REQUEST["$key"])) {\r
+                               gwvpmini_ChangeRepoPerm($rid, $key, 0);\r
+                       }\r
+               }\r
+       }\r
+\r
+       gwvpmini_SendMessage("info", "Repo permissions updated");\r
+       header("Location: $BASE_URL/view/$repo_view_call");\r
+       return;\r
+\r
 }\r
 
 ?>
\ No newline at end of file