Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
AuthControllerCoreTrait.php
Go to the documentation of this file.
1<?php
2
4
9
18
19 abstract public function loadView(string $viewName, $pData = NULL, bool $asString = false);
20
21 abstract protected function attemptsTimeout();
22
23 abstract protected function getFiles(): AuthFiles;
24
25 abstract public function _getBodySelector():string;
26
27 abstract public function _getBaseRoute():string;
28
29 abstract protected function _newAccountCreationRule(string $accountName):?bool;
30
31 abstract public function _getLoginInputName():string;
32
33 abstract protected function hasAccountCreation():bool;
34
35 abstract protected function hasAccountRecovery():bool;
36
37 abstract protected function canCreateAccountMessage(FlashMessage $fMessage);
38
39 abstract protected function getAccountRecoveryLink():string;
40
41 protected function getBaseUrl():string {
42 return URequest::getUrl ( $this->_getBaseRoute () );
43 }
44
45 protected function useAjax():bool{
46 return true;
47 }
48
49 #[\Ubiquity\attributes\items\router\NoRoute()]
50 public function message($type, $header, $body, $icon = 'info', $id = null):string {
51 return $this->loadView ( $this->_getFiles ()->getViewMessage (), \get_defined_vars (), true );
52 }
53
54 protected function fMessage(FlashMessage $fMessage, $id = null):string {
55 return $this->message ( $fMessage->getType (), $fMessage->getTitle (), $fMessage->getContent (), $fMessage->getIcon (), $id );
56 }
57
58 #[\Ubiquity\attributes\items\router\Post]
59 public function newAccountCreationRule(){
60 if (URequest::isPost()) {
61 $result = [];
62 UResponse::asJSON();
63 $result['result'] = $this->_newAccountCreationRule(URequest::post($this->_getLoginInputName()));
64 echo \json_encode($result);
65 }
66 }
67
68 protected function noAttempts() {
69 $timeout = $this->attemptsTimeout ();
70 $plus = '';
71 if (\is_numeric ( $timeout )) {
72 $this->jquery->exec ( "$('._login').addClass('disabled');", true );
73 $plus = " You can try again {_timer}";
74 $this->jquery->exec ( "var startTimer=function(duration, display) {var timer = duration, minutes, seconds;
75 var interval=setInterval(function () {
76 minutes = parseInt(timer / 60, 10);seconds = parseInt(timer % 60, 10);
77 minutes = minutes < 10 ? '0' + minutes : minutes;
78 seconds = seconds < 10 ? '0' + seconds : seconds;
79 display.html(minutes + ':' + seconds);
80 if (--timer < 0) {clearInterval(interval);$('#timeout-message').hide();$('#bad-login').removeClass('attached');$('._login').removeClass('disabled');}
81 }, 1000);
82 }", true );
83 $timeToLeft = USession::getTimeout ( $this->_attemptsSessionKey );
84 $this->jquery->exec ( "startTimer({$timeToLeft},$('#timer'));", true );
85 $this->jquery->compile ( $this->view );
86 }
87 return new FlashMessage ( "<i class='ui warning icon'></i> You have no more attempt of connection !" . $plus, null, "bottom attached error", "" );
88 }
89
90 protected function authLoadView($viewName, $vars = [ ]):void {
91 if($this->useAjax()){
92 $loadView=function($vn,$v){$this->jquery->renderView($vn,$v);};
93 }else{
94 $loadView=function($vn,$v) {$this->loadView($vn, $v);};
95 }
96 if($this->hasAccountRecovery()){
97 $vars['resetPassword']=$this->getAccountRecoveryLink();
98 }
99 $files = $this->_getFiles ();
100 $mainTemplate = $files->getBaseTemplate ();
101 if (isset ( $mainTemplate )) {
102 $vars ['_viewname'] = $viewName;
103 $vars ['_base'] = $mainTemplate;
104 $loadView ( $files->getViewBaseTemplate (), $vars );
105 } else {
106 $loadView ( $viewName, $vars );
107 }
108 }
109
110 protected function getOriginalURL() {
111 return USession::get ( 'urlParts' );
112 }
113
114 protected function _getFiles(): AuthFiles {
115 if (! isset ( $this->authFiles )) {
116 $this->authFiles = $this->getFiles ();
117 }
118 return $this->authFiles;
119 }
120
121 protected function getViewVars($viewname) {
122 return [ 'authURL' => $this->getBaseUrl (),'bodySelector' => $this->_getBodySelector (),'_loginCaption' => $this->_loginCaption ];
123 }
124
125 protected function addAccountCreationViewData(array &$vData,$forMessage=false){
126 if($this->hasAccountCreation()){
127 if($forMessage){
128 $fMessage = new FlashMessage ( "<p>You can create one now!</p><a href='{createAccountUrl}' class='ui button black ajax _create' data-target='{accountCreationTarget}'>Create account</a>", "Don't have an account yet?", "", "question" );
129 $this->canCreateAccountMessage ( $fMessage->parseContent(['accountCreationTarget'=>$this->_getBodySelector(),'createAccountUrl'=>$this->getBaseUrl().'/addAccount']) );
130 $vData['canCreateAccountMessage'] = $this->fMessage ( $fMessage );
131 }else{
132 $vData['createAccountUrl']=$this->getBaseUrl().'/addAccount';
133 $vData['accountCreationTarget']=$this->_getBodySelector();
134 }
135 }
136 }
137}
138
loadView(string $viewName, $pData=NULL, bool $asString=false)
message($type, $header, $body, $icon='info', $id=null)
Ubiquity\controllers\auth$AuthFiles This class is part of Ubiquity.
Definition AuthFiles.php:13
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
Definition URequest.php:18
Http Response utilities.
Definition UResponse.php:17
Http Session utilities This class is part of Ubiquity.
Definition USession.php:16