2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
11 // | There are only two ways to violate the license:
13 // | 1. To redistribute this code in source form, with the copyright
14 // | notice or license removed or altered. (Distributing in compiled
15 // | forms without embedded copyright notices is permitted).
17 // | 2. To redistribute modified versions of this code in *any* form
18 // | that bears insufficient indications that the modifications are
19 // | not the work of the original author(s).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import('ttTeamHelper');
31 // Class ttUserHelper contains helper functions for operations with users.
34 // The getUserDetails function returns user details.
35 static function getUserDetails($user_id) {
37 $mdb2 = getConnection();
39 $sql = "select * from tt_users where id = $user_id";
40 $res = $mdb2->query($sql);
42 if (!is_a($res, 'PEAR_Error')) {
43 $val = $res->fetchRow();
49 // The getUserName function returns user name.
50 static function getUserName($user_id) {
51 $mdb2 = getConnection();
53 $sql = "select name from tt_users where id = $user_id and (status = 1 or status = 0)";
54 $res = $mdb2->query($sql);
56 if (!is_a($res, 'PEAR_Error')) {
57 $val = $res->fetchRow();
63 // The getUserByLogin function obtains data for a user, who is identified by login.
64 static function getUserByLogin($login) {
65 $mdb2 = getConnection();
67 $sql = "select id, name from tt_users where login = ".$mdb2->quote($login)." and (status = 1 or status = 0)";
68 $res = $mdb2->query($sql);
69 if (!is_a($res, 'PEAR_Error')) {
70 if ($val = $res->fetchRow()) {
77 // The getUserByEmail function is a helper function that tries to obtain user details identified by email.
78 // This function works only when one such active user exists.
79 static function getUserByEmail($email) {
80 $mdb2 = getConnection();
82 $sql = "select login, count(*) as cnt from tt_users where email = ".$mdb2->quote($email)." and status = 1 group by email";
83 $res = $mdb2->query($sql);
85 if (is_a($res, 'PEAR_Error'))
88 $val = $res->fetchRow();
89 if (1 <> $val['cnt']) {
90 // We either have no users or multiple users with a given email.
96 // The getUserIdByTmpRef obtains user id from a temporary reference (used for password resets).
97 static function getUserIdByTmpRef($ref) {
98 $mdb2 = getConnection();
100 $sql = "select user_id from tt_tmp_refs where ref = ".$mdb2->quote($ref);
101 $res = $mdb2->query($sql);
103 if (!is_a($res, 'PEAR_Error')) {
104 $val = $res->fetchRow();
105 return $val['user_id'];
110 // insert - inserts a user into database.
111 static function insert($fields, $hash = true) {
112 $mdb2 = getConnection();
114 $password = $mdb2->quote($fields['password']);
116 $password = 'md5('.$password.')';
117 $email = isset($fields['email']) ? $fields['email'] : '';
118 $team_id = (int) $fields['team_id'];
119 $role = (int) $fields['role'];
120 $rate = str_replace(',', '.', isset($fields['rate']) ? $fields['rate'] : 0);
123 if (array_key_exists('status', $fields)) { // Key exists and may be NULL during migration of deleted acounts.
124 $status_f = ', status';
125 $status_v = ', '.$mdb2->quote($fields['status']);
128 $sql = "insert into tt_users (name, login, password, team_id, role, client_id, rate, email $status_f) values (".
129 $mdb2->quote($fields['name']).", ".$mdb2->quote($fields['login']).
130 ", $password, $team_id, $role, ".$mdb2->quote($fields['client_id']).", $rate, ".$mdb2->quote($email)." $status_v)";
131 $affected = $mdb2->exec($sql);
133 // Now deal with project assignment.
134 if (!is_a($affected, 'PEAR_Error')) {
135 $sql = "SELECT LAST_INSERT_ID() AS last_id";
136 $res = $mdb2->query($sql);
137 $val = $res->fetchRow();
138 $last_id = $val['last_id'];
140 $projects = isset($fields['projects']) ? $fields['projects'] : array();
141 if (count($projects) > 0) {
142 // We have at least one project assigned. Insert corresponding entries in tt_user_project_binds table.
143 foreach($projects as $p) {
144 if(!isset($p['rate']))
147 $p['rate'] = str_replace(',', '.', $p['rate']);
149 $sql = "insert into tt_user_project_binds (project_id, user_id, rate, status) values(".$p['id'].",".$last_id.",".$p['rate'].", 1)";
150 $affected = $mdb2->exec($sql);
158 // update - updates a user in database.
159 static function update($user_id, $fields) {
161 $mdb2 = getConnection();
164 if (!$user_id || !isset($fields['login']))
167 // Prepare query parts.
168 if (isset($fields['password']))
169 $pass_part = ', password = md5('.$mdb2->quote($fields['password']).')';
170 if (right_assign_roles & $user->rights) {
171 if (isset($fields['role'])) {
172 $role = (int) $fields['role'];
173 $role_part = ", role = $role";
175 if (array_key_exists('client_id', $fields)) // Could be NULL.
176 $client_part = ", client_id = ".$mdb2->quote($fields['client_id']);
179 if (array_key_exists('rate', $fields)) {
180 $rate = str_replace(',', '.', isset($fields['rate']) ? $fields['rate'] : 0);
181 if($rate == '') $rate = 0;
182 $rate_part = ", rate = ".$mdb2->quote($rate);
185 if (isset($fields['status'])) {
186 $status = (int) $fields['status'];
187 $status_part = ", status = $status";
190 $sql = "update tt_users set login = ".$mdb2->quote($fields['login']).
191 "$pass_part, name = ".$mdb2->quote($fields['name']).
192 "$role_part $client_part $rate_part $status_part, email = ".$mdb2->quote($fields['email']).
193 " where id = $user_id";
194 $affected = $mdb2->exec($sql);
195 if (is_a($affected, 'PEAR_Error')) return false;
197 if (array_key_exists('projects', $fields)) {
198 // Deal with project assignments.
199 // Note: we cannot simply delete old project binds and insert new ones because it screws up reporting
200 // (when looking for cost while entries for de-assigned projects exist).
201 // Therefore, we must iterate through all projects and only delete the binds when no time entries are present,
202 // otherwise de-activate the bind (set its status to inactive). This will keep the bind
203 // and its rate in database for reporting.
205 $all_projects = ttTeamHelper::getAllProjects($user->team_id);
206 $assigned_projects = isset($fields['projects']) ? $fields['projects'] : array();
208 foreach($all_projects as $p) {
209 // Determine if a project is assigned.
211 $project_id = $p['id'];
213 if (count($assigned_projects) > 0) {
214 foreach ($assigned_projects as $ap) {
215 if ($project_id == $ap['id']) {
219 $rate = str_replace(",",".",$rate);
227 ttUserHelper::deleteBind($user_id, $project_id);
229 // Here we need to either update or insert new tt_user_project_binds record.
230 // Determine if a record exists.
231 $sql = "select id from tt_user_project_binds where user_id = $user_id and project_id = $project_id";
232 $res = $mdb2->query($sql);
233 if (is_a($res, 'PEAR_Error')) die ($res->getMessage());
234 if ($val = $res->fetchRow()) {
235 // Record exists. Update it.
236 $sql = "update tt_user_project_binds set status = 1, rate = $rate where id = ".$val['id'];
237 $affected = $mdb2->exec($sql);
238 if (is_a($affected, 'PEAR_Error')) die ($affected->getMessage());
240 // Record does not exist. Insert it.
241 ttUserHelper::insertBind($user_id, $project_id, $rate, 1);
249 // markDeleted - marks user and its associated things as deleted.
250 static function markDeleted($user_id) {
251 $mdb2 = getConnection();
254 // Preliminary checks. Only managers, co-managers, and admin can do this.
255 if (!$user->canManageTeam() && !$user->isAdmin())
258 // Tho logic is different depending on who is doing the operation.
259 // Co-manager and admin - mark user deleted.
260 // Manager - mark user deleted. If manager is the only account in team, mark team items deleted.
263 if ($user->isAdmin()) {
264 // Mark user binds as deleted.
265 $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
266 $affected = $mdb2->exec($sql);
267 if (is_a($affected, 'PEAR_Error'))
270 // Mark favorite reports as deleted.
271 $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
272 $affected = $mdb2->exec($sql);
273 if (is_a($affected, 'PEAR_Error'))
276 // Mark user as deleted.
277 $sql = "update tt_users set status = NULL where id = $user_id";
278 $affected = $mdb2->exec($sql);
279 if (is_a($affected, 'PEAR_Error'))
282 } elseif ($user->isCoManager()) {
283 // Mark user binds as deleted.
284 $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
285 $affected = $mdb2->exec($sql);
286 if (is_a($affected, 'PEAR_Error'))
289 // Mark favorite reports as deleted.
290 $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
291 $affected = $mdb2->exec($sql);
292 if (is_a($affected, 'PEAR_Error'))
295 // Mark user as deleted.
296 $sql = "update tt_users set status = NULL where id = $user_id and team_id = ".$user->team_id;
297 $affected = $mdb2->exec($sql);
298 if (is_a($affected, 'PEAR_Error'))
301 } elseif ($user->isManager()) {
302 $user_count = ttTeamHelper::getUserCount($user->team_id);
304 // Marking deleted a manager with active users is not allowed.
305 if (($user_id == $user->id) && ($user_count > 1))
308 if (1 == $user_count) {
309 // Mark tasks deleted.
310 if (!ttTeamHelper::markTasksDeleted($user->team_id))
313 // Mark projects deleted.
314 $sql = "update tt_projects set status = NULL where team_id = $user->team_id";
315 $affected = $mdb2->exec($sql);
316 if (is_a($affected, 'PEAR_Error'))
319 // Mark clients deleted.
320 $sql = "update tt_clients set status = NULL where team_id = $user->team_id";
321 $affected = $mdb2->exec($sql);
322 if (is_a($affected, 'PEAR_Error'))
325 // Mark custom fields deleted.
326 $sql = "update tt_custom_fields set status = NULL where team_id = $user->team_id";
327 $affected = $mdb2->exec($sql);
328 if (is_a($affected, 'PEAR_Error'))
331 // Mark team deleted.
332 $sql = "update tt_teams set status = NULL where id = $user->team_id";
333 $affected = $mdb2->exec($sql);
334 if (is_a($affected, 'PEAR_Error'))
338 // Mark user binds as deleted.
339 $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
340 $affected = $mdb2->exec($sql);
341 if (is_a($affected, 'PEAR_Error'))
344 // Mark favorite reports as deleted.
345 $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
346 $affected = $mdb2->exec($sql);
347 if (is_a($affected, 'PEAR_Error'))
350 // Mark user as deleted.
351 $sql = "update tt_users set status = NULL where id = $user_id and team_id = ".$user->team_id;
352 $affected = $mdb2->exec($sql);
353 if (is_a($affected, 'PEAR_Error'))
360 // The delete function permanently deletes a user and all associated data.
361 static function delete($user_id) {
362 $mdb2 = getConnection();
364 // Delete custom field log entries for user, if we have them.
365 $sql = "delete from tt_custom_field_log where log_id in
366 (select id from tt_log where user_id = $user_id)";
367 $affected = $mdb2->exec($sql);
368 if (is_a($affected, 'PEAR_Error'))
371 // Delete log entries for user.
372 $sql = "delete from tt_log where user_id = $user_id";
373 $affected = $mdb2->exec($sql);
374 if (is_a($affected, 'PEAR_Error'))
377 // Delete expense items for user.
378 $sql = "delete from tt_expense_items where user_id = $user_id";
379 $affected = $mdb2->exec($sql);
380 if (is_a($affected, 'PEAR_Error'))
383 // Delete user binds.
384 $sql = "delete from tt_user_project_binds where user_id = $user_id";
385 $affected = $mdb2->exec($sql);
386 if (is_a($affected, 'PEAR_Error'))
389 // Clean up tt_config table.
390 $sql = "delete from tt_config where user_id = $user_id";
391 $affected = $mdb2->exec($sql);
392 if (is_a($affected, 'PEAR_Error'))
395 // Clean up tt_fav_reports table.
396 $sql = "delete from tt_fav_reports where user_id = $user_id";
397 $affected = $mdb2->exec($sql);
398 if (is_a($affected, 'PEAR_Error'))
402 $sql = "delete from tt_users where id = $user_id";
403 $affected = $mdb2->exec($sql);
404 if (is_a($affected, 'PEAR_Error'))
410 // The saveTmpRef saves a temporary reference for user that is used to reset user password.
411 static function saveTmpRef($ref, $user_id) {
412 $mdb2 = getConnection();
414 $sql = "delete from tt_tmp_refs where timestamp + 86400 < now()";
415 $affected = $mdb2->exec($sql);
417 $sql = "insert into tt_tmp_refs (ref, user_id) values(".$mdb2->quote($ref).", $user_id)";
418 $affected = $mdb2->exec($sql);
421 // The setPassword function updates password for user.
422 static function setPassword($user_id, $password) {
423 $mdb2 = getConnection();
425 $sql = "update tt_users set password = md5(".$mdb2->quote($password).") where id = $user_id";
426 $affected = $mdb2->exec($sql);
428 return (!is_a($affected, 'PEAR_Error'));
431 // insertBind - inserts a user to project bind into tt_user_project_binds table.
432 static function insertBind($user_id, $project_id, $rate, $status) {
433 $mdb2 = getConnection();
435 $sql = "insert into tt_user_project_binds (user_id, project_id, rate, status)
436 values($user_id, $project_id, ".$mdb2->quote($rate).", $status)";
437 $affected = $mdb2->exec($sql);
438 return (!is_a($affected, 'PEAR_Error'));
441 // deleteBind - deactivates user to project bind when time entries exist,
442 // otherwise deletes it entirely.
443 static function deleteBind($user_id, $project_id) {
444 $mdb2 = getConnection();
446 $sql = "select count(*) as cnt from tt_log where
447 user_id = $user_id and project_id = $project_id and status = 1";
448 $res = $mdb2->query($sql);
449 if (is_a($res, 'PEAR_Error')) die ($res->getMessage());
452 $val = $res->fetchRow();
453 $count = $val['cnt'];
456 // Deactivate user bind.
457 $sql = "select id from tt_user_project_binds where user_id = $user_id and project_id = $project_id";
458 $res = $mdb2->query($sql);
459 if (is_a($res, 'PEAR_Error')) die ($res->getMessage());
460 if ($val = $res->fetchRow()) {
461 $sql = "update tt_user_project_binds set status = 0 where id = ".$val['id'];
462 $affected = $mdb2->exec($sql);
463 if (is_a($affected, 'PEAR_Error')) die ($res->getMessage());
467 $sql = "delete from tt_user_project_binds where user_id = $user_id and project_id = $project_id";
468 $affected = $mdb2->exec($sql);
469 if (is_a($affected, 'PEAR_Error')) die ($res->getMessage());