diff options
author | emkael <emkael@tlen.pl> | 2016-10-25 18:59:02 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-10-25 18:59:02 +0200 |
commit | 7f15fb107344f5e876df6f053004415ea1759c70 (patch) | |
tree | 687955f48eacfa27a2e7f1375f7a24b311187df9 /app/frontend/facades | |
parent | 95e5a74b0663e9110ea638dd5d809e9fd1541fa9 (diff) |
* saving and restoring user data from cookie
Diffstat (limited to 'app/frontend/facades')
-rw-r--r-- | app/frontend/facades/UserFacade.php | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/app/frontend/facades/UserFacade.php b/app/frontend/facades/UserFacade.php index 5c8b6c0..a976af2 100644 --- a/app/frontend/facades/UserFacade.php +++ b/app/frontend/facades/UserFacade.php @@ -3,8 +3,10 @@ Prado::using('Application.facades.Facade'); Prado::using('Application.user.DbUser'); Prado::using('Application.model.User'); +Prado::using('Application.model.UserAuthKey'); Prado::using('Application.dto.TimezoneDTO'); Prado::using('Application.dto.LanguageDTO'); +Prado::using('Application.dto.UserKeyDTO'); class UserFacade extends Facade { @@ -118,6 +120,46 @@ class UserFacade extends Facade { return FALSE; } + public function createUserAuthKey(DbUser $user) { + if ($user->IsGuest) { + return NULL; + } + $authKey = new UserAuthKey(); + $authKey->AuthKey = md5(mt_rand()); + $authKey->IPAddress = Prado::getApplication()->Request->UserHostAddress; + $authKey->UserID = $user->DbRecord->ID; + $authKey->save(); + $dto = new UserKeyDTO(); + $dto->loadRecord($authKey); + return $dto; + } + + public function compileCookieData(UserKeyDTO $key) { + $data = base64_encode(serialize($key)); + return Prado::getApplication()->SecurityManager->hashData($data); + } + + public function getUserFromCookieData(THttpCookie $cookie) { + $application = Prado::getApplication(); + try { + $data = $application->SecurityManager->validateData($cookie->getValue()); + if ($data) { + $data = unserialize(base64_decode($data)); + if ($data instanceof UserKeyDTO) { + $dataRecord = UserAuthKey::finder()->findByAuthKey($data->Key); + if ($dataRecord + && $data->User === $dataRecord->User->Login + && $data->IPAddress === $application->Request->UserHostAddress) { + return $data; + } + } + } + return NULL; + } catch (Exception $e) { + return NULL; + } + } + } ?> |