3 * FusionForge observer role
5 * Copyright 2004, GForge, LLC
6 * Copyright 2009, Roland Mas
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 require_once $gfcommon.'include/rbac_texts.php' ;
26 class RoleObserver extends Error {
31 var $role_values=array(
32 'projectpublic'=>array('0','1'),
33 'scmpublic'=>array('0','1'),
34 'forumpublic'=>array('0','1'),
35 'forumanon'=>array('0','1'),
36 'trackerpublic'=>array('0','1'),
37 'trackeranon'=>array('0','1'),
38 'pmpublic'=>array('0','1'),
39 'frspackage'=>array('0','1'));
42 * Role($group,$id) - CONSTRUCTOR.
44 * @param object The Group object.
45 * @param int The role_id.
48 function RoleObserver ($Group) {
50 if (!$Group || !is_object($Group) || $Group->isError()) {
51 $this->setError('Role::'.$Group->getErrorMessage());
54 $this->Group =& $Group;
55 return $this->fetchData();
59 * getID - get the ID of this role.
61 * @return string The ID of the observer.
68 * getName - get the name of this role.
70 * @return string The name of this role.
77 * fetchData - May need to refresh database fields.
79 * If an update occurred and you need to access the updated info.
81 * @return boolean success;
83 function fetchData() {
84 $this->setting_array=array();
86 // Forum is_public/allow_anon
88 $res = db_query_params ('SELECT group_forum_id,is_public,allow_anonymous FROM forum_group_list WHERE group_id=$1',
89 array ($this->Group->getID())) ;
90 while ($arr = db_fetch_array($res)) {
91 $this->setting_array['forumpublic'][$arr['group_forum_id']] = $arr['is_public'];
92 $this->setting_array['forumanon'][$arr['group_forum_id']] = $arr['allow_anonymous'];
96 // Tasks is_public/allow_anon
98 $res = db_query_params ('SELECT group_project_id,is_public FROM project_group_list WHERE group_id=$1',
99 array ($this->Group->getID())) ;
100 while ($arr = db_fetch_array($res)) {
101 $this->setting_array['pmpublic'][$arr['group_project_id']] = $arr['is_public'];
105 // Tracker is_public/allow_anon
107 $res = db_query_params ('SELECT group_artifact_id,is_public,allow_anon FROM artifact_group_list WHERE group_id=$1',
108 array ($this->Group->getID())) ;
109 while ($arr = db_fetch_array($res)) {
110 $this->setting_array['trackerpublic'][$arr['group_artifact_id']] = $arr['is_public'];
111 $this->setting_array['trackeranon'][$arr['group_artifact_id']] = $arr['allow_anon'];
115 // FRS packages can be public/private now
117 $res = db_query_params ('SELECT package_id,is_public FROM frs_package WHERE group_id=$1',
118 array ($this->Group->getID())) ;
119 while ($arr = db_fetch_array($res)) {
120 $this->setting_array['frspackage'][$arr['package_id']] = $arr['is_public'];
126 $this->Group->fetchData( $this->Group->getID() );
127 $this->setting_array['scmpublic'][0]=$this->Group->enableAnonSCM();
128 $this->setting_array['projectpublic'][0]=$this->Group->isPublic();
129 //echo '<html><body><pre>'.print_r($this->setting_array).'</pre>';
135 * &getRoleVals - get all the values and language text strings for this section.
137 * @return array Assoc array of values for this section.
139 function &getRoleVals($section) {
140 global $role_vals, $rbac_permission_names;
141 setup_rbac_strings () ;
144 // Optimization - save array so it is only built once per page view
146 if (!isset($role_vals[$section])) {
148 for ($i=0; $i<count($this->role_values[$section]); $i++) {
150 // Build an associative array of these key values + localized description
152 $role_vals[$section][$this->role_values[$section][$i]]=$rbac_permission_names["$section".$this->role_values[$section][$i]];
155 return $role_vals[$section];
159 * getVal - get a value out of the array of settings for this role.
161 * @param string The name of the role.
162 * @param integer The ref_id (ex: group_artifact_id, group_forum_id) for this item.
163 * @return integer The value of this item.
165 function getVal($section,$ref_id) {
166 global $role_default_array;
170 if (!isset($this->setting_array) && !isset($this->data_array)) {
171 $this->setting_array=$role_default_array;
173 return $this->setting_array[$section][$ref_id];
177 * update - update a new in the database.
179 * @param array A multi-dimensional array of data in this format: $data['section_name']['
180 * @return boolean True on success or false on failure.
182 function update($data) {
183 if (!forge_check_perm ('project_admin', $this->Group->getID())) {
184 $this->setPermissionDeniedError();
190 ////$data['section_name']['ref_id']=$val
191 $arr1 = array_keys($data);
192 for ($i=0; $i<count($arr1); $i++) {
193 $arr2 = array_keys($data[$arr1[$i]]);
194 for ($j=0; $j<count($arr2); $j++) {
195 $usection_name=$arr1[$i];
197 $uvalue=$data[$usection_name][$uref_id];
205 // See if this setting changed. If so, then update it
207 if ($this->getVal($usection_name,$uref_id) != $uvalue) {
208 if ($usection_name == 'scmpublic' ||
209 $usection_name == 'projectpublic') {
210 if (!$data['scmpublic'][0]) {
211 $data['scmpublic'][0]=0;
213 if (!$data['projectpublic'][0]) {
214 $data['projectpublic'][0]=0;
215 // Groups cannot be private and have public SCM
216 // so we should always ensure that the scm is
217 // private if we change a group to private.
218 $data['scmpublic'][0]=0;
220 $res = db_query_params ('UPDATE groups SET enable_anonscm=$1, is_public=$2 WHERE group_id=$3',
221 array ($data['scmpublic'][0],
222 $data['projectpublic'][0],
223 $this->Group->getID())) ;
225 $this->setError('update::group::'.db_error());
233 } elseif ($usection_name == 'forumpublic' || $usection_name == 'forumanon') {
235 // prevent double-updating each forum
237 if ($updated['forum'][$uref_id]) {
240 $res = db_query_params ('UPDATE forum_group_list SET is_public=$1, allow_anonymous=$2 WHERE group_forum_id=$3 AND group_id=$4',
241 array ($data['forumpublic'][$uref_id],
242 $data['forumanon'][$uref_id],
244 $this->Group->getID())) ;
245 $updated['forum'][$uref_id]=1;
247 $this->setError('update::forum::'.db_error());
251 } elseif ($usection_name == 'pmpublic') {
252 $res = db_query_params ('UPDATE project_group_list SET is_public=$1 WHERE group_project_id=$2 AND group_id=$3',
255 $this->Group->getID())) ;
257 $this->setError('update::pm::'.db_error());
262 } elseif ($usection_name == 'frspackage') {
263 $res = db_query_params ('UPDATE frs_package SET is_public=$1 WHERE package_id=$2 AND group_id=$3',
266 $this->Group->getID())) ;
268 $this->setError('update::frspackage::'.db_error());
273 } elseif ($usection_name == 'trackerpublic' || $usection_name == 'trackeranon') {
275 // prevent double-updating each forum
277 if ($updated['tracker'][$uref_id]) {
280 $res = db_query_params ('UPDATE artifact_group_list SET is_public=$1, allow_anon=$2 WHERE group_artifact_id=$3 AND group_id=$4',
281 array ($data['trackerpublic'][$uref_id],
282 $data['trackeranon'][$uref_id],
284 $this->Group->getID())) ;
285 $updated['tracker'][$uref_id]=1;
287 $this->setError('update::tracker::'.db_error());
305 // c-file-style: "bsd"