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 = ', '.$mdb2->quote($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 $sql = "SELECT LAST_INSERT_ID() AS last_id";
124 $res = $mdb2->query($sql);
125 $val = $res->fetchRow();
126 $last_id = $val['last_id'];
128 $projects = isset($fields['projects']) ? $fields['projects'] : array();
129 if (count($projects) > 0) {
130 // We have at least one project assigned. Insert corresponding entries in tt_user_project_binds table.
131 foreach($projects as $p) {
132 if(!isset($p['rate']))
135 $p['rate'] = str_replace(',', '.', $p['rate']);
137 $sql = "insert into tt_user_project_binds (project_id, user_id, rate, status) values(".$p['id'].",".$last_id.",".$p['rate'].", 1)";
138 $affected = $mdb2->exec($sql);
146 // update - updates a user in database.
147 static function update($user_id, $fields) {
149 $mdb2 = getConnection();
152 if (!$user_id || !isset($fields['login']))
155 // Prepare query parts.
156 if (isset($fields['password']))
157 $pass_part = ', password = md5('.$mdb2->quote($fields['password']).')';
158 if (in_array('manage_users', $user->rights)) {
159 if (isset($fields['role_id'])) {
160 $role_id = (int) $fields['role_id'];
161 $role_id_part = ", role_id = $role_id";
163 if (array_key_exists('client_id', $fields)) // Could be NULL.
164 $client_part = ", client_id = ".$mdb2->quote($fields['client_id']);
167 if (array_key_exists('rate', $fields)) {
168 $rate = str_replace(',', '.', isset($fields['rate']) ? $fields['rate'] : 0);
169 if($rate == '') $rate = 0;
170 $rate_part = ", rate = ".$mdb2->quote($rate);
173 if (isset($fields['status'])) {
174 $status = (int) $fields['status'];
175 $status_part = ", status = $status";
178 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
180 $sql = "update tt_users set login = ".$mdb2->quote($fields['login']).
181 "$pass_part, name = ".$mdb2->quote($fields['name']).
182 "$role_id_part $client_part $rate_part $modified_part $status_part, email = ".$mdb2->quote($fields['email']).
183 " where id = $user_id";
184 $affected = $mdb2->exec($sql);
185 if (is_a($affected, 'PEAR_Error')) return false;
187 if (array_key_exists('projects', $fields)) {
188 // Deal with project assignments.
189 // Note: we cannot simply delete old project binds and insert new ones because it screws up reporting
190 // (when looking for cost while entries for de-assigned projects exist).
191 // Therefore, we must iterate through all projects and only delete the binds when no time entries are present,
192 // otherwise de-activate the bind (set its status to inactive). This will keep the bind
193 // and its rate in database for reporting.
195 $all_projects = ttTeamHelper::getAllProjects($user->group_id);
196 $assigned_projects = isset($fields['projects']) ? $fields['projects'] : array();
198 foreach($all_projects as $p) {
199 // Determine if a project is assigned.
201 $project_id = $p['id'];
203 if (count($assigned_projects) > 0) {
204 foreach ($assigned_projects as $ap) {
205 if ($project_id == $ap['id']) {
209 $rate = str_replace(",",".",$rate);
217 ttUserHelper::deleteBind($user_id, $project_id);
219 // Here we need to either update or insert new tt_user_project_binds record.
220 // Determine if a record exists.
221 $sql = "select id from tt_user_project_binds where user_id = $user_id and project_id = $project_id";
222 $res = $mdb2->query($sql);
223 if (is_a($res, 'PEAR_Error')) die ($res->getMessage());
224 if ($val = $res->fetchRow()) {
225 // Record exists. Update it.
226 $sql = "update tt_user_project_binds set status = 1, rate = $rate where id = ".$val['id'];
227 $affected = $mdb2->exec($sql);
228 if (is_a($affected, 'PEAR_Error')) die ($affected->getMessage());
230 // Record does not exist. Insert it.
231 ttUserHelper::insertBind($user_id, $project_id, $rate, 1);
239 // The delete function permanently deletes a user and all associated data.
240 static function delete($user_id) {
241 $mdb2 = getConnection();
243 // Delete custom field log entries for user, if we have them.
244 $sql = "delete from tt_custom_field_log where log_id in
245 (select id from tt_log where user_id = $user_id)";
246 $affected = $mdb2->exec($sql);
247 if (is_a($affected, 'PEAR_Error'))
250 // Delete log entries for user.
251 $sql = "delete from tt_log where user_id = $user_id";
252 $affected = $mdb2->exec($sql);
253 if (is_a($affected, 'PEAR_Error'))
256 // Delete expense items for user.
257 $sql = "delete from tt_expense_items where user_id = $user_id";
258 $affected = $mdb2->exec($sql);
259 if (is_a($affected, 'PEAR_Error'))
262 // Delete user binds.
263 $sql = "delete from tt_user_project_binds where user_id = $user_id";
264 $affected = $mdb2->exec($sql);
265 if (is_a($affected, 'PEAR_Error'))
268 // Clean up tt_config table.
269 $sql = "delete from tt_config where user_id = $user_id";
270 $affected = $mdb2->exec($sql);
271 if (is_a($affected, 'PEAR_Error'))
274 // Clean up tt_fav_reports table.
275 $sql = "delete from tt_fav_reports where user_id = $user_id";
276 $affected = $mdb2->exec($sql);
277 if (is_a($affected, 'PEAR_Error'))
281 $sql = "delete from tt_users where id = $user_id";
282 $affected = $mdb2->exec($sql);
283 if (is_a($affected, 'PEAR_Error'))
289 // The saveTmpRef saves a temporary reference for user that is used to reset user password.
290 static function saveTmpRef($ref, $user_id) {
291 $mdb2 = getConnection();
293 $sql = "delete from tt_tmp_refs where created < now() - interval 1 hour";
294 $affected = $mdb2->exec($sql);
296 $sql = "insert into tt_tmp_refs (created, ref, user_id) values(now(), ".$mdb2->quote($ref).", $user_id)";
297 $affected = $mdb2->exec($sql);
300 // The setPassword function updates password for user.
301 static function setPassword($user_id, $password) {
302 $mdb2 = getConnection();
304 $sql = "update tt_users set password = md5(".$mdb2->quote($password).") where id = $user_id";
305 $affected = $mdb2->exec($sql);
307 return (!is_a($affected, 'PEAR_Error'));
310 // insertBind - inserts a user to project bind into tt_user_project_binds table.
311 static function insertBind($user_id, $project_id, $rate, $status) {
312 $mdb2 = getConnection();
314 $sql = "insert into tt_user_project_binds (user_id, project_id, rate, status)
315 values($user_id, $project_id, ".$mdb2->quote($rate).", $status)";
316 $affected = $mdb2->exec($sql);
317 return (!is_a($affected, 'PEAR_Error'));
320 // deleteBind - deactivates user to project bind when time entries exist,
321 // otherwise deletes it entirely.
322 static function deleteBind($user_id, $project_id) {
323 $mdb2 = getConnection();
325 $sql = "select count(*) as cnt from tt_log where
326 user_id = $user_id and project_id = $project_id and status = 1";
327 $res = $mdb2->query($sql);
328 if (is_a($res, 'PEAR_Error')) die ($res->getMessage());
331 $val = $res->fetchRow();
332 $count = $val['cnt'];
335 // Deactivate user bind.
336 $sql = "select id from tt_user_project_binds where user_id = $user_id and project_id = $project_id";
337 $res = $mdb2->query($sql);
338 if (is_a($res, 'PEAR_Error')) die ($res->getMessage());
339 if ($val = $res->fetchRow()) {
340 $sql = "update tt_user_project_binds set status = 0 where id = ".$val['id'];
341 $affected = $mdb2->exec($sql);
342 if (is_a($affected, 'PEAR_Error')) die ($res->getMessage());
346 $sql = "delete from tt_user_project_binds where user_id = $user_id and project_id = $project_id";
347 $affected = $mdb2->exec($sql);
348 if (is_a($affected, 'PEAR_Error')) die ($res->getMessage());
353 // updateLastAccess - updates last access info for user in db.
354 static function updateLastAccess() {
356 $mdb2 = getConnection();
357 $accessed_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
358 $sql = "update tt_users set accessed = now(), accessed_ip = $accessed_ip where id = $user->id";