From 98ea64d926a23982b9cfb5b468be87f882268566 Mon Sep 17 00:00:00 2001 From: paulr Date: Tue, 8 Nov 2011 04:51:41 +1100 Subject: [PATCH] added a permissions unit test fixed group create to add owner user as a member --- gwvplib/gwvpdatabase.php | 43 ++++++++++++++++++++- gwvplib/gwvpgitcontrol.php | 41 +++++++++++++++++++- unittests/permissionunittest.php | 79 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 unittests/permissionunittest.php diff --git a/gwvplib/gwvpdatabase.php b/gwvplib/gwvpdatabase.php index 82533cb..22db79c 100644 --- a/gwvplib/gwvpdatabase.php +++ b/gwvplib/gwvpdatabase.php @@ -393,7 +393,15 @@ function gwvp_createGroup($group_name, $group_desc, $is_admin, $owner_id) $conn->query($sql); - + + // add the owner to the group + $gid = gwvp_getGroupId($group_name); + + + error_log("calling addgroupmember with $owner_id, $gid"); + gwvp_addGroupMemberById($owner_id, $gid); + + return true; } function gwvp_deleteGroup($groupname) @@ -685,6 +693,22 @@ function gwvp_addGroupMember($email, $groupname) return true; } +function gwvp_IsGroupMemberById($uid, $gid) +{ + $conn = gwvp_ConnectDB(); + + $sql = "select count(*) from group_membership where groupmember_userid='$uid' and groupmember_groupid='$gid'"; + + $res = $conn->query($sql); + $result = 0; + foreach($res as $u_res) { + $result = $u_res[0]; + } + + if($result == 0) return false; + if($result == 1) return true; +} + function gwvp_IsGroupMember($email, $groupname) { $conn = gwvp_ConnectDB(); @@ -857,6 +881,23 @@ function gwvp_ModifyGroup($groupid, $groupname = null, $group_is_admin = null, $ return true; } +function gwvp_GetRepoId($reponame) +{ + $conn = gwvp_ConnectDB(); + + $sql = "select repos_id from repos where repos_name='$reponame'"; + + $res = $conn->query($sql); + + $return = false; + foreach($res as $u_res) { + $return = $u_res["repos_id"]; + } + + return $return; + +} + function gwvp_GetRepo($rid) { $conn = gwvp_ConnectDB(); diff --git a/gwvplib/gwvpgitcontrol.php b/gwvplib/gwvpgitcontrol.php index 6383f9e..d1b0699 100644 --- a/gwvplib/gwvpgitcontrol.php +++ b/gwvplib/gwvpgitcontrol.php @@ -379,12 +379,49 @@ function gwvp_resolvRepoPerms($userid, $repoid) $usergroups = gwvp_getGroupsForUser(null, $userid); $maxperm = 0; - foreach($repoperms as $perm) { + if($repoperms != false) foreach($repoperms as $perm) { // need to go thru each perm, then check it agains the user we're trying to figure // the perms on + switch($perm["type"]) { + case "read": + $permval = 2; + break; + case "visible": + $permval = 1; + break; + case "write": + $permval = 3; + break; + default: + $permval = 0; + } - + // we only var if permval is greater then current + if($permval > $maxperm) { + //error_log("going into check for $maxperm/$permval, ".$perm["ref"]); + if($perm["ref"] == "anon") { + $maxperm = $permval; + } else if($perm["ref"] == "authed") { + $maxperm = $permval; + } else { + // now we do splits + $spl = explode(":", $perm["ref"]); + $idtype = $spl[0]; + $idval = $spl[1]; + if($idtype == "group") { + // function gwvp_IsGroupMember($email, $groupname) + if(gwvp_IsGroupMemberById($userid, $idval)) $maxperm = $permval; + } else if ($idtype == "user") { + //error_log("checking $userid, $idval"); + if($userid == $idval) $maxperm = $permval; + } + } + } } + + // thats TOTALLY going to work... -_0 we should really write a unit test for this, but thats a bit + // hard given the db req's so for now, we'll leave it as is + return $maxperm; } ?> \ No newline at end of file diff --git a/unittests/permissionunittest.php b/unittests/permissionunittest.php new file mode 100644 index 0000000..ee180bf --- /dev/null +++ b/unittests/permissionunittest.php @@ -0,0 +1,79 @@ + \ No newline at end of file -- 1.7.0.4