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(); |
26
|
|
|
|
27
|
|
|
abstract public function _getBaseRoute(); |
28
|
|
|
|
29
|
|
|
abstract protected function newAccountCreationRule(string $accountName):?bool; |
30
|
|
|
|
31
|
|
|
abstract public function _getLoginInputName(); |
32
|
|
|
|
33
|
|
|
abstract protected function hasAccountCreation():bool; |
34
|
|
|
|
35
|
|
|
abstract protected function canCreateAccountMessage(FlashMessage $fMessage); |
36
|
|
|
|
37
|
1 |
|
protected function getBaseUrl() { |
38
|
1 |
|
return URequest::getUrl ( $this->_getBaseRoute () ); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
protected function useAjax():bool{ |
42
|
1 |
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function message($type, $header, $body, $icon = 'info', $id = null) { |
46
|
1 |
|
return $this->loadView ( $this->_getFiles ()->getViewMessage (), get_defined_vars (), true ); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
protected function fMessage(FlashMessage $fMessage, $id = null) { |
50
|
1 |
|
return $this->message ( $fMessage->getType (), $fMessage->getTitle (), $fMessage->getContent (), $fMessage->getIcon (), $id ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function _newAccountCreationRule(){ |
54
|
|
|
if (URequest::isPost()) { |
55
|
|
|
$result = []; |
56
|
|
|
UResponse::asJSON(); |
57
|
|
|
$result['result'] = $this->newAccountCreationRule(URequest::post($this->_getLoginInputName())); |
|
|
|
|
58
|
|
|
echo \json_encode($result); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function noAttempts() { |
63
|
|
|
$timeout = $this->attemptsTimeout (); |
64
|
|
|
$plus = ""; |
65
|
|
|
if (is_numeric ( $timeout )) { |
66
|
|
|
$this->jquery->exec ( "$('._login').addClass('disabled');", true ); |
67
|
|
|
$plus = " You can try again {_timer}"; |
68
|
|
|
$this->jquery->exec ( "var startTimer=function(duration, display) {var timer = duration, minutes, seconds; |
69
|
|
|
var interval=setInterval(function () { |
70
|
|
|
minutes = parseInt(timer / 60, 10);seconds = parseInt(timer % 60, 10); |
71
|
|
|
minutes = minutes < 10 ? '0' + minutes : minutes; |
72
|
|
|
seconds = seconds < 10 ? '0' + seconds : seconds; |
73
|
|
|
display.html(minutes + ':' + seconds); |
74
|
|
|
if (--timer < 0) {clearInterval(interval);$('#timeout-message').hide();$('#bad-login').removeClass('attached');$('._login').removeClass('disabled');} |
75
|
|
|
}, 1000); |
76
|
|
|
}", true ); |
77
|
|
|
$timeToLeft = USession::getTimeout ( $this->_attemptsSessionKey ); |
78
|
|
|
$this->jquery->exec ( "startTimer({$timeToLeft},$('#timer'));", true ); |
79
|
|
|
$this->jquery->compile ( $this->view ); |
80
|
|
|
} |
81
|
|
|
return new FlashMessage ( "<i class='ui warning icon'></i> You have no more attempt of connection !" . $plus, null, "bottom attached error", "" ); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
protected function authLoadView($viewName, $vars = [ ]) { |
85
|
1 |
|
if($this->useAjax()){ |
86
|
1 |
|
$loadView=function($vn,$v){$this->jquery->renderView($vn,$v);}; |
87
|
|
|
}else{ |
88
|
|
|
$loadView=function($vn,$v) {$this->loadView($vn, $v);}; |
89
|
|
|
} |
90
|
1 |
|
$files = $this->_getFiles (); |
91
|
1 |
|
$mainTemplate = $files->getBaseTemplate (); |
92
|
1 |
|
if (isset ( $mainTemplate )) { |
93
|
|
|
$vars ['_viewname'] = $viewName; |
94
|
|
|
$vars ['_base'] = $mainTemplate; |
95
|
|
|
$loadView ( $files->getViewBaseTemplate (), $vars ); |
96
|
|
|
} else { |
97
|
1 |
|
$loadView ( $viewName, $vars ); |
98
|
|
|
} |
99
|
1 |
|
} |
100
|
|
|
|
101
|
1 |
|
protected function getOriginalURL() { |
102
|
1 |
|
return USession::get ( 'urlParts' ); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
protected function _getFiles(): AuthFiles { |
106
|
1 |
|
if (! isset ( $this->authFiles )) { |
107
|
1 |
|
$this->authFiles = $this->getFiles (); |
108
|
|
|
} |
109
|
1 |
|
return $this->authFiles; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function getViewVars($viewname) { |
113
|
|
|
return [ 'authURL' => $this->getBaseUrl (),'bodySelector' => $this->_getBodySelector (),'_loginCaption' => $this->_loginCaption ]; |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
protected function addAccountCreationViewData(array &$vData,$forMessage=false){ |
117
|
1 |
|
if($this->hasAccountCreation()){ |
118
|
|
|
if($forMessage){ |
119
|
|
|
$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" ); |
120
|
|
|
$this->canCreateAccountMessage ( $fMessage->parseContent(['accountCreationTarget'=>$this->_getBodySelector(),'createAccountUrl'=>$this->getBaseUrl().'/addAccount']) ); |
121
|
|
|
$vData['canCreateAccountMessage'] = $this->fMessage ( $fMessage ); |
122
|
|
|
}else{ |
123
|
|
|
$vData['createAccountUrl']=$this->getBaseUrl().'/addAccount'; |
124
|
|
|
$vData['accountCreationTarget']=$this->_getBodySelector(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
1 |
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|