Implement administrative increase in the user model
[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 $quantity = $values['quantity'];
101 do {
102 // Split up into multiple increases if fragment flag is set
103 if ($values['fragment'] == '1' &&
104 $quantity > self::ADMIN_INCREASE_FRAGMENT_SIZE) {
105 $points = self::ADMIN_INCREASE_FRAGMENT_SIZE;
106 $quantity -= self::ADMIN_INCREASE_FRAGMENT_SIZE;
107 } else {
108 $points = $quantity;
109 $quantity = 0;
110 }
111
112 // Only assign points within the limit if unlimited flag is not set
113 if ($values['unlimited'] != '1') {
114 if ($user->getPoints() >= self::MAX_POINTS_TOTAL) {
115 // No more administrative increases should be done
116 break;
117 } elseif ($user->getPoints() + $points > self::MAX_POINTS_TOTAL) {
118 $points = self::MAX_POINTS_TOTAL - $user->getPoints();
119 }
120 }
121
122 $user->adminIncrease($points, $values['location'], $values['date']);
123 $this->view->adminIncreasesDone[] = $points;
124 } while ($quantity > 0);
125
126 return;
127 }
128
129
130 public function assurerChallengeAction()
131 {
132 // Validate form
133 $form = $this->getAssurerChallengeForm();
134 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
135 $this->view->assurer_challenge_form = $form;
136 return $this->render('assurer-challenge-form');
137 }
138
139 // Form is valid -> get values for processing
140 $values = $form->getValues();
141
142 // Get user data
143 $user['id'] = $this->getUserId();
144
145 // Assign the assurer challenge
146 $challenge = array(); // Make sure the array is empty
147 $challenge['user_id'] = $user['id'];
148 $challenge['variant_id'] = $values['variant'];
149 $challenge['pass_date'] = date('Y-m-d H:i:s');
150 $this->db->insert('cats_passed', $challenge);
151
152 // Maybe user is now assurer
153 $this->fixAssurerFlag($user['id']);
154
155 return;
156 }
157
158 public function flagsAction()
159 {
160 // Get user data
161 $user['id'] = $this->getUserId();
162
163 // Validate form
164 $form = $this->getFlagsForm($user['id']);
165 $this->view->flags_form = $form;
166 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
167 return;
168 }
169
170 $flags = array('admin', 'codesign', 'orgadmin', 'ttpadmin', 'board',
171 'locadmin', 'locked', 'assurer_blocked');
172 $update = array(); // Make sure array is empty
173 foreach ($flags as $flag) {
174 if ($form->getElement($flag)->isChecked()) {
175 $update[$flag] = 1;
176 } else {
177 $update[$flag] = 0;
178 }
179 }
180 $this->db->update('users', $update, '`id` = '.$user['id']);
181
182 return;
183 }
184
185 protected function getAssuranceForm()
186 {
187 $form = new Zend_Form();
188 $form->setAction('/manage-account/assurance')->setMethod('post');
189
190 $quantity = new Zend_Form_Element_Text('quantity');
191 $quantity->setRequired(true)
192 ->setLabel(I18n::_('Number of Points'))
193 ->addFilter(new Zend_Filter_Int())
194 ->addValidator(new Zend_Validate_Between(0, 100));
195 $form->addElement($quantity);
196
197 $location = new Zend_Form_Element_Text('location');
198 $location->setRequired(true)
199 ->setLabel(I18n::_('Location'))
200 ->setValue(I18n::_('CAcert Test Manager'))
201 ->addValidator(new Zend_Validate_StringLength(1,255));
202 $form->addElement($location);
203
204 $date = new Zend_Form_Element_Text('date');
205 $date->setRequired(true)
206 ->setLabel(I18n::_('Date of Assurance'))
207 ->setValue(date('Y-m-d H:i:s'))
208 ->addValidator(new Zend_Validate_StringLength(1,255));
209 $form->addElement($date);
210
211 $submit = new Zend_Form_Element_Submit('submit');
212 $submit->setLabel(I18n::_('Assure Me'));
213 $form->addElement($submit);
214
215 return $form;
216 }
217
218 protected function getAdminIncreaseForm()
219 {
220 $form = new Zend_Form();
221 $form->setAction('/manage-account/admin-increase')->setMethod('post');
222
223 $quantity = new Zend_Form_Element_Text('quantity');
224 $quantity->setRequired(true)
225 ->setLabel(I18n::_('Number of Points'))
226 ->addFilter(new Zend_Filter_Int())
227 ->addValidator(new Zend_Validate_GreaterThan(0));
228 $form->addElement($quantity);
229
230 $fragment = new Zend_Form_Element_Checkbox('fragment');
231 $fragment->setLabel(I18n::_('Split into 2-Point Fragments'))
232 ->setChecked(true);
233 $form->addElement($fragment);
234
235 $unlimited = new Zend_Form_Element_Checkbox('unlimited');
236 $unlimited->setLabel(I18n::_('Assign Points even if the Limit of 150 '.
237 'is exceeded'))
238 ->setChecked(false);
239 $form->addElement($unlimited);
240
241 $location = new Zend_Form_Element_Text('location');
242 $location->setRequired(true)
243 ->setLabel(I18n::_('Location'))
244 ->setValue(I18n::_('CAcert Test Manager'))
245 ->addValidator(new Zend_Validate_StringLength(1,255));
246 $form->addElement($location);
247
248 $date = new Zend_Form_Element_Text('date');
249 $date->setRequired(true)
250 ->setLabel(I18n::_('Date of Increase'))
251 ->setValue(date('Y-m-d H:i:s'))
252 ->addValidator(new Zend_Validate_StringLength(1,255));
253 $form->addElement($date);
254
255 $submit = new Zend_Form_Element_Submit('submit');
256 $submit->setLabel(I18n::_('Give Me Points'));
257 $form->addElement($submit);
258
259 return $form;
260 }
261
262 protected function getAssurerChallengeForm()
263 {
264 $form = new Zend_Form();
265 $form->setAction('/manage-account/assurer-challenge')
266 ->setMethod('post');
267
268 $variant = new Zend_Form_Element_Select('variant');
269 $variant->setLabel(I18n::_('Variant'));
270 // Get the available variants from the database
271 $query = 'select `id`, `test_text` from `cats_variant`
272 where `type_id` = 1';
273 $options = $this->db->fetchPairs($query);
274 $variant->setMultiOptions($options)
275 ->setRequired(true);
276 $form->addElement($variant);
277
278 $submit = new Zend_Form_Element_Submit('submit');
279 $submit->setLabel(I18n::_('Challenge Me'));
280 $form->addElement($submit);
281
282 return $form;
283 }
284
285 protected function getFlagsForm($user_id)
286 {
287 $form = new Zend_Form();
288 $form->setAction('/manage-account/flags')
289 ->setMethod('post');
290
291 // Get the current setting of the flags
292 $query = 'select `admin`, `codesign`, `orgadmin`, `ttpadmin`, `board`,
293 `tverify`, `locadmin`, `locked`, `assurer_blocked` from `users`
294 where `id` = :user';
295 $query_params['user'] = $user_id;
296 $result = $this->db->query($query, $query_params);
297 if ($result->rowCount() !== 1) {
298 throw new Exception(__METHOD__ . ': user ID not found in the data base');
299 }
300 $row = $result->fetch();
301
302 // Add a checkbox for each flag
303 $labels = array();
304 $labels['admin'] = I18n::_('Support Engineer');
305 $labels['codesign'] = I18n::_('Code Signing');
306 $labels['orgadmin'] = I18n::_('Organisation Admin');
307 $labels['ttpadmin'] = I18n::_('TTP Admin');
308 $labels['board'] = I18n::_('Board Member');
309 $labels['locadmin'] = I18n::_('Location Admin');
310 $labels['locked'] = I18n::_('Lock Account');
311 $labels['assurer_blocked'] = I18n::_('Block Assurer');
312
313 foreach ($labels as $flag => $label) {
314 $checkbox = new Zend_Form_Element_Checkbox($flag);
315 $checkbox->setLabel($label)
316 ->setChecked($row[$flag] === '1');
317 $form->addElement($checkbox);
318 }
319
320 $submit = new Zend_Form_Element_Submit('submit');
321 $submit->setLabel(I18n::_('Save Flags'));
322 $form->addElement($submit);
323
324 return $form;
325 }
326 }