diff options
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; + } + } + } ?> |