Browse Source

updated Libraries/Aauth

- reorderd methods
- added method seperators
- added getUser & logout
- updated login
v3-dev
REJack 7 years ago
parent
commit
361bedf421
  1. 167
      application/Libraries/Aauth.php

167
application/Libraries/Aauth.php

@ -37,21 +37,21 @@ class Aauth
* *
* @var \Config\Aauth * @var \Config\Aauth
*/ */
private $config; protected $config;
/** /**
* Variable for loading the session service into * Variable for loading the session service into
* *
* @var \CodeIgniter\Session\Session * @var \CodeIgniter\Session\Session
*/ */
private $session; protected $session;
/** /**
* Array to store error messages * Array to store error messages
* *
* @var array * @var array
*/ */
private $errors = []; protected $errors = [];
/** /**
* Local temporary storage for current flash errors * Local temporary storage for current flash errors
@ -60,14 +60,14 @@ class Aauth
* *
* @var array * @var array
*/ */
private $flashErrors = []; protected $flashErrors = [];
/** /**
* Array to store info messages * Array to store info messages
* *
* @var array * @var array
*/ */
private $infos = []; protected $infos = [];
/** /**
* Local temporary storage for current flash infos * Local temporary storage for current flash infos
@ -76,21 +76,21 @@ class Aauth
* *
* @var array * @var array
*/ */
private $flashInfos = []; protected $flashInfos = [];
/** /**
* Array to cache permission-ids. * Array to cache permission-ids.
* *
* @var array * @var array
*/ */
private $cachePermId = []; protected $cachePermIds = [];
/** /**
* Array to cache group-ids. * Array to cache group-ids.
* *
* @var array * @var array
*/ */
private $cacheGroupId = []; protected $cacheGroupIds = [];
/** /**
* Constructor * Constructor
@ -103,6 +103,12 @@ class Aauth
$this->session = \Config\Services::session(); $this->session = \Config\Services::session();
} }
/*
|--------------------------------------------------------------------------
| User Functions
|--------------------------------------------------------------------------
*/
/** /**
* Create user * Create user
* *
@ -146,43 +152,6 @@ class Aauth
return $userId; return $userId;
} }
/**
* Send verification email
*
* Sends a verification email based on user id
*
* @param integer $userId User id to send verification email to
* @param string $email Email to send verification email to
*
* @todo return boolean success indicator
*
* @return boolean
*/
public function sendVerification(int $userId, string $email)
{
helper('text');
$userModel = new UserModel();
$userVariableModel = new UserVariableModel();
$emailService = \Config\Services::email();
$verificationCode = random_string('alnum', 16);
$userModel->skipValidation()->protect(false)->update($userId, ['banned' => 1]);
$userVariableModel->save($userId, 'verification_code', $verificationCode, true);
$messageData['code'] = $verificationCode;
$messageData['link'] = site_url($this->config->linkVerification . '/' . $userId . '/' . $verificationCode);
$message = view('Aauth/Verification', $messageData);
$emailService->initialize(isset($this->config->emailConfig) ? $this->config->emailConfig : []);
$emailService->setFrom($this->config->emailFrom, $this->config->emailFromName);
$emailService->setTo($email);
$emailService->setSubject(lang('Aauth.subjectVerification'));
$emailService->setMessage($message);
return $emailService->send();
}
/** /**
* Update user * Update user
* *
@ -258,6 +227,39 @@ class Aauth
} }
} }
/**
* Send verification email
*
* Sends a verification email based on user id
*
* @param integer $userId User id to send verification email to
* @param string $email Email to send verification email to
*
* @todo return boolean success indicator
*
* @return boolean
*/
public function sendVerification(int $userId, string $email)
{
helper('text');
$userVariableModel = new UserVariableModel();
$emailService = \Config\Services::email();
$verificationCode = random_string('alnum', 16);
$userVariableModel->save($userId, 'verification_code', $verificationCode, true);
$messageData['code'] = $verificationCode;
$messageData['link'] = site_url($this->config->linkVerification . '/' . $userId . '/' . $verificationCode);
$emailService->initialize(isset($this->config->emailConfig) ? $this->config->emailConfig : []);
$emailService->setFrom($this->config->emailFrom, $this->config->emailFromName);
$emailService->setTo($email);
$emailService->setSubject(lang('Aauth.subjectVerification'));
$emailService->setMessage(view('Aauth/Verification', $messageData));
return $emailService->send();
}
/** /**
* List users * List users
* *
@ -291,6 +293,39 @@ class Aauth
return $user->findAll(); return $user->findAll();
} }
/**
* Get user
*
* Get user information
*
* @param integer|boolean $userId User id to get or FALSE for current user
*
* @return object|boolean User information or false if user not found
*/
public function getUser($userId = null)
{
$userModel = new UserModel();
if ($userId)
{
$userId = $this->session->id;
}
if ($user = $userModel->find($userId))
{
return $user;
}
$this->error(lang('Aauth.notFoundUser'));
return false;
}
/*
|--------------------------------------------------------------------------
| Login Functions
|--------------------------------------------------------------------------
*/
/** /**
* Login user * Login user
* *
@ -309,13 +344,13 @@ class Aauth
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)
{ {
helper('cookie'); helper('cookie');
delete_cookie('user'); delete_cookie('remember');
$userModel = new UserModel(); $userModel = new UserModel();
$loginAttemptModel = new LoginAttemptModel(); $loginAttemptModel = new LoginAttemptModel();
$userVariableModel = new UserVariableModel(); $userVariableModel = new UserVariableModel();
if ($this->config->loginProtection && ! $loginAttemptModel->update()) if ($this->config->loginProtection && ! $loginAttemptModel->save())
{ {
$this->error(lang('Aauth.loginAttemptsExceeded')); $this->error(lang('Aauth.loginAttemptsExceeded'));
return false; return false;
@ -362,7 +397,7 @@ class Aauth
} }
} }
if ($user['banned'] && ! empty($userVariableModel->find($user['id'], 'verification_code', true))) if (! empty($userVariableModel->find($user['id'], 'verification_code', true)))
{ {
$this->error(lang('Aauth.notVerified')); $this->error(lang('Aauth.notVerified'));
return false; return false;
@ -474,6 +509,20 @@ class Aauth
} }
} }
/**
* Logout
*
* Deletes session and cookie
*
* @return void
*/
public function logout()
{
helper('cookie');
delete_cookie('remember');
$this->session->stop();
}
/** /**
* Fast login * Fast login
* *
@ -483,7 +532,7 @@ class Aauth
* *
* @return boolean * @return boolean
*/ */
private function loginFast(int $userId) protected function loginFast(int $userId)
{ {
$userModel = new UserModel(); $userModel = new UserModel();
$userModel->select('id, email, username'); $userModel->select('id, email, username');
@ -505,6 +554,12 @@ class Aauth
return false; return false;
} }
/*
|--------------------------------------------------------------------------
| Access Functions
|--------------------------------------------------------------------------
*/
/** /**
* Check user login * Check user login
* *
@ -532,7 +587,7 @@ class Aauth
else else
{ {
$loginTokenModel = new LoginTokenModel(); $loginTokenModel = new LoginTokenModel();
$loginTokens = $loginTokenModel->getAllByUserId($cookie[0]); $loginTokens = $loginTokenModel->findAllByUserId($cookie[0]);
foreach ($loginTokens as $loginToken) foreach ($loginTokens as $loginToken)
{ {
@ -557,6 +612,12 @@ class Aauth
return false; return false;
} }
/*
|--------------------------------------------------------------------------
| Error Functions
|--------------------------------------------------------------------------
*/
/** /**
* Error * Error
* *
@ -665,6 +726,12 @@ class Aauth
$this->session->remove('errors'); $this->session->remove('errors');
} }
/*
|--------------------------------------------------------------------------
| Info Functions
|--------------------------------------------------------------------------
*/
/** /**
* Info * Info
* *

Loading…
Cancel
Save