Admin increase: get rid of the "fragment" flag as experience points should
[cacert-mgr.git] / manager / application / controllers / ManageAccountController.php
1 <?php
2 /**
3 * @author Michael Tänzer
4 */
5
6 class ManageAccountController extends Zend_Controller_Action
7 {
8 const MAX_POINTS_PER_ASSURANCE = 35;
9 const MAX_POINTS_TOTAL = 150;
10 const ADMIN_INCREASE_FRAGMENT_SIZE = 2;
11
12 // Value used in the database to identify a admin increase
13 const ADMIN_INCREASE_METHOD = 'Administrative Increase';
14
15 protected $db;
16
17 public function init()
18 {
19 $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini',
20 APPLICATION_ENV);
21
22 $this->db = Zend_Db::factory($config->ca_mgr->db->auth->pdo,
23 $config->ca_mgr->db->auth);
24
25 // Build the left navigation
26 $actions = array();
27 $actions['assurance'] = I18n::_('Automated Assurance');
28 $actions['admin-increase'] = I18n::_('Administrative Increase');
29 $actions['assurer-challenge'] = I18n::_('Assurer Challenge');
30 $actions['flags'] = I18n::_('Set Flags');
31 $url = array('controller' => 'manage-account');
32 foreach ($actions as $action => $label) {
33 $url['action'] = $action;
34 $link = '<a href="'.$this->view->url($url, 'default', true).'">'.
35 $label . '</a>';
36 $this->view->leftNav($link);
37 }
38
39 }
40
41 public function indexAction()
42 {
43 // Just render the view
44 return;
45 }
46
47 public function assuranceAction()
48 {
49 // Validate form
50 $form = $this->getAssuranceForm();
51 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
52 $this->view->assurance_form = $form;
53 return $this->render('assuranceform');
54 }
55
56 // Form is valid -> get values for processing
57 $values = $form->getValues();
58
59 // Get the current user
60 $user = Default_Model_User::findCurrentUser();
61
62 $this->view->assurancesDone = array();
63 $quantity = $values['quantity'];
64 do {
65 // split up into multiple assurances
66 if ($quantity > self::MAX_POINTS_PER_ASSURANCE) {
67 $points = self::MAX_POINTS_PER_ASSURANCE;
68 $quantity -= self::MAX_POINTS_PER_ASSURANCE;
69 } else {
70 $points = $quantity;
71 $quantity = 0;
72 }
73
74 // Get the assurer for this assurance
75 $issued = $user->findNewAssurer()
76 ->assure($user, $points, $values['location'], $values['date']);
77
78 $this->view->assurancesDone[] = $issued;
79 } while ($quantity > 0);
80
81 return;
82 }
83
84 public function adminIncreaseAction()
85 {
86 // Validate form
87 $form = $this->getAdminIncreaseForm();
88 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
89 $this->view->admin_increase_form = $form;
90 return $this->render('admin-increase-form');
91 }
92
93 // Form is valid -> get values for processing
94 $values = $form->getValues();
95
96 // Get user data
97 $user = Default_Model_User::findCurrentUser();
98
99 $this->view->adminIncreasesDone = array();
100 $points = $values['points'];
101
102 // Only assign points within the limit if unlimited flag is not set
103 if ($values['unlimited'] != '1') {
104 if ($user->getPoints() >= self::MAX_POINTS_TOTAL) {
105 // No more administrative increases should be done
106 return;
107 } elseif ($user->getPoints() + $points > self::MAX_POINTS_TOTAL) {
108 $points = self::MAX_POINTS_TOTAL - $user->getPoints();
109 }
110 }
111
112 $user->adminIncrease($points, $values['location'], $values['date']);
113 $this->view->adminIncreasesDone[] = $points;
114
115 return;
116 }
117
118
119 public function assurerChallengeAction()
120 {
121 // Validate form
122 $form = $this->getAssurerChallengeForm();
123 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
124 $this->view->assurer_challenge_form = $form;
125 return $this->render('assurer-challenge-form');
126 }
127
128 // Form is valid -> get values for processing
129 $values = $form->getValues();
130
131 // Get user data
132 $user['id'] = $this->getUserId();
133
134 // Assign the assurer challenge
135 $challenge = array(); // Make sure the array is empty
136 $challenge['user_id'] = $user['id'];
137 $challenge['variant_id'] = $values['variant'];
138 $challenge['pass_date'] = date('Y-m-d H:i:s');
139 $this->db->insert('cats_passed', $challenge);
140
141 // Maybe user is now assurer
142 $this->fixAssurerFlag($user['id']);
143
144 return;
145 }
146
147 public function flagsAction()
148 {
149 // Get user data
150 $user['id'] = $this->getUserId();
151
152 // Validate form
153 $form = $this->getFlagsForm($user['id']);
154 $this->view->flags_form = $form;
155 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
156 return;
157 }
158
159 $flags = array('admin', 'codesign', 'orgadmin', 'ttpadmin', 'board',
160 'locadmin', 'locked', 'assurer_blocked');
161 $update = array(); // Make sure array is empty
162 foreach ($flags as $flag) {
163 if ($form->getElement($flag)->isChecked()) {
164 $update[$flag] = 1;
165 } else {
166 $update[$flag] = 0;
167 }
168 }
169 $this->db->update('users', $update, '`id` = '.$user['id']);
170
171 return;
172 }
173
174 protected function getAssuranceForm()
175 {
176 $form = new Zend_Form();
177 $form->setAction('/manage-account/assurance')->setMethod('post');
178
179 $quantity = new Zend_Form_Element_Text('quantity');
180 $quantity->setRequired(true)
181 ->setLabel(I18n::_('Number of Points'))
182 ->addFilter(new Zend_Filter_Int())
183 ->addValidator(new Zend_Validate_Between(0, 100));
184 $form->addElement($quantity);
185
186 $location = new Zend_Form_Element_Text('location');
187 $location->setRequired(true)
188 ->setLabel(I18n::_('Location'))
189 ->setValue(I18n::_('CAcert Test Manager'))
190 ->addValidator(new Zend_Validate_StringLength(1,255));
191 $form->addElement($location);
192
193 $date = new Zend_Form_Element_Text('date');
194 $date->setRequired(true)
195 ->setLabel(I18n::_('Date of Assurance'))
196 ->setValue(date('Y-m-d H:i:s'))
197 ->addValidator(new Zend_Validate_StringLength(1,255));
198 $form->addElement($date);
199
200 $submit = new Zend_Form_Element_Submit('submit');
201 $submit->setLabel(I18n::_('Assure Me'));
202 $form->addElement($submit);
203
204 return $form;
205 }
206
207 protected function getAdminIncreaseForm()
208 {
209 $form = new Zend_Form();
210 $form->setAction('/manage-account/admin-increase')->setMethod('post');
211
212 $points = new Zend_Form_Element_Text('points');
213 $points->setRequired(true)
214 ->setLabel(I18n::_('Number of Points'))
215 ->addFilter(new Zend_Filter_Int())
216 ->addValidator(new Zend_Validate_GreaterThan(0));
217 $form->addElement($points);
218
219 $unlimited = new Zend_Form_Element_Checkbox('unlimited');
220 $unlimited->setLabel(I18n::_('Assign Points even if the Limit of 150 '.
221 'is exceeded'))
222 ->setChecked(false);
223 $form->addElement($unlimited);
224
225 $location = new Zend_Form_Element_Text('location');
226 $location->setRequired(true)
227 ->setLabel(I18n::_('Location'))
228 ->setValue(I18n::_('CAcert Test Manager'))
229 ->addValidator(new Zend_Validate_StringLength(1,255));
230 $form->addElement($location);
231
232 $date = new Zend_Form_Element_Text('date');
233 $date->setRequired(true)
234 ->setLabel(I18n::_('Date of Increase'))
235 ->setValue(date('Y-m-d H:i:s'))
236 ->addValidator(new Zend_Validate_StringLength(1,255));
237 $form->addElement($date);
238
239 $submit = new Zend_Form_Element_Submit('submit');
240 $submit->setLabel(I18n::_('Give Me Points'));
241 $form->addElement($submit);
242
243 return $form;
244 }
245
246 protected function getAssurerChallengeForm()
247 {
248 $form = new Zend_Form();
249 $form->setAction('/manage-account/assurer-challenge')
250 ->setMethod('post');
251
252 $variant = new Zend_Form_Element_Select('variant');
253 $variant->setLabel(I18n::_('Variant'));
254 // Get the available variants from the database
255 $query = 'select `id`, `test_text` from `cats_variant`
256 where `type_id` = 1';
257 $options = $this->db->fetchPairs($query);
258 $variant->setMultiOptions($options)
259 ->setRequired(true);
260 $form->addElement($variant);
261
262 $submit = new Zend_Form_Element_Submit('submit');
263 $submit->setLabel(I18n::_('Challenge Me'));
264 $form->addElement($submit);
265
266 return $form;
267 }
268
269 protected function getFlagsForm($user_id)
270 {
271 $form = new Zend_Form();
272 $form->setAction('/manage-account/flags')
273 ->setMethod('post');
274
275 // Get the current setting of the flags
276 $query = 'select `admin`, `codesign`, `orgadmin`, `ttpadmin`, `board`,
277 `tverify`, `locadmin`, `locked`, `assurer_blocked` from `users`
278 where `id` = :user';
279 $query_params['user'] = $user_id;
280 $result = $this->db->query($query, $query_params);
281 if ($result->rowCount() !== 1) {
282 throw new Exception(__METHOD__ . ': user ID not found in the data base');
283 }
284 $row = $result->fetch();
285
286 // Add a checkbox for each flag
287 $labels = array();
288 $labels['admin'] = I18n::_('Support Engineer');
289 $labels['codesign'] = I18n::_('Code Signing');
290 $labels['orgadmin'] = I18n::_('Organisation Admin');
291 $labels['ttpadmin'] = I18n::_('TTP Admin');
292 $labels['board'] = I18n::_('Board Member');
293 $labels['locadmin'] = I18n::_('Location Admin');
294 $labels['locked'] = I18n::_('Lock Account');
295 $labels['assurer_blocked'] = I18n::_('Block Assurer');
296
297 foreach ($labels as $flag => $label) {
298 $checkbox = new Zend_Form_Element_Checkbox($flag);
299 $checkbox->setLabel($label)
300 ->setChecked($row[$flag] === '1');
301 $form->addElement($checkbox);
302 }
303
304 $submit = new Zend_Form_Element_Submit('submit');
305 $submit->setLabel(I18n::_('Save Flags'));
306 $form->addElement($submit);
307
308 return $form;
309 }
310 }