Completed
Push — master ( 98e1dc...22775a )
by Alexander
06:03 queued 02:58
created

UsrAuth::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * User authentication class
4
 *
5
 * @file      UsrAuth.php
6
 *
7
 * PHP version 5.4+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2015 Alexander Yancharuk <alex at itvault at info>
11
 * @date      Птн Мар 16 21:45:26 2012
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Auth;
17
18
use Veles\Model\User;
19
use Veles\Traits\SingletonInstance;
20
21
/**
22
 * User authentication class
23
 * @author  Alexander Yancharuk <alex at itvault dot info>
24
 */
25
class UsrAuth
26
{
27
	protected $identified = false;
28
	protected $strategy;
29
30
	use SingletonInstance;
31
32
	/**
33
	 * User authentication strategy initialization
34
	 */
35 1
	protected function __construct()
36
	{
37 1
		$this->strategy   = UsrAuthFactory::create();
38 1
		$this->identified = $this->strategy->identify();
39 1
	}
40
41
	/**
42
	 * Method returns bitwise values of auth-errors
43
	 *
44
	 * @return int Bitwise auth-errors
45
	 */
46 1
	public static function getErrors()
47
	{
48 1
		return self::instance()->strategy->getErrors();
49
	}
50
51
	/**
52
	 * Check for user groups
53
	 *
54
	 * @param int $groups Sum of user groups
55
	 *
56
	 * @return bool
57
	 */
58 6
	public static function hasAccess($groups)
59
	{
60 6
		$user_group = self::getUser()->getGroup();
61
62 6
		return $groups === ($user_group & $groups);
63
	}
64
65
	/**
66
	 * Get user
67
	 *
68
	 * @return User
69
	 */
70 1
	public static function getUser()
71
	{
72 1
		return self::instance()->strategy->getUser();
73
	}
74
}
75