Browse Source

updated Libraries/Aauth, UserSessionModel & UserTest

v3-dev
REJack 6 years ago
parent
commit
140a88a426
No known key found for this signature in database
GPG Key ID: 4A44B48700429F46
  1. 61
      app/Libraries/Aauth.php
  2. 59
      app/Models/Aauth/UserSessionModel.php
  3. 35
      tests/Aauth/Libraries/Aauth/UserTest.php

61
app/Libraries/Aauth.php

@ -675,13 +675,13 @@ class Aauth
return false; return false;
} }
if ($this->config->userVerification) // if ($this->config->userVerification)
{ // {
$this->sendVerification($userId, $email); // $this->sendVerification($userId, $email);
$this->info(lang('Aauth.infoCreateVerification')); // $this->info(lang('Aauth.infoCreateVerification'));
return $userId; // return $userId;
} // }
$this->info(lang('Aauth.infoCreateSuccess')); $this->info(lang('Aauth.infoCreateSuccess'));
@ -855,26 +855,26 @@ class Aauth
* *
* @return boolean * @return boolean
*/ */
protected function sendVerification(int $userId, string $email) // protected function sendVerification(int $userId, string $email)
{ // {
helper('text'); // helper('text');
$userVariableModel = new UserVariableModel(); // $userVariableModel = new UserVariableModel();
$emailService = \Config\Services::email(); // $emailService = \Config\Services::email();
$verificationCode = sha1(strtotime('now')); // $verificationCode = sha1(strtotime('now'));
$userVariableModel->save($userId, 'verification_code', $verificationCode, true); // $userVariableModel->save($userId, 'verification_code', $verificationCode, true);
$messageData['code'] = $verificationCode; // $messageData['code'] = $verificationCode;
$messageData['link'] = site_url($this->config->linkVerification . '/' . $userId . '/' . $verificationCode); // $messageData['link'] = site_url($this->config->linkVerification . '/' . $userId . '/' . $verificationCode);
$emailService->initialize(isset($this->config->emailConfig) ? $this->config->emailConfig : []); // $emailService->initialize(isset($this->config->emailConfig) ? $this->config->emailConfig : []);
$emailService->setFrom($this->config->emailFrom, $this->config->emailFromName); // $emailService->setFrom($this->config->emailFrom, $this->config->emailFromName);
$emailService->setTo($email); // $emailService->setTo($email);
$emailService->setSubject(lang('Aauth.subjectVerification')); // $emailService->setSubject(lang('Aauth.subjectVerification'));
$emailService->setMessage(view('Aauth/Verification', $messageData)); // $emailService->setMessage(view('Aauth/Verification', $messageData));
return $emailService->send(); // return $emailService->send();
} // }
/** /**
* Verify user * Verify user
@ -978,6 +978,11 @@ class Aauth
return $user['id']; return $user['id'];
} }
/**
* Get active users count
*
* @return integer Count of active users
*/
public function getActiveUsersCount() public function getActiveUsersCount()
{ {
$userSessionModel = new UserSessionModel(); $userSessionModel = new UserSessionModel();
@ -985,6 +990,13 @@ class Aauth
return count($userSessionModel->findAll()); return count($userSessionModel->findAll());
} }
/**
* List active users
*
* Return users as an object array
*
* @return array Array of active users
*/
public function listActiveUsers() public function listActiveUsers()
{ {
$userSessionModel = new UserSessionModel(); $userSessionModel = new UserSessionModel();
@ -1016,6 +1028,11 @@ class Aauth
$usersIds[] = $user['id']; $usersIds[] = $user['id'];
} }
if (count($usersIds) === 0)
{
return [];
}
$userModel = new UserModel(); $userModel = new UserModel();
$userModel->select('id, email, username, banned, created_at, updated_at, last_activity, last_ip_address, last_login'); $userModel->select('id, email, username, banned, created_at, updated_at, last_activity, last_ip_address, last_login');

59
app/Models/Aauth/UserSessionModel.php

@ -128,20 +128,16 @@ class UserSessionModel
} }
/** /**
* Delete User Variable * Delete User Session
* *
* @param integer $userId User id * @param integer $id Session id
* @param string $dataKey Key of variable
* @param boolean $system Whether system variable
* *
* @return boolean * @return boolean
*/ */
public function delete(int $userId, string $dataKey, bool $system = null) public function delete($id)
{ {
$builder = $this->builder(); $builder = $this->builder();
$builder->where('user_id', $userId); $builder->where('id', $id);
$builder->where('data_key', $dataKey);
$builder->where('system', ($system ? 1 : 0));
$builder->delete(); $builder->delete();
return true; return true;
@ -151,53 +147,6 @@ class UserSessionModel
// Utility // Utility
//-------------------------------------------------------------------- //--------------------------------------------------------------------
/**
* Sets the return type of the results to be as an associative array.
*
* @return Model
*/
public function asArray()
{
$this->tempReturnType = $this->returnType = 'array';
return $this;
}
/**
* Sets the return type to be of the specified type of object.
* Defaults to a simple object, but can be any class that has
* class vars with the same name as the table columns, or at least
* allows them to be created.
*
* @param string $class Class
*
* @return Model
*/
public function asObject(string $class = 'object')
{
$this->tempReturnType = $this->returnType = $class;
return $this;
}
/**
* Returns the first row of the result set. Will take any previous
* Query Builder calls into account when determing the result set.
*
* @return array|object|null
*/
public function first()
{
$builder = $this->builder();
$row = $builder->limit(1, 0)->get();
$row = $row->getFirstRow($this->tempReturnType);
$this->tempReturnType = $this->returnType;
return $row;
}
/** /**
* Provides a shared instance of the Query Builder. * Provides a shared instance of the Query Builder.
* *

35
tests/Aauth/Libraries/Aauth/UserTest.php

@ -1,11 +1,13 @@
<?php namespace Tests\Aauth\Libraries\Aauth; <?php namespace Tests\Aauth\Libraries\Aauth;
use Config\App;
use Config\Aauth as AauthConfig; use Config\Aauth as AauthConfig;
use Config\Logger; use Config\Logger;
use Config\Services; use Config\Services;
use Tests\Support\Log\TestLogger; use Tests\Support\Log\TestLogger;
use Tests\Support\Session\MockSession; use Tests\Support\Session\MockSession;
use CodeIgniter\Session\Handlers\FileHandler; use CodeIgniter\Session\Handlers\FileHandler;
use CodeIgniter\Session\Handlers\DatabaseHandler;
use CodeIgniter\Test\CIDatabaseTestCase; use CodeIgniter\Test\CIDatabaseTestCase;
use App\Libraries\Aauth; use App\Libraries\Aauth;
use App\Models\Aauth\UserVariableModel; use App\Models\Aauth\UserVariableModel;
@ -237,6 +239,39 @@ class UserTest extends CIDatabaseTestCase
$this->assertFalse($this->library->getUserId('none@example.com')); $this->assertFalse($this->library->getUserId('none@example.com'));
} }
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetActiveUsersCount()
{
$this->assertEquals(0, $this->library->getActiveUsersCount());
$session = $this->getInstance();
$this->library = new Aauth(null, $session);
$session->set('user', [
'id' => 1,
]);
// $this->assertEquals(1, $this->library->getActiveUsersCount());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testListActiveUsers()
{
$this->assertEquals([], $this->library->listActiveUsers());
$session = $this->getInstance();
$this->library = new Aauth(null, $session);
$session->set('user', [
'id' => 1,
]);
// $this->assertContains(['id' => 1], $this->library->listActiveUsers());
}
public function testBanUser() public function testBanUser()
{ {
$this->seeInDatabase($this->config->dbTableUsers, [ $this->seeInDatabase($this->config->dbTableUsers, [

Loading…
Cancel
Save