Skip to content

Instantly share code, notes, and snippets.

@hissy
Last active July 29, 2019 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hissy/9b83bc4d8f75eae76144246768cbfd95 to your computer and use it in GitHub Desktop.
Save hissy/9b83bc4d8f75eae76144246768cbfd95 to your computer and use it in GitHub Desktop.
#concrete5 Force specific authentication type by environment
<?php
// Put this file on application/controllers/single_page/login.php
namespace Application\Controller\SinglePage;
use Concrete\Core\Routing\RedirectResponse;
use Concrete\Core\Support\Facade\Facade;
use Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface;
class Login extends \Concrete\Controller\SinglePage\Login
{
/**
* The name of environment tha you want to force authentication type
*/
private static $forceEnvironment = 'production';
/**
* The handle of authentication type that you want to force users to use
*/
private static $forceAuthenticationType = 'google';
public function view($type = null, $element = 'form')
{
$response = $this->handleAuthenticationTypeByEnvironment($type);
if (is_object($response)) {
return $response;
}
return parent::view($type, $element);
}
/**
* Force "google" authentication type on the "production" environment
*
* @param $type
* @return RedirectResponse
*/
protected function handleAuthenticationTypeByEnvironment($type = '')
{
$app = Facade::getFacadeApplication();
$env = $app->environment();
/** @var ResolverManagerInterface $resolver */
$resolver = $app->make(ResolverManagerInterface::class);
if ($env == self::$forceEnvironment && $type != self::$forceAuthenticationType) {
return new RedirectResponse($resolver->resolve(['login', self::$forceAuthenticationType]));
}
}
public function authenticate($type = '')
{
$response = $this->handleAuthenticationTypeByEnvironment($type);
if (is_object($response)) {
return $response;
}
return parent::authenticate($type);
}
public function session_invalidated()
{
$this->flash('error', t('Your session has expired. Please sign in again.'));
return $this->handleAuthenticationTypeByEnvironment();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment