Passed
Push — master ( df4f71...b35ad3 )
by Jean-Christophe
18:08
created

AuthControllerOverrideTrait::getFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\controllers\auth;
4
5
use Ubiquity\cache\ClassUtils;
6
use Ubiquity\utils\http\USession;
7
use Ubiquity\utils\http\UCookie;
8
9
/**
10
 * Trait AuthControllerOverrideTrait
11
 *
12
 */
13
trait AuthControllerOverrideTrait {
14
	
15
	abstract public function badLogin();
16
	
17
	abstract public function _getBodySelector():string;
18
19
	abstract protected function getBaseUrl():string;
20
21
22
	/**
23
	 * To override
24
	 * Return the base route for this Auth controller
25
	 * @return string
26
	 */
27
	public function _getBaseRoute(){
28
		return ClassUtils::getClassSimpleName(\get_class($this));
29
	}
30
	
31
	/**
32
	 * Processes the data posted by the login form
33
	 * Have to return the connected user instance
34
	 */
35
	abstract protected function _connect();
36
	
37
	/**
38
	 * @param object $connected
39
	 */
40
	abstract protected function onConnect($connected);
41
	
42
	/**
43
	 * To override for defining a new action when creditentials are invalid.
44
	 */
45 1
	protected function onBadCreditentials(){
46 1
		$this->badLogin();
47
	}
48
	
49
50
51
	/**
52
	 * To override for defining user session key, default : "activeUser"
53
	 * @return string
54
	 */
55 1
	public function _getUserSessionKey():string {
56 1
		return 'activeUser';
57
	}
58
	
59
	/**
60
	 * To override for getting active user, default : USession::get("activeUser")
61
	 * @return string
62
	 */
63 1
	public function _getActiveUser(){
64 1
		return USession::get($this->_getUserSessionKey());
65
	}
66
	
67
	/**
68
	 * Checks if user is valid for the action
69
	 * @param string $action
70
	 * return boolean true if activeUser is valid
71
	 */
72
	abstract public function _isValidUser($action=null);
73
	
74
	/**
75
	 * Returns the value from connected user to save it in the cookie for auto connection
76
	 * @param object $connected
77
	 */
78
	protected function toCookie($connected){
79
		return;
80
	}
81
	
82
	/**
83
	 * Loads the user from database using the cookie value
84
	 * @param string $cookie
85
	 */
86
	protected function fromCookie($cookie){
87
		return;
88
	}
89
	
90
	
91
	/**
92
	 * Saves the connected user identifier in a cookie
93
	 * @param object $connected
94
	 */
95
	protected function rememberMe($connected){
96
		$id= $this->toCookie($connected);
97
		if(isset($id)){
98
			UCookie::set($this->_getUserSessionKey(),$id);
99
		}
100
	}
101
	
102
	/**
103
	 * Returns the cookie for auto connection
104
	 * @return NULL|string
105
	 */
106 1
	protected function getCookieUser(){
107 1
		return UCookie::get($this->_getUserSessionKey());
108
	}
109
	
110
	/**
111
	 * To override for changing view files
112
	 * @return AuthFiles
113
	 */
114
	protected function getFiles ():AuthFiles{
115
		return new AuthFiles();
116
	}
117
118
}
119
120