Completed
Push — development ( dff276...1af152 )
by Alexander
03:35
created

AbstractAuthStrategy::findUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Usr authentication strategy base class
4
 *
5
 * @file      AbstractAuthStrategy.php
6
 *
7
 * PHP version 5.6+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2016 Alexander Yancharuk <alex at itvault at info>
11
 * @date      Вск Янв 27 17:29:50 2013
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>.
14
 */
15
16
namespace Veles\Auth\Strategies;
17
18
use Veles\Auth\UsrGroup;
19
use Veles\DataBase\DbFilter;
20
use Veles\Model\User;
21
22
/**
23
 * Class AbstractAuthStrategy
24
 *
25
 * @author   Alexander Yancharuk <alex at itvault dot info>
26
 */
27
abstract class AbstractAuthStrategy
28
{
29
	const ERR_USER_NOT_FOUND     = 1;
30
	const ERR_WRONG_PASSWORD     = 2;
31
	const ERR_NOT_VALID_PASSWORD = 4;
32
	const ERR_NOT_VALID_LOGIN    = 8;
33
	const ERR_NOT_VALID_UID      = 16;
34
	const ERR_NOT_VALID_HASH     = 32;
35
36
	// This var contains bit-wise error info
37
	protected $errors = 0;
38
	protected $user;
39
40
	/**
41
	 * Constructor
42
	 *
43
	 * @param User $user
44
	 */
45 1
	public function __construct(User $user)
46
	{
47 1
		$this->user = $user;
48 1
	}
49
50
	/**
51
	 * User authentication
52
	 *
53
	 * @return bool
54
	 */
55
	abstract public function identify();
56
57
	/**
58
	 * Auth cookies setup
59
	 *
60
	 * @param array $params		Cookie params
61
	 */
62 1
	protected function setCookie(array $params = [])
63
	{
64 1
		$expire = $path = $domain = $secure = $http_only = null;
65 1
		extract($params, EXTR_IF_EXISTS);
66 1
		$id   = $this->user->id;
67 1
		$hash = $this->user->getCookieHash();
68
69 1
		setcookie('id', $id, $expire, $path, $domain, $secure, $http_only);
70 1
		setcookie('pw', $hash, $expire, $path, $domain, $secure, $http_only);
71 1
	}
72
73
	/**
74
	 * Delete auth cookies
75
	 *
76
	 * @param array $params
77
	 */
78 4
	protected function delCookie(array $params = [])
79
	{
80 4
		$expire = $path = $domain = $secure = $http_only = null;
81 4
		extract($params, EXTR_IF_EXISTS);
82
83 4
		setcookie('id', null, $expire, $path, $domain, $secure, $http_only);
84 4
		setcookie('pw', null, $expire, $path, $domain, $secure, $http_only);
85 4
	}
86
87
	/**
88
	 * User search
89
	 *
90
	 * @param DbFilter $filter
91
	 *
92
	 * @return bool
93
	 */
94 3
	protected function findUser(DbFilter $filter)
95
	{
96 3
		if ($this->user->find($filter)) {
97 2
			return true;
98
		}
99
100 1
		$this->delCookie();
101
102 1
		$props = ['group' => UsrGroup::GUEST];
103 1
		$this->user->setProperties($props);
104
105 1
		$this->errors |= self::ERR_USER_NOT_FOUND;
106
107 1
		return false;
108
	}
109
110
	/**
111
	 * Get user
112
	 *
113
	 * @return User
114
	 */
115 1
	public function getUser()
116
	{
117 1
		return $this->user;
118
	}
119
120
	/**
121
	 * Get errors
122
	 *
123
	 * @return int
124
	 */
125 1
	public function getErrors()
126
	{
127 1
		return $this->errors;
128
	}
129
130
	/**
131
	 * Set error
132
	 *
133
	 * @param int $error
134
	 */
135 1
	public function setError($error)
136
	{
137 1
		$this->errors |= (int) $error;
138 1
	}
139
}
140