From: Nik Okuntseff Date: Wed, 21 Mar 2018 19:12:59 +0000 (+0000) Subject: Refactored register.php by encapsulating stuff in ttRegistrator class. X-Git-Tag: timetracker_1.19-1~984 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=d40763fdef73f3118da81b497baafff29a73c5ba;p=timetracker.git Refactored register.php by encapsulating stuff in ttRegistrator class. --- diff --git a/WEB-INF/lib/ttRegistrator.class.php b/WEB-INF/lib/ttRegistrator.class.php index 9a650c9c..0e51ce39 100644 --- a/WEB-INF/lib/ttRegistrator.class.php +++ b/WEB-INF/lib/ttRegistrator.class.php @@ -35,6 +35,9 @@ class ttRegistrator { var $group_name = null; // Group name. var $currency = null; // Currency. var $lang = null; // Language. + var $group_id = null; // Group id, set after we create a group. + var $role_id = null; // Role id for top managers. + var $user_id = null; // User id after registration. var $err = null; // Error object, passed to us as reference. // We use it to communicate errors to caller. @@ -48,7 +51,7 @@ class ttRegistrator { $this->group_name = $fields['group_name']; $this->currency = $fields['currency']; $this->lang = $fields['lang']; - if (!$thins->lang) $this->lang = 'en'; + if (!$this->lang) $this->lang = 'en'; $this->err = $err; // Validate passed in parameters. @@ -78,6 +81,76 @@ class ttRegistrator { // The register function registers a user in Time Tracker. function register() { - // TODO: work in progress. Not implemented. + global $i18n; + + if ($this->err->yes()) + return; // There are errors, do not proceed. + + import('ttUserHelper'); + if (ttUserHelper::getUserByLogin($this->login)) { + // User login already exists. + $this->err->add($i18n->getKey('error.user_exists')); + return; + } + + // Create a new group. + $this->group_id = $this->createGroup(); + if (!$this->group_id) { + $this->err->add($i18n->getKey('error.db')); + return; + } + + import('ttRoleHelper'); + if (!ttRoleHelper::createPredefinedRoles($this->group_id, $this->lang)) { + $err->add($i18n->getKey('error.db')); + return; + } + $this->role_id = ttRoleHelper::getTopManagerRoleID(); + $this->user_id = $this->createUser(); + + if (!$this->user_id) { + $err->add($i18n->getKey('error.db')); + return; + } + } + + // The createGroup function creates a group in Time Tracker as part + // of user registration process. This is a top group for user as top manager. + function createGroup() { + $mdb2 = getConnection(); + + $name = $mdb2->quote($this->group_name); + $currency = $mdb2->quote($this->currency); + $lang = $mdb2->quote($this->lang); + + $sql = "insert into tt_teams (name, currency, lang) values($name, $currency, $lang)"; + $affected = $mdb2->exec($sql); + + if (!is_a($affected, 'PEAR_Error')) { + $group_id = $mdb2->lastInsertID('tt_teams', 'id'); + return $group_id; + } + return false; + } + + // The createUser creates a user in database as part of registration process. + function createUser() { + $mdb2 = getConnection(); + + $login = $mdb2->quote($this->login); + $password = 'md5('.$mdb2->quote($this->password1).')'; + $name = $mdb2->quote($this->user_name); + $email = $mdb2->quote($this->email); + $created = 'now()'; + $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']); + $values = "values($login, $password, $name, $this->group_id, $this->role_id, $email, $created, $created_ip)"; + + $sql = 'insert into tt_users (login, password, name, team_id, role_id, email, created, created_ip) '.$values; + $affected = $mdb2->exec($sql); + if (!is_a($affected, 'PEAR_Error')) { + $user_id = $mdb2->lastInsertID('tt_users', 'id'); + return $user_id; + } + return false; } } diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index aaaf8db2..0f5a4f2d 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.17.64.4125 | Copyright © Anuko | +  Anuko Time Tracker 1.17.64.4126 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/register.php b/register.php index a34a78fb..3ddcb990 100644 --- a/register.php +++ b/register.php @@ -28,8 +28,6 @@ require_once('initialize.php'); import('form.Form'); -import('ttUserHelper'); -import('ttRoleHelper'); if (!isTrue(MULTITEAM_MODE) || $auth->isPasswordExternal()) { header('Location: login.php'); @@ -99,39 +97,17 @@ if ($request->isPost()) { // Create an instance of ttRegistrator class. import('ttRegistrator'); $registrator = new ttRegistrator($fields, $err); - // Validation of user input occurs in ttRegistrator constructor above. + $registrator->register(); if ($err->no()) { - if (!ttUserHelper::getUserByLogin($cl_manager_login)) { - // Create a new team. - $team_id = ttTeamHelper::insert(array('name'=>$cl_team_name,'currency'=>$cl_currency,'lang'=>$cl_lang)); - if ($team_id) { - if (!ttRoleHelper::createPredefinedRoles($team_id, $cl_lang)) - $err->add($i18n->getKey('error.db')); - - $role_id = ttRoleHelper::getTopManagerRoleID(); - - // Team created, now create a team manager. - $user_id = ttUserHelper::insert(array( - 'team_id' => $team_id, - 'role_id' => $role_id, - 'name' => $cl_manager_name, - 'login' => $cl_manager_login, - 'password' => $cl_password1, - 'email' => $cl_manager_email)); - } - if ($team_id && $user_id) { - if ($auth->doLogin($cl_manager_login, $cl_password1)) { - setcookie('tt_login', $cl_manager_login, time() + COOKIE_EXPIRE, '/'); - header('Location: time.php'); - } else { - header('Location: login.php'); - } - exit(); - } else - $err->add($i18n->getKey('error.db')); - } else - $err->add($i18n->getKey('error.user_exists')); + // Registration successful. + if ($auth->doLogin($cl_manager_login, $cl_password1)) { + setcookie('tt_login', $cl_manager_login, time() + COOKIE_EXPIRE, '/'); + header('Location: time.php'); + } else { + header('Location: login.php'); + } + exit(); } } // isPost