3 * A base permissions class.
5 * SourceForge: Breaking Down the Barriers to Open Source Development
6 * Copyright 1999-2001 (c) VA Linux Systems
7 * http://sourceforge.net
13 require_once('common/include/Error.class');
15 $PERMISSION_OBJ=array();
18 * permission_get_object() - Get permission objects
20 * permission_get_object is useful so you can pool Permission objects/save database queries
21 * You should always use this instead of instantiating the object directly
23 * @param object The Group in question
24 * @param object The User needing Permission
25 * @return a Permission or false on failure
28 function &permission_get_object($_Group, $_User) {
29 //create a common set of Permission objects
30 //saves a little wear on the database
32 global $PERMISSION_OBJ;
34 if (is_object($_Group)) {
35 $group_id = $_Group->getID();
40 if (is_object($_User)) {
41 $user_id = $_User->getID();
43 //invalid object, probably from user not being logged in
47 if (!isset($PERMISSION_OBJ["_".$group_id."_".$user_id])) {
48 $PERMISSION_OBJ["_".$group_id."_".$user_id]= new Permission($_Group, $_User);
50 return $PERMISSION_OBJ["_".$group_id."_".$user_id];
53 class Permission extends Error {
55 * Associative array of data from db
57 * @var array $data_array
76 * Whether the user is an admin/super user of this project
83 * Whether the user is an admin/super user of the entire site
85 * @var bool $is_site_admin
90 * Constructor for this object
92 * @param object Group Object required
93 * @param object User Object required
96 function Permission (&$_Group, &$_User) {
97 if (!$_Group || !is_object($_Group)) {
98 $this->setError('No Valid Group Object');
101 if ($_Group->isError()) {
102 $this->setError('Permission: '.$_Group->getErrorMessage());
105 $this->Group =& $_Group;
107 if (!$_User || !is_object($_User)) {
108 $this->setError('No Valid User Object');
111 if ($_User->isError()) {
112 $this->setError('Permission: '.$_User->getErrorMessage());
115 $this->User =& $_User;
117 if (!$this->fetchData()) {
125 * fetchData() - fetch the data for this Permission from the database
130 function fetchData() {
131 $res=db_query("SELECT * FROM user_group
132 WHERE user_id='". $this->User->getID() ."'
133 AND group_id='". $this->Group->getID() ."'");
134 if (!$res || db_numrows($res) < 1) {
135 $this->setError('Permission: User Not Found');
137 if ($this->setUpSuperUser()) {
141 $this->data_array =& db_fetch_array($res);
142 if (trim($this->data_array['admin_flags']) == 'A') {
143 $this->is_admin=true;
145 $this->setUpSuperUser();
147 db_free_result($res);
153 * setUpSuperUser() - check to see if this User is a site super-user
158 function setUpSuperUser() {
160 // see if they are a site super-user
161 // if not a member of this group
163 if ($this->isSuperUser()) {
165 $this->is_admin = true;
173 * getUser() - get the User object this Permission is associated with
175 * @return the User object
177 function &getUser() {
182 * getGroup() - get the Group object this ArtifactType is associated with
184 * @return the Group object
186 function &getGroup() {
191 * isSuperUser() - whether the current user has site admin privilege
195 function isSuperUser() {
196 if (isset($this->is_site_admin)) {
197 return $this->is_site_admin;
200 $res = db_query("SELECT * FROM user_group
201 WHERE user_id='". $this->User->getID() ."'
203 AND admin_flags='A'");
205 if (!$res || db_numrows($res) < 1) {
206 $this->is_site_admin = false;
208 $this->is_site_admin = true;
211 db_free_result($res);
212 return $this->is_site_admin;
216 * isForumAdmin() - whether the current user has form admin perms
220 function isForumAdmin() {
221 return $this->isMember('forum_flags',2);
225 * isDocEditor() - whether the current user has form doc editor perms
229 function isDocEditor() {
230 return $this->isMember('doc_flags',1);
234 * isReleaseTechnician() - whether the current user has FRS admin perms
238 function isReleaseTechnician() {
239 return $this->isMember('release_flags',1);
243 * useIsArtifactAdmin() - whether the current user has artifact admin perms
247 function isArtifactAdmin() {
248 return $this->isMember('artifact_flags',2);
251 function isPMAdmin() {
252 return $this->isMember('project_flags',2);
256 * isMember() - Simple test to see if the current user is a member of this project
258 * Can optionally pass in vars to test other permissions
260 * @param string The field to check
261 * @param int The value that $field should have
264 function isMember($field='user_id',$value='-1') {
265 if ($this->isAdmin()) {
266 //admins are tested first so that super-users can return true
267 //and admins of a project should always have full privileges
271 $arr =& $this->getPermData();
272 if ($arr[$field] >= $value) {
281 * userIsAdmin() - User is an admin of the project or admin of the entire site
286 return $this->is_admin;
290 * getPermData() - returns the assocative array from the db
292 * @return associative array
295 function &getPermData() {
296 return $this->data_array;