3 * FusionForge observer role
5 * Copyright 2004, GForge, LLC
6 * Copyright 2009, Roland Mas
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with FusionForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 require_once $gfcommon.'include/rbac_texts.php' ;
28 class RoleObserver extends Error {
33 var $role_values=array(
34 'projectpublic'=>array('0','1'),
35 'scmpublic'=>array('0','1'),
36 'forumpublic'=>array('0','1'),
37 'forumanon'=>array('0','1'),
38 'trackerpublic'=>array('0','1'),
39 'trackeranon'=>array('0','1'),
40 'pmpublic'=>array('0','1'),
41 'frspackage'=>array('0','1'));
44 * Role($group,$id) - CONSTRUCTOR.
46 * @param object The Group object.
47 * @param int The role_id.
50 function RoleObserver ($Group) {
52 if (!$Group || !is_object($Group) || $Group->isError()) {
53 $this->setError('Role::'.$Group->getErrorMessage());
56 $this->Group =& $Group;
57 return $this->fetchData();
61 * getID - get the ID of this role.
63 * @return string The ID of the observer.
70 * getName - get the name of this role.
72 * @return string The name of this role.
79 * fetchData - May need to refresh database fields.
81 * If an update occurred and you need to access the updated info.
83 * @return boolean success;
85 function fetchData() {
86 $this->setting_array=array();
88 // Forum is_public/allow_anon
90 $res = db_query_params ('SELECT group_forum_id,is_public,allow_anonymous FROM forum_group_list WHERE group_id=$1',
91 array ($this->Group->getID())) ;
92 while ($arr =& db_fetch_array($res)) {
93 $this->setting_array['forumpublic'][$arr['group_forum_id']] = $arr['is_public'];
94 $this->setting_array['forumanon'][$arr['group_forum_id']] = $arr['allow_anonymous'];
98 // Task Manager is_public/allow_anon
100 $res = db_query_params ('SELECT group_project_id,is_public FROM project_group_list WHERE group_id=$1',
101 array ($this->Group->getID())) ;
102 while ($arr =& db_fetch_array($res)) {
103 $this->setting_array['pmpublic'][$arr['group_project_id']] = $arr['is_public'];
107 // Tracker is_public/allow_anon
109 $res = db_query_params ('SELECT group_artifact_id,is_public,allow_anon FROM artifact_group_list WHERE group_id=$1',
110 array ($this->Group->getID())) ;
111 while ($arr =& db_fetch_array($res)) {
112 $this->setting_array['trackerpublic'][$arr['group_artifact_id']] = $arr['is_public'];
113 $this->setting_array['trackeranon'][$arr['group_artifact_id']] = $arr['allow_anon'];
117 // FRS packages can be public/private now
119 $res = db_query_params ('SELECT package_id,is_public FROM frs_package WHERE group_id=$1',
120 array ($this->Group->getID())) ;
121 while ($arr =& db_fetch_array($res)) {
122 $this->setting_array['frspackage'][$arr['package_id']] = $arr['is_public'];
128 $this->Group->fetchData( $this->Group->getID() );
129 $this->setting_array['scmpublic'][0]=$this->Group->enableAnonSCM();
130 $this->setting_array['projectpublic'][0]=$this->Group->isPublic();
131 //echo '<html><body><pre>'.print_r($this->setting_array).'</pre>';
137 * &getRoleVals - get all the values and language text strings for this section.
139 * @return array Assoc array of values for this section.
141 function &getRoleVals($section) {
142 global $role_vals, $rbac_permission_names;
145 // Optimization - save array so it is only built once per page view
147 if (!isset($role_vals[$section])) {
149 for ($i=0; $i<count($this->role_values[$section]); $i++) {
151 // Build an associative array of these key values + localized description
153 $role_vals[$section][$this->role_values[$section][$i]]=$rbac_permission_names["$section".$this->role_values[$section][$i]];
156 return $role_vals[$section];
160 * getVal - get a value out of the array of settings for this role.
162 * @param string The name of the role.
163 * @param integer The ref_id (ex: group_artifact_id, group_forum_id) for this item.
164 * @return integer The value of this item.
166 function getVal($section,$ref_id) {
167 global $role_default_array;
171 if (!isset($this->setting_array) && !isset($this->data_array)) {
172 $this->setting_array=$role_default_array;
174 return $this->setting_array[$section][$ref_id];
178 * update - update a new in the database.
180 * @param array A multi-dimensional array of data in this format: $data['section_name']['
181 * @return boolean True on success or false on failure.
183 function update($data) {
184 $perm =& $this->Group->getPermission( session_get_user() );
185 if (!$perm || !is_object($perm) || $perm->isError() || !$perm->isAdmin()) {
186 $this->setPermissionDeniedError();
192 ////$data['section_name']['ref_id']=$val
193 $arr1 = array_keys($data);
194 for ($i=0; $i<count($arr1); $i++) {
195 $arr2 = array_keys($data[$arr1[$i]]);
196 for ($j=0; $j<count($arr2); $j++) {
197 $usection_name=$arr1[$i];
199 $uvalue=$data[$usection_name][$uref_id];
207 // See if this setting changed. If so, then update it
209 if ($this->getVal($usection_name,$uref_id) != $uvalue) {
210 if ($usection_name == 'scmpublic' ||
211 $usection_name == 'projectpublic') {
212 if (!$data['scmpublic'][0]) {
213 $data['scmpublic'][0]=0;
215 if (!$data['projectpublic'][0]) {
216 $data['projectpublic'][0]=0;
217 // Groups cannot be private and have public SCM
218 // so we should always ensure that the scm is
219 // private if we change a group to private.
220 $data['scmpublic'][0]=0;
222 $res = db_query_params ('UPDATE groups SET enable_anonscm=$1, is_public=$2 WHERE group_id=$3',
223 array ($data['scmpublic'][0],
224 $data['projectpublic'][0],
225 $this->Group->getID())) ;
227 $this->setError('update::group::'.db_error());
235 } elseif ($usection_name == 'forumpublic' || $usection_name == 'forumanon') {
237 // prevent double-updating each forum
239 if ($updated['forum'][$uref_id]) {
242 $res = db_query_params ('UPDATE forum_group_list SET is_public=$1, allow_anonymous=$2 WHERE group_forum_id=$3 AND group_id=$4',
243 array ($data['forumpublic'][$uref_id],
244 $data['forumanon'][$uref_id],
246 $this->Group->getID())) ;
247 $updated['forum'][$uref_id]=1;
249 $this->setError('update::forum::'.db_error());
253 } elseif ($usection_name == 'pmpublic') {
254 $res = db_query_params ('UPDATE project_group_list SET is_public=$1 WHERE group_project_id=$2 AND group_id=$3',
257 $this->Group->getID())) ;
259 $this->setError('update::pm::'.db_error());
264 } elseif ($usection_name == 'frspackage') {
265 $res = db_query_params ('UPDATE frs_package SET is_public=$1 WHERE package_id=$2 AND group_id=$3',
268 $this->Group->getID())) ;
270 $this->setError('update::frspackage::'.db_error());
275 } elseif ($usection_name == 'trackerpublic' || $usection_name == 'trackeranon') {
277 // prevent double-updating each forum
279 if ($updated['tracker'][$uref_id]) {
282 $res = db_query_params ('UPDATE artifact_group_list SET is_public=$1, allow_anon=$2 WHERE group_artifact_id=$3 AND group_id=$4',
283 array ($data['trackerpublic'][$uref_id],
284 $data['trackeranon'][$uref_id],
286 $this->Group->getID())) ;
287 $updated['tracker'][$uref_id]=1;
289 $this->setError('update::tracker::'.db_error());
307 // c-file-style: "bsd"