- ⌂ login
- Methods
io_comand_login\login
Provides the primary authentication interface for the login package. This provides PHP functionality on top of the authentication feature content types in the repository, and provides a centralized class for interacting with them.
Example
<?php
require_once('/var/www/webcomand/comand.php');
class example_login {
const LOGIN_POLICY_OID = '123';
const RESET_PASSWORD_URL = 'https://presidentsdemo.com/login/reset';
const RESET_LOCK_URL = 'https://demo.webcomand.com/com_webcomand/components/login/reset_lock_link';
private static $login = NULL;
public function __construct(array $options = []) {
$repo = $$options['repo'] ?? \comand::repo();
$policy = $$this->repo->get(self::LOGIN_POLICY_OID);
$this->login = new \io_comand_login\login($$policy, $$repo);
$this->login->set('ResetLockLink', self::RESET_LOCK_URL);
return self::$login_object;
}
private static $user=false;//false is we dont' know if we're logged in, null is we're not
public static function login(string $username,string $password){
try{
$login=self::get_login_object();
$login->set('account', $username);
$login->set('password', $password);
self::$user=$login->login->login();
return self::$user;
}catch(\io_comand_login\exception $e){
switch($e->getCode()){
case \io_comand_login\exception::LOGIN_ERROR_CONFIGURATION:
case \io_comand_login\exception::LOGIN_ERROR_SYSTEMLOCKED:
throw $e;
break;
default:
throw new \io_comand_login\exception("No user found for given credentials.", \io_comand_login\exception::LOGIN_ERROR_NOUSER);
}
}
}
public static function logout(){
$login=self::get_login_object();
$login->login->logout();
self::$user=null;
}
public static function is_logged_in(){
if(self::$user===false){
$login=self::get_login_object();
self::$user=$login->login->is_logged_in();
}
return self::$user;
}
public static function has_authorization(int $authorization_type){
if($authorization_type===32683)//if public
return true;
$user=self::is_logged_in();
if(!$user)
return null;
if(!$user->authorized_for($authorization_type))
return false;
return $user;
}
public static function change_password(string $old_password,string $new_password,string $confirm_password){
$login=self::get_login_object();
$login->set('account', self::$user->OID);
$login->set('old_password', $old_password);
$login->set('new_password', $new_password);
$login->set('confirm_password', $confirm_password);
$login->change->change_password();
self::$user->SecurePassword=true;
self::$user->approve();
}
public static function change_security_question(string $password,string $question,string $answer){
$login=self::get_login_object();
$login->set('account', self::$user->OID);
$question_model=$login->get_model('securityquestion');
$credentials_model=$login->get_model('credentials');
if(self::$user->OID!=$credentials_model->challenge($password))
throw new exception("User mismatch or cannot discover user from old password", \io_comand_login\exception::LOGIN_ERROR_BADUSERINPUT);
$question_model->invalidate_all_questions(self::$user->OID);
$question_model->add_question(self::$user->OID,$question,$answer);
}
public static function forgot_password(string $email){
try{
$login=self::get_login_object();
$login->set('account', $email);
$login->set('link', 'RESET_PASSWORD_URL');
$login->reset->forgot_password();
}catch(\io_comand_login\exception $e){
switch($e->getCode()){
case \io_comand_login\exception::LOGIN_ERROR_CONFIGURATION:
case \io_comand_login\exception::LOGIN_ERROR_SYSTEMLOCKED:
throw $e;
}
}
}
public static function verify_reset_code(string $code){
try{
$login=self::get_login_object();
$login->set('account', $code);
if($login->reset->verify_code($code))
return [$login->get('question_id'),$login->get('question')];
}catch(\io_comand_login\exception $e){
switch($e->getCode()){
case \io_comand_login\exception::LOGIN_ERROR_CONFIGURATION:
case \io_comand_login\exception::LOGIN_ERROR_SYSTEMLOCKED:
throw $e;
}
}
return null;
}
public static function reset_password(string $code,int $question_id,string $answer,string $new_password,string $confirm_password){
$login=self::get_login_object();
$login->set('account', $code);
$login->set('question_id', $question_id);
$login->set('answer', $answer);
$login->set('new_password', $new_password);
$login->set('confirm_password', $confirm_password);
$login->reset->reset_password($code);
}
}