Passed
Push — main ( c1d479...a20d4a )
by Rafael
46:41
created

DolibarrAuth   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A login() 0 10 2
A checkLogin() 0 15 4
A setSession() 0 27 3
1
<?php
2
3
namespace DoliLib;
4
5
use DoliCore\Base\Config;
6
use DoliCore\Tools\Load;
7
8
class DolibarrAuth
9
{
10
    public static function login($username, $password, $entity = 1)
11
    {
12
        $conf = Config::getConfig();
13
        $mode = $conf->security->authentication_method ?? 'dolibarr';
14
        $authmode = explode(',', $mode);
15
        if (!self::checkLogin($authmode, $username, $password, $entity)) {
16
            return false;
17
        }
18
19
        return static::setSession($username);
20
    }
21
22
    private static function checkLogin($authmode, $user, $pass, $entity): bool
23
    {
24
        foreach ($authmode as $mode) {
25
            $method = realpath(BASE_PATH . '/../Dolibarr/Core/Login') . '/functions_' . $mode . '.php';
26
            if (!file_exists($method)) {
27
                continue;
28
            }
29
            $function = 'check_user_password_' . $mode;
30
            require_once $method;
31
            if ($function($user, $pass, $entity)) {
32
                return true;
33
            }
34
        }
35
36
        return false;
37
    }
38
39
    public static function setSession($username, $entitytotest = 1)
40
    {
41
42
        $user = Load::getUser();
43
        $result = $user->fetch('', $username, '', 1, ($entitytotest > 0 ? $entitytotest : -1)); // value for $login was retrieved previously when checking password.
44
        if ($result <= 0) {
45
            return false;
46
        }
47
48
        // Store value into session (values always stored)
49
        $_SESSION['dol_login'] = $user->login;
50
        /*
51
        $_SESSION["dol_logindate"] = dol_now('gmt');
52
        $_SESSION["dol_authmode"] = isset($dol_authmode) ? $dol_authmode : '';
53
        $_SESSION["dol_tz"] = isset($dol_tz) ? $dol_tz : '';
54
        $_SESSION["dol_tz_string"] = isset($dol_tz_string) ? $dol_tz_string : '';
55
        $_SESSION["dol_dst"] = isset($dol_dst) ? $dol_dst : '';
56
        $_SESSION["dol_dst_observed"] = isset($dol_dst_observed) ? $dol_dst_observed : '';
57
        $_SESSION["dol_dst_first"] = isset($dol_dst_first) ? $dol_dst_first : '';
58
        $_SESSION["dol_dst_second"] = isset($dol_dst_second) ? $dol_dst_second : '';
59
        $_SESSION["dol_screenwidth"] = isset($dol_screenwidth) ? $dol_screenwidth : '';
60
        $_SESSION["dol_screenheight"] = isset($dol_screenheight) ? $dol_screenheight : '';
61
        $_SESSION["dol_company"] = getDolGlobalString("MAIN_INFO_SOCIETE_NOM");
62
        $_SESSION["dol_entity"] = $conf->entity;
63
        */
64
65
        return true;
66
    }
67
}
68