Skip to content

Instantly share code, notes, and snippets.

@hissy
Last active January 5, 2018 02:27
Show Gist options
  • Save hissy/06f96ac4686d58146453037c36dedfe2 to your computer and use it in GitHub Desktop.
Save hissy/06f96ac4686d58146453037c36dedfe2 to your computer and use it in GitHub Desktop.
[concrete5][v8] Simplest authentication type example
<?php
namespace Application\Authentication\Example;
use Concrete\Core\Authentication\AuthenticationTypeController;
use Concrete\Core\Authentication\AuthenticationTypeFailureException;
use Concrete\Core\User\User;
class Controller extends AuthenticationTypeController
{
public function getAuthenticationTypeIconHTML()
{
return "<i class='fa fa-bug'></i>";
}
public function view()
{
// blank
}
/**
* @return string
*/
public function getHandle()
{
return 'example';
}
/**
* Method used to verify the user and log them in.
* Returning user will cause finishAuthentication to run, otherwise it's expected that the subclass manage completion.
*
* @throws AuthenticationTypeFailureException
*
* @return \User|null
*/
public function authenticate()
{
$password = $this->post('password');
// If the password matches
if ($password == 'foo') {
$user = User::loginByUserID(1);
} else {
throw new \Exception('Invalid password.');
}
return $user;
}
/**
* Method used to clean up.
* This method must be defined, if it isn't needed, leave it blank.
*
* @param \User $u
*/
public function deauthenticate(User $u)
{
// blank
}
/**
* Test user authentication status.
*
* @param \User $u
*
* @return bool Returns true if user is authenticated, false if not
*/
public function isAuthenticated(User $u)
{
return $u->isLoggedIn();
}
/**
* Create a cookie hash to identify the user indefinitely.
*
* @param \User $u
*
* @return string Unique hash to be used to verify the users identity
*/
public function buildHash(User $u)
{
return '';
}
/**
* Verify cookie hash to identify user.
*
* @param $u User object requesting verification.
* @param string $hash
*
* @return bool returns true if the hash is valid, false if not
*/
public function verifyHash(User $u, $hash)
{
return true;
}
}
<?php defined('C5_EXECUTE') or die('Access denied.');
$form = Core::make('helper/form');
/* @var Concrete\Core\Form\Service\Form $form */
?>
<form method="post" action="<?= URL::to('/login', 'authenticate', $this->getAuthenticationTypeHandle()) ?>">
<div class="form-group">
<label class="control-label"><?=t('Password')?></label>
<input name="password" class="form-control" type="password" />
</div>
<div class="form-group">
<button class="btn btn-primary"><?= t('Log in') ?></button>
</div>
<?php Core::make('helper/validation/token')->output('login_' . $this->getAuthenticationTypeHandle()); ?>
</form>
@hissy
Copy link
Author

hissy commented Jan 5, 2018

// Install
try {
    $type = \Concrete\Core\Authentication\AuthenticationType::getByHandle('example');
} catch (Exception $e) {
    $type = \Concrete\Core\Authentication\AuthenticationType::add('example', 'Example');
}

// Uninstall
try {
    $type = \Concrete\Core\Authentication\AuthenticationType::getByHandle('example');
    $type->delete();
} catch (Exception $e) {
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment