File: RoleObserver.class

package info (click to toggle)
gforge 4.5.14-22etch13
  • links: PTS
  • area: main
  • in suites: etch
  • size: 13,004 kB
  • ctags: 11,918
  • sloc: php: 36,047; sql: 29,050; sh: 10,538; perl: 6,496; xml: 3,810; makefile: 341; python: 263; ansic: 256
file content (331 lines) | stat: -rw-r--r-- 9,260 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
 * RoleObserver Class - this class handles the privacy settings
 * for an entire project
 *
 * Copyright 2004 (c) GForge LLC
 *
 * @version   $Id: RoleObserver.class 4420 2005-06-16 13:49:51Z vittal $
 * @author Tim Perdue tim@gforge.org
 * @date 2004-03-16
 *
 * This file is part of GForge.
 *
 * GForge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GForge; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


class RoleObserver extends Error {

	var $setting_array;
	var $role_vals;
	var $Group;
	var $role_values=array(
	'projectpublic'=>array('0','1'),
	'scmpublic'=>array('0','1'),
	'forumpublic'=>array('0','1'),
	'forumanon'=>array('0','1'),
	'trackerpublic'=>array('0','1'),
	'trackeranon'=>array('0','1'),
	'pmpublic'=>array('0','1'),
	'frspackage'=>array('0','1'));

	/**
	 *  Role($group,$id) - CONSTRUCTOR.
	 *
	 *  @param  object	 The Group object.
	 *  @param  int	 The role_id.
	 */

	function RoleObserver ($Group) {
		$this->Error();
		if (!$Group || !is_object($Group) || $Group->isError()) {
			$this->setError('Role::'.$Group->getErrorMessage());
			return false;
		}
		$this->Group =& $Group;
		return $this->fetchData();
	}

    /**
     *  getID - get the ID of this role.
     *
     *  @return string The ID of the observer.
     */
	function getID() {
		return 'observer';
	}

    /**
     *  getName - get the name of this role.
     *
     *  @return string  The name of this role.
     */
	function getName() {
		return 'Observer';
	}

	/**
	 *  fetchData - May need to refresh database fields.
	 *
	 *  If an update occurred and you need to access the updated info.
	 *
	 *  @return boolean success;
	 */
	function fetchData() {
		$this->setting_array=array();
		//
		//	Forum is_public/allow_anon
		//
		$res=db_query("SELECT group_forum_id,is_public,allow_anonymous 
			FROM forum_group_list 
			WHERE group_id='".$this->Group->getID()."'");
		while ($arr =& db_fetch_array($res)) {
			$this->setting_array['forumpublic'][$arr['group_forum_id']] = $arr['is_public'];
			$this->setting_array['forumanon'][$arr['group_forum_id']] = $arr['allow_anonymous'];
		}

		//
		//	Task Manager is_public/allow_anon
		//
		$res=db_query("SELECT group_project_id,is_public
			FROM project_group_list 
			WHERE group_id='".$this->Group->getID()."'");
		while ($arr =& db_fetch_array($res)) {
			$this->setting_array['pmpublic'][$arr['group_project_id']] = $arr['is_public'];
		}

		//
		//	Tracker is_public/allow_anon
		//
		$res=db_query("SELECT group_artifact_id,is_public,allow_anon
			FROM artifact_group_list 
			WHERE group_id='".$this->Group->getID()."'");
		while ($arr =& db_fetch_array($res)) {
			$this->setting_array['trackerpublic'][$arr['group_artifact_id']] = $arr['is_public'];
			$this->setting_array['trackeranon'][$arr['group_artifact_id']] = $arr['allow_anon'];
		}

		//
		//	FRS packages can be public/private now
		//
		$res=db_query("SELECT package_id,is_public
			FROM frs_package
			WHERE group_id='".$this->Group->getID()."'");
		while ($arr =& db_fetch_array($res)) {
			$this->setting_array['frspackage'][$arr['package_id']] = $arr['is_public'];
		}

		//
		//	AnonSCM
		//
		$this->Group->fetchData( $this->Group->getID() );
		$this->setting_array['scmpublic'][0]=$this->Group->enableAnonSCM();
		$this->setting_array['projectpublic'][0]=$this->Group->isPublic();
//echo '<html><body><pre>'.print_r($this->setting_array).'</pre>';
//exit;
		return true;
	}

	/**
	 *  &getRoleVals - get all the values and language text strings for this section.
	 *
	 *  @return array	Assoc array of values for this section.
	 */
	function &getRoleVals($section) {
		global $Language,$role_vals;

		//
		//	Optimization - save array so it is only built once per page view
		//
		if (!isset($role_vals[$section])) {

			for ($i=0; $i<count($this->role_values[$section]); $i++) {
				//
				//	Build an associative array of these key values + localized description
				//
				$role_vals[$section][$this->role_values[$section][$i]]=$Language->getText('rbac_vals',"$section".$this->role_values[$section][$i]);
			}
		}
		return $role_vals[$section];
	}

    /**
     *  getVal - get a value out of the array of settings for this role.
     *
     *  @param  string  The name of the role.
     *  @param  integer The ref_id (ex: group_artifact_id, group_forum_id) for this item.
     *  @return integer The value of this item.
     */
	function getVal($section,$ref_id) {
		global $role_default_array;
		if (!$ref_id) {
			$ref_id=0;
		}
		if (!isset($this->setting_array) && !isset($this->data_array)) {
			$this->setting_array=$role_default_array;
		}
		return $this->setting_array[$section][$ref_id];
	}

    /**
     *  update - update a new in the database.
     *
     *  @param  array   A multi-dimensional array of data in this format: $data['section_name']['
     *  @return boolean True on success or false on failure.
     */
	function update($data) {
		$perm =& $this->Group->getPermission( session_get_user() );
		if (!$perm || !is_object($perm) || $perm->isError() || !$perm->isAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}

		db_begin();

////$data['section_name']['ref_id']=$val
		$arr1 = array_keys($data);
		for ($i=0; $i<count($arr1); $i++) {	
			$arr2 = array_keys($data[$arr1[$i]]);
			for ($j=0; $j<count($arr2); $j++) {
				$usection_name=$arr1[$i];
				$uref_id=$arr2[$j];
				$uvalue=$data[$usection_name][$uref_id];
				if (!$uref_id) {
					$uref_id=0;
				}
				if (!$uvalue) {
					$uvalue=0;
				}
				//
				//	See if this setting changed. If so, then update it
				//
				if ($this->getVal($usection_name,$uref_id) != $uvalue) {
					if ($usection_name == 'scmpublic' || 
						$usection_name == 'projectpublic') {
						if (!$data['scmpublic'][0]) {
							$data['scmpublic'][0]=0;
						}
						if (!$data['projectpublic'][0]) {
							$data['projectpublic'][0]=0;
							// Groups cannot be private and have public SCM
							// so we should always ensure that the scm is 
							// private if we change a group to private.
							$data['scmpublic'][0]=0;
						}
						$sql="UPDATE groups
							SET
							enable_anonscm='".$data['scmpublic'][0]."',
							is_public='".$data['projectpublic'][0]."'
							WHERE group_id='".$this->Group->getID()."'";
							$res=db_query($sql);
							if (!$res) {
								$this->setError('update::group::'.db_error());
								db_rollback();
								return false;
							}

					//
					//	Forum
					//
					} elseif ($usection_name == 'forumpublic' || $usection_name == 'forumanon') {
						//
						//	prevent double-updating each forum
						//
						if ($updated['forum'][$uref_id]) {
							continue;
						}
						$sql="UPDATE forum_group_list
							SET 
							is_public='".$data['forumpublic'][$uref_id]."',
							allow_anonymous='".$data['forumanon'][$uref_id]."'
							WHERE
							group_forum_id='$uref_id'
							AND group_id='".$this->Group->getID()."'";
//echo "\n<br>$sql";
						$res=db_query($sql);
						$updated['forum'][$uref_id]=1;
						if (!$res) {
							$this->setError('update::forum::'.db_error());
							db_rollback();
							return false;
						}
					} elseif ($usection_name == 'pmpublic') {

						$sql="UPDATE project_group_list
							SET 
							is_public='$uvalue'
							WHERE
							group_project_id='$uref_id'
							AND group_id='".$this->Group->getID()."'";
//echo "\n<br>$sql";
						$res=db_query($sql);
						if (!$res) {
							$this->setError('update::pm::'.db_error());
							db_rollback();
							return false;
						}

					} elseif ($usection_name == 'frspackage') {

						$sql="UPDATE frs_package
							SET 
							is_public='$uvalue'
							WHERE
							package_id='$uref_id'
							AND group_id='".$this->Group->getID()."'";
//echo "\n<br>$sql";
						$res=db_query($sql);
						if (!$res) {
							$this->setError('update::frspackage::'.db_error());
							db_rollback();
							return false;
						}

					} elseif ($usection_name == 'trackerpublic' || $usection_name == 'trackeranon') {
						//
						//	prevent double-updating each forum
						//
						if ($updated['tracker'][$uref_id]) {
							continue;
						}
						$sql="UPDATE artifact_group_list
							SET
							is_public='".$data['trackerpublic'][$uref_id]."',
							allow_anon='".$data['trackeranon'][$uref_id]."'
							WHERE
							group_artifact_id='$uref_id'
							AND group_id='".$this->Group->getID()."'";
//echo "\n<br>$sql";
						$res=db_query($sql);
						$updated['tracker'][$uref_id]=1;
						if (!$res) {
							$this->setError('update::tracker::'.db_error());
							db_rollback();
							return false;
						}
					}
				}
			}
		}

		db_commit();
		$this->fetchData();
		return true;
	}

}

?>