Created
November 23, 2010 18:14
-
-
Save fprochazka/712231 to your computer and use it in GitHub Desktop.
Zámek aplikace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Kdyby\Security; | |
use Nette; | |
use Kdyby; | |
/** | |
* Globální zámek pro aplikaci, vyžaduje auth handler, ideálně napojit http_authentication | |
* | |
* @author Filip Procházka <[email protected]> | |
*/ | |
class ApplicationLock extends Nette\Object | |
{ | |
/** @var array */ | |
private $users; | |
/** @var Nette\Web\HttpRequest */ | |
private $httpRequest; | |
/** @var string */ | |
public $message = "Access denied"; | |
/** @var string */ | |
public $realm = "test"; | |
public function __construct() | |
{ | |
$this->httpRequest = Nette\Environment::getHttpRequest(); | |
} | |
/** | |
* @param string $name | |
* @param string $pass | |
*/ | |
public function addUser($name, $pass) | |
{ | |
$this->users[$name] = $pass; | |
} | |
/** | |
* @return Kdyby\Security\ApplicationLock | |
*/ | |
public function authorize() | |
{ | |
$uri = $this->httpRequest->getUri(); | |
if (!isset($uri->user) || !isset($uri->password)) { | |
$this->kill(); | |
} | |
$user = $uri->user; | |
$pass = $uri->password; | |
if (isset($this->users[$user]) && $this->users[$user] === $pass) { | |
header('Content-Type: text/html; charset=utf-8'); | |
return $this; | |
} | |
return $this->kill(); | |
} | |
private function kill() | |
{ | |
header('Content-Type: text/plain; charset=utf-8'); | |
header('HTTP/1.1 401 Unauthorized'); | |
header('WWW-Authenticate: Basic realm="'.(string)$this->realm.'"'); | |
die((string)$this->message); | |
} | |
/** | |
* @param array $options | |
* @return Kdyby\Security\ApplicationLock | |
*/ | |
public static function createApplicationLock($options) | |
{ | |
$lock = new self; | |
foreach ((array)$options['user'] as $name => $pass) { | |
//list($name, $pass) = explode('=', $user); | |
$lock->addUser($name, $pass); | |
} | |
$lock->message = isset($options['message']) ? $options['message'] : $lock->message; | |
$lock->realm = isset($options['realm']) ? $options['realm'] : $lock->realm; | |
return $lock->authorize(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
service.SecurityLock.factory = "Kdyby\\Security\\ApplicationLock::createApplicationLock" | |
service.SecurityLock.option.user.franta = "@!$%@#&" | |
service.SecurityLock.realm = "Moje Aplikace" | |
service.SecurityLock.run = TRUE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment