Passed
Push — master ( dfeadf...0d8182 )
by Jean-Christophe
04:51
created

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