Security fix - improved access checks for task edit and deletes.
[timetracker.git] / user_edit.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 require_once('initialize.php');
30 import('form.Form');
31 import('ttProjectHelper');
32 import('ttTeamHelper');
33 import('ttUserHelper');
34 import('form.Table');
35 import('form.TableColumn');
36 import('ttRoleHelper');
37
38 // Access checks.
39 if (!ttAccessAllowed('manage_users')) {
40   header('Location: access_denied.php');
41   exit();
42 }
43 $user_id = (int)$request->getParameter('id');
44 $user_details = $user->getUser($user_id);
45 if (!$user_details) {
46   header('Location: access_denied.php');
47   exit();
48 }
49 // End of access checks.
50
51 if ($user->isPluginEnabled('cl'))
52   $clients = ttTeamHelper::getActiveClients($user->team_id);
53
54 $projects = ttTeamHelper::getActiveProjects($user->team_id);
55 $assigned_projects = array();
56
57 if ($request->isPost()) {
58   $cl_name = trim($request->getParameter('name'));
59   $cl_login = trim($request->getParameter('login'));
60   if (!$auth->isPasswordExternal()) {
61     $cl_password1 = $request->getParameter('pas1');
62     $cl_password2 = $request->getParameter('pas2');
63   }
64   $cl_email = trim($request->getParameter('email'));
65   $cl_role_id = $request->getParameter('role');
66   $cl_client_id = $request->getParameter('client');
67   $cl_status = $request->getParameter('status');
68   $cl_rate = $request->getParameter('rate');
69   $cl_projects = $request->getParameter('projects');
70   if (is_array($cl_projects)) {
71     foreach ($cl_projects as $p) {
72       if (ttValidFloat($request->getParameter('rate_'.$p), true)) {
73         $project_with_rate = array();
74         $project_with_rate['id'] = $p;
75         $project_with_rate['rate'] = $request->getParameter('rate_'.$p);
76         $assigned_projects[] = $project_with_rate;
77       } else
78         $err->add($i18n->get('error.field'), 'rate_'.$p);
79     }
80   }
81 } else {
82   $cl_name = $user_details['name'];
83   $cl_login = $user_details['login'];
84   $cl_email = $user_details['email'];
85   $cl_rate = str_replace('.', $user->decimal_mark, $user_details['rate']);
86   $cl_role_id = $user_details['role_id'];
87   $cl_client_id = $user_details['client_id'];
88   $cl_status = $user_details['status'];
89   $cl_projects = array();
90   $assigned_projects = ttProjectHelper::getAssignedProjects($user_id);
91   foreach($assigned_projects as $p) {
92     $cl_projects[] = $p['id'];
93   }
94 }
95
96 $form = new Form('userForm');
97 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','style'=>'width: 300px;','value'=>$cl_name));
98 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','style'=>'width: 300px;','value'=>$cl_login));
99 if (!$auth->isPasswordExternal()) {
100   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas1','value'=>$cl_password1));
101   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'pas2','value'=>$cl_password2));
102 }
103 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','style'=>'width: 300px;','value'=>$cl_email));
104
105 $active_roles = ttTeamHelper::getActiveRolesForUser();
106 $form->addInput(array('type'=>'combobox','onchange'=>'handleClientControl()','name'=>'role','value'=>$cl_role_id,'data'=>$active_roles, 'datakeys'=>array('id', 'name')));
107 if ($user->isPluginEnabled('cl'))
108   $form->addInput(array('type'=>'combobox','name'=>'client','value'=>$cl_client_id,'data'=>$clients,'datakeys'=>array('id', 'name'),'empty'=>array(''=>$i18n->get('dropdown.select'))));
109
110 $form->addInput(array('type'=>'combobox','name'=>'status','value'=>$cl_status,
111   'data'=>array(ACTIVE=>$i18n->get('dropdown.status_active'),INACTIVE=>$i18n->get('dropdown.status_inactive'))));
112 $form->addInput(array('type'=>'floatfield','maxlength'=>'10','name'=>'rate','format'=>'.2','value'=>$cl_rate));
113
114 // Define classes for the projects table.
115 class NameCellRenderer extends DefaultCellRenderer {
116   function render(&$table, $value, $row, $column, $selected = false) {
117     $this->setOptions(array('width'=>200));
118     $this->setValue('<label for = "'.$table->name.'_'.$row.'">'.htmlspecialchars($value).'</label>');
119     return $this->toString();
120   }
121 }
122 class RateCellRenderer extends DefaultCellRenderer {
123   function render(&$table, $value, $row, $column, $selected = false) {
124     global $assigned_projects;
125
126     $field = new FloatField('rate_'.$table->getValueAtName($row,'id'));
127     $field->setFormName($table->getFormName());
128     $field->setSize(5);
129     $field->setFormat('.2');
130     foreach ($assigned_projects as $p) {
131       if ($p['id'] == $table->getValueAtName($row,'id')) $field->setValue($p['rate']);
132     }
133     $this->setValue($field->getHtml());
134     return $this->toString();
135   }
136 }
137 // Create projects table.
138 $table = new Table('projects');
139 $table->setIAScript('setRate');
140 $table->setTableOptions(array('width'=>'100%','cellspacing'=>'1','cellpadding'=>'3','border'=>'0'));
141 $table->setRowOptions(array('valign'=>'top','class'=>'tableHeader'));
142 $table->setData($projects);
143 $table->setKeyField('id');
144 $table->setValue($cl_projects);
145 $table->addColumn(new TableColumn('name', $i18n->get('label.project'), new NameCellRenderer()));
146 $table->addColumn(new TableColumn('p_rate', $i18n->get('form.users.rate'), new RateCellRenderer()));
147 $form->addInputElement($table);
148
149 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$user_id));
150 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.save')));
151
152 if ($request->isPost()) {
153   // Validate user input.
154   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
155   if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
156   if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
157     if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
158     if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
159     if ($cl_password1 !== $cl_password2)
160       $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
161   }
162   if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
163   // Require selection of a client for a client role.
164   if ($user->isPluginEnabled('cl') && ttRoleHelper::isClientRole($cl_role_id) && !$cl_client_id) $err->add($i18n->get('error.client'));
165   if (!ttValidFloat($cl_rate, true)) $err->add($i18n->get('error.field'), $i18n->get('form.users.default_rate'));
166
167   if ($err->no()) {
168     $existing_user = ttUserHelper::getUserByLogin($cl_login);
169     if (!$existing_user || ($user_id == $existing_user['id'])) {
170
171         $fields = array(
172         'name' => $cl_name,
173         'login' => $cl_login,
174         'password' => $cl_password1,
175         'email' => $cl_email,
176         'status' => $cl_status,
177         'rate' => $cl_rate,
178         'projects' => $assigned_projects);
179       if (in_array('manage_users', $user->rights) && $cl_role_id) {
180         $fields['role_id'] = $cl_role_id;
181         $fields['client_id'] = $cl_client_id;
182       }
183
184       if (ttUserHelper::update($user_id, $fields)) {
185
186         // If our own login changed, set new one in cookie to remember it.
187         if (($user_id == $user->id) && ($user->login != $cl_login)) {
188           setcookie('tt_login', $cl_login, time() + COOKIE_EXPIRE, '/');
189         }
190
191         // In case the name of the "on behalf" user has changed - set it in session.
192         if (($user->behalf_id == $user_id) && ($user->behalf_name != $cl_name)) {
193           $_SESSION['behalf_name'] = $cl_name;
194         }
195
196         // If we deactivated our own account, do housekeeping and logout.
197         if ($user->id == $user_id && !is_null($cl_status) && $cl_status == INACTIVE) {
198           // Remove tt_login cookie that stores login name.
199           unset($_COOKIE['tt_login']);
200           setcookie('tt_login', NULL, -1);
201
202           $auth->doLogout();
203           header('Location: login.php');
204           exit();
205         }
206
207         header('Location: users.php');
208         exit();
209
210       } else
211         $err->add($i18n->get('error.db'));
212     } else
213       $err->add($i18n->get('error.user_exists'));
214   }
215 } // isPost
216
217 $can_swap = false;
218 if ($user->id == $user_id && $user->can('swap_roles')) {
219   $users_for_swap = ttTeamHelper::getUsersForSwap();
220   if (is_array($users_for_swap) && sizeof($users_for_swap) > 0)
221     $can_swap = true;
222 }
223
224 $rates = ttProjectHelper::getRates($user_id);
225 $smarty->assign('rates', $rates);
226
227 $smarty->assign('auth_external', $auth->isPasswordExternal());
228 $smarty->assign('active_roles', $active_roles);
229 $smarty->assign('can_swap', $can_swap);
230 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
231 $smarty->assign('onload', 'onLoad="document.userForm.name.focus();handleClientControl();"');
232 $smarty->assign('user_id', $user_id);
233 $smarty->assign('title', $i18n->get('title.edit_user'));
234 $smarty->assign('content_page_name', 'user_edit.tpl');
235 $smarty->display('index.tpl');