CookieStrategy::setId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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