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;
143 setup_rbac_strings () ;
146 // Optimization - save array so it is only built once per page view
148 if (!isset($role_vals[$section])) {
150 for ($i=0; $i<count($this->role_values[$section]); $i++) {
152 // Build an associative array of these key values + localized description
154 $role_vals[$section][$this->role_values[$section][$i]]=$rbac_permission_names["$section".$this->role_values[$section][$i]];
157 return $role_vals[$section];
161 * getVal - get a value out of the array of settings for this role.
163 * @param string The name of the role.
164 * @param integer The ref_id (ex: group_artifact_id, group_forum_id) for this item.
165 * @return integer The value of this item.
167 function getVal($section,$ref_id) {
168 global $role_default_array;
172 if (!isset($this->setting_array) && !isset($this->data_array)) {
173 $this->setting_array=$role_default_array;
175 return $this->setting_array[$section][$ref_id];
179 * update - update a new in the database.
181 * @param array A multi-dimensional array of data in this format: $data['section_name']['
182 * @return boolean True on success or false on failure.
184 function update($data) {
185 $perm =& $this->Group->getPermission( session_get_user() );
186 if (!$perm || !is_object($perm) || $perm->isError() || !$perm->isAdmin()) {
187 $this->setPermissionDeniedError();
193 ////$data['section_name']['ref_id']=$val
194 $arr1 = array_keys($data);
195 for ($i=0; $i<count($arr1); $i++) {
196 $arr2 = array_keys($data[$arr1[$i]]);
197 for ($j=0; $j<count($arr2); $j++) {
198 $usection_name=$arr1[$i];
200 $uvalue=$data[$usection_name][$uref_id];
208 // See if this setting changed. If so, then update it
210 if ($this->getVal($usection_name,$uref_id) != $uvalue) {
211 if ($usection_name == 'scmpublic' ||
212 $usection_name == 'projectpublic') {
213 if (!$data['scmpublic'][0]) {
214 $data['scmpublic'][0]=0;
216 if (!$data['projectpublic'][0]) {
217 $data['projectpublic'][0]=0;
218 // Groups cannot be private and have public SCM
219 // so we should always ensure that the scm is
220 // private if we change a group to private.
221 $data['scmpublic'][0]=0;
223 $res = db_query_params ('UPDATE groups SET enable_anonscm=$1, is_public=$2 WHERE group_id=$3',
224 array ($data['scmpublic'][0],
225 $data['projectpublic'][0],
226 $this->Group->getID())) ;
228 $this->setError('update::group::'.db_error());
236 } elseif ($usection_name == 'forumpublic' || $usection_name == 'forumanon') {
238 // prevent double-updating each forum
240 if ($updated['forum'][$uref_id]) {
243 $res = db_query_params ('UPDATE forum_group_list SET is_public=$1, allow_anonymous=$2 WHERE group_forum_id=$3 AND group_id=$4',
244 array ($data['forumpublic'][$uref_id],
245 $data['forumanon'][$uref_id],
247 $this->Group->getID())) ;
248 $updated['forum'][$uref_id]=1;
250 $this->setError('update::forum::'.db_error());
254 } elseif ($usection_name == 'pmpublic') {
255 $res = db_query_params ('UPDATE project_group_list SET is_public=$1 WHERE group_project_id=$2 AND group_id=$3',
258 $this->Group->getID())) ;
260 $this->setError('update::pm::'.db_error());
265 } elseif ($usection_name == 'frspackage') {
266 $res = db_query_params ('UPDATE frs_package SET is_public=$1 WHERE package_id=$2 AND group_id=$3',
269 $this->Group->getID())) ;
271 $this->setError('update::frspackage::'.db_error());
276 } elseif ($usection_name == 'trackerpublic' || $usection_name == 'trackeranon') {
278 // prevent double-updating each forum
280 if ($updated['tracker'][$uref_id]) {
283 $res = db_query_params ('UPDATE artifact_group_list SET is_public=$1, allow_anon=$2 WHERE group_artifact_id=$3 AND group_id=$4',
284 array ($data['trackerpublic'][$uref_id],
285 $data['trackeranon'][$uref_id],
287 $this->Group->getID())) ;
288 $updated['tracker'][$uref_id]=1;
290 $this->setError('update::tracker::'.db_error());
308 // c-file-style: "bsd"