File: SpotPage_edituser.php

package info (click to toggle)
spotweb 20130826%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,132 kB
  • ctags: 11,281
  • sloc: php: 31,367; xml: 1,009; sh: 148; makefile: 83
file content (201 lines) | stat: -rwxr-xr-x 6,993 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
<?php
class SpotPage_edituser extends SpotPage_Abs {
	private $_editUserForm;
	private $_userIdToEdit;
	
	function __construct(SpotDb $db, SpotSettings $settings, $currentSession, $params) {
		parent::__construct($db, $settings, $currentSession);
		$this->_editUserForm = $params['edituserform'];
		$this->_userIdToEdit = $params['userid'];
	} # ctor
	
	/* 
	 * Erase all fields from the user record which shouldn't
	 * be in the form anyways 
	 */
	function cleanseEditForm($editForm) {
		/* Make sure the preferences aren't set using this page as it might override security */
		$validFields = array('firstname', 'lastname', 'mail', 'newpassword1', 'newpassword2', 'grouplist', 'prefs');
		foreach($editForm as $key => $value) {
			if (in_array($key, $validFields) === false) {
				unset($editForm[$key]);
			} # if
		} # foreach
		
		return $editForm;
	} # cleanseEditForm

	function render() {
		$groupMembership = array();
		$formMessages = array('errors' => array(),
							  'info' => array());
							  
		# check the users' permissions
		if ($this->_userIdToEdit == $this->_currentSession['user']['userid']) {
			$this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_edit_own_user, '');
		} else {
			$this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_edit_other_users, '');
		} # if
		
		# per default the result is 'not tried'
		$editResult = array();

		# Instantiate the spotuser object
		$spotUserSystem = new SpotUserSystem($this->_db, $this->_settings);
		
		# and create a nic and shiny page title
		$this->_pageTitle = "spot: edit user";
		
		# retrieve the to-edit user
		$spotUser = $this->_db->getUser($this->_userIdToEdit);
		if ($spotUser === false) {
			$formMessages['errors'][] = sprintf(_('User %d can not be found'), $this->_userIdToEdit);
			$editResult = array('result' => 'failure');
		} # if
		
		# request the users' groupmembership
		if ($spotUser != false) {
			$groupMembership = $this->_db->getGroupList($spotUser['userid']);
		} # if

		/* 
		 * bring the forms' action into the local scope for 
		 * easier access
		 */
		$formAction = $this->_editUserForm['action'];

		# Only perform certain validations when the form is actually submitted
		if ((!empty($formAction)) && (empty($formMessages['errors']))) {
			# sta niet toe, dat de admin user gewist wordt
			if (($spotUser['userid'] <= SPOTWEB_ADMIN_USERID) && ($formAction == 'delete')) {
				$formMessages['errors'][] = _('Admin and Anonymous can not be deleted');
				$editResult = array('result' => 'failure');
			} # if
		} # if


		# Only perform certain validations when the form is actually submitted
		if ((!empty($formAction)) && (empty($formMessages['errors']))) {
			switch($formAction) {
				case 'delete' : {
					$this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_delete_user, '');

					$spotUser = array_merge($spotUser, $this->_editUserForm);
					$spotUserSystem->removeUser($spotUser['userid']);
					$editResult = array('result' => 'success');

					break;
				} # case delete

				case 'edit'	: {
					# Remove any non-valid fields from the array
					$this->_editUserForm = $this->cleanseEditForm($this->_editUserForm);
					
					# validate the user fields
					$spotUser = array_merge($spotUser, $this->_editUserForm);
					$formMessages['errors'] = $spotUserSystem->validateUserRecord($spotUser, true);

					if (empty($formMessages['errors'])) {
						# actually update the user record
						$spotUserSystem->setUser($spotUser);

						/*
						 * Update the users' password, but only when
						 * a new password is given
						 */
						if (!empty($spotUser['newpassword1'])) {
							$spotUserSystem->setUserPassword($spotUser);
						} # if

						/*
						 * Did we get an groupmembership list? If so,
						 * try to update it as well
						 */						
						if (isset($this->_editUserForm['grouplist'])) {
							# retrieve the list of user groups
							$groupList = array();
							foreach($this->_editUserForm['grouplist'] as $val) {
								if ($val != 'dummy') {
									$groupList[] = array('groupid' => $val,
														'prio' => count($groupList));
								} # if
							} # for

							# make sure there is at least one group
							if (count($groupList) < 1) {
								$formMessages['errors'][] = _('A user must be member of at least one group');
								$editResult = array('result' => 'failure');
							} else {
								# Mangle the current group membership to a common format
								$currentGroupList = array();
								foreach($groupList as $value) {
									$currentGroupList[] = $value['groupid'];
								} # foreach

								# and mangle the new requested group membership
								$tobeGroupList = array();
								foreach($groupMembership as $value) {
									$tobeGroupList[] = $value['id'];
								} # foreach

								/*
								 * Try to compare the grouplist with the current
								 * grouplist. If the grouplist changes, the user 
								 * needs change group membership permissions
								 */
								sort($currentGroupList, SORT_NUMERIC);
								sort($tobeGroupList, SORT_NUMERIC);

								/* 
								 * If the groupmembership list changes, lets make sure
								 * the user has the specific permission
								 */
								$groupDiff = (count($currentGroupList) != count($tobeGroupList));
								for ($i = 0; $i < count($currentGroupList) && !$groupDiff; $i++) {
									$groupDiff = ($currentGroupList[$i] != $tobeGroupList[$i]);
								} # for

								if ($groupDiff) {
									if ($this->_spotSec->allowed(SpotSecurity::spotsec_edit_groupmembership, '')) {
										$spotUserSystem->setUserGroupList($spotUser, $groupList);
									} else {
										$formMessages['errors'][] = _('Changing group membership is not allowed');
										$editResult = array('result' => 'failure');										
									} # else
								} # if
							} # if
						} # if

						# report success
						$editResult = array('result' => 'success');
					} else {
						$editResult = array('result' => 'failure');
					} # else
					break;
				} # case 'edit' 
				
				case 'removeallsessions' : {
					$spotUserSystem->removeAllUserSessions($spotUser['userid']);
					$editResult = array('result' => 'success');

					break;
				} # case 'removeallsessions'

				case 'resetuserapi' : {
					$this->_spotSec->fatalPermCheck(SpotSecurity::spotsec_consume_api, '');

					$user = $spotUserSystem->resetUserApi($spotUser);
					$editResult = array('result' => 'success', 'newapikey' => $user['apikey']);
					break;
				} # case resetuserapi
			} # switch
		} # if

		#- display stuff -#
		$this->template('edituser', array('edituserform' => $spotUser,
										    'formmessages' => $formMessages,
											'editresult' => $editResult,
											'groupMembership' => $groupMembership));
	} # render
	
} # class SpotPage_edituser