_record = $record; } public function getDbRecord() { if (!$this->_record) { $this->_record = User::finder()->findByLogin($this->Name); } return $this->_record; } public function createUser($username) { $dbUser = User::finder()->findByLogin($username); if (!$dbUser) { return NULL; } $user = new DbUser($this->Manager); $user->setDbRecord($dbUser); $user->Name = $dbUser->Login; if ($dbUser->IsAdmin) { $user->Roles = 'Admin'; } $user->IsGuest = FALSE; return $user; } public function validateUser($login, $password) { $user = User::finder()->findByLogin($login); $dbPassword = $user ? $user->Password : ''; if (self::verifyPassword($password, $dbPassword) && $user) { $user->LastLogin = date('Y-m-d H:i:s'); $user->save(); return TRUE; } else { return FALSE; } } public function __call($name, $args) { $match = []; if (preg_match('/^getIs(.+)$/', $name, $match)) { return $this->isInRole($match[1]); } throw new Exception('Unimplemented CustomDbUser method'); } public function getCalendarPreference() { if ($this->IsGuest) { return Calendar::finder()->findAllByIsVisible(1); } else { return $this->DbRecord->Calendars; } } 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); } } ?>