From 01ff92a41e38ad256e68809374b6909bc2a37317 Mon Sep 17 00:00:00 2001 From: xamgore <xamgore@ya.ru> Date: Sun, 14 Jun 2015 17:47:45 +0300 Subject: [PATCH] Class Account is not a singleton Moved some functions from Account to Model_Account & Model_Teacher. --- ~dev_rating/application/classes/Account.php | 72 ++++--------------- .../classes/Controller/Authentication.php | 2 +- .../classes/Controller/Handler/Sign.php | 8 +-- .../application/classes/FileParser.php | 2 +- .../application/classes/Model/Account.php | 59 ++++++++------- .../application/classes/Model/Teacher.php | 15 ++++ ~dev_rating/application/classes/User.php | 5 +- 7 files changed, 68 insertions(+), 95 deletions(-) diff --git a/~dev_rating/application/classes/Account.php b/~dev_rating/application/classes/Account.php index 32e6f8115..d99d9aac3 100644 --- a/~dev_rating/application/classes/Account.php +++ b/~dev_rating/application/classes/Account.php @@ -41,63 +41,15 @@ function gradeSendMail($subject, $body, $sendToEmail, $sendToName) { } } -class Account { - - protected static $_instance; - protected $_config; - - /** - * Вовзращает экземпляр класса (singleton-паттерн) - * @return self - */ - public static function instance() { - if (!isset(self::$_instance)) { - $config = Kohana::$config->load('account'); - self::$_instance = new self($config); - } - return self::$_instance; - } - - private function __construct($config = array()) { - $this->_config = $config; - } - - private function generateActivationCode() { - $activationCode = Text::random('ABDCEFGHJKLMNPQRSTUVWXYZ123456789', 10); - return UTF8::strtoupper($activationCode); - } - - public function createTeacher($lastName, $firstName, $secondName, $degreeID, $departmentID) { - $code = $this->generateActivationCode(); - $response = Model_Account::createTeacher($lastName, $firstName, $secondName, $degreeID, $departmentID, $code); - return $response == -1 ? -1 : $code; - } - - public function createTeacherByDepName($lastName, $firstName, $secondName, $departmentName, $facultyID) { - $code = $this->generateActivationCode(); - $response = Model_Account::createTeacherByDepName($lastName, $firstName, $secondName, $departmentName, $facultyID, $code); - return $response == -1 ? -1 : $code; - } - - public function createStudent($lastName, $firstName, $secondName, $grade, $groupNum, $facultyID) { - $code = $this->generateActivationCode(); - $response = Model_Account::createStudent($lastName, $firstName, $secondName, $grade, $groupNum, $facultyID, $code); - return $response == -1 ? -1 : $code; - } - - public function createStudentEx($lastName, $firstName, $secondName, $grade, $groupNum, $degree, $spec, $facultyID) { - $code = $this->generateActivationCode(); - $response = Model_Account::createStudentEx($lastName, $firstName, $secondName, $grade, $groupNum, $degree, $spec, $facultyID, $code); - return $response == -1 ? -1 : $code; - } - - private function checkTokenLifetime($creationDate) { +class Account +{ + private static function checkTokenLifetime($creationDate) { $config = Kohana::$config->load('security.securityPolicy'); $lifetime = $config['recoveryToken']['lifetime']; return (time() - $creationDate) > $lifetime; } - public function checkToken($token) { + public static function checkToken($token) { $recovery = Model_Account::getRecoveryInfoByToken($token)[0]; $response = true; @@ -105,7 +57,7 @@ class Account { $response = false; } else { $date = strtotime($recovery['Date']); - if ($this->checkTokenLifetime($date)) { + if (self::checkTokenLifetime($date)) { Model_Account::useRecoveryToken($recovery['Token']); $response = false; } @@ -113,7 +65,7 @@ class Account { return $response; } - public function createRecoveryRequest($email) { + public static function createRecoveryRequest($email) { $requestToken = sha1($email.time().Cookie::$salt); $UserFullName = Model_Account::createRecoveryToken($email, $requestToken); @@ -140,30 +92,30 @@ class Account { gradeSendMail($subject, $twig->render(), $email, $UserFullName); } - public function changePasswordByToken($token, $password) { + public static function changePasswordByToken($token, $password) { $recovery = Model_Account::getRecoveryInfoByToken($token)[0]; - $this->changePassword($recovery['AccountID'], $password); + self::changePassword($recovery['AccountID'], $password); Model_Account::useRecoveryToken($token); } - public function doesLoginExist($login) { + public static function doesLoginExist($login) { $login_count = Model_Account::checkAccountExistence($login, 'login'); return $login_count > 0; } - public function isMailValid($email) { + public static function isMailValid($email) { $email_count = Model_Account::checkAccountExistence($email, 'email'); return $email_count > 0; } // // We don't change email address:\ -// public function changeEMail($id, $newEMail) { +// public static function changeEMail($id, $newEMail) { // $response = Model_Account::changeMail($id, $newEMail); // return $response != -1; // } - public function changePassword($id, $newPassword) { + public static function changePassword($id, $newPassword) { $response = Model_Account::changeAccountData($id, $newPassword, 'password'); return $response != -1; } diff --git a/~dev_rating/application/classes/Controller/Authentication.php b/~dev_rating/application/classes/Controller/Authentication.php index 127ce5d0f..a9dd895b9 100644 --- a/~dev_rating/application/classes/Controller/Authentication.php +++ b/~dev_rating/application/classes/Controller/Authentication.php @@ -64,7 +64,7 @@ class Controller_Authentication extends Controller public function action_restore() { $token = $this->request->param('token'); - if (!Account::instance()->checkToken($token)) { + if (!Account::checkToken($token)) { $message = "Данная ссылка для восстановления пароля более не действительна!\n" . "Либо истекло время действия ссылки, либо она уже была использована."; throw HTTP_Exception::factory(403, $message); diff --git a/~dev_rating/application/classes/Controller/Handler/Sign.php b/~dev_rating/application/classes/Controller/Handler/Sign.php index 28c8d3145..afb4afb9e 100644 --- a/~dev_rating/application/classes/Controller/Handler/Sign.php +++ b/~dev_rating/application/classes/Controller/Handler/Sign.php @@ -78,9 +78,9 @@ class Controller_Handler_Sign extends Controller_Handler { if($this->post->check()) { $email = $this->post['email']; - if(Account::instance()->isMailValid($email)) + if(Account::isMailValid($email)) { - Account::instance()->createRecoveryRequest($this->post['email']); + Account::createRecoveryRequest($this->post['email']); $response['success'] = true; } else @@ -102,9 +102,9 @@ class Controller_Handler_Sign extends Controller_Handler { if($this->post->check()) { $token = $this->post['token']; - if(Account::instance()->checkToken($token)) + if(Account::checkToken($token)) { - Account::instance()->changePasswordByToken($token, $this->post['password']); + Account::changePasswordByToken($token, $this->post['password']); $response['success'] = true; } } diff --git a/~dev_rating/application/classes/FileParser.php b/~dev_rating/application/classes/FileParser.php index ba29f1b2d..73032ba16 100644 --- a/~dev_rating/application/classes/FileParser.php +++ b/~dev_rating/application/classes/FileParser.php @@ -17,7 +17,7 @@ class FileParser $groupNum = $line[2]; $degree = Model_Grades::getDegreeType($line[3]); $specialization = $line[4]; - $attempt = Account::instance()->createStudentEx( + $attempt = Model_Account::createStudentEx( $lastName, $firstName, $secondName, $gradeNum, $groupNum, $degree, $specialization, $facultyID ); diff --git a/~dev_rating/application/classes/Model/Account.php b/~dev_rating/application/classes/Model/Account.php index e07b2dee6..b2dd66958 100644 --- a/~dev_rating/application/classes/Model/Account.php +++ b/~dev_rating/application/classes/Model/Account.php @@ -33,52 +33,53 @@ class Model_Account extends Model ->execute()->get('ID'); } - public static function changeTeacherInfo($id, $lastName, $firstName, $secondName, $degreeID, $departmentID) { - $sql = "SELECT `ChangeTeacherInfo`(:id, :last, :first, :second, :degree, :department) AS `UserID`;"; - return DB::query(Database::SELECT, $sql) - ->parameters([ - ':id' => $id, - ':last' => $lastName, - ':first' => $firstName, - ':second' => $secondName, - ':degree' => $degreeID, - 'department' => $departmentID, - ])->execute()->get('UserID'); + + private static function generateActivationCode() { + $activationCode = Text::random('ABDCEFGHJKLMNPQRSTUVWXYZ123456789', 10); + return UTF8::strtoupper($activationCode); } - public static function createTeacher($lastName, $firstName, $secondName, $degreeID, $departmentID, $activationCode) { + public static function createTeacher($lastName, $firstName, $secondName, $degreeID, $departmentID) { + $code = self::generateActivationCode(); + $sql = "SELECT `CreateTeacher`() AS `UserID`;"; - return DB::query(Database::SELECT, $sql) + $response = DB::query(Database::SELECT, $sql) ->parameters([ ':last' => $lastName, ':first' => $firstName, ':second' => $secondName, ':degree' => $degreeID, ':department' => $departmentID, - ':code' => $activationCode, + ':code' => $code, ])->execute()->get('UserID'); + + return $response == -1 ? -1 : $code; } - public static function createTeacherByDepName($lastName, $firstName, $secondName, $departmentID, $facultyID, $activationCode) { - if ($departmentID == '') { - return -1; - } + public static function createTeacherByDepName($lastName, $firstName, $secondName, $departmentID, $facultyID) { + if ($departmentID == '') return -1; + + $code = self::generateActivationCode(); $sql = "SELECT `CreateTeacherByDepName`(:last, :first, :second, :department, :faculty, :code) AS `UserID`;"; - return DB::query(Database::SELECT, $sql) + $response = DB::query(Database::SELECT, $sql) ->parameters([ ':last' => $lastName, ':first' => $firstName, ':second' => $secondName, ':department' => $departmentID, ':faculty' => $facultyID, - ':code' => $activationCode, + ':code' => $code, ])->execute()->get('UserID'); + + return $response == -1 ? -1 : $code; } - public static function createStudent($lastName, $firstName, $secondName, $grade, $groupNum, $facultyID, $activationCode) { + public static function createStudent($lastName, $firstName, $secondName, $grade, $groupNum, $facultyID) { + $code = self::generateActivationCode(); + $sql = "SELECT `CreateStudent`(:last, :first, :second, :grade, :group, :faculty, :code) AS `UserID`;"; - return DB::query(Database::SELECT, $sql) + $response = DB::query(Database::SELECT, $sql) ->parameters([ ':last' => $lastName, ':first' => $firstName, @@ -86,13 +87,17 @@ class Model_Account extends Model ':grade' => $grade, ':group' => $groupNum, ':faculty' => $facultyID, - ':code' => $activationCode, + ':code' => $code, ])->execute()->get('UserID'); + + return $response == -1 ? -1 : $code; } - public static function createStudentEx($lastName, $firstName, $secondName, $gradeNum, $groupNum, $degree, $specialization, $facultyID, $activationCode) { + public static function createStudentEx($lastName, $firstName, $secondName, $gradeNum, $groupNum, $degree, $specialization, $facultyID) { + $code = self::generateActivationCode(); + $sql = "SELECT `CreateStudentEx`(:last, :first, :second, :grade, :group, :degree, :spec, :faculty, :code) AS `UserID`;"; - return DB::query(Database::SELECT, $sql) + $response = DB::query(Database::SELECT, $sql) ->parameters([ ':last' => $lastName, ':first' => $firstName, @@ -102,8 +107,10 @@ class Model_Account extends Model ':degree' => $degree, ':spec' => $specialization, ':faculty' => $facultyID, - ':code' => $activationCode, + ':code' => $code, ])->execute()->get('UserID'); + + return $response == -1 ? -1 : $code; } diff --git a/~dev_rating/application/classes/Model/Teacher.php b/~dev_rating/application/classes/Model/Teacher.php index 6676691d3..a21e4a208 100644 --- a/~dev_rating/application/classes/Model/Teacher.php +++ b/~dev_rating/application/classes/Model/Teacher.php @@ -14,6 +14,21 @@ class Model_Teacher extends Model return $t; } + + public function changeInfo($lastName, $firstName, $secondName, $degreeID, $departmentID) { + $sql = "SELECT `ChangeTeacherInfo`(:id, :last, :first, :second, :degree, :department) AS `UserID`;"; + return DB::query(Database::SELECT, $sql) + ->parameters([ + ':id' => $this->ID, + ':last' => $lastName, + ':first' => $firstName, + ':second' => $secondName, + ':degree' => $degreeID, + 'department' => $departmentID, + ])->execute()->get('UserID'); + // todo: what does it return? + } + /** @return Model_Discipline[] */ public function getDisciplines() { $semesterID = User::instance()->SemesterID; diff --git a/~dev_rating/application/classes/User.php b/~dev_rating/application/classes/User.php index 718c50a0d..52c2a3f3f 100644 --- a/~dev_rating/application/classes/User.php +++ b/~dev_rating/application/classes/User.php @@ -260,7 +260,7 @@ class User implements ArrayAccess } public function changeMail($email) { - if (!$this->isSignedIn() || Account::instance()->isMailValid($email)) + if (!$this->isSignedIn() || Account::isMailValid($email)) return false; $token = md5(time() . $this->EMail . $email); @@ -282,8 +282,7 @@ class User implements ArrayAccess public function changeProfile($data) { if ($this->Type == 'teacher') { - Model_Account::changeTeacherInfo( - $this['TeacherID'], + Model_Teacher::with($this['TeacherID'])->changeInfo( $data['lastName'], $data['firstName'], $data['secondName'], $data['jobPositionID'], $data['departmentID'] ); -- GitLab