Commit 8a2a9721b5ed07edcab2d3fd3627ef7db75e483b
1 parent
b0d1654b
added FolderAccess class for folder/group permissions
git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@1282 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
2 changed files
with
227 additions
and
0 deletions
lib/foldermanagement/FolderAccess.inc
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * $Id$ | ||
| 4 | + * | ||
| 5 | + * Represents the group_folders_link table in the db used to represent | ||
| 6 | + * folder access | ||
| 7 | + * | ||
| 8 | + * Licensed under the GNU GPL. For full terms see the file COPYING. | ||
| 9 | + * | ||
| 10 | + * @version $Revision$ | ||
| 11 | + * @author Michael Joseph <michael@jamwarehouse.com>, Jam Warehouse (Pty) Ltd, South Africa | ||
| 12 | + * @package lib.foldermanagement | ||
| 13 | + */ | ||
| 14 | +class FolderAccess { | ||
| 15 | + | ||
| 16 | + /** primary key of current object */ | ||
| 17 | + var $iId; | ||
| 18 | + /** primary key of folder */ | ||
| 19 | + var $iFolderID; | ||
| 20 | + /** primary key of group from which user can be assigned */ | ||
| 21 | + var $iGroupID; | ||
| 22 | + /** read access */ | ||
| 23 | + var $bCanRead; | ||
| 24 | + /** write access */ | ||
| 25 | + var $bCanWrite; | ||
| 26 | + | ||
| 27 | + function FolderAccess($iNewFolderID, $iNewGroupID, $iNewCanRead, $iNewCanWrite) { | ||
| 28 | + //object not created in database yet | ||
| 29 | + $this->iId = -1; | ||
| 30 | + $this->iFolderID = $iNewFolderID; | ||
| 31 | + $this->iGroupID = $iNewGroupID; | ||
| 32 | + $this->bCanRead = $iNewCanRead; | ||
| 33 | + $this->bCanWrite = $iNewCanWrite; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + function getID() { | ||
| 37 | + return $this->iId; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + function getFolderID() { | ||
| 41 | + return $this->iFolderID; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + function setFolderID($iNewValue) { | ||
| 45 | + $this->iFolderID = $iNewValue; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + function getGroupID() { | ||
| 49 | + return $this->iGroupID; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + function setGroupID($iNewValue) { | ||
| 53 | + $this->iGroupID = $iNewValue; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + function getCanRead() { | ||
| 57 | + return $this->bCanRead; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + function setCanRead($bNewValue) { | ||
| 61 | + $this->bCanRead = $bNewValue; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + function getCanWrite() { | ||
| 65 | + return $this->bCanWrite; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + function setCanWrite($iNewValue) { | ||
| 69 | + $this->bCanWrite = $iNewValue; | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * Create the current object in the database | ||
| 74 | + * | ||
| 75 | + * @return boolean on successful store, false otherwise and set $_SESSION["errorMessage"] | ||
| 76 | + * | ||
| 77 | + */ | ||
| 78 | + function create() { | ||
| 79 | + global $default, $lang_err_database, $lang_err_object_exists; | ||
| 80 | + //if the object hasn't been created | ||
| 81 | + if ($this->iId < 0) { | ||
| 82 | + $sql = $default->db; | ||
| 83 | + $result = $sql->query("INSERT INTO " . $default->owl_groups_folders_table . " (folder_id, group_id, can_read, can_write) VALUES ($this->iFolderID, $this->iGroupID, " . ($this->bCanRead ? 1 : 0) . ", " . ($this->bCanWrite ? 1 : 0) . ")"); | ||
| 84 | + if ($result) { | ||
| 85 | + $this->iId = $sql->insert_id(); | ||
| 86 | + return true; | ||
| 87 | + } | ||
| 88 | + $_SESSION["errorMessage"] = $lang_err_database; | ||
| 89 | + return false; | ||
| 90 | + } | ||
| 91 | + $_SESSION["errorMessage"] = $lang_err_object_exists . "id = " . $this->iId . " table = $default->owl_groups_folders_table"; | ||
| 92 | + return false; | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + /** | ||
| 96 | + * Update the values in the database table with the object's current values | ||
| 97 | + * | ||
| 98 | + * @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"] | ||
| 99 | + * | ||
| 100 | + */ | ||
| 101 | + function update() { | ||
| 102 | + global $default, $lang_err_database, $lang_err_object_key; | ||
| 103 | + //only update if the object has been stored | ||
| 104 | + if ($this->iId > 0) { | ||
| 105 | + $sql = $default->db; | ||
| 106 | + $result = $sql->query("UPDATE $default->owl_groups_folders_table SET group_id = $this->iGroupID, folder_id = $this->iFolderID, can_read = " . ($this->bCanRead ? 1 : 0) .", can_write = " . ($this->bCanWrite ? 1 : 0) . " WHERE id = $this->iId"); | ||
| 107 | + if ($result) { | ||
| 108 | + return true; | ||
| 109 | + } | ||
| 110 | + $_SESSION["errorMessage"] = $lang_err_database; | ||
| 111 | + return false; | ||
| 112 | + } | ||
| 113 | + $_SESSION["errorMessage"] = $lang_err_object_key; | ||
| 114 | + return false; | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + /** | ||
| 118 | + * Delete the current object from the database | ||
| 119 | + * | ||
| 120 | + * @return boolean true on successful deletion, false otherwise and set $_SESSION["errorMessage"] | ||
| 121 | + * | ||
| 122 | + */ | ||
| 123 | + function delete() { | ||
| 124 | + global $default, $lang_err_database, $lang_err_object_key; | ||
| 125 | + //only delete the object if it exists in the database | ||
| 126 | + if ($this->iId >= 0) { | ||
| 127 | + $sql = $default->db; | ||
| 128 | + $result = $sql->query("DELETE FROM $default->owl_groups_folders_table WHERE id = $this->iId"); | ||
| 129 | + if ($result) { | ||
| 130 | + return true; | ||
| 131 | + } | ||
| 132 | + $_SESSION["errorMessage"] = $lang_err_database; | ||
| 133 | + return false; | ||
| 134 | + } | ||
| 135 | + $_SESSION["errorMessage"] = $lang_err_object_key; | ||
| 136 | + return false; | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + /** | ||
| 140 | + * Static function. | ||
| 141 | + * Given a primary key it will create a FolderAccess object and populate it with the | ||
| 142 | + * corresponding database values | ||
| 143 | + * | ||
| 144 | + * @return FolderAccess populated FolderAccess object on successful query, false otherwise and set $_SESSION["errorMessage"] | ||
| 145 | + */ | ||
| 146 | + function & get($iFolderAccessID) { | ||
| 147 | + global $default; | ||
| 148 | + $sql = $default->db; | ||
| 149 | + $result = $sql->query("SELECT * FROM $default->owl_groups_folders_table WHERE id = $iFolderAccessID"); | ||
| 150 | + if ($result) { | ||
| 151 | + if ($sql->next_record()) { | ||
| 152 | + $oFolderAccess = & new FolderAccess($sql->f("folder_id"), $sql->f("group_id"), $sql->f("can_read"), $sql->f("can_write")); | ||
| 153 | + $oFolderAccess->iId = $iFolderAccessID; | ||
| 154 | + return $oFolderAccess; | ||
| 155 | + } | ||
| 156 | + $_SESSION["errorMessage"] = $lang_err_object_not_exist . "id = " . $iFolderAccessID . " table = $default->owl_groups_folders_table"; | ||
| 157 | + return false; | ||
| 158 | + } | ||
| 159 | + $_SESSION["errorMessage"] = $lang_err_database; | ||
| 160 | + return false; | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + /** | ||
| 164 | + * Static function | ||
| 165 | + * Get a list of folder collaborations | ||
| 166 | + * | ||
| 167 | + * @param String Where clause (not required) | ||
| 168 | + * | ||
| 169 | + * @return Array array of FolderCollaboration objects, false otherwise and set $_SESSION["errorMessage"] | ||
| 170 | + */ | ||
| 171 | + function getList($sWhereClause = null) { | ||
| 172 | + global $default, $lang_err_database; | ||
| 173 | + $oFolderAccessArray = array(); | ||
| 174 | + $sql = $default->db; | ||
| 175 | + $result = $sql->query("SELECT * FROM " . $default->owl_groups_folders_table . (isset($sWhereClause) ? " " . $sWhereClause : "")); | ||
| 176 | + if ($result) { | ||
| 177 | + while ($sql->next_record()) { | ||
| 178 | + $oFolderAccessArray[] = & FolderAccess::get($sql->f("id")); | ||
| 179 | + } | ||
| 180 | + return $oFolderAccessArray; | ||
| 181 | + } | ||
| 182 | + $_SESSION["errorMessage"] = $lang_err_database; | ||
| 183 | + return false; | ||
| 184 | + } | ||
| 185 | +} | ||
| 186 | +?> |
tests/foldermanagement/folderAccess.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +require_once("../../config/dmsDefaults.php"); | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * $Id$ | ||
| 7 | + * | ||
| 8 | + * Unit tests for FolderAccess class found in /lib/foldermanagement/FolderAccess.inc | ||
| 9 | + * | ||
| 10 | + * Licensed under the GNU GPL. For full terms see the file COPYING. | ||
| 11 | + * | ||
| 12 | + * @version $Revision$ | ||
| 13 | + * @author Michael Joseph <michael@jamwarehouse.com>, Jam Warehouse (Pty) Ltd, South Africa | ||
| 14 | + * @package tests.foldermanagement | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +if (checkSession) { | ||
| 18 | + require_once("$default->fileSystemRoot/lib/foldermanagement/FolderAccess.inc"); | ||
| 19 | + echo "<pre>"; | ||
| 20 | + $oFolderAccess = & new FolderAccess(1, 1, true, true); | ||
| 21 | + echo "Create ? " . ($oFolderAccess->create() ? "Yes" : "No") . "<br>"; | ||
| 22 | + $oFolderAccess->delete(); | ||
| 23 | + $oFolderAccess = & new FolderAccess(1, 2, true, false); | ||
| 24 | + $oFolderAccess->create();$oFolderAccess->delete(); | ||
| 25 | + $oFolderAccess = & new FolderAccess(1, 3, false, true); | ||
| 26 | + $oFolderAccess->create();$oFolderAccess->delete(); | ||
| 27 | + $oFolderAccess = & new FolderAccess(1, 4, false, true); | ||
| 28 | + $oFolderAccess->create();$oFolderAccess->delete(); | ||
| 29 | + $oFolderAccess = & new FolderAccess(1, 5, true, true); | ||
| 30 | + $oFolderAccess->create(); | ||
| 31 | + echo "Update ? " . ($oFolderAccess->update() ? "Yes" : "No") . "<br>"; | ||
| 32 | + echo "Delete ? " . ($oFolderAccess->delete() ? "Yes" : "No") . "<br>"; | ||
| 33 | + $oNewFolderAccess = FolderAccess::get(1); | ||
| 34 | + echo "Get ? " . print_r($oNewFolderAccess) . "\n"; | ||
| 35 | + $oNewFolderAccess = FolderAccess::getList(); | ||
| 36 | + echo "GetList ? " . print_r($oNewFolderAccess) . "\n"; | ||
| 37 | + $oNewFolderAccess = FolderAccess::getList("WHERE can_read = 1"); | ||
| 38 | + echo "GetList ? " . print_r($oNewFolderAccess) . "\n"; | ||
| 39 | + echo "</pre>"; | ||
| 40 | +} | ||
| 41 | +?> |