LoginFormStrategy::setPassword()   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 form authentication strategy
4
 *
5
 * @file      LoginFormStrategy.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:40:18 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 Exception;
19
use Veles\Auth\UsrGroup;
20
use Veles\DataBase\DbFilter;
21
use Veles\Model\User;
22
23
/**
24
 * Class LoginFormStrategy
25
 *
26
 * @author  Alexander Yancharuk <alex at itvault dot info>
27
 */
28
class LoginFormStrategy extends AbstractAuthStrategy
29
{
30
	protected $login;
31
	protected $password;
32
33
	/**
34
	 * @param string $login User login
35
	 * @param string $password
36
	 * @param User   $user
37
	 */
38 7
	public function __construct($login, $password, User $user)
39
	{
40 7
		parent::__construct($user);
41 7
		$this->setLogin($login)->setPassword($password);
42
	}
43
44
	/**
45
	 * User authentication by login form
46
	 *
47
	 * @return bool
48
	 * @throws Exception
49
	 */
50 5
	public function identify(): bool
51
	{
52 5
		$filter = new DbFilter;
53
54 5
		$where = 'email = \'' . $this->getLogin() . '\'
55 5
			AND "group" & ' . UsrGroup::DELETED . ' = 0 ';
56 5
		$filter->setWhere($where);
57
58 5
		if (!$this->findUser($filter)) {
59 3
			return false;
60
		}
61
62 2
		$this->delCookie();
63
64 2
		if (!password_verify($this->password, $this->user->getHash())) {
0 ignored issues
show
Bug introduced by
It seems like $this->user->getHash() can also be of type null; however, parameter $hash of password_verify() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
		if (!password_verify($this->password, /** @scrutinizer ignore-type */ $this->user->getHash())) {
Loading history...
65 1
			$this->errors |= self::ERR_WRONG_PASSWORD;
66
67 1
			return false;
68
		}
69
70 1
		$this->setCookie(['expired' => strtotime('+365 days')]);
71
72 1
		return true;
73
	}
74
75
	/**
76
	 * @return string
77
	 */
78 6
	public function getLogin()
79
	{
80 6
		return $this->login;
81
	}
82
83
	/**
84
	 * @param string $login
85
	 *
86
	 * @return LoginFormStrategy
87
	 */
88 7
	public function setLogin($login)
89
	{
90 7
		$this->login = $login;
91
92 7
		return $this;
93
	}
94
95
	/**
96
	 * @return string
97
	 */
98 1
	public function getPassword()
99
	{
100 1
		return $this->password;
101
	}
102
103
	/**
104
	 * @param string $password
105
	 *
106
	 * @return LoginFormStrategy
107
	 */
108 7
	public function setPassword($password)
109
	{
110 7
		$this->password = $password;
111
112 7
		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['ln'] || $this->setError(static::ERR_NOT_VALID_LOGIN);
125 1
		$input['pw'] || $this->setError(static::ERR_NOT_VALID_PASSWORD);
126
	}
127
}
128