summaryrefslogtreecommitdiff
path: root/manager/application/controllers/ManageAccountController.php
blob: 1bf970091a951bc13e8098ee1cb9c5f088e01be4 (plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
 * @author Michael Tänzer
 */

class ManageAccountController extends Zend_Controller_Action
{
    const MAX_POINTS_PER_ASSURANCE = 35;
    const MAX_POINTS_TOTAL = 150;
    const ADMIN_INCREASE_FRAGMENT_SIZE = 2;
    
    // Value used in the database to identify a admin increase
    const ADMIN_INCREASE_METHOD = 'Administrative Increase';
    
    protected $db;
    
    public function init()
    {
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini',
            APPLICATION_ENV);

        $this->db = Zend_Db::factory($config->ca_mgr->db->auth->pdo,
            $config->ca_mgr->db->auth);
        
        // Build the left navigation
        $actions = array();
        $actions['assurance'] = I18n::_('Automated Assurance');
        $actions['batch-assurance'] = I18n::_('Batch Assurance');
        $actions['admin-increase'] = I18n::_('Administrative Increase');
        $actions['assurer-challenge'] = I18n::_('Assurer Challenge');
        $actions['flags'] = I18n::_('Set Flags');
        $url = array('controller' => 'manage-account');
        foreach ($actions as $action => $label) {
            $url['action'] = $action;
            $link = '<a href="'.$this->view->url($url, 'default', true).'">'.
                $label . '</a>';
            $this->view->leftNav($link);
    	}
    	
    }
    
    public function indexAction()
    {
        // Just render the view
        return;
    }
    
    public function assuranceAction()
    {
        // Validate form
        $form = $this->getAssuranceForm();
        if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
            $this->view->assurance_form = $form;
            return $this->render('assuranceform');
        }
        
        // Form is valid -> get values for processing
        $values = $form->getValues();
        
        // Get the current user
        $user = Default_Model_User::findCurrentUser();
        
        $this->view->assurancesDone = array();
        $quantity = $values['quantity'];
        do {
            // split up into multiple assurances
            if ($quantity > self::MAX_POINTS_PER_ASSURANCE) {
                $points = self::MAX_POINTS_PER_ASSURANCE;
                $quantity -= self::MAX_POINTS_PER_ASSURANCE;
            } else {
                $points = $quantity;
                $quantity = 0;
            }
            
            // Get the assurer for this assurance
            $issued = $user->findNewAssurer()
                ->assure($user, $points, $values['location'], $values['date']);
            
            $this->view->assurancesDone[] = $issued;
        } while ($quantity > 0);
        
        return;
    }
    
    public function batchAssuranceAction() {
    	// Validate form
        $form = $this->getBatchAssuranceForm();
        if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
            $this->view->batch_assurance_form = $form;
            return $this->render('batch-assurance-form');
        }
        
        // Form is valid -> get values for processing
        $values = $form->getValues();
        
        $user = Default_Model_User::findCurrentUser();
        
        $location = $values['location'];
        $date = $values['date'];
        
        $this->view->assurances = array();
        
        for ($i = 0; $i < intval($values['quantity']); $i++) {
            $assuree = $user->findNewAssuree();
            
            if ($values['percentage'] === 'percentage') {
                $points = ($user->maxpoints() * intval($values['points'])) /100;
            }elseif ($values['percentage'] === 'absolute') {
                $points = intval($values['points']);
            }
            
            $user->assure($assuree, $points, $location, $date);
            
            $this->view->assurances[] = array(
                    'assuree'=>$assuree->getPrimEmail(),
                    'points'=>$points,
                    'location'=>$location,
                    'date'=>$date);
        }
        
        return;
    }
    
    public function adminIncreaseAction()
    {
        // Validate form
        $form = $this->getAdminIncreaseForm();
        if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
            $this->view->admin_increase_form = $form;
            return $this->render('admin-increase-form');
        }
        
        // Form is valid -> get values for processing
        $values = $form->getValues();
        
        // Get current user
        $user = Default_Model_User::findCurrentUser();
        
        $this->view->adminIncreasesDone = array();
        $points = $values['points'];
        
        // Only assign points within the limit if unlimited flag is not set
        if ($values['unlimited'] != '1') {
            if ($user->getPoints() >= self::MAX_POINTS_TOTAL) {
                // No more administrative increases should be done
                return;
            } elseif ($user->getPoints() + $points > self::MAX_POINTS_TOTAL) {
                $points = self::MAX_POINTS_TOTAL - $user->getPoints();
            }
        }
        
        $user->adminIncrease($points, $values['location'], $values['date']);
        $this->view->adminIncreasesDone[] = $points;
    
        return;
    }
    
    
    public function assurerChallengeAction()
    {
        // Validate form
        $form = $this->getAssurerChallengeForm();
        if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
            $this->view->assurer_challenge_form = $form;
            return $this->render('assurer-challenge-form');
        }
        
        // Form is valid -> get values for processing
        $values = $form->getValues();
        
        // Get user data
        $user = Default_Model_User::findCurrentUser();
        
        $user->assignChallenge(1, $values['variant']);
    }
    
    public function flagsAction()
    {
        $user = Default_Model_User::findCurrentUser();
        
        // Validate form
        $form = $this->getFlagsForm($user);
        $this->view->flags_form = $form;
        if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
            return;
        }
        
        $flags = $user->getFlags();
        foreach ($flags as $flag => $value) {
            $element = $form->getElement($flag);
            if ($element !== null) {
                $flags[$flag] = $element->isChecked();
            }
        }
        
        $user->setFlags($flags);
        return;
    }
    
    protected function getAssuranceForm()
    {
        $form = new Zend_Form();
        $form->setAction('/manage-account/assurance')->setMethod('post');
        
        $quantity = new Zend_Form_Element_Text('quantity');
        $quantity->setRequired(true)
                ->setLabel(I18n::_('Number of Points'))
                ->addFilter(new Zend_Filter_Int())
                ->addValidator(new Zend_Validate_Between(0, 100));
        $form->addElement($quantity);
        
        $location = new Zend_Form_Element_Text('location');
        $location->setRequired(true)
                ->setLabel(I18n::_('Location'))
                ->setValue(I18n::_('CAcert Test Manager'))
                ->addValidator(new Zend_Validate_StringLength(1,255));
        $form->addElement($location);
        
        $date = new Zend_Form_Element_Text('date');
        $date->setRequired(true)
            ->setLabel(I18n::_('Date of Assurance'))
            ->setValue(date('Y-m-d H:i:s'))
            ->addValidator(new Zend_Validate_StringLength(1,255));
        $form->addElement($date);
        
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel(I18n::_('Assure Me'));
        $form->addElement($submit);
        
        return $form;
    }
    
    protected function getBatchAssuranceForm() {
    	$form = new Zend_Form();
        $form->setAction('/manage-account/batch-assurance')->setMethod('post');
        
        $quantity = new Zend_Form_Element_Text('quantity');
        $quantity->setRequired(true)
                ->setLabel(I18n::_('Number of Assurances'))
                ->setValue('25')
                ->addFilter(new Zend_Filter_Int())
                ->addValidator(new Zend_Validate_Between(0, 100));
        $form->addElement($quantity);
        
        $percentage = new Zend_Form_Element_Select('percentage');
        $percentage->setRequired(true)
                ->setLabel(I18n::_('Are the points specified absolute?'))
                ->setValue('percentage')
                ->setMultiOptions(array(
                	    'percentage' => I18n::_('Percentage'),
                        'absolute' => I18n::_('Absolute'),
                    ));
        $form->addElement($percentage);
        
        $points = new Zend_Form_Element_Text('points');
        $points->setRequired(true)
            ->setLabel(I18n::_('Points per Assurance'))
            ->setValue('100')
            ->addFilter(new Zend_Filter_Int())
            ->addValidator(new Zend_Validate_Between(0, 100));
        $form->addElement($points);
        
        $location = new Zend_Form_Element_Text('location');
        $location->setRequired(true)
                ->setLabel(I18n::_('Location'))
                ->setValue(I18n::_('CAcert Test Manager Batch Assurance'))
                ->addValidator(new Zend_Validate_StringLength(1,255));
        $form->addElement($location);
        
        $date = new Zend_Form_Element_Text('date');
        $date->setRequired(true)
            ->setLabel(I18n::_('Date of Assurance'))
            ->setValue(date('Y-m-d H:i:s'))
            ->addValidator(new Zend_Validate_StringLength(1,255));
        $form->addElement($date);
        
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel(I18n::_('Make Batch Assurance'));
        $form->addElement($submit);
        
        return $form;
    }
    
    protected function getAdminIncreaseForm()
    {
        $form = new Zend_Form();
        $form->setAction('/manage-account/admin-increase')->setMethod('post');
        
        $points = new Zend_Form_Element_Text('points');
        $points->setRequired(true)
                ->setLabel(I18n::_('Number of Points'))
                ->addFilter(new Zend_Filter_Int())
                ->addValidator(new Zend_Validate_GreaterThan(0));
        $form->addElement($points);
        
        $unlimited = new Zend_Form_Element_Checkbox('unlimited');
        $unlimited->setLabel(I18n::_('Assign Points even if the Limit of 150 '.
                        'is exceeded'))
                ->setChecked(false);
        $form->addElement($unlimited);
        
        $location = new Zend_Form_Element_Text('location');
        $location->setRequired(true)
                ->setLabel(I18n::_('Location'))
                ->setValue(I18n::_('CAcert Test Manager'))
                ->addValidator(new Zend_Validate_StringLength(1,255));
        $form->addElement($location);
        
        $date = new Zend_Form_Element_Text('date');
        $date->setRequired(true)
            ->setLabel(I18n::_('Date of Increase'))
            ->setValue(date('Y-m-d H:i:s'))
            ->addValidator(new Zend_Validate_StringLength(1,255));
        $form->addElement($date);
        
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel(I18n::_('Give Me Points'));
        $form->addElement($submit);
        
        return $form;
    }
    
    protected function getAssurerChallengeForm()
    {
        $form = new Zend_Form();
        $form->setAction('/manage-account/assurer-challenge')
            ->setMethod('post');
        
        $variant = new Zend_Form_Element_Select('variant');
        $variant->setLabel(I18n::_('Variant'));
        $options =
            Default_Model_User::getAvailableChallengeVariants($this->db, 1);
        $variant->setMultiOptions($options)
            ->setRequired(true);
        $form->addElement($variant);
        
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel(I18n::_('Challenge Me'));
        $form->addElement($submit);
        
        return $form;
    }
    
    protected function getFlagsForm(Default_Model_User $user)
    {
        $form = new Zend_Form();
        $form->setAction('/manage-account/flags')
            ->setMethod('post');
        
        $flags = $user->getFlags();
        
        // Add a checkbox for each flag
        $labels = array();
        $labels['admin']           = I18n::_('Support Engineer');
        $labels['codesign']        = I18n::_('Code Signing');
        $labels['orgadmin']        = I18n::_('Organisation Assurer');
        $labels['ttpadmin']        = I18n::_('TTP Admin');
        $labels['board']           = I18n::_('Board Member');
        $labels['locadmin']        = I18n::_('Location Admin');
        $labels['tverify']         = I18n::_('TVerify');
        $labels['locked']          = I18n::_('Lock Account');
        $labels['assurer_blocked'] = I18n::_('Block Assurer');
        
        foreach ($labels as $flag => $label) {
            $checkbox = new Zend_Form_Element_Checkbox($flag);
            $checkbox->setLabel($label)
                ->setChecked($flags[$flag]);
            $form->addElement($checkbox);
        }
        
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel(I18n::_('Save Flags'));
        $form->addElement($submit);
        
        return $form;
    }
}