5ead08bc8ca0bec895f513af4ade4a5373fb35f5
[timetracker.git] / WEB-INF / lib / ttUser.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 class ttUser {
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). TODO: remove when new roles are done.
35   var $role_id = null;          // Role id.
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 $currency = null;         // Currency.
57   var $plugins = null;          // Comma-separated list of enabled plugins.
58   var $config = null;           // Comma-separated list of miscellaneous config options.
59   var $team = null;             // Team name.
60   var $custom_logo = 0;         // Whether to use a custom logo for team.
61   var $lock_spec = null;        // Cron specification for record locking.
62   var $workday_minutes = 480;   // Number of work minutes in a regular day.
63   var $rights = 0;              // A mask of user rights.
64   var $rights_array = array();  // An array of user rights, planned replacement of array mask.
65
66   // Constructor.
67   function __construct($login, $id = null) {
68     if (!$login && !$id) {
69       // nothing to initialize
70       return;
71     }
72
73     $mdb2 = getConnection();
74
75     $sql = "SELECT u.id, u.login, u.name, u.team_id, u.role, u.role_id, r.rank, u.client_id, u.email, t.name as team_name,
76       t.currency, t.lang, t.decimal_mark, t.date_format, t.time_format, t.week_start,
77       t.tracking_mode, t.project_required, t.task_required, t.record_type,
78       t.bcc_email, t.plugins, t.config, t.lock_spec, t.workday_minutes, t.custom_logo
79       FROM tt_users u LEFT JOIN tt_teams t ON (u.team_id = t.id) LEFT JOIN tt_roles r on (r.id = u.role_id) WHERE ";
80     if ($id)
81       $sql .= "u.id = $id";
82     else
83       $sql .= "u.login = ".$mdb2->quote($login);
84     $sql .= " AND u.status = 1";
85
86     $res = $mdb2->query($sql);
87     if (is_a($res, 'PEAR_Error')) {
88       return;
89     }
90
91     $val = $res->fetchRow();
92     if ($val['id'] > 0) {
93       $this->login = $val['login'];
94       $this->name = $val['name'];
95       $this->id = $val['id'];
96       $this->team_id = $val['team_id'];
97       $this->role = $val['role'];
98       $this->role_id = $val['role_id'];
99       $this->rank = $val['rank'];
100       // Downgrade rank to legacy role, if it is still in use.
101       if ($this->role > 0 && $this->rank > $this->role)
102         $this->rank = $this->role; // TODO: remove after roles revamp.
103       // Upgrade rank from legacy role, for user who does not yet have a role_id.
104       if (!$this->rank && !$this->role_id && $this->role > 0)
105         $this->rank = $this->role; // TODO: remove after roles revamp.
106       $this->client_id = $val['client_id'];
107       $this->email = $val['email'];
108       $this->lang = $val['lang'];
109       $this->decimal_mark = $val['decimal_mark'];
110       $this->date_format = $val['date_format'];
111       $this->time_format = $val['time_format'];
112       $this->week_start = $val['week_start'];
113       $this->tracking_mode = $val['tracking_mode'];
114       $this->project_required = $val['project_required'];
115       $this->task_required = $val['task_required'];
116       $this->record_type = $val['record_type'];
117       $this->bcc_email = $val['bcc_email'];
118       $this->team = $val['team_name'];
119       $this->currency = $val['currency'];
120       $this->plugins = $val['plugins'];
121       $this->lock_spec = $val['lock_spec'];
122       $this->workday_minutes = $val['workday_minutes'];
123       $this->custom_logo = $val['custom_logo'];
124
125       $this->config = $val['config'];
126       $config_array = explode(',', $this->config);
127
128       // Set user config options.
129       $this->show_holidays = in_array('show_holidays', $config_array);
130       $this->punch_mode = in_array('punch_mode', $config_array);
131       $this->allow_overlap = in_array('allow_overlap', $config_array);
132       $this->future_entries = in_array('future_entries', $config_array);
133       $this->uncompleted_indicators = in_array('uncompleted_indicators', $config_array);
134
135       // Set "on behalf" id and name.
136       if (isset($_SESSION['behalf_id'])) {
137           $this->behalf_id = $_SESSION['behalf_id'];
138           $this->behalf_name = $_SESSION['behalf_name'];
139       }
140
141       // Set user rights.
142       if ($this->role == ROLE_USER) {
143         $this->rights = right_data_entry|right_view_charts|right_view_reports;
144         // TODO: get customized rights from the database instead.
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       } elseif ($this->role == ROLE_CLIENT) {
150         $this->rights = right_view_reports|right_view_invoices; // TODO: how about right_view_charts, too?
151         // $this->rights_array[] = "view_own_data";       // View own reports, charts, and invoices.
152         // $this->rights_array[] = "manage_own_settings"; // Edit own settings.
153       } elseif ($this->role == ROLE_COMANAGER) {
154         $this->rights = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team;
155         // $this->rights_array[] = "data_entry";          // Enter time and expense records into Time Tracker.
156         // $this->rights_array[] = "view_own_data";       // View own reports and charts.
157         // $this->rights_array[] = "manage_own_settings"; // Edit own settings.
158         // $this->rights_array[] = "view_users";          // View user names and roles in a group.
159         // $this->rights_array[] = "on_behalf_data_entry";// Can enter data on behalf of lower roles.
160         // $this->rights_array[] = "view_data";           // Can view data for lower roles.
161         $this->rights_array[] = "override_punch_mode"; // Can input any start and finish times for self and lower roles.
162         // TODO: get rights from the database instead.
163       } elseif ($this->role == ROLE_MANAGER) {
164         $this->rights = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team|right_assign_roles|right_export_team;
165         $this->rights_array[] = "override_punch_mode"; // Can input any start and finish times for self and lower roles.
166       } elseif ($this->role == ROLE_SITE_ADMIN) {
167         $this->rights = right_administer_site;
168       }
169
170 /*
171 // TODO: redesign of user rights and roles is currently ongoing.
172 // As we run our of bits for sure at some point, rights should be strings instead,
173 // for example: "data_entry".
174 // Also, we need rights editor page and team-customized roles.
175 // Move this stuff from here to ttUser class.
176 //
177 // User access rights - bits that collectively define an access mask to the system (a role).
178 // We'll have some bits here (1,2, etc...) reserved for future use.
179 define('right_data_entry', 4);     // Right to enter work hours and expenses.
180 define('right_view_charts', 8);    // Right to view charts.
181 define('right_view_reports', 16);  // Right to view reports.
182 define('right_view_invoices', 32); // Right to view invoices.
183 define('right_manage_team', 64);   // Right to manage team. Note that this is not full access to team.
184 define('right_assign_roles', 128); // Right to assign user roles.
185 define('right_export_team', 256);  // Right to export team data to a file.
186 define('right_administer_site', 1024); // Admin account right to manage the application as a whole.
187
188 // User roles.
189 define('ROLE_USER', 4);          // Regular user.
190 define('ROLE_CLIENT', 16);       // Client (to view reports and invoices).
191 define('ROLE_COMANAGER', 68);    // Team co-manager. Can do many things but not as much as team manager.
192 define('ROLE_MANAGER', 324);     // Team manager. Can do everything for a team.
193 define('ROLE_SITE_ADMIN', 1024); // Site administrator.
194 */
195     }
196   }
197
198   // The getActiveUser returns user id on behalf of whom current user is operating.
199   function getActiveUser() {
200     return ($this->behalf_id ? $this->behalf_id : $this->id);
201   }
202
203   // isAdmin - determines whether current user is admin (has right_administer_site).
204   function isAdmin() {
205     return (right_administer_site & $this->role);
206   }
207
208   // isManager - determines whether current user is team manager.
209   function isManager() {
210     return (ROLE_MANAGER == $this->role);
211   }
212
213   // isCoManager - determines whether current user is team comanager.
214   function isCoManager() {
215     return (ROLE_COMANAGER == $this->role);
216   }
217
218   // isClient - determines whether current user is a client.
219   function isClient() {
220     return (ROLE_CLIENT == $this->role);
221   }
222
223   // canManageTeam - determines whether current user is manager or co-manager.
224   function canManageTeam() {
225     return (right_manage_team & $this->role);
226   }
227
228   // isPluginEnabled checks whether a plugin is enabled for user.
229   function isPluginEnabled($plugin)
230   {
231     return in_array($plugin, explode(',', $this->plugins));
232   }
233
234   // getAssignedProjects - returns an array of assigned projects.
235   function getAssignedProjects()
236   {
237     $result = array();
238     $mdb2 = getConnection();
239
240     // Do a query with inner join to get assigned projects.
241     $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
242       inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
243       where p.team_id = $this->team_id and p.status = 1 order by p.name";
244     $res = $mdb2->query($sql);
245     if (!is_a($res, 'PEAR_Error')) {
246       while ($val = $res->fetchRow()) {
247         $result[] = $val;
248       }
249     }
250     return $result;
251   }
252
253   // isDateLocked checks whether a specifc date is locked for modifications.
254   function isDateLocked($date)
255   {
256     if ($this->isPluginEnabled('lk') && $this->lock_spec) {
257       // Override for managers.
258       if ($this->canManageTeam()) return false;
259
260       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
261       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
262
263       // Calculate the last occurrence of a lock.
264       $last = tdCron::getLastOccurrence($this->lock_spec, time());
265       $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
266       if ($date->before($lockdate)) {
267         return true;
268       }
269     }
270     return false;
271   }
272
273   // migrateLegacyRole makes changes to user database record and assigns a user to
274   // one of pre-defined roles, which are created if necessary.
275   // No changes to $this instance are done.
276   function migrateLegacyRole() {
277     // Do nothing if we already have a role_id.
278     if ($this->role_id) return false;
279
280     // Create default roles if necessary.
281     import ('ttRoleHelper');
282     if (!ttRoleHelper::rolesExist()) ttRoleHelper::createDefaultRoles(); // TODO: refactor or remove after roles revamp.
283
284     // Obtain new role id based on legacy role.
285     $role_id = ttRoleHelper::getRoleByRank($this->role);
286     if (!$role_id) return false; // Role not found, nothing to do.
287
288     $mdb2 = getConnection();
289     $sql = "update tt_users set role_id = $role_id where id = $this->id and team_id = $this->team_id";
290     $affected = $mdb2->exec($sql);
291     if (is_a($affected, 'PEAR_Error'))
292       return false;
293
294     return true;
295   }
296 }