diff options
-rw-r--r-- | app/php/controls/AddToFilter.tpl | 2 | ||||
-rw-r--r-- | app/php/controls/PasswordChange.php | 8 | ||||
-rw-r--r-- | app/php/controls/RegistrationForm.php | 14 | ||||
-rw-r--r-- | app/php/controls/TimezoneSelect.php | 9 | ||||
-rw-r--r-- | app/php/dto/EventDTO.php | 5 | ||||
-rw-r--r-- | app/php/facades/CalendarFacade.php | 22 | ||||
-rw-r--r-- | app/php/facades/EventFacade.php | 33 | ||||
-rw-r--r-- | app/php/facades/UserFacade.php | 66 | ||||
-rw-r--r-- | app/php/pages/Profile.page | 2 | ||||
-rw-r--r-- | app/php/pages/Signup.page | 4 | ||||
-rw-r--r-- | app/php/user/DbUser.php | 64 |
11 files changed, 138 insertions, 91 deletions
diff --git a/app/php/controls/AddToFilter.tpl b/app/php/controls/AddToFilter.tpl index 0c38584..5b63be8 100644 --- a/app/php/controls/AddToFilter.tpl +++ b/app/php/controls/AddToFilter.tpl @@ -1,6 +1,6 @@ <com:TActiveCheckBox ID="Box" OnCheckedChanged="setUserPreference" CssClass="addToFilterBox"> <prop:Enabled><%= !$this->UserToManage->IsGuest %></prop:Enabled> - <prop:Checked><%= $this->User->isCalendarPreferred($this->getCalendar()->ID) %></prop:Checked> + <prop:Checked><%= $this->Facade->isCalendarPreferred($this->UserToManage, $this->getCalendar()->ID) %></prop:Checked> <prop:ToolTip><%= $this->UserToManage->IsGuest ? 'log in to manage your selections' : '' %></prop:ToolTip> </com:TActiveCheckBox> <com:TLabel ForControl="Box"> diff --git a/app/php/controls/PasswordChange.php b/app/php/controls/PasswordChange.php index 7c177a6..0f0300c 100644 --- a/app/php/controls/PasswordChange.php +++ b/app/php/controls/PasswordChange.php @@ -3,6 +3,7 @@ Prado::using('Application.web.FacadeTemplateControl'); Prado::using('Application.user.DbUser'); +Prado::using('Application.facades.UserFacade'); class PasswordChange extends FacadeTemplateControl { @@ -20,15 +21,16 @@ class PasswordChange extends FacadeTemplateControl { } public function checkPassword($sender, $param) { - $param->IsValid = DbUser::verifyPassword( - $this->Password->Text, $this->UserToChange->getPassword() + $param->IsValid = $this->getFacade()->verifyUserPassword( + $this->Password->Text, $this->UserToChange ); } public function changePassword($sender, $param) { $this->SuccessMessage->Visible = FALSE; if ($this->Page->IsValid) { - $this->UserToChange->changePassword( + $this->getFacade()->changePassword( + $this->UserToChange, $this->NewPassword->Text ); $this->SuccessMessage->Visible = TRUE; diff --git a/app/php/controls/RegistrationForm.php b/app/php/controls/RegistrationForm.php index d88fb95..46494e3 100644 --- a/app/php/controls/RegistrationForm.php +++ b/app/php/controls/RegistrationForm.php @@ -2,21 +2,21 @@ Prado::using('Application.web.FacadeTemplateControl'); -Prado::using('Application.model.User'); +Prado::using('Application.facades.UserFacade'); class RegistrationForm extends FacadeTemplateControl { public function checkUsername($sender, $param) { - $param->IsValid = !User::finder()->countByLogin($this->Login->SafeText); + $param->IsValid = $this->getFacade()->checkForUsername($this->Login->SafeText); } public function registerUser($sender, $param) { if ($this->Page->IsValid) { - $newUser = new User(); - $newUser->Login = $this->Login->SafeText; - $newUser->Password = DbUser::generatePassword($this->Password->Text); - $newUser->IsAdmin = $this->Admin->Checked; - $newUser->save(); + $this->getFacade()->registerUser( + $this->Login->SafeText, + $this->Password->Text, + $this->Admin->Checked + ); $this->Response->redirect( $this->Service->constructUrl(NULL) ); diff --git a/app/php/controls/TimezoneSelect.php b/app/php/controls/TimezoneSelect.php index 31ba15d..7ef30cb 100644 --- a/app/php/controls/TimezoneSelect.php +++ b/app/php/controls/TimezoneSelect.php @@ -3,6 +3,8 @@ Prado::using('Application.web.FacadeTemplateControl'); Prado::using('Application.user.DbUser'); +Prado::using('Application.facades.UserFacade'); + Prado::using('Application.dto.TimezoneDTO'); class TimezoneSelect extends FacadeTemplateControl { @@ -27,12 +29,15 @@ class TimezoneSelect extends FacadeTemplateControl { $this->Timezones->DataTextField = 'Label'; $this->Timezones->dataBind(); $this->Timezones->setSelectedValue( - $this->UserToChange->getTimezonePreference()->Name + $this->getFacade()->getTimezonePreference($this->UserToChange)->Name ); } public function saveTimezone($sender, $param) { - $this->UserToChange->setTimezonePreference($this->Timezones->SelectedValue); + $this->getFacade()->setTimezonePreference( + $this->UserToChange, + $this->Timezones->SelectedValue + ); } private function _getTimezones() { diff --git a/app/php/dto/EventDTO.php b/app/php/dto/EventDTO.php index b1042b1..856bcd5 100644 --- a/app/php/dto/EventDTO.php +++ b/app/php/dto/EventDTO.php @@ -2,6 +2,7 @@ Prado::using('Application.model.Entry'); Prado::using('Application.dto.CalendarDTO'); +Prado::using('Application.facades.UserFacade'); class EventDTO { @@ -16,7 +17,9 @@ class EventDTO { $utc = new DateTimeZone('UTC'); $targetTz = new DateTimeZone( - Prado::getApplication()->getUser()->getTimezonePreference()->Name + UserFacade::getInstance()->getTimezonePreference( + Prado::getApplication()->getUser() + )->Name ); $beginDate = new DateTime($event->BeginDate, $utc); $endDate = new DateTime($event->EndDate, $utc); diff --git a/app/php/facades/CalendarFacade.php b/app/php/facades/CalendarFacade.php index a1ff42d..c342cd2 100644 --- a/app/php/facades/CalendarFacade.php +++ b/app/php/facades/CalendarFacade.php @@ -22,8 +22,16 @@ class CalendarFacade extends Facade { ); } + public function getCalendarPreference(DbUser $user) { + if ($user->IsGuest) { + return Calendar::finder()->findAllByIsVisible(1); + } else { + return $user->DbRecord->Calendars; + } + } + public function getPreferenceList(DbUser $user) { - $calendars = $user->getCalendarPreference(); + $calendars = $this->getCalendarPreference($user); if ($calendars) { $categories = array_map( function($category) use($calendars) { @@ -39,6 +47,18 @@ class CalendarFacade extends Facade { return []; } + public function isCalendarPreferred(DbUser $user, $calendarID) { + return in_array( + $calendarID, + array_map( + function($calendar) { + return $calendar->UID; + }, + $this->getCalendarPreference($user) + ) + ); + } + public function addToPreference(DbUser $user, $calendarID) { if (!$user->IsGuest) { $calendar = Calendar::finder()->findByPk($calendarID); diff --git a/app/php/facades/EventFacade.php b/app/php/facades/EventFacade.php index a41b2f7..9533448 100644 --- a/app/php/facades/EventFacade.php +++ b/app/php/facades/EventFacade.php @@ -3,6 +3,7 @@ Prado::using('Application.facades.Facade'); Prado::using('Application.dto.EventDTO'); Prado::using('Application.model.Calendar'); +Prado::using('Application.facades.CalendarFacade'); Prado::using('Application.user.DbUser'); class EventFacade extends Facade { @@ -37,20 +38,24 @@ class EventFacade extends Facade { public function getTimeframeListForUser(DbUser $user, DateTime $dateFrom, DateTime $dateTo) { - $events = $this->getEventList( - $dateFrom->format('Y-m-d H:i:s'), - $dateTo->format('Y-m-d H:i:s'), - $user->getCalendarPreference() - ); - $calendars = $this->_getCalendarsForEvents($events); - return array_map( - function($event) use($calendars) { - $dto = new EventDTO(); - $dto->loadRecord($event, $calendars); - return $dto; - }, - $events - ); + $calendars = CalendarFacade::getInstance()->getCalendarPreference($user); + if ($calendars) { + $events = $this->getEventList( + $dateFrom->format('Y-m-d H:i:s'), + $dateTo->format('Y-m-d H:i:s'), + $calendars + ); + $calendars = $this->_getCalendarsForEvents($events); + return array_map( + function($event) use($calendars) { + $dto = new EventDTO(); + $dto->loadRecord($event, $calendars); + return $dto; + }, + $events + ); + } + return []; } private function _getCalendarsForEvents(array $events) { 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); + } + + + +} + +?> diff --git a/app/php/pages/Profile.page b/app/php/pages/Profile.page index 7bd6718..163d3fa 100644 --- a/app/php/pages/Profile.page +++ b/app/php/pages/Profile.page @@ -1,9 +1,11 @@ <com:TContent ID="Content"> <com:PasswordChange> + <prop:Facade><%= UserFacade::getInstance() %></prop:Facade> <prop:UserToChange><%= $this->User %></prop:UserToChange> </com:PasswordChange> <br /> <com:TimezoneSelect> + <prop:Facade><%= UserFacade::getInstance() %></prop:Facade> <prop:UserToChange><%= $this->User %></prop:UserToChange> </com:TimezoneSelect> <br /> diff --git a/app/php/pages/Signup.page b/app/php/pages/Signup.page index da863d4..834b7cf 100644 --- a/app/php/pages/Signup.page +++ b/app/php/pages/Signup.page @@ -1,3 +1,5 @@ <com:TContent ID="Content"> - <com:RegistrationForm /> + <com:RegistrationForm> + <prop:Facade><%= UserFacade::getInstance() %></prop:Facade> + </com:RegistrationForm> </com:TContent> diff --git a/app/php/user/DbUser.php b/app/php/user/DbUser.php index 7e6096c..ce4da4c 100644 --- a/app/php/user/DbUser.php +++ b/app/php/user/DbUser.php @@ -2,8 +2,7 @@ Prado::using('System.Security.TDbUserManager'); Prado::using('Application.model.User'); -Prado::using('Application.model.Calendar'); -Prado::using('Application.dto.TimezoneDTO'); +Prado::using('Application.facades.UserFacade'); class DbUser extends TDbUser { @@ -38,7 +37,8 @@ class DbUser extends TDbUser { public function validateUser($login, $password) { $user = User::finder()->findByLogin($login); $dbPassword = $user ? $user->Password : ''; - if (self::verifyPassword($password, $dbPassword) && $user) { + if (UserFacade::getInstance()->verifyPassword($password, $dbPassword) + && $user) { $user->LastLogin = date('Y-m-d H:i:s'); $user->save(); return TRUE; @@ -55,64 +55,6 @@ class DbUser extends TDbUser { throw new Exception('Unimplemented CustomDbUser method'); } - public function getCalendarPreference() { - if ($this->IsGuest) { - return Calendar::finder()->findAllByIsVisible(1); - } else { - return $this->DbRecord->Calendars; - } - } - - public function isCalendarPreferred($calendarID) { - return in_array( - $calendarID, - array_map( - function($calendar) { - return $calendar->UID; - }, - $this->getCalendarPreference() - ) - ); - } - - public function setTimezonePreference($timezone) { - if ($this->IsGuest) { - throw new TInvalidDataException('Timezone preference change impossible for guest user'); - } - $this->DbRecord->Timezone = $timezone; - $this->DbRecord->save(); - } - - public function getTimezonePreference() { - if (!$this->IsGuest) { - try { - return new TimezoneDTO($this->DbRecord->Timezone); - } catch(Exception $e) {} - } - return new TimezoneDTO(date_default_timezone_get()); - } - - public function changePassword($pass) { - if (!$this->IsGuest) { - $this->DbRecord->Password = self::generatePassword($pass); - $this->DbRecord->save(); - } - } - - public function getPassword() { - if (!$this->IsGuest) { - return $this->DbRecord->Password; - } - } - - public static function generatePassword($password) { - return password_hash($password, PASSWORD_DEFAULT); - } - - public static function verifyPassword($password, $dbPassword) { - return password_verify($password, $dbPassword); - } - } ?> |