First approach to port user classes to abstract MVC classes as well

This commit is contained in:
2019-04-18 15:59:44 +02:00
parent 47c81d3ce9
commit 17e737b64c
4 changed files with 152 additions and 326 deletions

View File

@@ -11,11 +11,10 @@ class UserInvalidException extends Exception
class PasswordInvalidException extends Exception
{}
class ClubsUser
class ClubsUser extends AbstractClubsModel
{
protected $id;
protected $user;
protected $hash;
protected $password;
protected $name;
protected $address;
protected $city;
@@ -34,19 +33,11 @@ class ClubsUser
/**
* @param string $mail
*/
public function setMail(string $mail)
public function setMail($mail)
{
$this->mail = $mail;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
@@ -60,7 +51,7 @@ class ClubsUser
*/
public function getHash()
{
return $this->hash;
return $this->password;
}
/**
@@ -106,7 +97,7 @@ class ClubsUser
/**
* @param string $user
*/
public function setUser(string $user, bool $force = false)
public function setUser($user, bool $force = false)
{
if($this->id === 'new')
$valid = self::isUserNameFree($user);
@@ -124,19 +115,19 @@ class ClubsUser
*/
public function setPassword(string $password)
{
if(! $this->checkPassword($password))
if(! $this->checkPasswordStrength($password))
throw new PasswordInvalidException();
$this->hash = password_hash($password, PASSWORD_DEFAULT);
$this->password = password_hash($password, PASSWORD_DEFAULT);
}
public function isPasswordValid(string $password)
{
$valid = password_verify($password, $this->hash);
$valid = password_verify($password, $this->password);
if($valid)
{
$this->checkHash($password);
$this->checkForRehashing($password);
}
return $valid;
@@ -145,7 +136,7 @@ class ClubsUser
/**
* @param string $name
*/
public function setName(string $name)
public function setName($name)
{
$this->name = $name;
}
@@ -153,7 +144,7 @@ class ClubsUser
/**
* @param string $address
*/
public function setAddress(string $address)
public function setAddress($address)
{
$this->address = $address;
}
@@ -161,7 +152,7 @@ class ClubsUser
/**
* @param string $city
*/
public function setCity(string $city)
public function setCity($city)
{
$this->city = $city;
}
@@ -169,7 +160,7 @@ class ClubsUser
/**
* @param string $phone
*/
public function setPhone(string $phone)
public function setPhone($phone)
{
$this->phone = $phone;
}
@@ -177,7 +168,7 @@ class ClubsUser
/**
* @param string $mobile
*/
public function setMobile(string $mobile)
public function setMobile($mobile)
{
$this->mobile = $mobile;
}
@@ -185,60 +176,17 @@ class ClubsUser
protected function __construct()
{}
private const tableName = '#__club_users';
private const className = 'ClubsUser';
public static function loadUsers()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')
->from('#__club_users');
$dbo->setQuery($q);
$dbo->execute();
$list = $dbo->loadAssocList('id');
$ret = array();
foreach($list as $u)
{
$uo = new ClubsUser($dbo);
$uo->loadData($u);
$ret[] = $uo;
}
return $ret;
}
protected function loadData(array $data)
{
$this->id = $data['id'];
$this->user = $data['user'];
$this->hash = $data['password'];
$this->name = $data['name'];
$this->address = $data['address'];
$this->city = $data['city'];
$this->mail = $data['mail'];
$this->phone = isset($data['phone']) ? $data['phone'] : null;
$this->mobile = isset($data['mobile']) ? $data['mobile'] : null;
return self::loadElements(self::tableName, self::className);
}
public static function loadUser(int $id)
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->select('*')->from('#__club_users')->where('id=' . (int) $id);
$dbo->setQuery($q);
$dbo->execute();
$row = $dbo->loadAssoc();
if($row == null)
{
throw new Exception("No user found.");
// TODO
}
$user = new ClubsUser();
$user->loadData($row);
return $user;
return self::loadElement($id, self::tableName, self::className);
}
public static function createUser()
@@ -248,54 +196,20 @@ class ClubsUser
return $user;
}
public function save()
{
if($this->id === 'new')
$this->insertUser();
else
$this->updateUser();
}
private function insertUser()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vuser = $q->q($this->user);
$vpassword = $q->q($this->hash);
$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);
$q->insert('#__club_users')
->columns(array('user', 'password', 'name', 'address', 'city', 'mail', 'phone', 'mobile'))
->values("$vuser, $vpassword, $vname, $vaddress, $vcity, $vmail, $vphone, $vmobile")
;
$dbo->transactionStart();
$dbo->setQuery($q);
$dbo->execute();
$this->id = $dbo->insertid();
$dbo->transactionCommit();
}
private function updateUser()
{
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$vuser = $q->q($this->user);
$vpassword = $q->q($this->hash);
$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",
@@ -314,43 +228,29 @@ class ClubsUser
$dbo->execute();
}
private function checkHash(string $password)
private function checkForRehashing(string $password)
{
if($this->id === 'new')
return;
if(password_needs_rehash($this->hash, PASSWORD_DEFAULT))
if(password_needs_rehash($this->password, PASSWORD_DEFAULT))
{
$this->hash = password_hash($password, PASSWORD_DEFAULT);
$this->password = password_hash($password, PASSWORD_DEFAULT);
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->update('#__club_users')->set('password=' . $q->q($this->hash))->where('id=' . (int) $this->id);
$q->update(self::tableName)->set('password=' . $q->q($this->password))->where('id=' . (int) $this->id);
$dbo->setQuery($q);
$dbo->execute();
}
}
public function delete()
{
if($this->id === 'new')
return;
$dbo = Factory::getDbo();
$q = $dbo->getQuery(true);
$q->delete('#__club_users')
->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('#__club_users')
$q->select('COUNT(*)')->from(self::tableName)
->where('id <> ' . (int) $id)
->where('user = ' . $q->q($username));
$db->setQuery($q);
@@ -359,7 +259,7 @@ class ClubsUser
return $row[0] == 0;
}
public function checkPassword($pwd)
public static function checkPasswordStrength($pwd)
{
if(strlen($pwd) < 6)
return false;
@@ -375,4 +275,20 @@ class ClubsUser
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;
}
}