Passed
Push — master ( 0b9e3e...5a5c41 )
by Cody
04:44
created

Auth_Base::check_password()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 2
rs 10
1
<?php
2
class Auth_Base {
3
	private $pdo;
4
5
	const AUTH_SERVICE_API = '_api';
6
7
	function __construct() {
8
		$this->pdo = Db::pdo();
9
	}
10
11
	// Auto-creates specified user if allowed by system configuration
12
	// Can be used instead of find_user_by_login() by external auth modules
13
	function auto_create_user($login, $password = false) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
14
		if ($login && defined('AUTH_AUTO_CREATE') && AUTH_AUTO_CREATE) {
0 ignored issues
show
Bug introduced by
The constant AUTH_AUTO_CREATE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
			$user_id = $this->find_user_by_login($login);
16
17
			if (!$password) $password = make_password();
18
19
			if (!$user_id) {
20
				$salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
21
				$pwd_hash = encrypt_password($password, $salt, true);
22
23
				$sth = $this->pdo->prepare("INSERT INTO ttrss_users
24
						(login,access_level,last_login,created,pwd_hash,salt)
25
						VALUES (?, 0, null, NOW(), ?,?)");
26
				$sth->execute([$login, $pwd_hash, $salt]);
27
28
				return $this->find_user_by_login($login);
29
30
			} else {
31
				return $user_id;
32
			}
33
		}
34
35
		return $this->find_user_by_login($login);
36
	}
37
38
	function find_user_by_login($login) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
		$sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE login = ?");
40
		$sth->execute([$login]);
41
42
		if ($row = $sth->fetch()) {
43
			return $row["id"];
44
		} else {
45
			return false;
46
		}
47
	}
48
}
49