Add models for every case, so much work..
This commit is contained in:
parent
a1e13a9919
commit
297cebd57b
8 changed files with 537 additions and 44 deletions
6
include/php/models/AbstractMultiRedirect.php
Normal file
6
include/php/models/AbstractMultiRedirect.php
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
abstract class AbstractMultiRedirect extends AbstractRedirect
|
||||
{
|
||||
|
||||
}
|
255
include/php/models/AbstractRedirect.php
Normal file
255
include/php/models/AbstractRedirect.php
Normal file
|
@ -0,0 +1,255 @@
|
|||
<?php
|
||||
|
||||
abstract class AbstractRedirect extends AbstractModel
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static $table = DBT_ALIASES;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static $idAttribute = DBC_ALIASES_ID;
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setupDbMapping($childMapping = array())
|
||||
{
|
||||
$thisMapping = array(
|
||||
'source' => DBC_ALIASES_SOURCE,
|
||||
'destination' => DBC_ALIASES_DESTINATION,
|
||||
);
|
||||
|
||||
if(defined('DBC_ALIASES_MULTI_SOURCE')){
|
||||
$thisMapping['multiHash'] = DBC_ALIASES_MULTI_SOURCE;
|
||||
}
|
||||
|
||||
return array_replace(
|
||||
parent::setupDbMapping($thisMapping),
|
||||
$childMapping
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function preSave($data)
|
||||
{
|
||||
$data = parent::preSave($data);
|
||||
|
||||
$data['source'] = emailsToString($data['source']);
|
||||
$data['destination'] = emailsToString($data['destination']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function __construct($data)
|
||||
{
|
||||
parent::__construct($data);
|
||||
|
||||
$source = stringToEmails($data[DBC_ALIASES_SOURCE]);
|
||||
$destination = stringToEmails($data[DBC_ALIASES_DESTINATION]);
|
||||
|
||||
if(static::class === Alias::class || static::class === Redirect::class){
|
||||
$source = $source[0];
|
||||
}
|
||||
|
||||
if(static::class === Alias::class || static::class === MultiAlias::class){
|
||||
$destination = $destination[0];
|
||||
}
|
||||
|
||||
$this->setSource($source);
|
||||
$this->setDestination($destination);
|
||||
|
||||
if(defined('DBC_ALIASES_MULTI_SOURCE')){
|
||||
$this->setMultiHash($data[DBC_ALIASES_MULTI_SOURCE]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static function create($data)
|
||||
{
|
||||
if(static::class !== AbstractRedirect::class){
|
||||
return parent::create($data);
|
||||
}
|
||||
|
||||
$hasMultipleSources = array_key_exists(DBC_ALIASES_SOURCE, $data)
|
||||
&& strpos($data[DBC_ALIASES_SOURCE], ',') !== false;
|
||||
|
||||
$hasMultipleDestinations = array_key_exists(DBC_ALIASES_DESTINATION, $data)
|
||||
&& strpos($data[DBC_ALIASES_DESTINATION], ',') !== false;
|
||||
|
||||
if(defined('DBC_ALIASES_MULTI_SOURCE') && $hasMultipleSources
|
||||
){
|
||||
if($hasMultipleDestinations){
|
||||
return MultiRedirect::create($data);
|
||||
}
|
||||
else{
|
||||
return MultiAlias::create($data);
|
||||
}
|
||||
}
|
||||
else{
|
||||
if($hasMultipleDestinations){
|
||||
return Redirect::create($data);
|
||||
}
|
||||
else{
|
||||
return Alias::create($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array|string
|
||||
*/
|
||||
public function getSource()
|
||||
{
|
||||
return $this->getAttribute('source');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|array $value
|
||||
*/
|
||||
public function setSource($value)
|
||||
{
|
||||
if(is_array($value)){
|
||||
$this->setAttribute('source', array_map('strtolower', $value));
|
||||
}
|
||||
else{
|
||||
$this->setAttribute('source', strtolower($value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array|string
|
||||
*/
|
||||
public function getDestination()
|
||||
{
|
||||
return $this->getAttribute('destination');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string|array $value
|
||||
*/
|
||||
public function setDestination($value)
|
||||
{
|
||||
if(is_array($value)){
|
||||
$this->setAttribute('destination', array_map('strtolower', $value));
|
||||
}
|
||||
else{
|
||||
$this->setAttribute('destination', strtolower($value));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMultiHash()
|
||||
{
|
||||
return $this->getAttribute('multiHash');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setMultiHash($value)
|
||||
{
|
||||
$this->setAttribute('multiHash', $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static function findAll($orderBy = array(DBC_ALIASES_SOURCE))
|
||||
{
|
||||
return parent::findAll($orderBy);
|
||||
}
|
||||
|
||||
|
||||
private static $baseSqlQueryCache = null;
|
||||
|
||||
private static function generateRedirectBaseQuery()
|
||||
{
|
||||
if(is_null(static::$baseSqlQueryCache)){
|
||||
if(defined('DBC_ALIASES_MULTI_SOURCE')){
|
||||
static::$baseSqlQueryCache = "SELECT r.* FROM (
|
||||
SELECT
|
||||
GROUP_CONCAT(g.`".static::$idAttribute."` ORDER BY g.`".static::$idAttribute."` SEPARATOR ',') AS `".static::$idAttribute."`,
|
||||
GROUP_CONCAT(g.`".DBC_ALIASES_SOURCE."` SEPARATOR ',') AS `".DBC_ALIASES_SOURCE."`,
|
||||
g.`".DBC_ALIASES_DESTINATION."`,
|
||||
g.`".DBC_ALIASES_MULTI_SOURCE."`
|
||||
FROM `".static::$table."` AS g
|
||||
WHERE g.`".DBC_ALIASES_MULTI_SOURCE."` IS NOT NULL
|
||||
GROUP BY g.`".DBC_ALIASES_MULTI_SOURCE."`
|
||||
UNION
|
||||
SELECT
|
||||
s.`".DBC_ALIASES_ID."`,
|
||||
s.`".DBC_ALIASES_SOURCE."`,
|
||||
s.`".DBC_ALIASES_DESTINATION."`,
|
||||
s.`".DBC_ALIASES_MULTI_SOURCE."`
|
||||
FROM `".static::$table."` AS s
|
||||
WHERE s.`".DBC_ALIASES_MULTI_SOURCE."` IS NULL
|
||||
) AS r";
|
||||
}
|
||||
else{
|
||||
static::$baseSqlQueryCache = "SELECT * FROM `".static::$table."`";
|
||||
}
|
||||
}
|
||||
|
||||
return static::$baseSqlQueryCache;
|
||||
}
|
||||
|
||||
|
||||
public static function findMultiAll($orderBy = array(DBC_ALIASES_SOURCE))
|
||||
{
|
||||
$sql = static::generateRedirectBaseQuery()
|
||||
.static::sqlHelperOrderBy($orderBy);
|
||||
|
||||
return static::findAllRaw($sql);
|
||||
}
|
||||
|
||||
|
||||
public static function findMultiWhere($conditions = array(), $conditionConnector = 'AND', $orderBy = null, $limit = 0)
|
||||
{
|
||||
$sql = static::generateRedirectBaseQuery()
|
||||
.static::sqlHelperWhere($conditions, $conditionConnector)
|
||||
.static::sqlHelperOrderBy($orderBy)
|
||||
.static::sqlHelperLimit($limit);
|
||||
|
||||
if($limit === 1){
|
||||
return static::findRaw($sql);
|
||||
}
|
||||
|
||||
return static::findAllRaw($sql);
|
||||
}
|
||||
|
||||
|
||||
public static function findMultiWhereFirst($conditions = array(), $conditionConnector = 'AND', $orderBy = null)
|
||||
{
|
||||
return static::findMultiWhere($conditions, $conditionConnector, $orderBy, 1);
|
||||
}
|
||||
|
||||
|
||||
public static function findMulti($id)
|
||||
{
|
||||
return static::findMultiWhereFirst(array(static::$idAttribute, $id));
|
||||
}
|
||||
|
||||
}
|
10
include/php/models/Alias.php
Normal file
10
include/php/models/Alias.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @method string getSource()
|
||||
* @method string getDestination()
|
||||
*/
|
||||
class Alias extends Redirect
|
||||
{
|
||||
|
||||
}
|
90
include/php/models/Domain.php
Normal file
90
include/php/models/Domain.php
Normal file
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
class Domain extends AbstractModel
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static $table = DBT_DOMAINS;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static $idAttribute = DBC_DOMAINS_ID;
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setupDbMapping($childMapping = array())
|
||||
{
|
||||
return array_replace(
|
||||
parent::setupDbMapping(
|
||||
array(
|
||||
'domain' => DBC_DOMAINS_DOMAIN,
|
||||
)
|
||||
),
|
||||
$childMapping
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function __construct($data)
|
||||
{
|
||||
parent::__construct($data);
|
||||
|
||||
$this->setDomain($data[DBC_DOMAINS_DOMAIN]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDomain()
|
||||
{
|
||||
return $this->getAttribute('domain');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setDomain($value)
|
||||
{
|
||||
$this->setAttribute('domain', strtolower($value));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function countUsers()
|
||||
{
|
||||
global $db;
|
||||
|
||||
if(!$result = $db->query("SELECT COUNT(`".DBC_USERS_ID."`) FROM `".DBT_USERS."` WHERE `".DBC_USERS_DOMAIN."` = '{$this->getDomain()}'")){
|
||||
dbError($db->error);
|
||||
}
|
||||
|
||||
return $result->fetch_array(MYSQLI_NUM)[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function countRedirects()
|
||||
{
|
||||
global $db;
|
||||
|
||||
if(!$result = $db->query("SELECT COUNT(`".DBC_ALIASES_ID."`) FROM `".DBT_ALIASES."` WHERE `".DBC_ALIASES_SOURCE."` LIKE '%@{$this->getDomain()}%'")){
|
||||
dbError($db->error);
|
||||
}
|
||||
|
||||
return $result->fetch_array(MYSQLI_NUM)[0];
|
||||
}
|
||||
}
|
10
include/php/models/MultiAlias.php
Normal file
10
include/php/models/MultiAlias.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @method array getSource()
|
||||
* @method string getDestination()
|
||||
*/
|
||||
class MultiAlias extends AbstractMultiRedirect
|
||||
{
|
||||
|
||||
}
|
10
include/php/models/MultiRedirect.php
Normal file
10
include/php/models/MultiRedirect.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @method array getSource()
|
||||
* @method array getDestination()
|
||||
*/
|
||||
class MultiRedirect extends AbstractMultiRedirect
|
||||
{
|
||||
|
||||
}
|
10
include/php/models/Redirect.php
Normal file
10
include/php/models/Redirect.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @method string getSource()
|
||||
* @method array getDestination()
|
||||
*/
|
||||
class Redirect extends AbstractRedirect
|
||||
{
|
||||
|
||||
}
|
|
@ -1,60 +1,57 @@
|
|||
<?php
|
||||
|
||||
class User
|
||||
class User extends AbstractModel
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static $table = DBT_USERS;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static $idAttribute = DBC_USERS_ID;
|
||||
|
||||
|
||||
const ROLE_USER = 'user';
|
||||
const ROLE_ADMIN = 'admin';
|
||||
|
||||
/**
|
||||
* @var int|string
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @inheritdoc
|
||||
*/
|
||||
private $username;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $domain;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $mailboxLimit = 0;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $role;
|
||||
|
||||
|
||||
/**
|
||||
* User constructor.
|
||||
*
|
||||
* @param array $userData
|
||||
*/
|
||||
function __construct($userData)
|
||||
protected function setupDbMapping($childMapping = array())
|
||||
{
|
||||
$this->id = $userData[DBC_USERS_ID];
|
||||
$this->username = $userData[DBC_USERS_USERNAME];
|
||||
$this->domain = $userData[DBC_USERS_DOMAIN];
|
||||
$this->role = static::getRoleByEmail($this->getEmail());
|
||||
$thisMappings = array(
|
||||
'username' => DBC_USERS_USERNAME,
|
||||
'domain' => DBC_USERS_DOMAIN,
|
||||
);
|
||||
|
||||
if(defined('DBC_USERS_MAILBOXLIMIT')){
|
||||
$this->mailboxLimit = $userData[DBC_USERS_MAILBOXLIMIT];
|
||||
$thisMappings['mailboxLimit'] = DBC_USERS_MAILBOXLIMIT;
|
||||
}
|
||||
|
||||
return array_replace(
|
||||
parent::setupDbMapping($thisMappings),
|
||||
$childMapping
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int|string
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getId()
|
||||
protected function __construct($data)
|
||||
{
|
||||
return $this->id;
|
||||
parent::__construct($data);
|
||||
|
||||
$this->setUsername($data[DBC_USERS_USERNAME]);
|
||||
$this->setDomain($data[DBC_USERS_DOMAIN]);
|
||||
$this->setPasswordHash($data[DBC_USERS_PASSWORD]);
|
||||
$this->setMailboxLimit(defined('DBC_USERS_MAILBOXLIMIT') ? intval($data[DBC_USERS_MAILBOXLIMIT]) : 0);
|
||||
|
||||
$this->setAttribute('role', static::getRoleByEmail($this->getEmail()));
|
||||
}
|
||||
|
||||
|
||||
|
@ -63,7 +60,16 @@ class User
|
|||
*/
|
||||
public function getUsername()
|
||||
{
|
||||
return $this->username;
|
||||
return $this->getAttribute('username');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setUsername($value)
|
||||
{
|
||||
$this->setAttribute('username', strtolower($value));
|
||||
}
|
||||
|
||||
|
||||
|
@ -72,7 +78,16 @@ class User
|
|||
*/
|
||||
public function getDomain()
|
||||
{
|
||||
return $this->domain;
|
||||
return $this->getAttribute('domain');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setDomain($value)
|
||||
{
|
||||
$this->setAttribute('domain', strtolower($value));
|
||||
}
|
||||
|
||||
|
||||
|
@ -81,7 +96,25 @@ class User
|
|||
*/
|
||||
public function getEmail()
|
||||
{
|
||||
return $this->username.'@'.$this->domain;
|
||||
return $this->getUsername().'@'.$this->getDomain();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPasswordHash()
|
||||
{
|
||||
return $this->getAttribute('password_hash');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*/
|
||||
public function setPasswordHash($value)
|
||||
{
|
||||
$this->setAttribute('password_hash', $value);
|
||||
}
|
||||
|
||||
|
||||
|
@ -90,7 +123,43 @@ class User
|
|||
*/
|
||||
public function getMailboxLimit()
|
||||
{
|
||||
return $this->mailboxLimit;
|
||||
return $this->getAttribute('mailboxLimit');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $value
|
||||
*/
|
||||
public function setMailboxLimit($value)
|
||||
{
|
||||
$this->setAttribute('mailboxLimit', $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get mailbox limit default via database default value
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function getMailboxLimitDefault()
|
||||
{
|
||||
global $db;
|
||||
|
||||
if(defined('DBC_USERS_MAILBOXLIMIT')){
|
||||
|
||||
$sql = "SELECT DEFAULT(".DBC_USERS_MAILBOXLIMIT.") FROM `".static::$table."` LIMIT 1";
|
||||
if(!$result = $db->query($sql)){
|
||||
dbError($db->error, $sql);
|
||||
}
|
||||
|
||||
if($result->num_rows === 1){
|
||||
$row = $result->fetch_array();
|
||||
|
||||
return intval($row[0]);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
@ -99,7 +168,7 @@ class User
|
|||
*/
|
||||
public function getRole()
|
||||
{
|
||||
return $this->role;
|
||||
return $this->getAttribute('role');
|
||||
}
|
||||
|
||||
|
||||
|
@ -132,6 +201,39 @@ class User
|
|||
{
|
||||
Auth::validateNewPassword($password, $passwordRepeated);
|
||||
|
||||
Auth::changeUserPassword($this->id, $password);
|
||||
Auth::changeUserPassword($this->getId(), $password);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static function findAll($orderBy = array(DBC_USERS_DOMAIN, DBC_USERS_USERNAME))
|
||||
{
|
||||
return parent::findAll($orderBy);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
*
|
||||
* @return static|null
|
||||
*/
|
||||
public static function findByEmail($email)
|
||||
{
|
||||
$emailInParts = explode("@", $email);
|
||||
if(count($emailInParts) !== 2){
|
||||
return null;
|
||||
}
|
||||
$username = $emailInParts[0];
|
||||
$domain = $emailInParts[1];
|
||||
|
||||
return static::findWhereFirst(
|
||||
array(
|
||||
array('username', $username),
|
||||
array('domain', $domain)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue