Completed
Push — master ( 138234...77e005 )
by Anton
12s
created

Table::checkCookie()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 17
rs 9.2
ccs 0
cts 9
cp 0
crap 20
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link      https://github.com/bluzphp/skeleton
5
 */
6
7
declare(strict_types=1);
8
9
namespace Application\Auth;
10
11
use Application\Exception;
12
use Application\Users;
13
use Bluz\Application;
14
use Bluz\Auth\AbstractTable;
15
use Bluz\Auth\AuthException;
16
use Bluz\Proxy\Auth;
17
use Bluz\Proxy\Session;
18
19
/**
20
 * Auth Table
21
 *
22
 * @package  Application\Auth
23
 *
24
 * @method   static Row findRow($primaryKey)
25
 * @method   static Row findRowWhere($whereList)
26
 *
27
 * @author   Anton Shevchuk
28
 * @created  12.07.11 15:28
29
 */
30
class Table extends AbstractTable
31
{
32
    /**
33
     * Call Hash Function
34
     *
35
     * @param string $password
36
     *
37
     * @throws \Application\Exception
38
     * @return string
39
     */
40
    public static function hash($password)
41
    {
42
        $hash = Auth::getInstance()->getOption('hash');
43
44
        if (!$hash || !is_callable($hash)) {
45
            throw new Exception('Hash function for `Auth` package is not callable');
46
        }
47
48
        // encrypt password with secret
49
        return $hash($password);
50
    }
51
52
    /**
53
     * Call Verify Function
54
     *
55
     * @param string $password
56
     * @param string $hash
57
     *
58
     * @throws \Application\Exception
59
     * @return string
60
     */
61 4
    public static function verify($password, $hash)
62
    {
63 4
        $verify = Auth::getInstance()->getOption('verify');
64
65 4
        if (!$verify || !is_callable($verify)) {
66
            throw new Exception('Verify function for `Auth` package is not callable');
67
        }
68
69
        // verify password with hash
70 4
        return $verify($password, $hash);
71
    }
72
73
    /**
74
     * Can entity login
75
     *
76
     * @param $user
77
     *
78
     * @throws AuthException
79
     */
80 1
    public static function tryLogin($user)
81
    {
82 1
        switch ($user->status) {
83 1
            case (Users\Table::STATUS_PENDING):
84
                throw new AuthException('Your account is pending activation', 403);
85 1
            case (Users\Table::STATUS_DISABLED):
86
                throw new AuthException('Your account is disabled by administrator', 403);
87 1
            case (Users\Table::STATUS_ACTIVE):
88
                // save user to new session
89 1
                Auth::setIdentity($user);
90 1
                break;
91
            default:
92
                throw new AuthException('User not found');
93
        }
94 1
    }
95
}
96