// ttAdmin class is used to perform admin tasks.
class ttAdmin {
- var $err = null; // Error object, passed to us as reference.
- // We use it to communicate errors to caller.
+ var $err = null; // Error object, passed to us as reference.
+ // We use it to communicate errors to caller.
// Constructor.
function __construct(&$err = null) {
// Mark user binds as deleted.
$sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
$affected = $mdb2->exec($sql);
- if (is_a($affected, 'PEAR_Error'))
- return false;
+ if (is_a($affected, 'PEAR_Error')) return false;
// Mark favorite reports as deleted.
$sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
$affected = $mdb2->exec($sql);
- if (is_a($affected, 'PEAR_Error'))
- return false;
+ if (is_a($affected, 'PEAR_Error')) return false;
// Mark user as deleted.
global $user;
$modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
$sql = "update tt_users set status = NULL $modified_part where id = $user_id";
$affected = $mdb2->exec($sql);
- if (is_a($affected, 'PEAR_Error'))
- return false;
+ if (is_a($affected, 'PEAR_Error')) return false;
return true;
}
$affected = $mdb2->exec($sql);
if (is_a($affected, 'PEAR_Error')) return false;
}
+
return true;
}
return true;
}
+ // validateUserInfo validates account information entered by user.
+ function validateUserInfo($fields) {
+ global $i18n;
+ global $user;
+ global $auth;
+
+ $result = true;
+
+ if (!ttValidString($fields['name'])) {
+ $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.person_name'));
+ $result = false;
+ }
+ if (!ttValidString($fields['login'])) {
+ $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.login'));
+ $result = false;
+ }
+ // If we change login, it must be unique.
+ if ($fields['login'] != $user->login) {
+ if (ttUserHelper::getUserByLogin($fields['login'])) {
+ $this->err->add($i18n->getKey('error.user_exists'));
+ $result = false;
+ }
+ }
+ if (!$auth->isPasswordExternal() && ($fields['password1'] || $fields['password2'])) {
+ if (!ttValidString($fields['password1'])) {
+ $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
+ $result = false;
+ }
+ if (!ttValidString($fields['password2'])) {
+ $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
+ $result = false;
+ }
+ if ($fields['password1'] !== $fields['password2']) {
+ $this->err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
+ $result = false;
+ }
+ }
+ if (!ttValidEmail($fields['email'], true)) {
+ $this->err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
+ $result = false;
+ }
+
+ return $result;
+ }
+
// updateSelf validates user input and updates admin account with new information.
function updateSelf($fields) {
- return false; // Not yet implemeneted.
+ if (!$this->validateUserInfo($fields)) return false; // Can't continue as user input is invalid.
+
+ global $user;
+ global $i18n;
+ $mdb2 = getConnection();
+
+ // Update self.
+ $user_id = $user->id;
+ $login_part = 'login = '.$mdb2->quote($fields['login']);
+ if ($fields['password1'])
+ $password_part = ', password = md5('.$mdb2->quote($fields['password1']).')';
+ $name_part = ', name = '.$mdb2->quote($fields['name']);
+ $email_part = ', email = '.$mdb2->quote($fields['email']);
+ $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
+ $sql = 'update tt_users set '.$login_part.$password_part.$name_part.$email_part.$modified_part.'where id = '.$user_id;
+ $affected = $mdb2->exec($sql);
+ if (is_a($affected, 'PEAR_Error')) {
+ $this->err->add($i18n->getKey('error.db'));
+ return false;
+ }
+
+ return true;
}
}
// The register function registers a user in Time Tracker.
function register() {
- if ($this->err->yes()) return; // There are errors, do not proceed.
+ if ($this->err->yes()) return false; // There are errors, do not proceed.
global $i18n;
if (ttUserHelper::getUserByLogin($this->login)) {
// User login already exists.
$this->err->add($i18n->getKey('error.user_exists'));
- return;
+ return false;
}
// Create a new group.
$this->group_id = $this->createGroup();
if (!$this->group_id) {
$this->err->add($i18n->getKey('error.db'));
- return;
+ return false;
}
import('ttRoleHelper');
if (!ttRoleHelper::createPredefinedRoles($this->group_id, $this->lang)) {
$err->add($i18n->getKey('error.db'));
- return;
+ return false;
}
$this->role_id = ttRoleHelper::getTopManagerRoleID();
$this->user_id = $this->createUser();
if (!$this->user_id) {
$err->add($i18n->getKey('error.db'));
- return;
+ return false;
}
- $this->setCreatedBy($this->user_id);
+ if (!$this->setCreatedBy($this->user_id))
+ return false;
+
+ return true;
}
// The createGroup function creates a group in Time Tracker as part
$form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.submit')));
if ($request->isPost()) {
-/*
- * This is work in progress... refactoring to replace the block below.
-
// Create fields array for ttAdmin instance.
$fields = array(
'name' => $cl_name,
'password2' => $cl_password2,
'email' => $cl_email);
- import('ttAdmin');
- $admin = new ttAdmin($err);
- $result = $admin->updateSelf($fields);
- if ($result) {
- header('Location: admin_teams.php');
- exit();
- }
- */
-
- // Validate user input.
- if (!ttValidString($cl_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.person_name'));
- if (!ttValidString($cl_login)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.login'));
- // New login must be unique.
- if ($cl_login != $user->login && ttUserHelper::getUserByLogin($cl_login))
- $err->add($i18n->getKey('error.user_exists'));
- if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
- if (!ttValidString($cl_password1)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
- if (!ttValidString($cl_password2)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
- if ($cl_password1 !== $cl_password2)
- $err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
- }
- if (!ttValidEmail($cl_email, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
- // Finished validating user input.
-
- if ($err->no()) {
- if (ttUserHelper::update($user->id, array(
- 'name' => $cl_name,
- 'login' => $cl_login,
- 'password' => $cl_password1,
- 'email' => $cl_email,
- 'status' => ACTIVE))) {
- header('Location: admin_teams.php');
- exit();
- } else {
- $err->add($i18n->getKey('error.db'));
- }
+ import('ttAdmin');
+ $admin = new ttAdmin($err);
+ $result = $admin->updateSelf($fields);
+ if ($result) {
+ header('Location: admin_teams.php');
+ exit();
}
} // isPost