3 * RoleObserver Class - this class handles the privacy settings
4 * for an entire project
6 * Copyright 2004 (c) GForge LLC
9 * @author Tim Perdue tim@gforge.org
12 * This file is part of GForge.
14 * GForge is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * GForge is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with GForge; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 require_once ('common/include/rbac_texts.php') ;
31 class RoleObserver extends Error {
36 var $role_values=array(
37 'projectpublic'=>array('0','1'),
38 'scmpublic'=>array('0','1'),
39 'forumpublic'=>array('0','1'),
40 'forumanon'=>array('0','1'),
41 'trackerpublic'=>array('0','1'),
42 'trackeranon'=>array('0','1'),
43 'pmpublic'=>array('0','1'),
44 'frspackage'=>array('0','1'));
47 * Role($group,$id) - CONSTRUCTOR.
49 * @param object The Group object.
50 * @param int The role_id.
53 function RoleObserver ($Group) {
55 if (!$Group || !is_object($Group) || $Group->isError()) {
56 $this->setError('Role::'.$Group->getErrorMessage());
59 $this->Group =& $Group;
60 return $this->fetchData();
64 * getID - get the ID of this role.
66 * @return string The ID of the observer.
73 * getName - get the name of this role.
75 * @return string The name of this role.
82 * fetchData - May need to refresh database fields.
84 * If an update occurred and you need to access the updated info.
86 * @return boolean success;
88 function fetchData() {
89 $this->setting_array=array();
91 // Forum is_public/allow_anon
93 $res=db_query("SELECT group_forum_id,is_public,allow_anonymous
95 WHERE group_id='".$this->Group->getID()."'");
96 while ($arr =& db_fetch_array($res)) {
97 $this->setting_array['forumpublic'][$arr['group_forum_id']] = $arr['is_public'];
98 $this->setting_array['forumanon'][$arr['group_forum_id']] = $arr['allow_anonymous'];
102 // Task Manager is_public/allow_anon
104 $res=db_query("SELECT group_project_id,is_public
105 FROM project_group_list
106 WHERE group_id='".$this->Group->getID()."'");
107 while ($arr =& db_fetch_array($res)) {
108 $this->setting_array['pmpublic'][$arr['group_project_id']] = $arr['is_public'];
112 // Tracker is_public/allow_anon
114 $res=db_query("SELECT group_artifact_id,is_public,allow_anon
115 FROM artifact_group_list
116 WHERE group_id='".$this->Group->getID()."'");
117 while ($arr =& db_fetch_array($res)) {
118 $this->setting_array['trackerpublic'][$arr['group_artifact_id']] = $arr['is_public'];
119 $this->setting_array['trackeranon'][$arr['group_artifact_id']] = $arr['allow_anon'];
123 // FRS packages can be public/private now
125 $res=db_query("SELECT package_id,is_public
127 WHERE group_id='".$this->Group->getID()."'");
128 while ($arr =& db_fetch_array($res)) {
129 $this->setting_array['frspackage'][$arr['package_id']] = $arr['is_public'];
135 $this->Group->fetchData( $this->Group->getID() );
136 $this->setting_array['scmpublic'][0]=$this->Group->enableAnonSCM();
137 $this->setting_array['projectpublic'][0]=$this->Group->isPublic();
138 //echo '<html><body><pre>'.print_r($this->setting_array).'</pre>';
144 * &getRoleVals - get all the values and language text strings for this section.
146 * @return array Assoc array of values for this section.
148 function &getRoleVals($section) {
152 // Optimization - save array so it is only built once per page view
154 if (!isset($role_vals[$section])) {
156 for ($i=0; $i<count($this->role_values[$section]); $i++) {
158 // Build an associative array of these key values + localized description
160 $role_vals[$section][$this->role_values[$section][$i]]=$rbac_permission_names["$section".$this->role_values[$section][$i]];
163 return $role_vals[$section];
167 * getVal - get a value out of the array of settings for this role.
169 * @param string The name of the role.
170 * @param integer The ref_id (ex: group_artifact_id, group_forum_id) for this item.
171 * @return integer The value of this item.
173 function getVal($section,$ref_id) {
174 global $role_default_array;
178 if (!isset($this->setting_array) && !isset($this->data_array)) {
179 $this->setting_array=$role_default_array;
181 return $this->setting_array[$section][$ref_id];
185 * update - update a new in the database.
187 * @param array A multi-dimensional array of data in this format: $data['section_name']['
188 * @return boolean True on success or false on failure.
190 function update($data) {
191 $perm =& $this->Group->getPermission( session_get_user() );
192 if (!$perm || !is_object($perm) || $perm->isError() || !$perm->isAdmin()) {
193 $this->setPermissionDeniedError();
199 ////$data['section_name']['ref_id']=$val
200 $arr1 = array_keys($data);
201 for ($i=0; $i<count($arr1); $i++) {
202 $arr2 = array_keys($data[$arr1[$i]]);
203 for ($j=0; $j<count($arr2); $j++) {
204 $usection_name=$arr1[$i];
206 $uvalue=$data[$usection_name][$uref_id];
214 // See if this setting changed. If so, then update it
216 if ($this->getVal($usection_name,$uref_id) != $uvalue) {
217 if ($usection_name == 'scmpublic' ||
218 $usection_name == 'projectpublic') {
219 if (!$data['scmpublic'][0]) {
220 $data['scmpublic'][0]=0;
222 if (!$data['projectpublic'][0]) {
223 $data['projectpublic'][0]=0;
224 // Groups cannot be private and have public SCM
225 // so we should always ensure that the scm is
226 // private if we change a group to private.
227 $data['scmpublic'][0]=0;
231 enable_anonscm='".$data['scmpublic'][0]."',
232 is_public='".$data['projectpublic'][0]."'
233 WHERE group_id='".$this->Group->getID()."'";
236 $this->setError('update::group::'.db_error());
244 } elseif ($usection_name == 'forumpublic' || $usection_name == 'forumanon') {
246 // prevent double-updating each forum
248 if ($updated['forum'][$uref_id]) {
251 $sql="UPDATE forum_group_list
253 is_public='".$data['forumpublic'][$uref_id]."',
254 allow_anonymous='".$data['forumanon'][$uref_id]."'
256 group_forum_id='$uref_id'
257 AND group_id='".$this->Group->getID()."'";
260 $updated['forum'][$uref_id]=1;
262 $this->setError('update::forum::'.db_error());
266 } elseif ($usection_name == 'pmpublic') {
268 $sql="UPDATE project_group_list
272 group_project_id='$uref_id'
273 AND group_id='".$this->Group->getID()."'";
277 $this->setError('update::pm::'.db_error());
282 } elseif ($usection_name == 'frspackage') {
284 $sql="UPDATE frs_package
288 package_id='$uref_id'
289 AND group_id='".$this->Group->getID()."'";
293 $this->setError('update::frspackage::'.db_error());
298 } elseif ($usection_name == 'trackerpublic' || $usection_name == 'trackeranon') {
300 // prevent double-updating each forum
302 if ($updated['tracker'][$uref_id]) {
305 $sql="UPDATE artifact_group_list
307 is_public='".$data['trackerpublic'][$uref_id]."',
308 allow_anon='".$data['trackeranon'][$uref_id]."'
310 group_artifact_id='$uref_id'
311 AND group_id='".$this->Group->getID()."'";
314 $updated['tracker'][$uref_id]=1;
316 $this->setError('update::tracker::'.db_error());
334 // c-file-style: "bsd"