diff --git a/application/Libraries/Aauth.php b/application/Libraries/Aauth.php index ddfb3c3..f8e2aa7 100644 --- a/application/Libraries/Aauth.php +++ b/application/Libraries/Aauth.php @@ -100,11 +100,9 @@ class Aauth $this->session = \Config\Services::session(); } - /* - |-------------------------------------------------------------------------- - | User Functions - |-------------------------------------------------------------------------- - */ + //-------------------------------------------------------------------- + // User Functions + //-------------------------------------------------------------------- /** * Create user @@ -168,6 +166,7 @@ class Aauth if (! $userModel->existsById($userId)) { $this->error(lang('Aauth.notFoundUser')); + return false; } else if (! is_null($email) && ! is_null($password) && ! is_null($username)) @@ -216,6 +215,7 @@ class Aauth if (! $userModel->existsById($userId)) { $this->error(lang('Aauth.notFoundUser')); + return false; } 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 * @@ -232,11 +263,9 @@ class Aauth * @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) + protected function sendVerification(int $userId, string $email) { helper('text'); $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(); - $user = $userModel->limit($limit, $offset); - // eanbool $group_par = null, + $userVariableModel = new UserVariableModel(); - 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)) - { - $user->orderBy($orderBy[0], $orderBy[1]); + return true; + } + else + { + $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 */ - 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; } 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; } $this->error(lang('Aauth.notFoundUser')); + return false; } - /* - |-------------------------------------------------------------------------- - | Login Functions - |-------------------------------------------------------------------------- - */ + /** + * Get user id + * + * 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 @@ -333,9 +401,6 @@ class Aauth * @param boolean $remember Whether to remember login * @param string $totpCode TOTP Code * - * @todo add TOTP - * @todo add reCAPTCHA - * * @return boolean */ public function login(string $identifier, string $password, bool $remember = null, string $totpCode = null) @@ -502,6 +567,7 @@ class Aauth else { $this->error(lang('Aauth.loginFailedAll')); + return false; } } @@ -551,11 +617,9 @@ class Aauth return false; } - /* - |-------------------------------------------------------------------------- - | Access Functions - |-------------------------------------------------------------------------- - */ + //-------------------------------------------------------------------------- + // Access Functions + //-------------------------------------------------------------------------- /** * Check user login @@ -609,11 +673,9 @@ class Aauth return false; } - /* - |-------------------------------------------------------------------------- - | Error Functions - |-------------------------------------------------------------------------- - */ + //-------------------------------------------------------------------------- + // Error Functions + //-------------------------------------------------------------------------- /** * Error @@ -723,11 +785,9 @@ class Aauth $this->session->remove('errors'); } - /* - |-------------------------------------------------------------------------- - | Info Functions - |-------------------------------------------------------------------------- - */ + //-------------------------------------------------------------------------- + // Info Functions + //-------------------------------------------------------------------------- /** * Info