e012318dbc3b4dcf16996b9885603da19ae73995
[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['batch-assurance'] = I18n::_('Batch Assurance');
29 $actions['admin-increase'] = I18n::_('Administrative Increase');
30 $actions['assurer-challenge'] = I18n::_('Assurer Challenge');
31 $actions['flags'] = I18n::_('Set Flags');
32 $url = array('controller' => 'manage-account');
33 foreach ($actions as $action => $label) {
34 $url['action'] = $action;
35 $link = '<a href="'.$this->view->url($url, 'default', true).'">'.
36 $label . '</a>';
37 $this->view->leftNav($link);
38 }
39
40 }
41
42 public function indexAction()
43 {
44 // Just render the view
45 return;
46 }
47
48 public function assuranceAction()
49 {
50 // Validate form
51 $form = $this->getAssuranceForm();
52 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
53 $this->view->assurance_form = $form;
54 return $this->render('assuranceform');
55 }
56
57 // Form is valid -> get values for processing
58 $values = $form->getValues();
59
60 // Get the current user
61 $user = Default_Model_User::findCurrentUser();
62
63 $this->view->assurancesDone = array();
64 $quantity = $values['quantity'];
65 do {
66 // split up into multiple assurances
67 if ($quantity > self::MAX_POINTS_PER_ASSURANCE) {
68 $points = self::MAX_POINTS_PER_ASSURANCE;
69 $quantity -= self::MAX_POINTS_PER_ASSURANCE;
70 } else {
71 $points = $quantity;
72 $quantity = 0;
73 }
74
75 // Get the assurer for this assurance
76 $issued = $user->findNewAssurer()
77 ->assure($user, $points, $values['location'], $values['date']);
78
79 $this->view->assurancesDone[] = $issued;
80 } while ($quantity > 0);
81
82 return;
83 }
84
85 public function batchAssuranceAction() {
86 // Validate form
87 $form = $this->getBatchAssuranceForm();
88 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
89 $this->view->batch_assurance_form = $form;
90 return $this->render('batch-assurance-form');
91 }
92
93 // Form is valid -> get values for processing
94 $values = $form->getValues();
95
96 $user = Default_Model_User::findCurrentUser();
97
98 $location = $values['location'];
99 $date = $values['date'];
100
101 $this->view->assurances = array();
102
103 for ($i = 0; $i < intval($values['quantity']); $i++) {
104 $assuree = $user->findNewAssuree();
105
106 if ($values['percentage'] === 'percentage') {
107 $points = ($user->maxpoints() * intval($values['points']) /100);
108 }elseif ($values['percentage'] === 'absolute') {
109 $points = intval($values['points']);
110 }
111
112 $user->assure($assuree, $points, $location, $date);
113
114 $this->view->assurances[] = array(
115 'assuree'=>$assuree->getPrimEmail(),
116 'points'=>$points,
117 'location'=>$location,
118 'date'=>$date);
119 }
120
121 return;
122 }
123
124 public function adminIncreaseAction()
125 {
126 // Validate form
127 $form = $this->getAdminIncreaseForm();
128 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
129 $this->view->admin_increase_form = $form;
130 return $this->render('admin-increase-form');
131 }
132
133 // Form is valid -> get values for processing
134 $values = $form->getValues();
135
136 // Get current user
137 $user = Default_Model_User::findCurrentUser();
138
139 $this->view->adminIncreasesDone = array();
140 $points = $values['points'];
141
142 // Only assign points within the limit if unlimited flag is not set
143 if ($values['unlimited'] != '1') {
144 if ($user->getPoints() >= self::MAX_POINTS_TOTAL) {
145 // No more administrative increases should be done
146 return;
147 } elseif ($user->getPoints() + $points > self::MAX_POINTS_TOTAL) {
148 $points = self::MAX_POINTS_TOTAL - $user->getPoints();
149 }
150 }
151
152 $user->adminIncrease($points, $values['location'], $values['date']);
153 $this->view->adminIncreasesDone[] = $points;
154
155 return;
156 }
157
158
159 public function assurerChallengeAction()
160 {
161 // Validate form
162 $form = $this->getAssurerChallengeForm();
163 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
164 $this->view->assurer_challenge_form = $form;
165 return $this->render('assurer-challenge-form');
166 }
167
168 // Form is valid -> get values for processing
169 $values = $form->getValues();
170
171 // Get user data
172 $user = Default_Model_User::findCurrentUser();
173
174 $user->assignChallenge(1, $values['variant']);
175 }
176
177 public function flagsAction()
178 {
179 $user = Default_Model_User::findCurrentUser();
180
181 // Validate form
182 $form = $this->getFlagsForm($user);
183 $this->view->flags_form = $form;
184 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
185 return;
186 }
187
188 $flags = $user->getFlags();
189 foreach ($flags as $flag => $value) {
190 $element = $form->getElement($flag);
191 if ($element !== null) {
192 $flags[$flag] = $element->isChecked();
193 }
194 }
195
196 $user->setFlags($flags);
197 return;
198 }
199
200 protected function getAssuranceForm()
201 {
202 $form = new Zend_Form();
203 $form->setAction('/manage-account/assurance')->setMethod('post');
204
205 $quantity = new Zend_Form_Element_Text('quantity');
206 $quantity->setRequired(true)
207 ->setLabel(I18n::_('Number of Points'))
208 ->addFilter(new Zend_Filter_Int())
209 ->addValidator(new Zend_Validate_Between(0, 100));
210 $form->addElement($quantity);
211
212 $location = new Zend_Form_Element_Text('location');
213 $location->setRequired(true)
214 ->setLabel(I18n::_('Location'))
215 ->setValue(I18n::_('CAcert Test Manager'))
216 ->addValidator(new Zend_Validate_StringLength(1,255));
217 $form->addElement($location);
218
219 $date = new Zend_Form_Element_Text('date');
220 $date->setRequired(true)
221 ->setLabel(I18n::_('Date of Assurance'))
222 ->setValue(date('Y-m-d H:i:s'))
223 ->addValidator(new Zend_Validate_StringLength(1,255));
224 $form->addElement($date);
225
226 $submit = new Zend_Form_Element_Submit('submit');
227 $submit->setLabel(I18n::_('Assure Me'));
228 $form->addElement($submit);
229
230 return $form;
231 }
232
233 protected function getBatchAssuranceForm() {
234 $form = new Zend_Form();
235 $form->setAction('/manage-account/batch-assurance')->setMethod('post');
236
237 $quantity = new Zend_Form_Element_Text('quantity');
238 $quantity->setRequired(true)
239 ->setLabel(I18n::_('Number of Assurances'))
240 ->setValue('25')
241 ->addFilter(new Zend_Filter_Int())
242 ->addValidator(new Zend_Validate_Between(0, 100));
243 $form->addElement($quantity);
244
245 $percentage = new Zend_Form_Element_Select('percentage');
246 $percentage->setRequired(true)
247 ->setLabel(I18n::_('Are the points specified absolute?'))
248 ->setValue('percentage')
249 ->setMultiOptions(array(
250 'percentage' => I18n::_('Percentage'),
251 'absolute' => I18n::_('Absolute'),
252 ));
253 $form->addElement($percentage);
254
255 $points = new Zend_Form_Element_Text('points');
256 $points->setRequired(true)
257 ->setLabel(I18n::_('Points per Assurance'))
258 ->setValue('100')
259 ->addFilter(new Zend_Filter_Int())
260 ->addValidator(new Zend_Validate_Between(0, 100));
261 $form->addElement($points);
262
263 $location = new Zend_Form_Element_Text('location');
264 $location->setRequired(true)
265 ->setLabel(I18n::_('Location'))
266 ->setValue(I18n::_('CAcert Test Manager Batch Assurance'))
267 ->addValidator(new Zend_Validate_StringLength(1,255));
268 $form->addElement($location);
269
270 $date = new Zend_Form_Element_Text('date');
271 $date->setRequired(true)
272 ->setLabel(I18n::_('Date of Assurance'))
273 ->setValue(date('Y-m-d H:i:s'))
274 ->addValidator(new Zend_Validate_StringLength(1,255));
275 $form->addElement($date);
276
277 $submit = new Zend_Form_Element_Submit('submit');
278 $submit->setLabel(I18n::_('Make Batch Assurance'));
279 $form->addElement($submit);
280
281 return $form;
282 }
283
284 protected function getAdminIncreaseForm()
285 {
286 $form = new Zend_Form();
287 $form->setAction('/manage-account/admin-increase')->setMethod('post');
288
289 $points = new Zend_Form_Element_Text('points');
290 $points->setRequired(true)
291 ->setLabel(I18n::_('Number of Points'))
292 ->addFilter(new Zend_Filter_Int())
293 ->addValidator(new Zend_Validate_GreaterThan(0));
294 $form->addElement($points);
295
296 $unlimited = new Zend_Form_Element_Checkbox('unlimited');
297 $unlimited->setLabel(I18n::_('Assign Points even if the Limit of 150 '.
298 'is exceeded'))
299 ->setChecked(false);
300 $form->addElement($unlimited);
301
302 $location = new Zend_Form_Element_Text('location');
303 $location->setRequired(true)
304 ->setLabel(I18n::_('Location'))
305 ->setValue(I18n::_('CAcert Test Manager'))
306 ->addValidator(new Zend_Validate_StringLength(1,255));
307 $form->addElement($location);
308
309 $date = new Zend_Form_Element_Text('date');
310 $date->setRequired(true)
311 ->setLabel(I18n::_('Date of Increase'))
312 ->setValue(date('Y-m-d H:i:s'))
313 ->addValidator(new Zend_Validate_StringLength(1,255));
314 $form->addElement($date);
315
316 $submit = new Zend_Form_Element_Submit('submit');
317 $submit->setLabel(I18n::_('Give Me Points'));
318 $form->addElement($submit);
319
320 return $form;
321 }
322
323 protected function getAssurerChallengeForm()
324 {
325 $form = new Zend_Form();
326 $form->setAction('/manage-account/assurer-challenge')
327 ->setMethod('post');
328
329 $variant = new Zend_Form_Element_Select('variant');
330 $variant->setLabel(I18n::_('Variant'));
331 $options =
332 Default_Model_User::getAvailableChallengeVariants($this->db, 1);
333 $variant->setMultiOptions($options)
334 ->setRequired(true);
335 $form->addElement($variant);
336
337 $submit = new Zend_Form_Element_Submit('submit');
338 $submit->setLabel(I18n::_('Challenge Me'));
339 $form->addElement($submit);
340
341 return $form;
342 }
343
344 protected function getFlagsForm(Default_Model_User $user)
345 {
346 $form = new Zend_Form();
347 $form->setAction('/manage-account/flags')
348 ->setMethod('post');
349
350 $flags = $user->getFlags();
351
352 // Add a checkbox for each flag
353 $labels = array();
354 $labels['admin'] = I18n::_('Support Engineer');
355 $labels['codesign'] = I18n::_('Code Signing');
356 $labels['orgadmin'] = I18n::_('Organisation Admin');
357 $labels['ttpadmin'] = I18n::_('TTP Admin');
358 $labels['board'] = I18n::_('Board Member');
359 $labels['locadmin'] = I18n::_('Location Admin');
360 $labels['tverify'] = I18n::_('TVerify');
361 $labels['locked'] = I18n::_('Lock Account');
362 $labels['assurer_blocked'] = I18n::_('Block Assurer');
363
364 foreach ($labels as $flag => $label) {
365 $checkbox = new Zend_Form_Element_Checkbox($flag);
366 $checkbox->setLabel($label)
367 ->setChecked($flags[$flag]);
368 $form->addElement($checkbox);
369 }
370
371 $submit = new Zend_Form_Element_Submit('submit');
372 $submit->setLabel(I18n::_('Save Flags'));
373 $form->addElement($submit);
374
375 return $form;
376 }
377 }