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 getUserName function returns user name.
35 static function getUserName($user_id) {
36 $mdb2 = getConnection();
38 $sql = "select name from tt_users where id = $user_id and (status = 1 or status = 0)";
39 $res = $mdb2->query($sql);
41 if (!is_a($res, 'PEAR_Error')) {
42 $val = $res->fetchRow();
48 // The getUserByLogin function obtains data for a user, who is identified by login.
49 static function getUserByLogin($login) {
50 $mdb2 = getConnection();
52 $sql = "select id, name from tt_users where login = ".$mdb2->quote($login)." and (status = 1 or status = 0)";
53 $res = $mdb2->query($sql);
54 if (!is_a($res, 'PEAR_Error')) {
55 if ($val = $res->fetchRow()) {
62 // The getUserByEmail function is a helper function that tries to obtain user details identified by email.
63 // This function works only when one such active user exists.
64 static function getUserByEmail($email) {
65 $mdb2 = getConnection();
67 $sql = "select login, count(*) as cnt from tt_users where email = ".$mdb2->quote($email)." and status = 1 group by email";
68 $res = $mdb2->query($sql);
70 if (is_a($res, 'PEAR_Error'))
73 $val = $res->fetchRow();
74 if (1 <> $val['cnt']) {
75 // We either have no users or multiple users with a given email.
81 // The getUserIdByTmpRef obtains user id from a temporary reference (used for password resets).
82 static function getUserIdByTmpRef($ref) {
83 $mdb2 = getConnection();
85 $sql = "select user_id from tt_tmp_refs where ref = ".$mdb2->quote($ref);
86 $res = $mdb2->query($sql);
88 if (!is_a($res, 'PEAR_Error')) {
89 $val = $res->fetchRow();
90 return $val['user_id'];
95 // insert - inserts a user into database.
96 static function insert($fields, $hash = true) {
98 $mdb2 = getConnection();
100 $password = $mdb2->quote($fields['password']);
102 $password = 'md5('.$password.')';
103 $email = isset($fields['email']) ? $fields['email'] : '';
104 $group_id = (int) $fields['group_id'];
105 $org_id = (int) $fields['org_id'];
106 $rate = str_replace(',', '.', isset($fields['rate']) ? $fields['rate'] : 0);
109 if (array_key_exists('status', $fields)) { // Key exists and may be NULL during migration of deleted acounts.
110 $status_f = ', status';
111 $status_v = ', '.$mdb2->quote($fields['status']);
113 $created_ip_v = ', '.$mdb2->quote($_SERVER['REMOTE_ADDR']);
114 $created_by_v = ', '.$user->id;
116 $sql = "insert into tt_users (name, login, password, group_id, org_id, role_id, client_id, rate, email, created, created_ip, created_by $status_f) values (".
117 $mdb2->quote($fields['name']).", ".$mdb2->quote($fields['login']).
118 ", $password, $group_id, $org_id, ".$mdb2->quote($fields['role_id']).", ".$mdb2->quote($fields['client_id']).", $rate, ".$mdb2->quote($email).", now() $created_ip_v $created_by_v $status_v)";
119 $affected = $mdb2->exec($sql);
121 // Now deal with project assignment.
122 if (!is_a($affected, 'PEAR_Error')) {
123 $last_id = $mdb2->lastInsertID('tt_users', 'id');
124 $projects = isset($fields['projects']) ? $fields['projects'] : array();
125 if (count($projects) > 0) {
126 // We have at least one project assigned. Insert corresponding entries in tt_user_project_binds table.
127 foreach($projects as $p) {
128 if(!isset($p['rate']))
131 $p['rate'] = str_replace(',', '.', $p['rate']);
133 $sql = "insert into tt_user_project_binds (project_id, user_id, group_id, org_id, rate, status)".
134 " values(".$p['id'].", $last_id, $group_id, $org_id, ".$p['rate'].", 1)";
135 $affected = $mdb2->exec($sql);
143 // update - updates a user in database.
144 static function update($user_id, $fields) {
146 $mdb2 = getConnection();
152 $group_id = $user->getGroup();
153 $org_id = $user->org_id;
155 // Prepare query parts.
156 if (isset($fields['login'])) {
157 $login_part = ", login = ".$mdb2->quote($fields['login']);
160 if (isset($fields['password']))
161 $pass_part = ', password = md5('.$mdb2->quote($fields['password']).')';
163 if (isset($fields['name']))
164 $name_part = ', name = '.$mdb2->quote($fields['name']);
166 if ($user->can('manage_users')) {
167 if (isset($fields['role_id'])) {
168 $role_id = (int) $fields['role_id'];
169 $role_part = ", role_id = $role_id";
171 if (array_key_exists('client_id', $fields)) // Could be NULL.
172 $client_part = ", client_id = ".$mdb2->quote($fields['client_id']);
175 if (array_key_exists('rate', $fields)) {
176 $rate = str_replace(',', '.', isset($fields['rate']) ? $fields['rate'] : 0);
177 if($rate == '') $rate = 0;
178 $rate_part = ", rate = ".$mdb2->quote($rate);
181 if (isset($fields['email']))
182 $email_part = ', email = '.$mdb2->quote($fields['email']);
184 if (isset($fields['status'])) {
185 $status = (int) $fields['status'];
186 $status_part = ", status = $status";
189 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
190 $parts = ltrim($login_part.$pass_part.$name_part.$role_part.$client_part.$rate_part.$email_part.$modified_part.$status_part, ',');
192 $sql = "update tt_users set $parts".
193 " where id = $user_id and group_id = $group_id and org_id = $org_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->getGroup());
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(array(
242 'user_id' => $user_id,
243 'project_id' => $project_id,
245 'status' => ACTIVE));
253 // The delete function permanently deletes a user and all associated data.
254 static function delete($user_id) {
255 $mdb2 = getConnection();
257 // Delete custom field log entries for user, if we have them.
258 $sql = "delete from tt_custom_field_log where log_id in
259 (select id from tt_log where user_id = $user_id)";
260 $affected = $mdb2->exec($sql);
261 if (is_a($affected, 'PEAR_Error'))
264 // Delete log entries for user.
265 $sql = "delete from tt_log where user_id = $user_id";
266 $affected = $mdb2->exec($sql);
267 if (is_a($affected, 'PEAR_Error'))
270 // Delete expense items for user.
271 $sql = "delete from tt_expense_items where user_id = $user_id";
272 $affected = $mdb2->exec($sql);
273 if (is_a($affected, 'PEAR_Error'))
276 // Delete user binds.
277 $sql = "delete from tt_user_project_binds where user_id = $user_id";
278 $affected = $mdb2->exec($sql);
279 if (is_a($affected, 'PEAR_Error'))
282 // Clean up tt_config table.
283 $sql = "delete from tt_config where user_id = $user_id";
284 $affected = $mdb2->exec($sql);
285 if (is_a($affected, 'PEAR_Error'))
288 // Clean up tt_fav_reports table.
289 $sql = "delete from tt_fav_reports where user_id = $user_id";
290 $affected = $mdb2->exec($sql);
291 if (is_a($affected, 'PEAR_Error'))
295 $sql = "delete from tt_users where id = $user_id";
296 $affected = $mdb2->exec($sql);
297 if (is_a($affected, 'PEAR_Error'))
303 // The saveTmpRef saves a temporary reference for user that is used to reset user password.
304 static function saveTmpRef($ref, $user_id) {
305 $mdb2 = getConnection();
307 $sql = "delete from tt_tmp_refs where created < now() - interval 1 hour";
308 $affected = $mdb2->exec($sql);
310 $sql = "insert into tt_tmp_refs (created, ref, user_id) values(now(), ".$mdb2->quote($ref).", $user_id)";
311 $affected = $mdb2->exec($sql);
314 // The setPassword function updates password for user.
315 static function setPassword($user_id, $password) {
316 $mdb2 = getConnection();
318 $sql = "update tt_users set password = md5(".$mdb2->quote($password).") where id = $user_id";
319 $affected = $mdb2->exec($sql);
321 return (!is_a($affected, 'PEAR_Error'));
324 // insertBind - inserts a user to project bind into tt_user_project_binds table.
325 static function insertBind($fields) {
327 $mdb2 = getConnection();
329 $group_id = $user->getGroup();
330 $org_id = $user->org_id;
331 $user_id = (int) $fields['user_id'];
332 $project_id = (int) $fields['project_id'];
333 $rate = $mdb2->quote($fields['rate']);
334 $status = $mdb2->quote($fields['status']);
336 $sql = "insert into tt_user_project_binds (user_id, project_id, group_id, org_id, rate, status)".
337 " values($user_id, $project_id, $group_id, $org_id, $rate, $status)";
338 $affected = $mdb2->exec($sql);
339 return (!is_a($affected, 'PEAR_Error'));
342 // deleteBind - deactivates user to project bind when time entries exist,
343 // otherwise deletes it entirely.
344 static function deleteBind($user_id, $project_id) {
345 $mdb2 = getConnection();
347 $sql = "select count(*) as cnt from tt_log where
348 user_id = $user_id and project_id = $project_id and status = 1";
349 $res = $mdb2->query($sql);
350 if (is_a($res, 'PEAR_Error')) die ($res->getMessage());
353 $val = $res->fetchRow();
354 $count = $val['cnt'];
357 // Deactivate user bind.
358 $sql = "select id from tt_user_project_binds where user_id = $user_id and project_id = $project_id";
359 $res = $mdb2->query($sql);
360 if (is_a($res, 'PEAR_Error')) die ($res->getMessage());
361 if ($val = $res->fetchRow()) {
362 $sql = "update tt_user_project_binds set status = 0 where id = ".$val['id'];
363 $affected = $mdb2->exec($sql);
364 if (is_a($affected, 'PEAR_Error')) die ($res->getMessage());
368 $sql = "delete from tt_user_project_binds where user_id = $user_id and project_id = $project_id";
369 $affected = $mdb2->exec($sql);
370 if (is_a($affected, 'PEAR_Error')) die ($res->getMessage());
375 // updateLastAccess - updates last access info for user in db.
376 static function updateLastAccess() {
378 $mdb2 = getConnection();
379 $accessed_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
380 $sql = "update tt_users set accessed = now(), accessed_ip = $accessed_ip where id = $user->id";