Browse Source

updated Libraries/Aauth

v3-dev
REJack 7 years ago
parent
commit
93c51e66ab
  1. 162
      application/Libraries/Aauth.php

162
application/Libraries/Aauth.php

@ -100,11 +100,9 @@ class Aauth
$this->session = \Config\Services::session(); $this->session = \Config\Services::session();
} }
/* //--------------------------------------------------------------------
|-------------------------------------------------------------------------- // User Functions
| User Functions //--------------------------------------------------------------------
|--------------------------------------------------------------------------
*/
/** /**
* Create user * Create user
@ -168,6 +166,7 @@ class Aauth
if (! $userModel->existsById($userId)) if (! $userModel->existsById($userId))
{ {
$this->error(lang('Aauth.notFoundUser')); $this->error(lang('Aauth.notFoundUser'));
return false; return false;
} }
else if (! is_null($email) && ! is_null($password) && ! is_null($username)) else if (! is_null($email) && ! is_null($password) && ! is_null($username))
@ -216,6 +215,7 @@ class Aauth
if (! $userModel->existsById($userId)) if (! $userModel->existsById($userId))
{ {
$this->error(lang('Aauth.notFoundUser')); $this->error(lang('Aauth.notFoundUser'));
return false; return false;
} }
else if ($userModel->delete($userId)) else if ($userModel->delete($userId))
@ -224,6 +224,37 @@ class Aauth
} }
} }
/**
* List users
*
* Return users as an object array
*
* @param integer $limit Limit of users to be returned
* @param integer $offset Offset for limited number of users
* @param boolean $includeBanneds Include banned users
* @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC')
*
* @return array Array of users
*/
public function listUsers(int $limit = 0, int $offset = 0, bool $includeBanneds = null, string $orderBy = null)
{
$userModel = new UserModel();
$user = $userModel->limit($limit, $offset);
// eanbool $group_par = null,
if (is_null($includeBanneds))
{
$user->where('banned', 0);
}
if (! is_null($orderBy))
{
$user->orderBy($orderBy[0], $orderBy[1]);
}
return $user->findAll();
}
/** /**
* Send verification email * Send verification email
* *
@ -232,11 +263,9 @@ class Aauth
* @param integer $userId User id to send verification email to * @param integer $userId User id to send verification email to
* @param string $email Email to send verification email to * @param string $email Email to send verification email to
* *
* @todo return boolean success indicator
*
* @return boolean * @return boolean
*/ */
public function sendVerification(int $userId, string $email) protected function sendVerification(int $userId, string $email)
{ {
helper('text'); helper('text');
$userVariableModel = new UserVariableModel(); $userVariableModel = new UserVariableModel();
@ -258,36 +287,36 @@ class Aauth
} }
/** /**
* List users * Verify user
* *
* Return users as an object array * Activates user account based on verification code
*
* @param integer $limit Limit of users to be returned
* @param integer $offset Offset for limited number of users
* @param boolean $includeBanneds Include banned users
* @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC')
* *
* @todo bool|integer $group_par Specify group id to list group or FALSE for all users * @param integer $userId User id to activate
* @param string $verificationCode Code to validate against
* *
* @return array Array of users * @return boolean Activation fails/succeeds
*/ */
public function listUsers(int $limit = 0, int $offset = 0, bool $includeBanneds = null, string $orderBy = null) public function verifyUser(int $userId, string $verificationCode)
{ {
$userModel = new UserModel(); $userVariableModel = new UserVariableModel();
$user = $userModel->limit($limit, $offset);
// eanbool $group_par = null,
if (is_null($includeBanneds)) if ($verificationCodeStored = $userVariableModel->find($userId, 'verification_code', true))
{ {
$user->where('banned', 0); if ($verificationCode === $verificationCodeStored)
} {
$userVariableModel->delete($userId, 'verification_code', true);
if (! is_null($orderBy)) return true;
}
else
{ {
$user->orderBy($orderBy[0], $orderBy[1]); $this->error(lang('Aauth.invalidVercode'));
return false;
}
} }
return $user->findAll(); return false;
} }
/** /**
@ -299,29 +328,68 @@ class Aauth
* *
* @return object|boolean User information or false if user not found * @return object|boolean User information or false if user not found
*/ */
public function getUser($userId = null) public function getUser($userId = null, bool $withVariables = false, bool $inclSystem = false)
{ {
$userModel = new UserModel(); $userModel = new UserModel();
$userVariableModel = new UserVariableModel();
if ($userId) if (! $userId)
{ {
$userId = $this->session->id; $userId = $this->session->id;
} }
if ($user = $userModel->find($userId)) if ($user = $userModel->find($userId))
{ {
if ($withVariables)
{
$variables = $userVariableModel->select('data_key, data_value' . ($inclSystem ? ', system' : ''));
$variables = $variables->findAll($userId, $inclSystem);
$user['variables'] = $variables;
}
return $user; return $user;
} }
$this->error(lang('Aauth.notFoundUser')); $this->error(lang('Aauth.notFoundUser'));
return false; return false;
} }
/* /**
|-------------------------------------------------------------------------- * Get user id
| Login Functions *
|-------------------------------------------------------------------------- * Get user id from email address, if par. not given, return current user's id
*
* @param string|boolean $email Email address for user
*
* @return object|boolean User information or false if user not found
*/ */
public function getUserId($email = null)
{
$userModel = new UserModel();
if (! $email)
{
$where = ['id' => $this->session->id];
}
else
{
$where = ['email' => $email];
}
if ($user = $userModel->where($where)->first())
{
return $user->id;
}
$this->error(lang('Aauth.notFoundUser'));
return false;
}
//--------------------------------------------------------------------------
// Login Functions
//--------------------------------------------------------------------------
/** /**
* Login user * Login user
@ -333,9 +401,6 @@ class Aauth
* @param boolean $remember Whether to remember login * @param boolean $remember Whether to remember login
* @param string $totpCode TOTP Code * @param string $totpCode TOTP Code
* *
* @todo add TOTP
* @todo add reCAPTCHA
*
* @return boolean * @return boolean
*/ */
public function login(string $identifier, string $password, bool $remember = null, string $totpCode = null) public function login(string $identifier, string $password, bool $remember = null, string $totpCode = null)
@ -502,6 +567,7 @@ class Aauth
else else
{ {
$this->error(lang('Aauth.loginFailedAll')); $this->error(lang('Aauth.loginFailedAll'));
return false; return false;
} }
} }
@ -551,11 +617,9 @@ class Aauth
return false; return false;
} }
/* //--------------------------------------------------------------------------
|-------------------------------------------------------------------------- // Access Functions
| Access Functions //--------------------------------------------------------------------------
|--------------------------------------------------------------------------
*/
/** /**
* Check user login * Check user login
@ -609,11 +673,9 @@ class Aauth
return false; return false;
} }
/* //--------------------------------------------------------------------------
|-------------------------------------------------------------------------- // Error Functions
| Error Functions //--------------------------------------------------------------------------
|--------------------------------------------------------------------------
*/
/** /**
* Error * Error
@ -723,11 +785,9 @@ class Aauth
$this->session->remove('errors'); $this->session->remove('errors');
} }
/* //--------------------------------------------------------------------------
|-------------------------------------------------------------------------- // Info Functions
| Info Functions //--------------------------------------------------------------------------
|--------------------------------------------------------------------------
*/
/** /**
* Info * Info

Loading…
Cancel
Save