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 // +----------------------------------------------------------------------+
30 var $login = null; // User login.
31 var $name = null; // User name.
32 var $id = null; // User id.
33 var $group_id = null; // Group id.
34 var $role_id = null; // Role id.
35 var $role_name = null; // Role name.
36 var $rank = null; // User role rank.
37 var $client_id = null; // Client id for client user role.
38 var $behalf_id = null; // User id, on behalf of whom we are working.
39 var $behalf_name = null; // User name, on behalf of whom we are working.
40 var $email = null; // User email.
41 var $lang = null; // Language.
42 var $decimal_mark = null; // Decimal separator.
43 var $date_format = null; // Date format.
44 var $time_format = null; // Time format.
45 var $week_start = 0; // Week start day.
46 var $show_holidays = 0; // Whether to show holidays in calendar.
47 var $tracking_mode = 0; // Tracking mode.
48 var $project_required = 0; // Whether project selection is required on time entires.
49 var $task_required = 0; // Whether task selection is required on time entires.
50 var $record_type = 0; // Record type (duration vs start and finish, or both).
51 var $punch_mode = 0; // Whether punch mode is enabled for user.
52 var $allow_overlap = 0; // Whether to allow overlapping time entries.
53 var $future_entries = 0; // Whether to allow creating future entries.
54 var $uncompleted_indicators = 0; // Uncompleted time entry indicators (show nowhere or on users page).
55 var $bcc_email = null; // Bcc email.
56 var $allow_ip = null; // Specification from where user is allowed access.
57 var $currency = null; // Currency.
58 var $plugins = null; // Comma-separated list of enabled plugins.
59 var $config = null; // Comma-separated list of miscellaneous config options.
60 var $team = null; // Team name.
61 var $custom_logo = 0; // Whether to use a custom logo for team.
62 var $lock_spec = null; // Cron specification for record locking.
63 var $workday_minutes = 480; // Number of work minutes in a regular day.
64 var $rights = array(); // An array of user rights such as 'track_own_time', etc.
65 var $is_client = false; // Whether user is a client as determined by missing 'track_own_time' right.
68 function __construct($login, $id = null) {
69 if (!$login && !$id) {
70 // nothing to initialize
74 $mdb2 = getConnection();
76 $sql = "SELECT u.id, u.login, u.name, u.group_id, u.role_id, r.rank, r.name as role_name, r.rights, u.client_id, u.email, g.name as group_name,
77 g.currency, g.lang, g.decimal_mark, g.date_format, g.time_format, g.week_start,
78 g.tracking_mode, g.project_required, g.task_required, g.record_type,
79 g.bcc_email, g.allow_ip, g.plugins, g.config, g.lock_spec, g.workday_minutes, g.custom_logo
80 FROM tt_users u LEFT JOIN tt_groups g ON (u.group_id = g.id) LEFT JOIN tt_roles r on (r.id = u.role_id) WHERE ";
84 $sql .= "u.login = ".$mdb2->quote($login);
85 $sql .= " AND u.status = 1";
87 $res = $mdb2->query($sql);
88 if (is_a($res, 'PEAR_Error')) {
92 $val = $res->fetchRow();
94 $this->login = $val['login'];
95 $this->name = $val['name'];
96 $this->id = $val['id'];
97 $this->group_id = $val['group_id'];
98 $this->role_id = $val['role_id'];
99 $this->role_name = $val['role_name'];
100 $this->rights = explode(',', $val['rights']);
101 $this->is_client = !in_array('track_own_time', $this->rights);
102 $this->rank = $val['rank'];
103 $this->client_id = $val['client_id'];
104 $this->email = $val['email'];
105 $this->lang = $val['lang'];
106 $this->decimal_mark = $val['decimal_mark'];
107 $this->date_format = $val['date_format'];
108 $this->time_format = $val['time_format'];
109 $this->week_start = $val['week_start'];
110 $this->tracking_mode = $val['tracking_mode'];
111 $this->project_required = $val['project_required'];
112 $this->task_required = $val['task_required'];
113 $this->record_type = $val['record_type'];
114 $this->bcc_email = $val['bcc_email'];
115 $this->allow_ip = $val['allow_ip'];
116 $this->team = $val['group_name'];
117 $this->currency = $val['currency'];
118 $this->plugins = $val['plugins'];
119 $this->lock_spec = $val['lock_spec'];
120 $this->workday_minutes = $val['workday_minutes'];
121 $this->custom_logo = $val['custom_logo'];
123 $this->config = $val['config'];
124 $config_array = explode(',', $this->config);
126 // Set user config options.
127 $this->show_holidays = in_array('show_holidays', $config_array);
128 $this->punch_mode = in_array('punch_mode', $config_array);
129 $this->allow_overlap = in_array('allow_overlap', $config_array);
130 $this->future_entries = in_array('future_entries', $config_array);
131 $this->uncompleted_indicators = in_array('uncompleted_indicators', $config_array);
133 // Set "on behalf" id and name.
134 if (isset($_SESSION['behalf_id'])) {
135 $this->behalf_id = $_SESSION['behalf_id'];
136 $this->behalf_name = $_SESSION['behalf_name'];
141 // The getActiveUser returns user id on behalf of whom the current user is operating.
142 function getActiveUser() {
143 return ($this->behalf_id ? $this->behalf_id : $this->id);
146 // can - determines whether user has a right to do something.
147 function can($do_something) {
148 return in_array($do_something, $this->rights);
151 // isAdmin - determines whether current user is admin (has right_administer_site).
153 return $this->can('administer_site');
156 // isManager - determines whether current user is team manager.
157 // This is a legacy function that we are getting rid of by replacing with rights check.
158 function isManager() {
159 return $this->can('export_data'); // By default this is assigned to managers but not co-managers.
160 // Which is sufficient for now until we refactor all calls
161 // to this function and then remove it.
164 // isCoManager - determines whether current user is team comanager.
165 // This is a legacy function that we are getting rid of by replacing with rights check.
166 function isCoManager() {
167 return ($this->can('manage_users') && !$this->can('export_data'));
170 // isClient - determines whether current user is a client.
171 function isClient() {
172 return $this->is_client;
175 // canManageTeam - determines whether current user is manager or co-manager.
176 // This is a legacy function that we are getting rid of by replacing with rights check.
177 function canManageTeam() {
178 return $this->can('manage_users'); // By default this is assigned to co-managers (an managers).
179 // Which is sufficient for now until we refactor all calls
180 // to this function and then remove it.
183 // isPluginEnabled checks whether a plugin is enabled for user.
184 function isPluginEnabled($plugin)
186 return in_array($plugin, explode(',', $this->plugins));
189 // getAssignedProjects - returns an array of assigned projects.
190 function getAssignedProjects()
193 $mdb2 = getConnection();
195 // Do a query with inner join to get assigned projects.
196 $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
197 inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
198 where p.group_id = $this->group_id and p.status = 1 order by p.name";
199 $res = $mdb2->query($sql);
200 if (!is_a($res, 'PEAR_Error')) {
201 while ($val = $res->fetchRow()) {
208 // getAssignedTasks - returns an array of assigned tasks.
209 function getAssignedTasks()
211 // Start with projects;
212 $projects = $this->getAssignedProjects();
213 if (!$projects) return false;
215 // Build an array of task ids.
217 foreach($projects as $project) {
218 $one_project_tasks = $project['tasks'] ? explode(',', $project['tasks']) : array();
219 $task_ids = array_unique(array_merge($task_ids, $one_project_tasks));
221 if (!$task_ids) return false;
223 // Get task descriptions.
225 $mdb2 = getConnection();
226 $tasks = implode(',', $task_ids); // This is a comma-separated list of task ids.
228 $sql = "select id, name, description from tt_tasks".
229 " where group_id = $this->group_id and status = 1 and id in ($tasks) order by name";
230 $res = $mdb2->query($sql);
231 if (!is_a($res, 'PEAR_Error')) {
232 while ($val = $res->fetchRow()) {
239 // isDateLocked checks whether a specifc date is locked for modifications.
240 function isDateLocked($date)
242 if (!$this->isPluginEnabled('lk'))
243 return false; // Locking feature is disabled.
245 if (!$this->lock_spec)
246 return false; // There is no lock specification.
248 if (!$this->behalf_id && $this->can('override_own_date_lock'))
249 return false; // User is working as self and can override own date lock.
251 if ($this->behalf_id && $this->can('override_date_lock'))
252 return false; // User is working on behalf of someone else and can override date lock.
254 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
255 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
257 // Calculate the last occurrence of a lock.
258 $last = tdCron::getLastOccurrence($this->lock_spec, time());
259 $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
260 if ($date->before($lockdate))
266 // canOverridePunchMode checks whether a user can override punch mode in a situation.
267 function canOverridePunchMode()
269 if (!$this->behalf_id && !$this->can('override_own_punch_mode'))
270 return false; // User is working as self and cannot override for self.
272 if ($this->behalf_id && !$this->can('override_punch_mode'))
273 return false; // User is working on behalf of someone else and cannot override.
278 // getUsers obtains users in a group, as specififed by options.
279 function getUsers($options) {
281 $mdb2 = getConnection();
283 $skipClients = !isset($options['include_clients']);
284 $includeSelf = isset($options['include_self']);
286 $select_part = 'select u.id, u.name';
287 if (isset($options['include_login'])) $select_part .= ', u.login';
288 if (!isset($options['include_clients'])) $select_part .= ', r.rights';
289 if (isset($options['include_role'])) $select_part .= ', r.name as role_name, r.rank';
291 $from_part = ' from tt_users u';
294 if (isset($options['max_rank']) || $skipClients || isset($options['include_role']))
295 $left_joins .= ' left join tt_roles r on (u.role_id = r.id)';
297 $where_part = " where u.group_id = $this->group_id";
298 if (isset($options['status']))
299 $where_part .= ' and u.status = '.(int)$options['status'];
301 $where_part .= ' and u.status is not null';
303 $where_part .= " and (u.id = $this->id || r.rank <= ".(int)$options['max_rank'].')';
305 if (isset($options['max_rank'])) $where_part .= ' and r.rank <= '.(int)$options['max_rank'];
308 $order_part = " order by upper(u.name)";
310 $sql = $select_part.$from_part.$left_joins.$where_part.$order_part;
311 $res = $mdb2->query($sql);
312 $user_list = array();
313 if (is_a($res, 'PEAR_Error'))
316 while ($val = $res->fetchRow()) {
318 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
320 continue; // Skip adding clients.
325 if (isset($options['self_first'])) {
326 // Put own entry at the front.
327 $cnt = count($user_list);
328 for($i = 0; $i < $cnt; $i++) {
329 if ($user_list[$i]['id'] == $this->id) {
330 $self = $user_list[$i]; // Found self.
331 array_unshift($user_list, $self); // Put own entry at the front.
332 array_splice($user_list, $i+1, 1); // Remove duplicate.
339 // getUser function is used to manage users in group and returns user details.
340 // At the moment, the function is used for user edits and deletes.
341 function getUser($user_id) {
342 if (!$this->can('manage_users')) return false;
344 $mdb2 = getConnection();
346 $sql = "select u.id, u.name, u.login, u.role_id, u.status, u.rate, u.email from tt_users u".
347 " left join tt_roles r on (u.role_id = r.id)".
348 " where u.id = $user_id and u.group_id = $this->group_id and u.status is not null".
349 " and (r.rank < $this->rank or (r.rank = $this->rank and u.id = $this->id))"; // Users with lesser roles or self.
350 $res = $mdb2->query($sql);
351 if (!is_a($res, 'PEAR_Error')) {
352 $val = $res->fetchRow();
358 // checkBehalfId checks whether behalf_id is appropriate.
359 // On behalf user must be active and have lower rank.
360 function checkBehalfId() {
361 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
362 $users = $this->getUsers($options);
363 foreach($users as $one_user) {
364 if ($one_user['id'] == $this->behalf_id)
370 // adjustBehalfId attempts to adjust behalf_id and behalf_name to a first found
373 // Needed for situations when user does not have do_own_something right.
374 // Example: has view_charts but does not have view_own_charts.
375 // In this case we still allow access to charts, but set behalf_id to someone else.
376 function adjustBehalfId() {
377 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
378 $users = $this->getUsers($options);
379 foreach($users as $one_user) {
380 // Fake loop to access first element.
381 $this->behalf_id = $one_user['id'];
382 $this->behalf_name = $one_user['name'];
383 $_SESSION['behalf_id'] = $this->behalf_id;
384 $_SESSION['behalf_name'] = $this->behalf_name;