Passed
Push — 1.11.x ( 0cbedc...2beec7 )
by Angel Fernando Quiroz
08:41 queued 15s
created

WebService::isValidUser()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 2
dl 0
loc 17
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\UserBundle\Entity\User;
6
7
/**
8
 * Base class for Web Services.
9
 *
10
 * @author Angel Fernando Quiroz Campos <[email protected]>
11
 */
12
class WebService
13
{
14
    /**
15
     * @var User
16
     */
17
    protected $user;
18
19
    /**
20
     * @var string
21
     */
22
    protected $apiKey;
23
24
    /**
25
     * Class constructor.
26
     *
27
     * @param $username
28
     * @param $apiKey
29
     */
30
    protected function __construct($username, $apiKey)
31
    {
32
        /** @var User user */
33
        $this->user = UserManager::getManager()->findUserByUsername($username);
34
        $this->apiKey = $apiKey;
35
36
        $_user = [
37
            'user_id' => $this->user->getId(),
38
            'status' => $this->user->getStatus(),
39
            'uidReset' => true,
40
        ];
41
        ChamiloSession::write('_user', $_user);
42
        ChamiloSession::write('is_allowed_in_course', true);
43
44
        Login::init_user($this->user->getId(), true);
45
    }
46
47
    /**
48
     * @param string $username
49
     * @param string $apiKeyToValidate
50
     *
51
     * @return WebService
52
     */
53
    public static function validate($username, $apiKeyToValidate)
54
    {
55
        return new self($username, $apiKeyToValidate);
56
    }
57
58
    /**
59
     * Find the api key for a user. If the api key does not exists is created.
60
     *
61
     * @param string $username
62
     * @param string $serviceName
63
     *
64
     * @return string
65
     */
66
    public static function findUserApiKey($username, $serviceName)
67
    {
68
        $user = UserManager::getManager()->findUserByUsername($username);
69
        if ($user) {
70
            $apiKeys = UserManager::get_api_keys($user->getId(), $serviceName);
71
72
            if (empty($apiKeys)) {
73
                UserManager::add_api_key($user->getId(), $serviceName);
74
            }
75
76
            $apiKeys = UserManager::get_api_keys($user->getId(), $serviceName);
77
78
            return current($apiKeys);
79
        }
80
81
        return '';
82
    }
83
84
    /**
85
     * Check whether the username and password are valid.
86
     *
87
     * @param string $username
88
     * @param string $password
89
     *
90
     * @throws Exception
91
     *
92
     * @return bool Return true if the password belongs to the username. Otherwise return false
93
     */
94
    public static function isValidUser($username, $password)
95
    {
96
        if (empty($username) || empty($password)) {
97
            return false;
98
        }
99
100
        $user = UserManager::getManager()->findUserByUsername($username);
101
102
        if (!$user) {
103
            return false;
104
        }
105
106
        return UserManager::checkPassword(
107
            $user->getPassword(),
108
            $password,
109
            $user->getSalt(),
110
            $user->getId()
111
        );
112
    }
113
114
    /**
115
     * @return User
116
     */
117
    public function getUser()
118
    {
119
        return $this->user;
120
    }
121
122
    /**
123
     * @throws Exception
124
     */
125
    protected static function throwNotAllowedException()
126
    {
127
        throw new Exception(get_lang('NotAllowed'));
128
    }
129
130
    /**
131
     * @throws Exception
132
     */
133
    protected static function protectAdminEndpoint()
134
    {
135
        if (!api_is_platform_admin()) {
136
            self::throwNotAllowedException();
137
        }
138
    }
139
}
140