1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\auth; |
4
|
|
|
|
5
|
|
|
use Ubiquity\utils\http\URequest; |
6
|
|
|
use Ubiquity\utils\http\USession; |
7
|
|
|
use Ubiquity\utils\flash\FlashMessage; |
8
|
|
|
use Ubiquity\utils\http\UResponse; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
* @author jcheron <[email protected]> |
13
|
|
|
* @property AuthFiles $authFiles |
14
|
|
|
* @property string $_loginCaption |
15
|
|
|
* @property \Ajax\php\ubiquity\JsUtils $jquery |
16
|
|
|
*/ |
17
|
|
|
trait AuthControllerCoreTrait { |
18
|
|
|
|
19
|
|
|
abstract public function loadView($viewName, $pData = NULL, $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
|
2 |
|
protected function getBaseUrl():string { |
42
|
2 |
|
return URequest::getUrl ( $this->_getBaseRoute () ); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
protected function useAjax():bool{ |
46
|
1 |
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
#[\Ubiquity\attributes\items\router\NoRoute()] |
50
|
2 |
|
public function message($type, $header, $body, $icon = 'info', $id = null):string { |
51
|
2 |
|
return $this->loadView ( $this->_getFiles ()->getViewMessage (), \get_defined_vars (), true ); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
protected function fMessage(FlashMessage $fMessage, $id = null):string { |
55
|
2 |
|
return $this->message ( $fMessage->getType (), $fMessage->getTitle (), $fMessage->getContent (), $fMessage->getIcon (), $id ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
#[\Ubiquity\attributes\items\router\Post] |
59
|
1 |
|
public function newAccountCreationRule(){ |
60
|
1 |
|
if (URequest::isPost()) { |
61
|
1 |
|
$result = []; |
62
|
1 |
|
UResponse::asJSON(); |
63
|
1 |
|
$result['result'] = $this->_newAccountCreationRule(URequest::post($this->_getLoginInputName())); |
64
|
1 |
|
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
|
2 |
|
protected function authLoadView($viewName, $vars = [ ]):void { |
91
|
2 |
|
if($this->useAjax()){ |
92
|
2 |
|
$loadView=function($vn,$v){$this->jquery->renderView($vn,$v);}; |
93
|
|
|
}else{ |
94
|
|
|
$loadView=function($vn,$v) {$this->loadView($vn, $v);}; |
95
|
|
|
} |
96
|
2 |
|
if($this->hasAccountRecovery()){ |
97
|
1 |
|
$vars['resetPassword']=$this->getAccountRecoveryLink(); |
98
|
|
|
} |
99
|
2 |
|
$files = $this->_getFiles (); |
100
|
2 |
|
$mainTemplate = $files->getBaseTemplate (); |
101
|
2 |
|
if (isset ( $mainTemplate )) { |
102
|
|
|
$vars ['_viewname'] = $viewName; |
103
|
|
|
$vars ['_base'] = $mainTemplate; |
104
|
|
|
$loadView ( $files->getViewBaseTemplate (), $vars ); |
105
|
|
|
} else { |
106
|
2 |
|
$loadView ( $viewName, $vars ); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
protected function getOriginalURL() { |
111
|
2 |
|
return USession::get ( 'urlParts' ); |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
protected function _getFiles(): AuthFiles { |
115
|
2 |
|
if (! isset ( $this->authFiles )) { |
116
|
2 |
|
$this->authFiles = $this->getFiles (); |
117
|
|
|
} |
118
|
2 |
|
return $this->authFiles; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function getViewVars($viewname) { |
122
|
|
|
return [ 'authURL' => $this->getBaseUrl (),'bodySelector' => $this->_getBodySelector (),'_loginCaption' => $this->_loginCaption ]; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
protected function addAccountCreationViewData(array &$vData,$forMessage=false){ |
126
|
2 |
|
if($this->hasAccountCreation()){ |
127
|
1 |
|
if($forMessage){ |
128
|
1 |
|
$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
|
1 |
|
$this->canCreateAccountMessage ( $fMessage->parseContent(['accountCreationTarget'=>$this->_getBodySelector(),'createAccountUrl'=>$this->getBaseUrl().'/addAccount']) ); |
130
|
1 |
|
$vData['canCreateAccountMessage'] = $this->fMessage ( $fMessage ); |
131
|
|
|
}else{ |
132
|
|
|
$vData['createAccountUrl']=$this->getBaseUrl().'/addAccount'; |
133
|
|
|
$vData['accountCreationTarget']=$this->_getBodySelector(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|