Started work on backend with self-written models
This commit is contained in:
329
src/admin/mymodels/user.php
Normal file
329
src/admin/mymodels/user.php
Normal file
@@ -0,0 +1,329 @@
|
||||
<?php
|
||||
|
||||
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
|
||||
use Joomla\CMS\Factory;
|
||||
|
||||
// No direct access.
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class ClubsUser
|
||||
{
|
||||
protected $dbo;
|
||||
protected $id;
|
||||
protected $user;
|
||||
protected $hash;
|
||||
protected $name;
|
||||
protected $address;
|
||||
protected $city;
|
||||
protected $mail;
|
||||
protected $phone;
|
||||
protected $mobile;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMail()
|
||||
{
|
||||
return $this->mail;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mail
|
||||
*/
|
||||
public function setMail(string $mail)
|
||||
{
|
||||
$this->mail = $mail;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUser()
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHash()
|
||||
{
|
||||
return $this->hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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(string $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $hash
|
||||
*/
|
||||
public function setPassword(string $password)
|
||||
{
|
||||
$this->hash = password_hash($password, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
public function isPasswordValid(string $password)
|
||||
{
|
||||
$valid = password_verify($password, $this->hash);
|
||||
|
||||
if($valid)
|
||||
{
|
||||
$this->checkHash($password);
|
||||
}
|
||||
|
||||
return $valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $address
|
||||
*/
|
||||
public function setAddress(string $address)
|
||||
{
|
||||
$this->address = $address;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $city
|
||||
*/
|
||||
public function setCity(string $city)
|
||||
{
|
||||
$this->city = $city;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $phone
|
||||
*/
|
||||
public function setPhone(string $phone)
|
||||
{
|
||||
$this->phone = $phone;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mobile
|
||||
*/
|
||||
public function setMobile(string $mobile)
|
||||
{
|
||||
$this->mobile = $mobile;
|
||||
}
|
||||
|
||||
protected function __construct()
|
||||
{
|
||||
$this->dbo = Factory::getDbo();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public static function createUser()
|
||||
{
|
||||
$user = new ClubsUser();
|
||||
$user->id = 'new';
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
if($this->id === 'new')
|
||||
$this->insertUser();
|
||||
else
|
||||
$this->updateUser();
|
||||
}
|
||||
|
||||
private function insertUser()
|
||||
{
|
||||
$q = $this->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")
|
||||
;
|
||||
|
||||
$this->dbo->transactionStart();
|
||||
$this->dbo->setQuery($q);
|
||||
$this->dbo->execute();
|
||||
$this->id = $this->dbo->insertid();
|
||||
$this->dbo->transactionCommit();
|
||||
}
|
||||
|
||||
private function updateUser()
|
||||
{
|
||||
$q = $this->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->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)
|
||||
;
|
||||
|
||||
$this->dbo->setQuery($q);
|
||||
$this->dbo->execute();
|
||||
}
|
||||
|
||||
private function checkHash(string $password)
|
||||
{
|
||||
if($this->id === 'new')
|
||||
return;
|
||||
|
||||
if(password_needs_rehash($this->hash, PASSWORD_DEFAULT))
|
||||
{
|
||||
$this->hash = password_hash($password, PASSWORD_DEFAULT);
|
||||
$q = $this->dbo->getQuery(true);
|
||||
$q->update('#__club_users')->set('password=' . $q->q($this->hash))->where('id=' . (int) $this->id);
|
||||
$this->dbo->setQuery($q);
|
||||
$this->dbo->execute();
|
||||
}
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
if($this->id === 'new')
|
||||
return;
|
||||
|
||||
$q = $this->dbo->getQuery(true);
|
||||
$q->delete('#__club_users')
|
||||
->where('id=' . (int) $this->id);
|
||||
|
||||
$this->dbo->setQuery($q);
|
||||
$this->dbo->execute();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user