Added None and Config auth plugins
This commit is contained in:
79
source/lib/SihnonFramework/Auth/Plugin/Config.class.php
Normal file
79
source/lib/SihnonFramework/Auth/Plugin/Config.class.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
class SihnonFramework_Auth_Plugin_Config
|
||||
extends Sihnon_PluginBase
|
||||
implements Sihnon_Auth_IPlugin,
|
||||
Sihnon_Auth_IUpdateable,
|
||||
Sihnon_Auth_IPermissionable {
|
||||
|
||||
protected $config;
|
||||
|
||||
protected function __construct($config) {
|
||||
$this->config = $config;
|
||||
|
||||
Sihnon_Auth_Plugin_Config_User::init($config);
|
||||
}
|
||||
|
||||
/*
|
||||
* IPlugin methods
|
||||
*/
|
||||
|
||||
public static function create(SihnonFramework_Config $config) {
|
||||
return new self($config);
|
||||
}
|
||||
|
||||
public function userExists($username) {
|
||||
return Sihnon_Auth_Plugin_Config_User::exists($username);
|
||||
}
|
||||
|
||||
public function listUsers() {
|
||||
return array(
|
||||
Sihnon_Auth_Plugin_Config_User::loadAdmin(),
|
||||
);
|
||||
}
|
||||
|
||||
public function authenticate($username, $password) {
|
||||
$user = Sihnon_Auth_Plugin_Config_User::load($username);
|
||||
|
||||
if ( ! $user->checkPassword($password)) {
|
||||
throw new Sihnon_Exception_IncorrectPassword();
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function authenticateSession($username) {
|
||||
return Sihnon_Auth_Plugin_Config_User::load($username);
|
||||
}
|
||||
|
||||
/*
|
||||
* IUpdateable methods
|
||||
*/
|
||||
|
||||
public function addUser($username, $password) {
|
||||
throw new Sihnon_Exception_NotImplemented();
|
||||
}
|
||||
|
||||
public function removeUser(Sihnon_Auth_IUser $user) {
|
||||
throw new Sihnon_Exception_NotImplemented();
|
||||
}
|
||||
|
||||
public function changePassword(Sihnon_Auth_IUser $user, $new_password) {
|
||||
$user->changePassword($new_password);
|
||||
}
|
||||
|
||||
/*
|
||||
* IPermissionable methods
|
||||
*/
|
||||
|
||||
public function isAdministrator(Sihnon_Auth_IUser $user) {
|
||||
return $user->isAdministrator();
|
||||
}
|
||||
|
||||
public function hasPermission(Sihnon_Auth_IUser $user, $permission) {
|
||||
return $user->isAdministrator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
77
source/lib/SihnonFramework/Auth/Plugin/Config/User.class.php
Normal file
77
source/lib/SihnonFramework/Auth/Plugin/Config/User.class.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
class SihnonFramework_Auth_Plugin_Config_User implements Sihnon_Auth_IUser {
|
||||
|
||||
protected static $config;
|
||||
|
||||
protected $username;
|
||||
protected $password;
|
||||
|
||||
public static function init($config) {
|
||||
static::$config = $config;
|
||||
}
|
||||
|
||||
public static function exists($username) {
|
||||
return static::administratorUsername() == $username;
|
||||
}
|
||||
|
||||
public static function load($username) {
|
||||
if ( ! static::exists($username)) {
|
||||
throw new Sihnon_Exception_UnknownUser($username);
|
||||
}
|
||||
|
||||
$user = new self();
|
||||
$user->username = static::$config->get('auth.Config.admin-username');
|
||||
$user->password = static::$config->get('auth.Config.admin-password');
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public static function loadAdmin() {
|
||||
return static::load(static::administratorUsername());
|
||||
}
|
||||
|
||||
public function username() {
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function checkPassword($password) {
|
||||
var_dump($password, sha1($password), $this->password);
|
||||
return ($this->password == sha1($password));
|
||||
}
|
||||
|
||||
public function changePassword($new_password) {
|
||||
$this->password = sha1($new_password);
|
||||
static::$config->set('auth.Config.admin-password', $this->password);
|
||||
}
|
||||
|
||||
public function isAdministrator() {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected static function administratorUsername() {
|
||||
if ( ! static::$config->exists('auth.Config.admin-username')) {
|
||||
return 'admin';
|
||||
}
|
||||
|
||||
return static::$config->get('auth.Config.admin-username');
|
||||
}
|
||||
|
||||
public function __get($name) {
|
||||
switch ($name) {
|
||||
case 'username': {
|
||||
return $this->username;
|
||||
} break;
|
||||
|
||||
default: {
|
||||
throw new Sihnon_Exception_InvalidProperty($name);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
public function __set($name, $value) {
|
||||
throw new Sihnon_Exception_NotImplemented();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
37
source/lib/SihnonFramework/Auth/Plugin/None.class.php
Normal file
37
source/lib/SihnonFramework/Auth/Plugin/None.class.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
class SihnonFramework_Auth_Plugin_None
|
||||
extends Sihnon_PluginBase
|
||||
implements Sihnon_Auth_IPlugin {
|
||||
|
||||
protected function __construct() {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* IPlugin methods
|
||||
*/
|
||||
|
||||
public static function create(SihnonFramework_Config $config) {
|
||||
return new self();
|
||||
}
|
||||
|
||||
public function userExists($username) {
|
||||
throw new SihnonFramework_Exception_NotImplemented();
|
||||
}
|
||||
|
||||
public function listUsers() {
|
||||
throw new SihnonFramework_Exception_NotImplemented();
|
||||
}
|
||||
|
||||
public function authenticate($username, $password) {
|
||||
throw new SihnonFramework_Exception_NotImplemented();
|
||||
}
|
||||
|
||||
public function authenticateSession($username) {
|
||||
throw new SihnonFramework_Exception_NotImplemented();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user