Last active
January 5, 2018 02:27
-
-
Save hissy/06f96ac4686d58146453037c36dedfe2 to your computer and use it in GitHub Desktop.
[concrete5][v8] Simplest authentication type example
This file contains hidden or 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 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; | |
} | |
} |
This file contains hidden or 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 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> |
Author
hissy
commented
Jan 5, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment