AddUser::addNotices()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Add user page controller class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Controllers
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Controllers;
10
11
use InvalidArgumentException;
12
use EBloodBank as EBB;
13
use EBloodBank\Notices;
14
use EBloodBank\Models\User;
15
use Symfony\Component\EventDispatcher\GenericEvent;
16
17
/**
18
 * Add user page controller class
19
 *
20
 * @since 1.0
21
 */
22
class AddUser extends Controller
23
{
24
    /**
25
     * @var \EBloodBank\Models\User
26
     * @since 1.0
27
     */
28
    protected $user;
29
30
    /**
31
     * @return void
32
     * @since 1.0
33
     */
34
    public function __invoke()
35
    {
36
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'User', 'add')) {
37
            $this->viewFactory->displayView('error-403');
38
            return; 
39
        }
40
41
        $this->user = new User();
42
43
        $this->doActions();
44
        $this->addNotices();
45
        $this->viewFactory->displayView(
46
            'add-user',
47
            [
48
                'user' => $this->user,
49
            ]
50
        );
51
    }
52
53
    /**
54
     * @return void
55
     * @since 1.0
56
     */
57
    protected function doActions()
58
    {
59
        switch (filter_input(INPUT_POST, 'action')) {
60
            case 'submit_user':
61
                $this->doSubmitAction();
62
                break;
63
        }
64
    }
65
66
    /**
67
     * @return void
68
     * @since 1.0
69
     */
70
    protected function addNotices()
71
    {
72
        if (filter_has_var(INPUT_GET, 'flag-added')) {
73
            Notices::addNotice('added', __('User added.'), 'success');
74
        }
75
    }
76
77
    /**
78
     * @return void
79
     * @since 1.0
80
     */
81
    protected function doSubmitAction()
82
    {
83
        try {
84
            if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'User', 'add')) {
85
                return;
86
            }
87
88
            $sessionToken = $this->getSession()->getCsrfToken();
89
            $actionToken = filter_input(INPUT_POST, 'token');
90
91
            if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
92
                return;
93
            }
94
95
            $user = $this->user;
96
97
            // Set the user name.
98
            $user->set('name', filter_input(INPUT_POST, 'user_name'), true);
99
100
            // Set the user email.
101
            $user->set('email', filter_input(INPUT_POST, 'user_email'), true);
102
103
            $duplicateUser = $this->getUserRepository()->findOneBy(['email' => $user->get('email'), 'status' => 'any']);
104
105
            if (! empty($duplicateUser)) {
106
                throw new InvalidArgumentException(__('Please enter a unique user e-mail.'));
107
            }
108
109
            $userPass1 = filter_input(INPUT_POST, 'user_pass_1', FILTER_UNSAFE_RAW);
110
            $userPass2 = filter_input(INPUT_POST, 'user_pass_2', FILTER_UNSAFE_RAW);
111
112
            if (empty($userPass1)) {
113
                throw new InvalidArgumentException(__('Please enter the password.'));
114
            }
115
116
            if (empty($userPass2)) {
117
                throw new InvalidArgumentException(__('Please confirm the password.'));
118
            }
119
120
            if ($userPass1 !== $userPass2) {
121
                throw new InvalidArgumentException(__('Please enter the same password.'));
122
            }
123
124
            // Set the user password.
125
            $user->set('pass', password_hash($userPass1, PASSWORD_BCRYPT), false);
126
127
            // Set the user role.
128
            $user->set('role', filter_input(INPUT_POST, 'user_role'), true);
129
130
            // Set the user status.
131
            if ($this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'User', 'activate')) {
132
                $user->set('status', 'activated');
133
            } else {
134
                $user->set('status', Options::getOption('new_user_status'), true);
0 ignored issues
show
Bug introduced by
The type EBloodBank\Controllers\Options 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...
135
            }
136
137
            $this->getEntityManager()->persist($user);
138
            $this->getEntityManager()->flush();
139
140
            $this->getEventManager()->getEventDispatcher()->dispatch('user.created', new GenericEvent($user));
141
142
            $added = $user->isExists();
143
144
            EBB\redirect(
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
            /** @scrutinizer ignore-call */ 
145
            EBB\redirect(
Loading history...
145
                EBB\addQueryArgs(
0 ignored issues
show
Bug introduced by
The function addQueryArgs was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
                /** @scrutinizer ignore-call */ 
146
                EBB\addQueryArgs(
Loading history...
146
                    EBB\getAddUserURL(),
0 ignored issues
show
Bug introduced by
The function getAddUserURL was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
                    /** @scrutinizer ignore-call */ 
147
                    EBB\getAddUserURL(),
Loading history...
147
                    ['flag-added' => $added]
148
                )
149
            );
150
        } catch (InvalidArgumentException $ex) {
151
            Notices::addNotice('invalid_user_argument', $ex->getMessage());
152
        }
153
    }
154
}
155