Completed
Push — master ( cf5944...0d6816 )
by Alexander
05:51 queued 02:56
created

CookieStrategy::errorHandle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 4
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * User cookie auth strategy
4
 *
5
 * @file      CookieStrategy.php
6
 *
7
 * PHP version 7.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2018 Alexander Yancharuk
11
 * @date      Вск Янв 27 17:44:08 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 CookieStrategy
24
 *
25
 * @author  Alexander Yancharuk <alex at itvault dot info>
26
 */
27
class CookieStrategy extends AbstractAuthStrategy
28
{
29
	protected $password_hash;
30
	protected $identifier;
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param string $id
36
	 * @param string $password_hash
37
	 * @param User   $user
38
	 */
39 6
	public function __construct($id, $password_hash, User $user)
40
	{
41 6
		parent::__construct($user);
42
43 6
		$this->setId($id)->setPasswordHash($password_hash);
44 6
	}
45
46
	/**
47
	 * User authentication by cookies
48
	 *
49
	 * @return bool
50
	 * @throws \Exception
51
	 */
52 6
	public function identify()
53
	{
54 6
		$filter = new DbFilter;
55
		// Search within not deleted users
56 6
		$where = 'id = ' . $this->getId() . '
57 6
			AND "group" & ' . UsrGroup::DELETED . ' = 0 ';
58 6
		$filter->setWhere($where);
59
60 6
		if (!$this->findUser($filter)) {
61 2
			return false;
62
		}
63
64
		// If hash doesn't match, delete cookies
65 4
		if ($this->getUser()->getCookieHash() !== $this->getPasswordHash()) {
66 2
			$this->delCookie();
67 2
			$this->errors |= self::ERR_WRONG_PASSWORD;
68
69 2
			return false;
70
		}
71
72 2
		return true;
73
	}
74
75
	/**
76
	 * @return string
77
	 */
78 2
	public function getPasswordHash()
79
	{
80 2
		return $this->password_hash;
81
	}
82
83
	/**
84
	 * @param string $password_hash
85
	 *
86
	 * @return $this
87
	 */
88 2
	public function setPasswordHash($password_hash)
89
	{
90 2
		$this->password_hash = $password_hash;
91
92 2
		return $this;
93
	}
94
95
	/**
96
	 * @return string
97
	 */
98 2
	public function getId()
99
	{
100 2
		return $this->identifier;
101
	}
102
103
	/**
104
	 * @param string $id
105
	 *
106
	 * @return $this
107
	 */
108 2
	public function setId($id)
109
	{
110 2
		$this->identifier = $id;
111
112 2
		return $this;
113
	}
114
115
	/**
116
	 * Error handling for current auth strategy
117
	 *
118
	 * @param array $input
119
	 *
120
	 * @return void
121
	 */
122
	public function errorHandle(array $input)
123
	{
124
		$input['id'] || $this->setError(static::ERR_NOT_VALID_UID);
125
		$input['pw'] || $this->setError(static::ERR_NOT_VALID_HASH);
126
	}
127
}
128