mail; } /** * @param string $mail */ public function setMail($mail) { $this->mail = $mail; } /** * @return string */ public function getUser() { return $this->user; } /** * @return string */ public function getHash() { return $this->password; } /** * @return string */ public function getName() { return $this->name; } /** * @return string */ public function getAddress() { return $this->address; } /** * @return string */ public function getCity() { return $this->city; } /** * @return string */ public function getPhone() { return $this->phone; } /** * @return string */ public function getMobile() { return $this->mobile; } /** * @param string $user */ public function setUser($user, bool $force = false) { if($this->id === 'new') $valid = self::isUserNameFree($user); else $valid = self::isUserNameFree($user, $this->id); if(!$force && ! $valid) throw new UserInvalidException(); $this->user = $user; } /** * @param string $hash */ public function setPassword(string $password) { if(! $this->checkPasswordStrength($password)) throw new PasswordInvalidException(); $this->password = password_hash($password, PASSWORD_DEFAULT); } public function isPasswordValid(string $password) { $valid = password_verify($password, $this->password); if($valid) { $this->checkForRehashing($password); } return $valid; } /** * @param string $name */ public function setName($name) { $this->name = $name; } /** * @param string $address */ public function setAddress($address) { $this->address = $address; } /** * @param string $city */ public function setCity($city) { $this->city = $city; } /** * @param string $phone */ public function setPhone($phone) { $this->phone = $phone; } /** * @param string $mobile */ public function setMobile($mobile) { $this->mobile = $mobile; } protected function __construct() {} private const tableName = '#__club_users'; private const className = 'ClubsUser'; public static function loadUsers() { return self::loadElements(self::tableName, self::className); } public static function loadUser(int $id) { return self::loadElement($id, self::tableName, self::className); } public static function createUser() { $user = new ClubsUser(); $user->id = 'new'; return $user; } private function updateUser() { $dbo = Factory::getDbo(); $q = $dbo->getQuery(true); $vuser = $q->q($this->user); $vpassword = $q->q($this->password); $vname = $q->q($this->name); $vaddress = $q->q($this->address); $vcity = $q->q($this->city); $vmail = $q->q($this->mail); $vphone = empty($this->phone) ? 'NULL' : $q->q($this->phone); $vmobile = empty($this->mobile) ? 'NULL' : $q->q($this->mobile); // FIXME Check null vlaues $q->update('#__club_users') ->set(array( "user=$vuser", "password=$vpassword", "name=$vname", "address = $vaddress", "city=$vcity", "mail=$vmail", "phone=$vphone", "mobile=$vmobile" )) ->where("id=". (int) $this->id) ; $dbo->setQuery($q); $dbo->execute(); } private function checkForRehashing(string $password) { if($this->id === 'new') return; if(password_needs_rehash($this->password, PASSWORD_DEFAULT) || true) { $copy = ClubsUser::loadUser($this->id); $copy->password = password_hash($password, PASSWORD_DEFAULT); $copy->save(); // $this->password = password_hash($password, PASSWORD_DEFAULT); // $dbo = Factory::getDbo(); // $q = $dbo->getQuery(true); // $q->update(self::tableName)->set('password=' . $q->q($this->password))->where('id=' . (int) $this->id); // $dbo->setQuery($q); // $dbo->execute(); } } public static function isUserNameFree($username, int $id = -1) { $db = Factory::getDbo(); $q = $db->getQuery(true); $q->select('COUNT(*)')->from(self::tableName) ->where('id <> ' . (int) $id) ->where('user = ' . $q->q($username)); $db->setQuery($q); $db->execute(); $row = $db->loadRow(); return $row[0] == 0; } public static function checkPasswordStrength($pwd) { if(strlen($pwd) < 6) return false; if(preg_match_all('/[A-Z]/', $pwd) === false) return false; if(preg_match_all('/[a-z]/', $pwd) === false) return false; if(preg_match_all('/[0-9]/', $pwd) === false) return false; return true; } protected function getDataMappings() { return array('user', 'password', 'name', 'address', 'city', 'mail', 'phone', 'mobile'); } protected function getRequiredDataMappings() { return array('user', 'password', 'name', 'address', 'city', 'mail'); } protected function getTableName() { return self::tableName; } }