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