Failed Conditions
Pull Request — master (#321)
by Anton
23:35 queued 08:33
created

application/models/Auth/Table.php (4 issues)

Labels
Severity
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;
0 ignored issues
show
The type Bluz\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
use Bluz\Auth\AuthException;
0 ignored issues
show
The type Bluz\Auth\AuthException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Bluz\Auth\Model\AbstractTable;
0 ignored issues
show
The type Bluz\Auth\Model\AbstractTable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Bluz\Proxy\Auth;
0 ignored issues
show
The type Bluz\Proxy\Auth was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
/**
20
 * Auth Table
21
 *
22
 * @package  Application\Auth
23
 *
24
 * @method   static ?Row findRow($primaryKey)
25
 * @see      \Bluz\Db\Table::findRow()
26
 * @method   static ?Row findRowWhere($whereList)
27
 * @see      \Bluz\Db\Table::findRowWhere()
28
 */
29
class Table extends AbstractTable
30
{
31
    /**
32
     * Call Hash Function
33
     *
34
     * @param string $password
35
     *
36
     * @return string
37
     * @throws \Application\Exception
38
     */
39
    public static function hash($password): string
40
    {
41
        $hash = Auth::getInstance()->getOption('hash');
42
43
        if (!$hash || !is_callable($hash)) {
44
            throw new Exception('Hash function for `Auth` package is not callable');
45
        }
46
47
        // encrypt password with secret
48
        return $hash($password);
49
    }
50
51
    /**
52
     * Call Verify Function
53
     *
54
     * @param string $password
55
     * @param string $hash
56
     *
57
     * @return bool
58
     * @throws \Application\Exception
59
     */
60 4
    public static function verify($password, $hash): bool
61
    {
62 4
        $verify = Auth::getInstance()->getOption('verify');
63
64 4
        if (!$verify || !is_callable($verify)) {
65
            throw new Exception('Verify function for `Auth` package is not callable');
66
        }
67
68
        // verify password with hash
69 4
        return $verify($password, $hash);
70
    }
71
72
    /**
73
     * Can entity login
74
     *
75
     * @param $user
76
     *
77
     * @throws AuthException
78
     */
79 1
    public static function tryLogin($user): void
80
    {
81 1
        switch ($user->status) {
82
            case (Users\Table::STATUS_PENDING):
83
                throw new AuthException('Your account is pending activation', 403);
84
            case (Users\Table::STATUS_DISABLED):
85
                throw new AuthException('Your account is disabled by administrator', 403);
86
            case (Users\Table::STATUS_ACTIVE):
87
                // save user to new session
88 1
                Auth::setIdentity($user);
89 1
                break;
90
            default:
91
                throw new AuthException('User not found');
92
        }
93 1
    }
94
}
95