222 lines
5.1 KiB
PHP

<?php
// No direct access.
defined('_JEXEC') or die;
class CommonClubsModelUser extends AbstractCommonClubsModel
{
protected function getFactory()
{
return new CommonClubsModelFactoryUser();
}
public function getName()
{
return $this->getValues()['name'];
}
public function setName($name)
{
$this->setValue('name', $name);
}
public function getUsername()
{
return $this->getValues()['user'];
}
public function setUsername($user)
{
// XXX Check for validity
$this->setValue('user', $user);
}
public function getAddress()
{
return $this->getValues()['address'];
}
public function setAddress($address)
{
$this->setValue('address', $address);
}
public function getCity()
{
return $this->getValues()['city'];
}
public function setCity($city)
{
$this->setValue('city', $city);
}
public function getMail()
{
return $this->getValues()['mail'];
}
public function setMail($mail) {
$this->setValue('mail', $mail);
}
public function getPhone()
{
return $this->getValues()['phone'];
}
public function setPhone($phone)
{
$this->setValue('phone', $phone);
}
public function getMobile()
{
return $this->getValues()['mobile'];
}
public function setMobile($mobile)
{
$this->setValue('mobile', $mobile);
}
public function isPasswordValid($password)
{
$hash = $this->getValues()['password'];
if(password_verify($password, $hash))
return true;
else
return false;
}
public function checkRehashNeeded($newPassword, $check = false)
{
$hash = $this->getValues()['password'];
if(password_needs_rehash($hash, PASSWORD_DEFAULT))
{
if($check)
{
if(! $this->isPasswordValid($newPassword))
throw new Exception('Password did not match.');
}
$this->setPassword($newPassword);
return true;
}
else
return false;
}
public function setPassword($password)
{
$hash = password_hash($password, PASSWORD_DEFAULT);
$this->setValue('password', $hash);
}
public function isPasswordSet()
{
$password = $this->getValues()['password'];
return isset($password) && strlen($password) > 0;
}
public function getPositions()
{
return $this->fetchAssociatedElements(new CommonClubsModelFactoryUserassoc(), 'userid');
}
public function getPresidentClubs()
{
return $this->fetchAssociatedElements(new CommonClubsModelFactoryClub(), 'president');
}
public function isPasswordSuitable($password)
{
if(strlen($password) < 8)
return false;
$pwdLower = strtolower($password);
$userLower = strtolower($this->getName());
if(strpos($pwdLower, $userLower) || strpos($userLower, $pwdLower))
return false;
if(
preg_match('/.*[a-z].*/', $password) === false ||
preg_match('/.*[A-Z].*/', $password) === false
)
return false;
return true;
}
public function isUsernameFree($user)
{
$factory = new CommonClubsModelFactoryUser();
$users = $factory->loadElements(null, null, function($q) use ($user){
$q->where('main.user = ' . $q->q($user));
});
if(sizeof($users) == 0)
return true;
elseif(sizeof($users) == 1)
{
if($this->getId() == $users[0]->getId())
return true;
else
return false;
}
else
throw new Exception('The database is inconsistent!');
}
protected function prepareDelete($db)
{
$q = $db->getQuery(true);
$q->delete('#__club_user_assocs')->where("userid = {$this->getId()}");
$db->setQuery($q);
$db->execute();
}
public function dataIsValid()
{
if(! parent::dataIsValid())
return false;
if(! $this->usernameIsValid())
{
return false;
}
return true;
}
private function usernameIsValid()
{
$factory = $this->getFactory();
$medb = $factory->loadById($this->getId(), false);
if($medb !== null && $medb->getUserName() === $this->getUsername())
// No change was made
return true;
if(! $this->isUsernameFree($this->getUsername()) )
return false;
return true;
}
protected function filterDatabaseRawData($values)
{
if(strlen($values['phone']) == 0)
$values['phone'] = null;
if(strlen($values['mobile']) == 0)
$values['mobile'] = null;
return $values;
}
}