f89b177e0e2e05ab094a036d33776c9683cc2886
[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 current user
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 = Default_Model_User::findCurrentUser();
133
134 $user->assignChallenge(1, $values['variant']);
135 }
136
137 public function flagsAction()
138 {
139 // Get user data
140 $user['id'] = $this->getUserId();
141
142 // Validate form
143 $form = $this->getFlagsForm($user['id']);
144 $this->view->flags_form = $form;
145 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
146 return;
147 }
148
149 $flags = array('admin', 'codesign', 'orgadmin', 'ttpadmin', 'board',
150 'locadmin', 'locked', 'assurer_blocked');
151 $update = array(); // Make sure array is empty
152 foreach ($flags as $flag) {
153 if ($form->getElement($flag)->isChecked()) {
154 $update[$flag] = 1;
155 } else {
156 $update[$flag] = 0;
157 }
158 }
159 $this->db->update('users', $update, '`id` = '.$user['id']);
160
161 return;
162 }
163
164 protected function getAssuranceForm()
165 {
166 $form = new Zend_Form();
167 $form->setAction('/manage-account/assurance')->setMethod('post');
168
169 $quantity = new Zend_Form_Element_Text('quantity');
170 $quantity->setRequired(true)
171 ->setLabel(I18n::_('Number of Points'))
172 ->addFilter(new Zend_Filter_Int())
173 ->addValidator(new Zend_Validate_Between(0, 100));
174 $form->addElement($quantity);
175
176 $location = new Zend_Form_Element_Text('location');
177 $location->setRequired(true)
178 ->setLabel(I18n::_('Location'))
179 ->setValue(I18n::_('CAcert Test Manager'))
180 ->addValidator(new Zend_Validate_StringLength(1,255));
181 $form->addElement($location);
182
183 $date = new Zend_Form_Element_Text('date');
184 $date->setRequired(true)
185 ->setLabel(I18n::_('Date of Assurance'))
186 ->setValue(date('Y-m-d H:i:s'))
187 ->addValidator(new Zend_Validate_StringLength(1,255));
188 $form->addElement($date);
189
190 $submit = new Zend_Form_Element_Submit('submit');
191 $submit->setLabel(I18n::_('Assure Me'));
192 $form->addElement($submit);
193
194 return $form;
195 }
196
197 protected function getAdminIncreaseForm()
198 {
199 $form = new Zend_Form();
200 $form->setAction('/manage-account/admin-increase')->setMethod('post');
201
202 $points = new Zend_Form_Element_Text('points');
203 $points->setRequired(true)
204 ->setLabel(I18n::_('Number of Points'))
205 ->addFilter(new Zend_Filter_Int())
206 ->addValidator(new Zend_Validate_GreaterThan(0));
207 $form->addElement($points);
208
209 $unlimited = new Zend_Form_Element_Checkbox('unlimited');
210 $unlimited->setLabel(I18n::_('Assign Points even if the Limit of 150 '.
211 'is exceeded'))
212 ->setChecked(false);
213 $form->addElement($unlimited);
214
215 $location = new Zend_Form_Element_Text('location');
216 $location->setRequired(true)
217 ->setLabel(I18n::_('Location'))
218 ->setValue(I18n::_('CAcert Test Manager'))
219 ->addValidator(new Zend_Validate_StringLength(1,255));
220 $form->addElement($location);
221
222 $date = new Zend_Form_Element_Text('date');
223 $date->setRequired(true)
224 ->setLabel(I18n::_('Date of Increase'))
225 ->setValue(date('Y-m-d H:i:s'))
226 ->addValidator(new Zend_Validate_StringLength(1,255));
227 $form->addElement($date);
228
229 $submit = new Zend_Form_Element_Submit('submit');
230 $submit->setLabel(I18n::_('Give Me Points'));
231 $form->addElement($submit);
232
233 return $form;
234 }
235
236 protected function getAssurerChallengeForm()
237 {
238 $form = new Zend_Form();
239 $form->setAction('/manage-account/assurer-challenge')
240 ->setMethod('post');
241
242 $variant = new Zend_Form_Element_Select('variant');
243 $variant->setLabel(I18n::_('Variant'));
244 $options =
245 Default_Model_User::getAvailableChallengeVariants($this->db, 1);
246 $variant->setMultiOptions($options)
247 ->setRequired(true);
248 $form->addElement($variant);
249
250 $submit = new Zend_Form_Element_Submit('submit');
251 $submit->setLabel(I18n::_('Challenge Me'));
252 $form->addElement($submit);
253
254 return $form;
255 }
256
257 protected function getFlagsForm($user_id)
258 {
259 $form = new Zend_Form();
260 $form->setAction('/manage-account/flags')
261 ->setMethod('post');
262
263 // Get the current setting of the flags
264 $query = 'select `admin`, `codesign`, `orgadmin`, `ttpadmin`, `board`,
265 `tverify`, `locadmin`, `locked`, `assurer_blocked` from `users`
266 where `id` = :user';
267 $query_params['user'] = $user_id;
268 $result = $this->db->query($query, $query_params);
269 if ($result->rowCount() !== 1) {
270 throw new Exception(__METHOD__ . ': user ID not found in the data base');
271 }
272 $row = $result->fetch();
273
274 // Add a checkbox for each flag
275 $labels = array();
276 $labels['admin'] = I18n::_('Support Engineer');
277 $labels['codesign'] = I18n::_('Code Signing');
278 $labels['orgadmin'] = I18n::_('Organisation Admin');
279 $labels['ttpadmin'] = I18n::_('TTP Admin');
280 $labels['board'] = I18n::_('Board Member');
281 $labels['locadmin'] = I18n::_('Location Admin');
282 $labels['locked'] = I18n::_('Lock Account');
283 $labels['assurer_blocked'] = I18n::_('Block Assurer');
284
285 foreach ($labels as $flag => $label) {
286 $checkbox = new Zend_Form_Element_Checkbox($flag);
287 $checkbox->setLabel($label)
288 ->setChecked($row[$flag] === '1');
289 $form->addElement($checkbox);
290 }
291
292 $submit = new Zend_Form_Element_Submit('submit');
293 $submit->setLabel(I18n::_('Save Flags'));
294 $form->addElement($submit);
295
296 return $form;
297 }
298 }