diff options
author | emkael <emkael@tlen.pl> | 2016-05-06 15:40:50 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-05-06 15:40:50 +0200 |
commit | a43e887c380fc414121792658136a34051526b6a (patch) | |
tree | 1816ec713671d02dd3d24e92c2f4675f7deab30d /app/php/facades/UserFacade.php | |
parent | 5bfe174b5907440a4f08bd45fd9c384ab5c58037 (diff) |
* DbUser refactored to use a facade
Diffstat (limited to 'app/php/facades/UserFacade.php')
-rw-r--r-- | app/php/facades/UserFacade.php | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/app/php/facades/UserFacade.php b/app/php/facades/UserFacade.php new file mode 100644 index 0000000..696f77f --- /dev/null +++ b/app/php/facades/UserFacade.php @@ -0,0 +1,66 @@ +<?php + +Prado::using('Application.facades.Facade'); +Prado::using('Application.user.DbUser'); +Prado::using('Application.model.User'); +Prado::using('Application.dto.TimezoneDTO'); + +class UserFacade extends Facade { + + public function setTimezonePreference(DbUser $user, $timezone) { + if ($user->IsGuest) { + throw new TInvalidDataException( + 'Timezone preference change impossible for guest user' + ); + } + $user->DbRecord->Timezone = $timezone; + $user->DbRecord->save(); + } + + public function getTimezonePreference(DbUser $user) { + if (!$user->IsGuest) { + try { + return new TimezoneDTO($user->DbRecord->Timezone); + } catch(Exception $e) {} + } + return new TimezoneDTO(date_default_timezone_get()); + } + + public function checkForUsername($login) { + return !User::finder()->countByLogin($login); + } + + public function registerUser($login, $password, $admin) { + $newUser = new User(); + $newUser->Login = $login; + $newUser->Password = $this->generatePassword($password); + $newUser->IsAdmin = $admin; + $newUser->save(); + return $newUser; + } + + public function changePassword(DbUser $user, $pass) { + if (!$user->IsGuest) { + $user->DbRecord->Password = $this->generatePassword($pass); + $user->DbRecord->save(); + } + } + + public function verifyUserPassword($password, DbUser $user) { + $dbPassword = $user->IsGuest ? '' : $user->DbRecord->Password; + return $this->verifyPassword($password, $dbPassword); + } + + public function generatePassword($password) { + return password_hash($password, PASSWORD_DEFAULT); + } + + public function verifyPassword($password, $dbPassword) { + return password_verify($password, $dbPassword); + } + + + +} + +?> |