1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
<?php
/**
* A base permissions class.
*
* Portions Copyright 1999-2001 (c) VA Linux Systems
* The rest Copyright 2002-2004 (c) GForge Team
* http://gforge.org/
*
* @version $Id: Permission.class 4994 2005-11-25 15:53:25Z danper $
*
* This file is part of GForge.
*
* GForge is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GForge is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GForge; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
require_once('common/include/Error.class');
$PERMISSION_OBJ=array();
/**
* permission_get_object() - Get permission objects
*
* permission_get_object is useful so you can pool Permission objects/save database queries
* You should always use this instead of instantiating the object directly
*
* @param object The Group in question
* @param object The User needing Permission
* @return a Permission or false on failure
*
*/
function &permission_get_object(&$_Group, &$_User) {
//create a common set of Permission objects
//saves a little wear on the database
global $PERMISSION_OBJ;
if (is_object($_Group)) {
$group_id = $_Group->getID();
} else {
$group_id = 0;
}
if (is_object($_User)) {
$user_id = $_User->getID();
} else {
//invalid object, probably from user not being logged in
$user_id = 0;
}
if (!isset($PERMISSION_OBJ["_".$group_id."_".$user_id])) {
$PERMISSION_OBJ["_".$group_id."_".$user_id]= new Permission($_Group, $_User);
}
return $PERMISSION_OBJ["_".$group_id."_".$user_id];
}
class Permission extends Error {
/**
* Associative array of data from db.
*
* @var array $data_array.
*/
var $data_array;
/**
* The Group object.
*
* @var object $Group.
*/
var $Group;
/**
* The User object.
*
* @var object $User.
*/
var $User;
/**
* Whether the user is an admin/super user of this project.
*
* @var bool $is_admin.
*/
var $is_admin=false;
/**
* Whether the user is an admin/super user of the entire site.
*
* @var bool $is_site_admin.
*/
var $is_site_admin;
/**
* Constructor for this object.
*
* @param object Group Object required.
* @param object User Object required.
*
*/
function Permission (&$_Group, &$_User) {
if (!$_Group || !is_object($_Group)) {
$this->setError('No Valid Group Object');
return false;
}
if ($_Group->isError()) {
$this->setError('Permission: '.$_Group->getErrorMessage());
return false;
}
$this->Group =& $_Group;
if (!$_User || !is_object($_User)) {
$this->setError('No Valid User Object');
return false;
}
if ($_User->isError()) {
$this->setError('Permission: '.$_User->getErrorMessage());
return false;
}
$this->User =& $_User;
if (!$this->fetchData()) {
return false;
} else {
return true;
}
}
/**
* fetchData - fetch the data for this Permission from the database.
*
* @return boolean success.
* @access private.
*/
function fetchData() {
$res=db_query("SELECT * FROM user_group
WHERE user_id='". $this->User->getID() ."'
AND group_id='". $this->Group->getID() ."'");
if (!$res || db_numrows($res) < 1) {
$this->setError('Permission: User Not Found');
if ($this->setUpSuperUser()) {
return true;
}
} else {
$this->data_array =& db_fetch_array($res);
if (trim($this->data_array['admin_flags']) == 'A') {
$this->is_admin=true;
} else {
$this->setUpSuperUser();
}
db_free_result($res);
return true;
}
}
/**
* setUpSuperUser - check to see if this User is a site super-user.
*
* @return boolean is_super_user.
* @access private
*/
function setUpSuperUser() {
//
// see if they are a site super-user
// if not a member of this group
//
if ($this->isSuperUser()) {
$this->clearError();
$this->is_admin = true;
return true;
}
return false;
}
/**
* getUser - get the User object this Permission is associated with.
*
* @return object The User object.
*/
function &getUser() {
return $this->User;
}
/**
* getGroup - get the Group object this Permission is associated with.
*
* @return the Group object.
*/
function &getGroup() {
return $this->Group;
}
/**
* isSuperUser - whether the current user has site admin privilege.
*
* @return boolean is_super_user.
*/
function isSuperUser() {
if (isset($this->is_site_admin)) {
return $this->is_site_admin;
}
$res = db_query("SELECT count(*) FROM user_group
WHERE user_id='". $this->User->getID() ."'
AND group_id='1'
AND admin_flags='A'");
$row_count = db_fetch_array($res);
$this->is_site_admin = $res && $row_count['count'] > 0;
db_free_result($res);
return $this->is_site_admin;
}
/**
* isForumAdmin - whether the current user has form admin perms.
*
* @return boolean is_forum_admin.
*/
function isForumAdmin() {
return $this->isMember('forum_flags',2);
}
/**
* isDocEditor - whether the current user has form doc editor perms.
*
* @return boolean is_doc_editor.
*/
function isDocEditor() {
return $this->isMember('doc_flags',1);
}
/**
* isReleaseTechnician - whether the current user has FRS admin perms.
*
* @return boolean is_release_technician.
*/
function isReleaseTechnician() {
return $this->isMember('release_flags',1);
}
/**
* isArtifactAdmin - whether the current user has artifact admin perms.
*
* @return boolean is_artifact_admin.
*/
function isArtifactAdmin() {
return $this->isMember('artifact_flags',2);
}
/**
* isPMAdmin - whether the current user has Task Manager admin perms.
*
* @return boolean is_projman_admin.
*/
function isPMAdmin() {
return $this->isMember('project_flags',2);
}
/**
* isMember - Simple test to see if the current user is a member of this project.
*
* Can optionally pass in vars to test other permissions.
*
* @param string The field to check.
* @param int The value that $field should have.
* @return boolean is_member.
*/
function isMember($field='user_id',$value='-1') {
if ($this->isAdmin()) {
//admins are tested first so that super-users can return true
//and admins of a project should always have full privileges
//on their project
return true;
} else {
$arr =& $this->getPermData();
if ($arr[$field] >= $value) {
return true;
} else {
return false;
}
}
}
/**
* isAdmin - User is an admin of the project or admin of the entire site.
*
* @return boolean is_admin.
*/
function isAdmin() {
return $this->is_admin;
}
/**
* getPermData - returns the assocative array from the db.
*
* @return array The array of data.
* @access private.
*/
function &getPermData() {
return $this->data_array;
}
/**
* isCVSReader - checks the cvs_flags field in user_group table.
*
* @return boolean cvs_flags
*/
function isCVSReader() {
return $this->isMember('cvs_flags',0);
}
/**
* isCVSWriter - checks if the user has CVS write access.
*
* @return boolean cvs_flags
*/
function isCVSWriter() {
return $this->isMember('cvs_flags',1);
}
}
?>
|