GithubHelp home page GithubHelp logo

rahulyhg / ext-auth-basic Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mvccore/ext-auth-basic

0.0 1.0 0.0 74 KB

Simple authentication extension to only signin/signout user by system config or database credentials, possible to extend.

License: BSD 3-Clause "New" or "Revised" License

PHP 100.00%

ext-auth-basic's Introduction

MvcCore Extension - Basic Authentication

Latest Stable Version License PHP Version

Simple authentication extension, only to signin/signout user. Extension can use credentials defined in system config.ini or in database. Possibility to extend user, signin/signout forms, submit controller and extension class itself.

Installation

composer require mvccore/ext-auth-basic

Usage

Add this to Bootstrap.php or to very application beginning, before application routing.

\MvcCore\Ext\Auths\Basic::GetInstance()
	->SetPasswordHashSalt('s9E56/QH6!a69sJML9aS$6s+')
	->SetUserClass('\\MvcCore\\Ext\\Auths\\Users\\SystemConfig');
	/* // or you can use database user:
	->SetUserClass('\\MvcCore\\Ext\\Auths\\Users\\Database')
	->SetTableStructureForDbUsers('users', array(
		'id'			=> 'id',
		'active'		=> 'active',
		'userName'		=> 'user_name',
		'passwordHash'	=> 'password_hash',
		'fullName'		=> 'full_name',
	));
	*/

For system config users, you need to specify users in system.ini like this:

[users]
0.userName		= admin
0.fullName		= Administrator
0.passwordHash	= $2y$10$czlFNTYvUUg2IWE2OXNKTO8PB5xPGXz9i8IH7Fa7M0YsPlSLriJZu
; admin password is `demo`

To get sign in form into view in your application controller:

...
	public function IndexAction () {
		if ($this->user !== NULL)
			self::Redirect($this->Url('administration_index_page'));
		$this->view->SignInForm = \MvcCore\Ext\Auths\Basic::GetInstance()
			->GetSignInForm()
			->SetValues(array(// set signed in url to administration index page by default:
				'successUrl' => $this->Url('administration_index_page'),
			));
	}
...

To get sign out form into view in your application controller:

...
	public function PreDispatch () {
		parent::PreDispatch();
		if ($this->viewEnabled && $this->user) {
			$this->view->SignOutForm =\MvcCore\Ext\Auths\Basic::GetInstance()
				->GetSignOutForm()
				->SetValues(array(
					'successUrl' => $this->Url('login_page')
				));
		}
	}
...

For any forms CSRF errors - you can call in base controller Init() action:

...
	public function Init() {
		parent::Init();
		// when any CSRF token is outdated or not the same - sign out user by default
		\MvcCore\Ext\Form::AddCsrfErrorHandler(function (\MvcCore\Ext\Form & $form, $errorMsg) {
			\MvcCore\Ext\Auths\Basics\User::LogOut();
			self::Redirect($this->Url(
				'Index:Index',
				array('absolute' => TRUE, 'sourceUrl'	=> rawurlencode($form->ErrorUrl))
			));
		});
	}
...

To translate your signin and signout form visible elements, use:

\MvcCore\Ext\Auths\Basic::GetInstance()->SetTranslator(function ($key, $lang = NULL) {
	// your custom translator model/service:
	return \App\Models\Translator::GetInstance()->Translate($key, $lang);
});

ext-auth-basic's People

Contributors

tomflidr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.