Finished up security class testing.
This commit is contained in:
parent
30b9616b12
commit
72be314073
3 changed files with 12 additions and 69 deletions
|
@ -225,73 +225,11 @@ class Security
|
|||
{
|
||||
return $_SESSION['__pickles']['security']['level'];
|
||||
}
|
||||
// Hits the database to determine the user's level
|
||||
// Used to hit the database to determine the user's level, found it
|
||||
// to be overkill and just opted for a simple logout.
|
||||
else
|
||||
{
|
||||
// Checks the session cache instead of hitting the database
|
||||
if (isset($_SESSION['__pickles']['security']['user_id'], self::$cache[(int)$_SESSION['__pickles']['security']['user_id']]))
|
||||
{
|
||||
return self::$cache[(int)$_SESSION['__pickles']['security']['user_id']];
|
||||
}
|
||||
else
|
||||
{
|
||||
// Pulls the config and defaults where necessary
|
||||
$config = Config::getInstance();
|
||||
|
||||
if ($config->security === false)
|
||||
{
|
||||
$config = [];
|
||||
}
|
||||
else
|
||||
{
|
||||
$config = $config->security;
|
||||
}
|
||||
|
||||
$defaults = [
|
||||
'login' => 'login',
|
||||
'model' => 'User',
|
||||
'column' => 'level',
|
||||
];
|
||||
|
||||
foreach ($defaults as $variable => $value)
|
||||
{
|
||||
if (!isset($config[$variable]))
|
||||
{
|
||||
$config[$variable] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Uses the model to pull the user's access level
|
||||
$class = $config['model'];
|
||||
$model = new $class([
|
||||
'fields' => $config['column'],
|
||||
'conditions' => [
|
||||
'id' => (int)$_SESSION['__pickles']['security']['user_id'],
|
||||
],
|
||||
]);
|
||||
|
||||
if ($model->count() == 0)
|
||||
{
|
||||
Security::logout();
|
||||
}
|
||||
else
|
||||
{
|
||||
$constant = 'SECURITY_LEVEL_' . $model->record[$config['column']];
|
||||
|
||||
if (defined($constant))
|
||||
{
|
||||
$constant = constant($constant);
|
||||
|
||||
self::$cache[(int)$_SESSION['__pickles']['security']['user_id']] = $constant;
|
||||
|
||||
return $constant;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('Security level constant is not defined');
|
||||
}
|
||||
}
|
||||
}
|
||||
Security::logout();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -100,7 +100,6 @@ class SecurityTest extends PHPUnit_Framework_TestCase
|
|||
$this->assertFalse(Security::isLevel(SECURITY_LEVEL_USER));
|
||||
}
|
||||
|
||||
/*
|
||||
public function testIsLevelDB()
|
||||
{
|
||||
$config = Config::getInstance();
|
||||
|
@ -127,6 +126,7 @@ class SecurityTest extends PHPUnit_Framework_TestCase
|
|||
'namespace' => '',
|
||||
],
|
||||
],
|
||||
'security' => ['model' => 'MockUserModel'],
|
||||
];
|
||||
|
||||
$model = new MockUserModel();
|
||||
|
@ -141,9 +141,14 @@ class SecurityTest extends PHPUnit_Framework_TestCase
|
|||
|
||||
Security::login(1, 10, 'USER');
|
||||
|
||||
//$this->assertTrue(Security::isLevel([SECURITY_LEVEL_USER, SECURITY_LEVEL_ADMIN]));
|
||||
unset(
|
||||
$_SESSION['__pickles']['security']['token'],
|
||||
$_COOKIE['pickles_security_token'],
|
||||
$_SESSION['__pickles']['security']['level']
|
||||
);
|
||||
|
||||
$this->assertFalse(Security::isLevel([SECURITY_LEVEL_USER, SECURITY_LEVEL_ADMIN]));
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -23,7 +23,7 @@ DROP TABLE IF EXISTS users;
|
|||
CREATE TABLE `users` (
|
||||
`id` int(1) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`username` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
|
||||
`role` varchar(10) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'USER',
|
||||
`level` varchar(10) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'USER',
|
||||
`created_id` int(1) unsigned DEFAULT NULL,
|
||||
`created_at` datetime NOT NULL,
|
||||
`updated_id` int(1) unsigned DEFAULT NULL,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue