Created
September 23, 2018 18:35
-
-
Save FredAzevedo/b54d4a1d6521a4558ffec45c8db6aaec to your computer and use it in GitHub Desktop.
login
This file contains 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 | |
class LoginForm extends TPage | |
{ | |
protected $form; // form | |
/** | |
* Class constructor | |
* Creates the page and the registration form | |
*/ | |
function __construct($param) | |
{ | |
parent::__construct(); | |
$ini = AdiantiApplicationConfig::get(); | |
$this->style = 'clear:both'; | |
// creates the form | |
$this->form = new BootstrapFormBuilder('form_login'); | |
$this->form->setFormTitle( ' | |
<div style="display: inline-block;float:none; width:100%;padding-top: 10px;"> | |
<img src="app/images/logoMacroERP.png" height="160%" width="80%"> | |
</div> | |
' ); | |
// create the form fields | |
$login = new TEntry('login'); | |
$password = new TPassword('password'); | |
// define the sizes | |
$login->setSize('100%', 40); | |
$password->setSize('100%', 40); | |
$login->style = 'height:50px; | |
font-size:14px; | |
float:left; | |
border-bottom-left-radius: 0; | |
border-top-left-radius: 0; | |
font-family: "Roboto", sans-serif;'; | |
$password->style = 'height:50px; | |
font-size:14px; | |
float:left; | |
border-bottom-left-radius: 0; | |
border-top-left-radius: 0;'; | |
$login->placeholder = 'Insira seu usuário'; | |
$password->placeholder = 'Insira sua senha'; | |
$login->autofocus = 'autofocus'; | |
/*$user = '<span style=" | |
float:left; | |
margin-left:44px; | |
height:40px;" | |
class="login-avatar"><span class="glyphicon glyphicon-user"></span></span>'; | |
$locker = '<span style="float:left;margin-left:44px;height:40px;" class="login-avatar"><span class="glyphicon glyphicon-lock"></span></span>'; | |
$unit = '<span style="float:left;margin-left:44px;height:40px;" class="login-avatar"><span class="fa fa-university"></span></span>';*/ | |
$this->form->addFields( [$user, $login] ); | |
$this->form->addFields( [$locker, $password] ); | |
if (!empty($ini['general']['multiunit']) and $ini['general']['multiunit'] == '1') | |
{ | |
$unit_id = new TCombo('unit_id'); | |
$unit_id->setSize('100%'); | |
$unit_id->style = ' | |
height:50px; | |
font-size:14px; | |
float:left; | |
border-bottom-left-radius: 0; | |
border-top-left-radius: 0;'; | |
$unit_id->placeholder = 'Unidade cadastrada'; | |
$this->form->addFields( [$unit, $unit_id] ); | |
$login->setExitAction(new TAction( [$this, 'onExitUser'] ) ); | |
} | |
$btn = $this->form->addAction(_t('Log in'), new TAction(array($this, 'onLogin')), ''); | |
$btn->class = 'btn btn-info btn-lg'; | |
$btn->style = 'height: 50px; | |
width: 90%; | |
display: block; | |
margin: auto; | |
font-size:20px;'; | |
$conta = new THyperLink('Criar conta', '../index.php?class=SystemRegistrationForm', '', 12, 'b'); | |
$conta->style = 'margin-left: 25px;'; | |
$this->form->add($conta); | |
$this->form->add( new TElement('br') ); | |
$redefinir = new THyperLink('Redefinir senha', '../index.php?class=SystemRequestPasswordResetForm', '', 12, 'b'); | |
$redefinir->style = 'margin-left: 25px'; | |
$this->form->add($redefinir); | |
$wrapper = new TElement('div'); | |
$wrapper->style = ' | |
margin:auto; | |
margin-top:100px; | |
max-width:360px; | |
height:450px; | |
box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2), 0 5px 5px 0 rgba(0, 0, 0, 0.24); | |
position: relative; | |
z-index: 1; | |
box-sizing: border-box;'; | |
$wrapper->id = 'login-wrapper'; | |
$wrapper->add($this->form); | |
// add the form to the page | |
parent::add($wrapper); | |
} | |
/** | |
* user exit action | |
* Populate unit combo | |
*/ | |
public static function onExitUser($param) | |
{ | |
try | |
{ | |
TTransaction::open('permission'); | |
$user = SystemUser::newFromLogin( $param['login'] ); | |
if ($user instanceof SystemUser) | |
{ | |
$units = $user->getSystemUserUnits(); | |
$options = []; | |
if ($units) | |
{ | |
foreach ($units as $unit) | |
{ | |
$options[$unit->id] = $unit->unidade; | |
} | |
} | |
TCombo::reload('form_login', 'unit_id', $options); | |
} | |
TTransaction::close(); | |
} | |
catch (Exception $e) | |
{ | |
new TMessage('error',$e->getMessage()); | |
TTransaction::rollback(); | |
} | |
} | |
/** | |
* Authenticate the User | |
*/ | |
public static function onLogin($param) | |
{ | |
$ini = AdiantiApplicationConfig::get(); | |
try | |
{ | |
TTransaction::open('permission'); | |
$data = (object) $param; | |
if (empty($data->login)) | |
{ | |
throw new Exception( AdiantiCoreTranslator::translate('The field ^1 is required', _t('Login')) ); | |
} | |
if (empty($data->password)) | |
{ | |
throw new Exception( AdiantiCoreTranslator::translate('The field ^1 is required', _t('Password')) ); | |
} | |
if (!empty($ini['general']['multiunit']) and $ini['general']['multiunit'] == '1' and empty($data->unit_id)) | |
{ | |
throw new Exception( AdiantiCoreTranslator::translate('The field ^1 is required', _t('Unit')) ); | |
} | |
$user = SystemUser::validate( $data->login ); | |
if ($user) | |
{ | |
if (!empty($ini['permission']['auth_service']) and class_exists($ini['permission']['auth_service'])) | |
{ | |
$service = $ini['permission']['auth_service']; | |
$service::authenticate( $data->login, $data->password ); | |
} | |
else | |
{ | |
SystemUser::authenticate( $data->login, $data->password ); | |
} | |
TSession::regenerate(); | |
$programs = $user->getPrograms(); | |
$programs['LoginForm'] = TRUE; | |
TSession::setValue('logged', TRUE); | |
TSession::setValue('login', $data->login); | |
TSession::setValue('userid', $user->id); | |
TSession::setValue('usergroupids', $user->getSystemUserGroupIds()); | |
TSession::setValue('userunitids', $user->getSystemUserUnitIds()); | |
TSession::setValue('username', $user->name); | |
TSession::setValue('usermail', $user->email); | |
TSession::setValue('frontpage', ''); | |
TSession::setValue('programs',$programs); | |
if (!empty($user->unit)) | |
{ | |
TSession::setValue('userunitid',$user->unit->id); | |
} | |
if (!empty($ini['general']['multiunit']) and $ini['general']['multiunit'] == '1' and !empty($data->unit_id)) | |
{ | |
TSession::setValue('userunitid', $data->unit_id ); | |
} | |
$frontpage = $user->frontpage; | |
SystemAccessLog::registerLogin(); | |
if ($frontpage instanceof SystemProgram and $frontpage->controller) | |
{ | |
AdiantiCoreApplication::gotoPage($frontpage->controller); // reload | |
TSession::setValue('frontpage', $frontpage->controller); | |
} | |
else | |
{ | |
AdiantiCoreApplication::gotoPage('EmptyPage'); // reload | |
TSession::setValue('frontpage', 'EmptyPage'); | |
} | |
} | |
TTransaction::close(); | |
} | |
catch (Exception $e) | |
{ | |
new TMessage('error',$e->getMessage()); | |
TSession::setValue('logged', FALSE); | |
TTransaction::rollback(); | |
} | |
} | |
/** | |
* Reload permissions | |
*/ | |
public static function reloadPermissions() | |
{ | |
try | |
{ | |
TTransaction::open('permission'); | |
$user = SystemUser::newFromLogin( TSession::getValue('login') ); | |
if ($user) | |
{ | |
$programs = $user->getPrograms(); | |
$programs['LoginForm'] = TRUE; | |
TSession::setValue('programs', $programs); | |
$frontpage = $user->frontpage; | |
if ($frontpage instanceof SystemProgram AND $frontpage->controller) | |
{ | |
TApplication::gotoPage($frontpage->controller); // reload | |
} | |
else | |
{ | |
TApplication::gotoPage('EmptyPage'); // reload | |
} | |
} | |
TTransaction::close(); | |
} | |
catch (Exception $e) | |
{ | |
new TMessage('error', $e->getMessage()); | |
} | |
} | |
/** | |
* | |
*/ | |
public function onLoad($param) | |
{ | |
} | |
/** | |
* Logout | |
*/ | |
public static function onLogout() | |
{ | |
SystemAccessLog::registerLogout(); | |
TSession::freeSession(); | |
AdiantiCoreApplication::gotoPage('LoginForm', ''); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment