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