Issues (50)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/User/UserController.php (23 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Sinergi\Users\User;
4
5
use Interop\Container\ContainerInterface;
6
use Sinergi\Users\Container;
7
use Sinergi\Users\Throttle\Throttle;
8
use Sinergi\Users\User\Exception\EmailAlreadyConfirmedException;
9
use Sinergi\Users\User\Exception\EmailConfirmationTokenBlockedException;
10
use Sinergi\Users\User\Exception\EmailConfirmationTokenExpiredException;
11
use Sinergi\Users\User\Exception\InvalidEmailConfirmationTokenException;
12
use Sinergi\Users\User\Exception\InvalidPasswordResetTokenException;
13
use Sinergi\Users\User\Exception\InvalidUserException;
14
use Sinergi\Users\User\Exception\PasswordResetTokenBlockedException;
15
use Sinergi\Users\User\Exception\PasswordResetTokenExpiredException;
16
use Sinergi\Users\User\Exception\TooManyEmailConfirmationTokenAttemptsException;
17
use DateInterval;
18
use Sinergi\Users\User\Exception\UserNotFoundException;
19
20
class UserController
21
{
22
    private $container;
23
24
    public function __construct(ContainerInterface $container)
25
    {
26
        if ($container instanceof Container) {
27
            $this->container = $container;
28
        } else {
29
            $this->container = new Container($container);
30
        }
31
    }
32
33
    public function generateEmailConfirmationToken(
34
        UserEntityInterface $user,
35
        string $token = null,
36
        DateInterval $expiration = null,
37
        bool $save = true
38
    ): UserEntityInterface {
39
        if ($user->isEmailConfirmed()) {
40
            throw new EmailAlreadyConfirmedException;
41
        } elseif ($user->getEmailConfirmationToken() && !$user->hasEmailConfirmationTokenCooldownExpired()) {
42
            throw new EmailConfirmationTokenBlockedException;
43
        }
44
45
        $user->generateEmailConfirmationToken($token, $expiration);
46
        if ($save) {
47
            /** @var UserRepositoryInterface $userRepository */
48
            $userRepository = $this->container->get(UserRepositoryInterface::class);
49
            $userRepository->save($user);
50
        }
51
        return $user;
52
    }
53
54
    public function generatePassswordResetToken(
55
        string $email,
56
        string $token = null,
57
        DateInterval $expiration = null,
58
        string $ip = null,
59
        bool $save = true
60
    ): UserEntityInterface {
61
        if ($ip) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ip of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
62
            Throttle::throttle($ip);
63
        }
64
        /** @var UserRepositoryInterface $userRepository */
65
        $userRepository = $this->container->get(UserRepositoryInterface::class);
66
        $user = $userRepository->findByEmail($email);
67
        if (!$user) {
68
            throw new UserNotFoundException;
69
        } elseif ($user->getPasswordResetToken() && !$user->hasPasswordResetTokenCooldownExpired()) {
70
            throw new PasswordResetTokenBlockedException;
71
        }
72
73
        $user->generatePasswordResetToken($token, $expiration);
74
        if ($save) {
75
            /** @var UserRepositoryInterface $userRepository */
76
            $userRepository = $this->container->get(UserRepositoryInterface::class);
77
            $userRepository->save($user);
78
        }
79
        return $user;
80
    }
81
82
    public function resetPassword(
83
        string $token,
84
        string $password,
85
        string $ip = null,
86
        bool $save = true
87
    ): UserEntityInterface {
88
        if ($ip) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ip of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
89
            Throttle::throttle($ip);
90
        }
91
92
        /** @var UserRepositoryInterface $userRepository */
93
        $userRepository = $this->container->get(UserRepositoryInterface::class);
94
        $user = $userRepository->findByResetPasswordToken($token);
95
96
        if (!$user) {
97
            throw new InvalidPasswordResetTokenException;
98
        } elseif ($user->hasPasswordResetTokenExpired()) {
99
            throw new PasswordResetTokenExpiredException;
100
        }
101
102
        $user->setPassword($password);
103
104
        /** @var UserValidatorInterface $userValidator */
105
        $userValidator = $this->container->get(UserValidatorInterface::class);
106
        $errors = $userValidator($user);
107
108
        if (count($errors)) {
109
            throw new InvalidUserException($errors);
110
        }
111
112
        $user->setPasswordResetToken(null);
113
        $user->setPasswordResetTokenExpirationDatetime(null);
114
115
        if ($save) {
116
            $userRepository->save($user);
117
        }
118
119
        return $user;
120
    }
121
122
    public function confirmEmail(UserEntityInterface $user, string $emailConfirmationToken): UserEntityInterface
123
    {
124
        if ($user->isEmailConfirmed()) {
125
            throw new EmailAlreadyConfirmedException;
126
        } elseif ($user->hasEmailConfirmationTokenExpired()) {
127
            throw new EmailConfirmationTokenExpiredException;
128
        } elseif ($user->hasTooManyEmailConfirmationTokenAttempts()) {
129
            throw new TooManyEmailConfirmationTokenAttemptsException;
130
        }
131
132
        /** @var UserRepositoryInterface $userRepository */
133
        $userRepository = $this->container->get(UserRepositoryInterface::class);
134
135
        if ($user->getEmailConfirmationToken() !== $emailConfirmationToken) {
136
            $user->setEmailConfirmationTokenAttempts($user->getEmailConfirmationTokenAttempts() + 1);
137
            $userRepository->save($user);
138
            throw new InvalidEmailConfirmationTokenException;
139
        }
140
141
        $user->setIsEmailConfirmed(true);
142
        $user->setEmailConfirmationToken(null);
143
        $user->setEmailConfirmationTokenAttempts(0);
144
        $user->setEmailConfirmationTokenExpirationDatetime(null);
145
        $userRepository->save($user);
146
        return $user;
147
    }
148
149
    public function createUser(UserEntityInterface $user): UserEntityInterface
150
    {
151
        /** @var UserValidatorInterface $userValidator */
152
        $userValidator = $this->container->get(UserValidatorInterface::class);
153
        $errors = $userValidator($user);
154
155
        if (!empty($errors)) {
156
            throw new InvalidUserException($errors);
157
        }
158
159
        /** @var UserRepositoryInterface $userRepository */
160
        $userRepository = $this->container->get(UserRepositoryInterface::class);
161
        $userRepository->save($user);
162
163
        $this->getEmailConfirmationController()
0 ignored issues
show
The method getEmailConfirmationController() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
164
            ->sendConfirmationEmail($user);
165
166
        return $user;
167
    }
168
169
    /**
170
     * @param UserEntity $userEntity
171
     * @param array      $parameters
172
     */
173
    public function updateUser(UserEntity $userEntity, array $parameters)
174
    {
175
        $options = [UserValidator::OPTION_SKIP_PASSWORD_VALIDATION];
176
177
        //todo : refactored this :
178
179
        $parameters = array_merge($userEntity->toArray(), $parameters);
180
181
        if ($userEntity->getEmail() === $parameters['email']) {
182
            $options[] = UserValidator::OPTION_SKIP_EMAIL_UNIQUENESS;
183
        }
184
185
        $this->getUserValidator()->validateParameters($parameters, $options);
0 ignored issues
show
The method getUserValidator() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
186
187
        if ($userEntity->getEmail() !== $parameters['email']) {
188
            $userEntity->setPendingEmail($parameters['email']);
189
190
            (new EmailConfirmationController($this->getContainer()))->emailUpdated($userEntity);
0 ignored issues
show
The method getContainer() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
191
        }
192
193
        $userEntity
194
            ->setName($parameters['name'])
195
            ->setPhone($parameters['phone']);
196
197
        $this->getEntityManager()->persist($userEntity);
0 ignored issues
show
The method getEntityManager() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
198
        $this->getEntityManager()->flush($userEntity);
0 ignored issues
show
The method getEntityManager() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
199
    }
200
201
    /**
202
     * @param userEntity $userEntity
203
     * @param array      $parameters
204
     * @throws AuthenticationException
205
     */
206
    public function updatePassword(userEntity $userEntity, array $parameters)
207
    {
208
        if (!$userEntity->testPassword($parameters['current-password'])) {
209
            throw new AuthenticationException($this->getDictionary()->get('user.password.error.wrong_password'));
0 ignored issues
show
The method getDictionary() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
210
        }
211
212
        $this->getUserValidator()->validateParameters($parameters,
0 ignored issues
show
The method getUserValidator() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
213
            [UserValidator::OPTION_JUST_PASSWORD]);
214
215
        $userEntity->setPassword($parameters['password']);
216
        $this->getEntityManager()->persist($userEntity);
0 ignored issues
show
The method getEntityManager() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
217
        $this->getEntityManager()->flush($userEntity);
0 ignored issues
show
The method getEntityManager() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
218
    }
219
220
    /**
221
     * @param userEntity $userEntity
222
     */
223 View Code Duplication
    public function deleteUser(userEntity $userEntity)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
    {
225
        $userEntity->setStatus(UserEntity::STATUS_DELETED);
226
        $userEntity->setDeletedEmail($userEntity->getEmail());
227
        $userEntity->setEmail(null);
228
        $this->getContainer()->getEntityManager()->persist($userEntity);
0 ignored issues
show
The method getContainer() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
229
        $this->getContainer()->getEntityManager()->flush($userEntity);
0 ignored issues
show
The method getContainer() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
230
    }
231
232
    /**
233
     * @param userEntity $userEntity
234
     */
235 View Code Duplication
    public function banUser(userEntity $userEntity)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
236
    {
237
        $userEntity->setStatus(UserEntity::STATUS_BANNED);
238
        $userEntity->setDeletedEmail($userEntity->getEmail());
239
        $userEntity->setEmail(null);
240
        $this->getContainer()->getEntityManager()->persist($userEntity);
0 ignored issues
show
The method getContainer() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
241
        $this->getContainer()->getEntityManager()->flush($userEntity);
0 ignored issues
show
The method getContainer() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
242
    }
243
244
    /**
245
     * @param $status
246
     *
247
     * @return mixed
248
     */
249
    public function getUserStatusLabel($status)
250
    {
251
        switch ($status) {
252
            case UserEntity::STATUS_ACTIVE:
253
                return $this->getDictionary()->get('user.status.active');
0 ignored issues
show
The method getDictionary() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
254
            case UserEntity::STATUS_DELETED:
255
                return $this->getDictionary()->get('user.status.deleted');
0 ignored issues
show
The method getDictionary() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
256
                break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
257
            case UserEntity::STATUS_BANNED:
258
                return $this->getDictionary()->get('user.status.banned');
0 ignored issues
show
The method getDictionary() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
259
                break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
260
            default:
261
                return $this->getDictionary()->get('user.status.inactive');
0 ignored issues
show
The method getDictionary() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
262
        }
263
    }
264
}
265