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