Passed
Push — master ( 8a8111...583a39 )
by Jean-Christophe
19:52
created

AuthControllerOverrideTrait::getCookieUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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