cc7efa0999f0b10fae3932ca5f19cd4070d725d1
[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_ASSURANCE_POINTS = 100;
10 const MAX_POINTS_TOTAL = 150;
11 const ADMIN_INCREASE_FRAGMENT_SIZE = 2;
12
13 // Value used in the database to identify a admin increase
14 const ADMIN_INCREASE_METHOD = 'Administrative Increase';
15
16 protected $db;
17
18 public function init()
19 {
20 $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini',
21 APPLICATION_ENV);
22
23 $this->db = Zend_Db::factory($config->ca_mgr->db->auth->pdo,
24 $config->ca_mgr->db->auth);
25 }
26
27 public function indexAction()
28 {
29 // Just render the view
30 return;
31 }
32
33 public function assuranceAction()
34 {
35 // Validate form
36 $form = $this->getAssuranceForm();
37 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
38 $this->view->assurance_form = $form;
39 return $this->render('assuranceform');
40 }
41
42 // Form is valid -> get values for processing
43 $values = $form->getValues();
44
45 // Get user data
46 $user['id'] = $this->getUserId();
47 $user['points'] = $this->getPoints($user['id']);
48
49
50 // Do the actual assurances
51 $assurance = array(); // Make sure the array is empty
52 $assurance['to'] = $user['id'];
53 $assurance['location'] = $values['location'];
54 $assurance['date'] = $values['date'];
55 $assurance['when'] = new Zend_Db_Expr('now()');
56 $this->view->assurancesDone = array();
57
58 $quantity = $values['quantity'];
59 do {
60 // split up into multiple assurances
61 if ($quantity > self::MAX_POINTS_PER_ASSURANCE) {
62 $assurance['awarded'] = self::MAX_POINTS_PER_ASSURANCE;
63 $quantity -= self::MAX_POINTS_PER_ASSURANCE;
64 } else {
65 $assurance['awarded'] = $quantity;
66 $quantity = 0;
67 }
68
69 // Get the assurer for this assurance
70 $assurance['from'] = $this->getNewAssurer($user['id']);
71
72 // only assign points whithin the limit
73 if ($user['points'] + $assurance['awarded'] > self::MAX_ASSURANCE_POINTS){
74 $assurance['points'] = self::MAX_ASSURANCE_POINTS - $user['points'];
75 } else {
76 $assurance['points'] = $assurance['awarded'];
77 }
78
79 $this->db->insert('notary', $assurance);
80
81 $user['points'] += $assurance['points'];
82 $this->view->assurancesDone[] = $assurance['points'];
83 } while ($quantity > 0);
84
85
86 // Maybe user is now assurer
87 $this->fixAssurerFlag($user['id']);
88
89 return;
90 }
91
92 public function adminIncreaseAction()
93 {
94 // Validate form
95 $form = $this->getAdminIncreaseForm();
96 if (!$this->getRequest()->isPost() || !$form->isValid($_POST)) {
97 $this->view->admin_increase_form = $form;
98 return $this->render('admin-increase-form');
99 }
100
101 // Form is valid -> get values for processing
102 $values = $form->getValues();
103
104 // Get user data
105 $user['id'] = $this->getUserId();
106 $user['points'] = $this->getPoints($user['id']);
107
108
109 // Do the actual increase
110 $increase = array(); // Make sure the array is empty
111 $increase['from'] = $user['id'];
112 $increase['to'] = $user['id'];
113 $increase['location'] = $values['location'];
114 $increase['date'] = $values['date'];
115 $increase['method'] = self::ADMIN_INCREASE_METHOD;
116 $increase['when'] = new Zend_Db_Expr('now()');
117 $this->view->adminIncreasesDone = array();
118
119 $quantity = $values['quantity'];
120 do {
121 // Split up into multiple increases if fragment flag is set
122 if ($values['fragment'] == '1' &&
123 $quantity > self::ADMIN_INCREASE_FRAGMENT_SIZE) {
124 $increase['awarded'] = self::ADMIN_INCREASE_FRAGMENT_SIZE;
125 $quantity -= self::ADMIN_INCREASE_FRAGMENT_SIZE;
126 } else {
127 $increase['awarded'] = $quantity;
128 $quantity = 0;
129 }
130
131 // Only assign points within the limit if unlimited flag is not set
132 if ($values['unlimited'] != '1') {
133 if ($user['points'] >= self::MAX_POINTS_TOTAL) {
134 // No more administrative increases should be done
135 break;
136 } elseif ($user['points'] + $increase['awarded'] > self::MAX_POINTS_TOTAL) {
137 $increase['awarded'] = self::MAX_POINTS_TOTAL - $user['points'];
138 }
139 }
140
141 // Admin increases always have `points` == `awarded`
142 $increase['points'] = $increase['awarded'];
143
144 $this->db->insert('notary', $increase);
145
146 $user['points'] += $increase['points'];
147 $this->view->adminIncreasesDone[] = $increase['points'];
148 } while ($quantity > 0);
149
150 // Maybe user is now assurer
151 $this->fixAssurerFlag($user['id']);
152
153 return;
154 }
155
156 /**
157 * Get and check the user ID of the current user
158 *
159 * @return int The ID of the current user
160 */
161 protected function getUserId()
162 {
163 $session = Zend_Registry::get('session');
164 if ($session->authdata['authed'] !== true) {
165 throw new Exception(__METHOD__ . ': you need to log in to use this feature');
166 }
167
168 // Check if the ID is present on the test server
169 $query = 'select `id` from `users` where `id` = :user';
170 $query_params['user'] = $session->authdata['authed_id'];
171 $result = $this->db->query($query, $query_params);
172 if ($result->rowCount() !== 1) {
173 throw new Exception(__METHOD__ . ': user ID not found in the data base');
174 }
175 $row = $result->fetch();
176
177 return $row['id'];
178 }
179
180 /**
181 * Get current points of the user
182 *
183 * @param int $user_id ID of the user
184 * @return int the amount of points the user currently has
185 */
186 protected function getPoints($user_id)
187 {
188 $query = 'select sum(`points`) as `total` from `notary` where `to` = :user';
189 $query_params['user'] = $user_id;
190 $row = $this->db->query($query, $query_params)->fetch();
191 if ($row['total'] === NULL) $row['total'] = 0;
192
193 return $row['total'];
194 }
195
196 /**
197 * Get the first assurer who didn't already assure the user
198 *
199 * @param int $user_id The ID of the user who should get assured
200 * @return int The ID of the selected assurer
201 */
202 protected function getNewAssurer($user_id)
203 {
204 $query = 'select min(`id`) as `assurer` from `users` ' .
205 'where `email` like \'john.doe-___@example.com\' and ' .
206 '`id` not in (select `from` from `notary` where `to` = :user)';
207 $query_params['user'] = $user_id;
208 $row = $this->db->query($query, $query_params)->fetch();
209
210 if ($row['assurer'] === NULL) {
211 throw new Exception(__METHOD__ . ': no more assurers that haven\'t '.
212 'already assured this account');
213 }
214
215 return $row['assurer'];
216 }
217
218 /**
219 * Fix the assurer flag for the given user
220 *
221 * @param $user_id ID of the user
222 */
223 protected function fixAssurerFlag($user_id)
224 {
225 // TODO: unset flag if requirements are not met
226
227 $query = 'UPDATE `users` SET `assurer` = 1 WHERE `users`.`id` = :user AND '.
228
229 'EXISTS(SELECT * FROM `cats_passed` AS `cp`, `cats_variant` AS `cv` '.
230 'WHERE `cp`.`variant_id` = `cv`.`id` AND `cv`.`type_id` = 1 AND '.
231 '`cp`.`user_id` = :user) AND '.
232
233 '(SELECT SUM(`points`) FROM `notary` WHERE `to` = :user AND '.
234 '`expire` < now()) >= 100';
235 $query_params['user'] = $user_id;
236 $this->db->query($query, $query_params);
237 }
238
239 protected function getAssuranceForm()
240 {
241 $form = new Zend_Form();
242 $form->setAction('/manage-account/assurance')->setMethod('post');
243
244 $quantity = new Zend_Form_Element_Text('quantity');
245 $quantity->setRequired(true)
246 ->setLabel(I18n::_('Number of Points'))
247 ->addFilter(new Zend_Filter_Int())
248 ->addValidator(new Zend_Validate_Between(0, 100));
249 $form->addElement($quantity);
250
251 $location = new Zend_Form_Element_Text('location');
252 $location->setRequired(true)
253 ->setLabel(I18n::_('Location'))
254 ->setValue(I18n::_('CAcert Test Manager'))
255 ->addValidator(new Zend_Validate_StringLength(1,255));
256 $form->addElement($location);
257
258 $date = new Zend_Form_Element_Text('date');
259 $date->setRequired(true)
260 ->setLabel(I18n::_('Date of Assurance'))
261 ->setValue(date('Y-m-d H:i:s'))
262 ->addValidator(new Zend_Validate_StringLength(1,255));
263 $form->addElement($date);
264
265 $submit = new Zend_Form_Element_Submit('submit');
266 $submit->setLabel(I18n::_('Assure Me'));
267 $form->addElement($submit);
268
269 return $form;
270 }
271
272 protected function getAdminIncreaseForm()
273 {
274 $form = new Zend_Form();
275 $form->setAction('/manage-account/admin-increase')->setMethod('post');
276
277 $quantity = new Zend_Form_Element_Text('quantity');
278 $quantity->setRequired(true)
279 ->setLabel(I18n::_('Number of Points'))
280 ->addFilter(new Zend_Filter_Int())
281 ->addValidator(new Zend_Validate_GreaterThan(0));
282 $form->addElement($quantity);
283
284 $fragment = new Zend_Form_Element_Checkbox('fragment');
285 $fragment->setLabel(I18n::_('Split into 2-Point Fragments'))
286 ->setChecked(true);
287 $form->addElement($fragment);
288
289 $unlimited = new Zend_Form_Element_Checkbox('unlimited');
290 $unlimited->setLabel(I18n::_('Assign Points even if the Limit of 150 '.
291 'is exceeded'))
292 ->setChecked(false);
293 $form->addElement($unlimited);
294
295 $location = new Zend_Form_Element_Text('location');
296 $location->setRequired(true)
297 ->setLabel(I18n::_('Location'))
298 ->setValue(I18n::_('CAcert Test Manager'))
299 ->addValidator(new Zend_Validate_StringLength(1,255));
300 $form->addElement($location);
301
302 $date = new Zend_Form_Element_Text('date');
303 $date->setRequired(true)
304 ->setLabel(I18n::_('Date of Increase'))
305 ->setValue(date('Y-m-d H:i:s'))
306 ->addValidator(new Zend_Validate_StringLength(1,255));
307 $form->addElement($date);
308
309 $submit = new Zend_Form_Element_Submit('submit');
310 $submit->setLabel(I18n::_('Give Me Points'));
311 $form->addElement($submit);
312
313 return $form;
314 }
315 }