1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225:
<?php
namespace Nethgui\Module;
use Nethgui\System\PlatformInterface as Valid;
class Login extends \Nethgui\Controller\AbstractController implements \Nethgui\Utility\SessionConsumerInterface, \Nethgui\Component\DependencyConsumer
{
private $session;
private $httpResponse;
private $userNotifications;
private $xhtmlDecoratorParams;
private $forcedRedirect;
private $languages = array();
private $loginValidator;
protected function initializeAttributes(\Nethgui\Module\ModuleAttributesInterface $base)
{
$attributes = new SystemModuleAttributesProvider();
$attributes->initializeFromModule($this);
return $attributes;
}
private function getLocales()
{
static $locales;
if(isset($locales)) {
return $locales;
}
$locales = array();
foreach($this->languages as $lang) {
$M = array();
if( ! preg_match('/^(?P<lang>[a-z][a-z])_(?P<region>[A-Z][A-Z])$/', $lang, $M)) {
continue;
}
$locales[] = $M['lang'] . '-' . strtoupper($M['region']);
}
$locales = array_unique($locales);
return $locales;
}
public function initialize()
{
parent::initialize();
$localeValidator = $this->createValidator()->memberOf($this->getLocales());
$this->declareParameter('username', Valid::NOTEMPTY);
$this->declareParameter('password', Valid::NOTEMPTY);
$this->declareParameter('path', Valid::NOTEMPTY);
$this->declareParameter('language', $localeValidator, array($this, 'getLocaleFromRequest'));
$this->declareParameter('hostname', FALSE, array('configuration', 'SystemName'));
}
public function bind(\Nethgui\Controller\RequestInterface $request) {
parent::bind($request);
if($this->forcedRedirect) {
$this->parameters['path'] = '/' . $this->forcedRedirect;
}
}
public function validate(\Nethgui\Controller\ValidationReportInterface $report)
{
parent::validate($report);
if($report->hasValidationErrors()) {
return;
}
$user = $this->getRequest()->getUser();
if ( ! $user->isAuthenticated() && $this->getRequest()->isMutation()) {
$authenticated = $user->authenticate($this->parameters['username'], $this->parameters['password']);
$user->setLocale($this->parameters['language']);
if( ! $authenticated) {
$report->addValidationError($this, 'password', $this->loginValidator);
}
}
}
public function getLocaleFromRequest()
{
return $this->getRequest()->getLocale();
}
public function prepareView(\Nethgui\View\ViewInterface $view)
{
parent::prepareView($view);
$user = $this->getRequest()->getUser();
$view->setTemplate('Nethgui\Template\Login');
$tmp = array();
foreach ($this->getLocales() as $l) {
$lang = substr($l, 0, 2);;
if (\extension_loaded('intl')) {
$tmp[$l] = sprintf('%s (%s)', ucfirst(\locale_get_display_language($l, $lang)), \locale_get_display_region($l, $lang));
} else {
$tmp[$lang][$l] = $l;
}
}
$view['languageDatasource'] = \Nethgui\Renderer\AbstractRenderer::hashToDatasource($tmp, TRUE);
$this->xhtmlDecoratorParams['disableHeader'] = TRUE;
$this->xhtmlDecoratorParams['disableMenu'] = TRUE;
$this->xhtmlDecoratorParams['disableFooter'] = FALSE;
$isAuthenticatedUserLoggingInAgain = $user->isAuthenticated() && ! $this->getRequest()->isMutation();
$isUnauthUserRequest = ! $user->isAuthenticated() && ! $this->getRequest()->isMutation() && $this->parameters['path'];
if ($isAuthenticatedUserLoggingInAgain) {
$this->httpResponse
->setStatus(302)
->addHeader('Location: ' . $view->getSiteUrl() . $view->getModuleUrl('/'))
;
} elseif ($isUnauthUserRequest) {
$this->httpResponse->setStatus(403, 'Forbidden');
}
}
public function nextPath()
{
if ($this->getRequest()->isMutation() && $this->getRequest()->getUser()->isAuthenticated()) {
if ( ! $this->parameters['path']) {
return '/';
} else {
return $this->parameters['path'] . sprintf(( $this->parameters['language'] !== $this->getRequest()->getLocale() ? '?Language[switch]=%s' : ''), $this->parameters['language']);
}
}
return FALSE;
}
public function setSession(\Nethgui\Utility\SessionInterface $session)
{
$this->session = $session;
return $this;
}
public function getDependencySetters()
{
$myHttpResponse = &$this->httpResponse;
$myUserNotifications = &$this->userNotifications;
$myXhtmlDecoratorParams = &$this->xhtmlDecoratorParams;
$myForcedRedirect = &$this->forcedRedirect;
$loginValidator = &$this->loginValidator;
$languages = &$this->languages;
return array(
'HttpResponse' => function (\Nethgui\Utility\HttpResponse $r) use (&$myHttpResponse) {
$myHttpResponse = $r;
},
'UserNotifications' => function(\Nethgui\Model\UserNotifications $n) use (&$myUserNotifications) {
$myUserNotifications = $n;
},
'decorator.xhtml.params' => function(\ArrayAccess $params) use (&$myXhtmlDecoratorParams) {
$myXhtmlDecoratorParams = $params;
},
'login.forced_redirect' => function($id) use (&$myForcedRedirect) {
$myForcedRedirect = $id;
},
'user.authenticate' => function(\Nethgui\System\ValidatorInterface $v) use (&$loginValidator) {
$loginValidator = $v;
},
'l10n.available_languages' => function ($langs) use (&$languages) {
$languages = $langs;
},
);
}
}