Passed
Push — Auth ( 474bc2...620889 )
by Stone
02:11
created

Home::viewUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 20
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers\Admin;
4
5
6
use App\Models\RoleModel;
7
use App\Models\UserModel;
8
use Core\Container;
9
use Core\Traits\PasswordFunctions;
10
use Core\Traits\StringFunctions;
11
12
class Home extends \Core\AdminController
13
{
14
    use StringFunctions;
0 ignored issues
show
Bug introduced by
The trait Core\Traits\StringFunctions requires the property $childNodes which is not provided by App\Controllers\Admin\Home.
Loading history...
15
    use PasswordFunctions;
16
    protected $siteConfig;
17
18
    private $userModel;
19
    private $roleModel;
20
21
    public function __construct(Container $container)
22
    {
23
        $this->loadModules[] = 'SiteConfig';
24
        parent::__construct($container);
25
        $this->userModel = new UserModel($this->container);
26
        $this->roleModel = new RoleModel($this->container);
27
28
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
29
    }
30
31
    /**
32
     * The front page of the admin section. We display the user info
33
     * @throws \ReflectionException
34
     * @throws \Twig_Error_Loader
35
     * @throws \Twig_Error_Runtime
36
     * @throws \Twig_Error_Syntax
37
     */
38
    public function index()
39
    {
40
        $this->onlyUser();
41
42
        //check if have prefilled form data and error mesages
43
        $this->data["registrationInfo"] = $this->session->get("registrationInfo");
44
        $this->data["registrationErrors"] = $this->session->get("registrationErrors");
45
46
        //remove the set data as it is now sent to the template
47
        $this->session->remove("registrationInfo");
48
        $this->session->remove("registrationErrors");
49
50
        $userId = $this->session->get("userId");
51
        $this->data["user"] = $this->userModel->getUserDetailsById($userId);
52
53
        $this->data["roles"] = $this->roleModel->getRoleList();
54
55
        $this->renderView('Admin/Home');
56
    }
57
58
    /**
59
     * Administrate a user as an admin
60
     * @param int $userId
61
     * @throws \ReflectionException
62
     * @throws \Twig_Error_Loader
63
     * @throws \Twig_Error_Runtime
64
     * @throws \Twig_Error_Syntax
65
     */
66
    public function viewUser(int $userId)
67
    {
68
        $this->onlyAdmin();
69
        if (!$this->isInt($userId)) {
70
            throw new \Exception("Error in passed ID");
71
        }
72
73
        //check if have prefilled form data and error mesages
74
        $this->data["registrationInfo"] = $this->session->get("registrationInfo");
75
        $this->data["registrationErrors"] = $this->session->get("registrationErrors");
76
77
        //remove the set data as it is now sent to the template
78
        $this->session->remove("registrationInfo");
79
        $this->session->remove("registrationErrors");
80
81
        $this->data["user"] = $this->userModel->getUserDetailsById($userId);
82
83
        $this->data["roles"] = $this->roleModel->getRoleList();
84
85
        $this->renderView('Admin/Home');
86
    }
87
88
    /**
89
     * Update the user info via post
90
     */
91
    public function updateUser()
92
    {
93
        $this->onlyUser();
94
        $this->onlyPost();
95
96
        $user = (object)$this->request->getDataFull();
97
        $redirectUrl = "/admin";
98
99
        if ($user->userId !== $this->session->get("userId") || isset($user->userRoleSelector)) {
100
            //an admin is trying to update a user or form tampered with
101
            $this->onlyAdmin();
102
            $redirectUrl = "/admin/home/view-user/" . $user->userId;
103
        } else {
104
            //set the role to the original state for update
105
            $beforeUser = $this->userModel->getUserDetailsById($user->userId);
106
            $user->userRoleSelector = $beforeUser->roles_idroles;
107
        }
108
109
        $userId = $user->userId;
110
        $password = $user->forgotPassword ?? "";
111
        $confirm = $user->forgotConfirm ?? "";
112
        $resetPassword = false;
113
        $error = false;
114
        $registerErrors = new \stdClass();
115
116
        if ($password !== "" || $confirm !== "") {
117
            //we are resetting the password
118
            $resetPassword = true;
119
            if ($password !== $confirm) {
120
                $error = true;
121
                $registerErrors->forgotPassword = "password and confirmation do not match";
122
                $registerErrors->forgotConfirm = "password and confirmation do not match";
123
            }
124
125
            $passwordError = $this->isPasswordComplex($password);
126
            if (!$passwordError["success"]) {
127
                $error = true;
128
                $registerErrors->forgotPassword = $passwordError["message"];
129
            }
130
        }
131
132
        if ($user->userName == "") {
133
            $error = true;
134
            $registerErrors->userName = "name must not be empty";
135
        }
136
        if ($user->userSurname == "") {
137
            $error = true;
138
            $registerErrors->userSurname = "surname must not be empty";
139
        }
140
        if ($user->userUsername == "") {
141
            $error = true;
142
            $registerErrors->userUsername = "username must not be empty";
143
        }
144
145
        if ($error) {
146
            $this->session->set("registrationErrors", $registerErrors);
147
            $this->response->redirect($redirectUrl);
148
        }
149
150
        if ($resetPassword) {
151
            $this->userModel->resetPassword($userId, $password);
152
        }
153
154
        $this->userModel->updateUser($user);
155
156
        $this->alertBox->setAlert('User details updated');
157
        $this->response->redirect($redirectUrl);
158
    }
159
}