Dropped legacy role field.
[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_id = null;          // Role id.
35   var $rank = null;             // User role rank.
36   var $client_id = null;        // Client id for client user role.
37   var $behalf_id = null;        // User id, on behalf of whom we are working.
38   var $behalf_name = null;      // User name, on behalf of whom we are working.
39   var $email = null;            // User email.
40   var $lang = null;             // Language.
41   var $decimal_mark = null;     // Decimal separator.
42   var $date_format = null;      // Date format.
43   var $time_format = null;      // Time format.
44   var $week_start = 0;          // Week start day.
45   var $show_holidays = 0;       // Whether to show holidays in calendar.
46   var $tracking_mode = 0;       // Tracking mode.
47   var $project_required = 0;    // Whether project selection is required on time entires.
48   var $task_required = 0;       // Whether task selection is required on time entires.
49   var $record_type = 0;         // Record type (duration vs start and finish, or both).
50   var $punch_mode = 0;          // Whether punch mode is enabled for user.
51   var $allow_overlap = 0;       // Whether to allow overlapping time entries.
52   var $future_entries = 0;      // Whether to allow creating future entries.
53   var $uncompleted_indicators = 0; // Uncompleted time entry indicators (show nowhere or on users page).
54   var $bcc_email = null;        // Bcc email.
55   var $currency = null;         // Currency.
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 = array();        // An array of user rights such as 'track_own_time', etc.
63   var $is_client = false;       // Whether user is a client as determined by missing 'track_own_time' right.
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_id, r.rank, r.rights, 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) LEFT JOIN tt_roles r on (r.id = u.role_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_id = $val['role_id'];
97       $this->rights = explode(',', $val['rights']);
98       $this->is_client = !in_array('track_own_time', $this->rights);
99       $this->rank = $val['rank'];
100       $this->client_id = $val['client_id'];
101       $this->email = $val['email'];
102       $this->lang = $val['lang'];
103       $this->decimal_mark = $val['decimal_mark'];
104       $this->date_format = $val['date_format'];
105       $this->time_format = $val['time_format'];
106       $this->week_start = $val['week_start'];
107       $this->tracking_mode = $val['tracking_mode'];
108       $this->project_required = $val['project_required'];
109       $this->task_required = $val['task_required'];
110       $this->record_type = $val['record_type'];
111       $this->bcc_email = $val['bcc_email'];
112       $this->team = $val['team_name'];
113       $this->currency = $val['currency'];
114       $this->plugins = $val['plugins'];
115       $this->lock_spec = $val['lock_spec'];
116       $this->workday_minutes = $val['workday_minutes'];
117       $this->custom_logo = $val['custom_logo'];
118
119       $this->config = $val['config'];
120       $config_array = explode(',', $this->config);
121
122       // Set user config options.
123       $this->show_holidays = in_array('show_holidays', $config_array);
124       $this->punch_mode = in_array('punch_mode', $config_array);
125       $this->allow_overlap = in_array('allow_overlap', $config_array);
126       $this->future_entries = in_array('future_entries', $config_array);
127       $this->uncompleted_indicators = in_array('uncompleted_indicators', $config_array);
128
129       // Set "on behalf" id and name.
130       if (isset($_SESSION['behalf_id'])) {
131           $this->behalf_id = $_SESSION['behalf_id'];
132           $this->behalf_name = $_SESSION['behalf_name'];
133       }
134     }
135   }
136
137   // The getActiveUser returns user id on behalf of whom the current user is operating.
138   function getActiveUser() {
139     return ($this->behalf_id ? $this->behalf_id : $this->id);
140   }
141
142   // can - determines whether user has a right to do something.
143   function can($do_something) {
144     return in_array($do_something, $this->rights);
145   }
146
147   // isAdmin - determines whether current user is admin (has right_administer_site).
148   function isAdmin() {
149     return $this->can('administer_site');
150   }
151
152   // isManager - determines whether current user is team manager.
153   // This is a legacy function that we are getting rid of by replacing with rights check.
154   function isManager() {
155     return $this->can('export_data'); // By default this is assigned to managers but not co-managers.
156                                       // Which is sufficient for now until we refactor all calls
157                                       // to this function and then remove it.
158   }
159
160   // isCoManager - determines whether current user is team comanager.
161   // This is a legacy function that we are getting rid of by replacing with rights check.
162   function isCoManager() {
163     return ($this->can('manage_users') && !$this->can('export_data'));
164   }
165
166   // isClient - determines whether current user is a client.
167   function isClient() {
168     return $this->is_client;
169   }
170
171   // canManageTeam - determines whether current user is manager or co-manager.
172   // This is a legacy function that we are getting rid of by replacing with rights check.
173   function canManageTeam() {
174     return $this->can('manage_users'); // By default this is assigned to co-managers (an managers).
175                                        // Which is sufficient for now until we refactor all calls
176                                        // to this function and then remove it.
177   }
178
179   // isPluginEnabled checks whether a plugin is enabled for user.
180   function isPluginEnabled($plugin)
181   {
182     return in_array($plugin, explode(',', $this->plugins));
183   }
184
185   // getAssignedProjects - returns an array of assigned projects.
186   function getAssignedProjects()
187   {
188     $result = array();
189     $mdb2 = getConnection();
190
191     // Do a query with inner join to get assigned projects.
192     $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
193       inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
194       where p.team_id = $this->team_id and p.status = 1 order by p.name";
195     $res = $mdb2->query($sql);
196     if (!is_a($res, 'PEAR_Error')) {
197       while ($val = $res->fetchRow()) {
198         $result[] = $val;
199       }
200     }
201     return $result;
202   }
203
204   // isDateLocked checks whether a specifc date is locked for modifications.
205   function isDateLocked($date)
206   {
207     if ($this->isPluginEnabled('lk') && $this->lock_spec) {
208
209       // Override.
210       if ($this->can('override_date_lock')) return false;
211
212       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
213       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
214
215       // Calculate the last occurrence of a lock.
216       $last = tdCron::getLastOccurrence($this->lock_spec, time());
217       $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
218       if ($date->before($lockdate)) {
219         return true;
220       }
221     }
222     return false;
223   }
224
225   // canOverridePunchMode checks whether a user can override punch mode in a situation.
226   function canOverridePunchMode()
227   {
228     if (!$this->behalf_id && !$this->can('override_own_punch_mode'))
229       return false; // User is working as self and cannot override for self.
230
231     if ($this->behalf_id && !$this->can('override_punch_mode'))
232       return false; // User is working on behalf of someone else and cannot override.
233
234     return true;
235   }
236 }