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 $team_id = null; // Team id.
34 var $role = null; // User role (user, client, comanager, manager, admin).
35 var $client_id = null; // Client id for client user role.
36 var $behalf_id = null; // User id, on behalf of whom we are working.
37 var $behalf_name = null; // User name, on behalf of whom we are working.
38 var $email = null; // User email.
39 var $lang = null; // Language.
40 var $decimal_mark = null; // Decimal separator.
41 var $date_format = null; // Date format.
42 var $time_format = null; // Time format.
43 var $week_start = 0; // Week start day.
44 var $show_holidays = 0; // Whether to show holidays in calendar.
45 var $tracking_mode = 0; // Tracking mode.
46 var $project_required = 0; // Whether project selection is required on time entires.
47 var $task_required = 0; // Whether task selection is required on time entires.
48 var $record_type = 0; // Record type (duration vs start and finish, or both).
49 var $punch_mode = 0; // Whether punch mode is enabled for user.
50 var $allow_overlap = 0; // Whether to allow overlapping time entries.
51 var $future_entries = 0; // Whether to allow creating future entries.
52 var $uncompleted_indicators = 0; // Uncompleted time entry indicators (show nowhere or on users page).
53 var $bcc_email = null; // Bcc email.
54 var $currency = null; // Currency.
55 var $plugins = null; // Comma-separated list of enabled plugins.
56 var $config = null; // Comma-separated list of miscellaneous config options.
57 var $team = null; // Team name.
58 var $custom_logo = 0; // Whether to use a custom logo for team.
59 var $lock_spec = null; // Cron specification for record locking.
60 var $workday_minutes = 480; // Number of work minutes in a regular day.
61 var $rights = 0; // A mask of user rights.
62 var $rights_array = array(); // An array of user rights, planned replacement of array mask.
65 function __construct($login, $id = null) {
66 if (!$login && !$id) {
67 // nothing to initialize
71 $mdb2 = getConnection();
73 $sql = "SELECT u.id, u.login, u.name, u.team_id, u.role, u.client_id, u.email, t.name as team_name,
74 t.currency, t.lang, t.decimal_mark, t.date_format, t.time_format, t.week_start,
75 t.tracking_mode, t.project_required, t.task_required, t.record_type,
76 t.bcc_email, t.plugins, t.config, t.lock_spec, t.workday_minutes, t.custom_logo
77 FROM tt_users u LEFT JOIN tt_teams t ON (u.team_id = t.id) WHERE ";
81 $sql .= "u.login = ".$mdb2->quote($login);
82 $sql .= " AND u.status = 1";
84 $res = $mdb2->query($sql);
85 if (is_a($res, 'PEAR_Error')) {
89 $val = $res->fetchRow();
91 $this->login = $val['login'];
92 $this->name = $val['name'];
93 $this->id = $val['id'];
94 $this->team_id = $val['team_id'];
95 $this->role = $val['role'];
96 $this->client_id = $val['client_id'];
97 $this->email = $val['email'];
98 $this->lang = $val['lang'];
99 $this->decimal_mark = $val['decimal_mark'];
100 $this->date_format = $val['date_format'];
101 $this->time_format = $val['time_format'];
102 $this->week_start = $val['week_start'];
103 $this->tracking_mode = $val['tracking_mode'];
104 $this->project_required = $val['project_required'];
105 $this->task_required = $val['task_required'];
106 $this->record_type = $val['record_type'];
107 $this->bcc_email = $val['bcc_email'];
108 $this->team = $val['team_name'];
109 $this->currency = $val['currency'];
110 $this->plugins = $val['plugins'];
111 $this->lock_spec = $val['lock_spec'];
112 $this->workday_minutes = $val['workday_minutes'];
113 $this->custom_logo = $val['custom_logo'];
115 $this->config = $val['config'];
116 $config_array = explode(',', $this->config);
118 // Set user config options.
119 $this->show_holidays = in_array('show_holidays', $config_array);
120 $this->punch_mode = in_array('punch_mode', $config_array);
121 $this->allow_overlap = in_array('allow_overlap', $config_array);
122 $this->future_entries = in_array('future_entries', $config_array);
123 $this->uncompleted_indicators = in_array('uncompleted_indicators', $config_array);
125 // Set "on behalf" id and name.
126 if (isset($_SESSION['behalf_id'])) {
127 $this->behalf_id = $_SESSION['behalf_id'];
128 $this->behalf_name = $_SESSION['behalf_name'];
132 if ($this->role == ROLE_USER) {
133 $this->rights = right_data_entry|right_view_charts|right_view_reports;
134 // TODO: get customized rights from the database instead.
135 // $this->rights_array[] = "data_entry"; // Enter time and expense records into Time Tracker.
136 // $this->rights_array[] = "view_own_data"; // View own reports and charts.
137 // $this->rights_array[] = "manage_own_settings"; // Edit own settings.
138 // $this->rights_array[] = "view_users"; // View user names and roles in a group.
139 } elseif ($this->role == ROLE_CLIENT) {
140 $this->rights = right_view_reports|right_view_invoices; // TODO: how about right_view_charts, too?
141 // $this->rights_array[] = "view_own_data"; // View own reports, charts, and invoices.
142 // $this->rights_array[] = "manage_own_settings"; // Edit own settings.
143 } elseif ($this->role == ROLE_COMANAGER) {
144 $this->rights = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team;
145 // $this->rights_array[] = "data_entry"; // Enter time and expense records into Time Tracker.
146 // $this->rights_array[] = "view_own_data"; // View own reports and charts.
147 // $this->rights_array[] = "manage_own_settings"; // Edit own settings.
148 // $this->rights_array[] = "view_users"; // View user names and roles in a group.
149 // $this->rights_array[] = "on_behalf_data_entry";// Can enter data on behalf of lower roles.
150 // $this->rights_array[] = "view_data"; // Can view data for lower roles.
151 $this->rights_array[] = "override_punch_mode"; // Can input any start and finish times for self and lower roles.
152 // TODO: get rights from the database instead.
153 } elseif ($this->role == ROLE_MANAGER) {
154 $this->rights = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team|right_assign_roles|right_export_team;
155 $this->rights_array[] = "override_punch_mode"; // Can input any start and finish times for self and lower roles.
156 } elseif ($this->role == ROLE_SITE_ADMIN) {
157 $this->rights = right_administer_site;
161 // TODO: redesign of user rights and roles is currently ongoing.
162 // As we run our of bits for sure at some point, rights should be strings instead,
163 // for example: "data_entry".
164 // Also, we need rights editor page and team-customized roles.
165 // Move this stuff from here to ttUser class.
167 // User access rights - bits that collectively define an access mask to the system (a role).
168 // We'll have some bits here (1,2, etc...) reserved for future use.
169 define('right_data_entry', 4); // Right to enter work hours and expenses.
170 define('right_view_charts', 8); // Right to view charts.
171 define('right_view_reports', 16); // Right to view reports.
172 define('right_view_invoices', 32); // Right to view invoices.
173 define('right_manage_team', 64); // Right to manage team. Note that this is not full access to team.
174 define('right_assign_roles', 128); // Right to assign user roles.
175 define('right_export_team', 256); // Right to export team data to a file.
176 define('right_administer_site', 1024); // Admin account right to manage the application as a whole.
179 define('ROLE_USER', 4); // Regular user.
180 define('ROLE_CLIENT', 16); // Client (to view reports and invoices).
181 define('ROLE_COMANAGER', 68); // Team co-manager. Can do many things but not as much as team manager.
182 define('ROLE_MANAGER', 324); // Team manager. Can do everything for a team.
183 define('ROLE_SITE_ADMIN', 1024); // Site administrator.
188 // The getActiveUser returns user id on behalf of whom current user is operating.
189 function getActiveUser() {
190 return ($this->behalf_id ? $this->behalf_id : $this->id);
193 // isAdmin - determines whether current user is admin (has right_administer_site).
195 return (right_administer_site & $this->role);
198 // isManager - determines whether current user is team manager.
199 function isManager() {
200 return (ROLE_MANAGER == $this->role);
203 // isCoManager - determines whether current user is team comanager.
204 function isCoManager() {
205 return (ROLE_COMANAGER == $this->role);
208 // isClient - determines whether current user is a client.
209 function isClient() {
210 return (ROLE_CLIENT == $this->role);
213 // canManageTeam - determines whether current user is manager or co-manager.
214 function canManageTeam() {
215 return (right_manage_team & $this->role);
218 // isPluginEnabled checks whether a plugin is enabled for user.
219 function isPluginEnabled($plugin)
221 return in_array($plugin, explode(',', $this->plugins));
224 // getAssignedProjects - returns an array of assigned projects.
225 function getAssignedProjects()
228 $mdb2 = getConnection();
230 // Do a query with inner join to get assigned projects.
231 $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
232 inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
233 where p.team_id = $this->team_id and p.status = 1 order by p.name";
234 $res = $mdb2->query($sql);
235 if (!is_a($res, 'PEAR_Error')) {
236 while ($val = $res->fetchRow()) {
243 // isDateLocked checks whether a specifc date is locked for modifications.
244 function isDateLocked($date)
246 if ($this->isPluginEnabled('lk') && $this->lock_spec) {
247 // Override for managers.
248 if ($this->canManageTeam()) return false;
250 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
251 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
253 // Calculate the last occurrence of a lock.
254 $last = tdCron::getLastOccurrence($this->lock_spec, time());
255 $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
256 if ($date->before($lockdate)) {