GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UserController   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 25
c 5
b 0
f 0
lcom 1
cbo 0
dl 0
loc 186
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A onDispatch() 0 11 2
B indexAction() 0 24 2
A deleteAction() 0 17 2
B editAction() 0 30 3
C addAction() 0 44 7
A isUsernameValid() 0 10 4
A isPasswordValid() 0 3 1
A getHtpasswdService() 0 8 2
A getUserService() 0 8 2
1
<?php
2
3
/**
4
 *
5
 * @copyright Steven Bühner
6
 * @license MIT
7
 */
8
namespace HtpasswdManager\Controller;
9
10
use Zend\Mvc\Controller\AbstractActionController;
11
use Zend\View\Model\ViewModel;
12
13
class UserController extends AbstractActionController {
14
    private $htpasswdService = NULL;
15
    private $userService = NULL;
16
17
    /**
18
     * Check Login
19
     *
20
     * @see \Zend\Mvc\Controller\AbstractActionController::onDispatch()
21
     */
22
    public function onDispatch(\Zend\Mvc\MvcEvent $e) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
23
        $username = $this->getUserService()->getCurrentUser();
24
25
        if (false === $this->getUserService()->isUserAllowedToManageUsers($username)) {
26
            $this->getResponse()->setStatusCode(401);
27
28
            return;
29
        }
30
31
        return parent::onDispatch($e);
32
    }
33
34
    public function indexAction() {
35
        $htpasswdService = $this->getHtpasswdService();
36
        $userList        = $htpasswdService->getUserList();
37
38
        $result = array();
39
        foreach ($userList as $username => $pass) {
40
41
            $result [] = array(
42
                'username'    => $username,
43
                'paswd'       => $pass,
44
                'isAdmin'     => $this->getUserService()->isUserAllowedToManageUsers($username),
45
                'isDeletable' => $this->getUserService()->isUserDeleteable($username)
46
            );
47
        }
48
49
        $userService = $this->getServiceLocator()->get('HtpasswdManager\Service\UserService');
50
51
        $model = new ViewModel (array(
52
            'userList'    => $result,
53
            'currentUser' => $userService->getCurrentUser()
54
        ));
55
56
        return $model;
57
    }
58
59
    public function deleteAction() {
60
        $user     = $this->params('user');
61
        $messages = array();
62
63
        if (true === $this->getUserService()->isUserDeleteable($user)) {
64
            $this->getHtpasswdService()->deleteUser($user);
65
            $messages [] = "Der Benutzer '{$user}' wurde gelöscht.";
66
        } else {
67
            $messages [] = "Der Benutzer '{$user}' steht auf einer Sperrliste und kann nicht gelöscht werden.";
68
        }
69
70
        $model = $this->indexAction();
71
        $model->setTemplate('htpasswd-manager/user/index');
72
        $model->setVariable('messages', $messages);
73
74
        return $model;
75
    }
76
77
    public function editAction() {
78
        $user     = $this->params('user');
79
        $messages = array();
80
        $htpasswd = $this->getHtpasswdService();
81
82
        $model = new ViewModel ();
83
        $model->setVariable('user', $user);
84
85
        $post = $this->getRequest()->getPost();
86
        if (!isset ($post ['password'])) {
87
            // Formular initialisieren
88
            if (false === $htpasswd->userExists($user)) {
89
                $this->redirect()->toRoute('htpasswdmanager', array(
90
                    'action' => 'index'
91
                ));
92
            }
93
        } else {
94
            // Formular speichern
95
            $password = $post ['password'];
96
            $htpasswd->updateUser($user, $password);
97
            $messages [] = "Passwort für '{$user}' wurde aktualisiert.";
98
99
            $model = $this->indexAction();
100
            $model->setTemplate('htpasswd-manager/user/index');
101
        }
102
103
        $model->setVariable('messages', $messages);
104
105
        return $model;
106
    }
107
108
    public function addAction() {
109
        $username = $this->getRequest()->getPost('username', '');
110
        $password = $this->getRequest()->getPost('password', '');
111
        $messages = array();
112
        $model    = new ViewModel ();
113
114
        $post = $this->getRequest()->getPost();
115
        if (!isset ($post ['username'])) {
116
            return new ViewModel (array(
117
                'username' => $username,
118
                'password' => $password,
119
                'messages' => $messages
120
            ));
121
        }
122
123
        $model->setVariable('username', $username);
124
        $uValid = $this->isUsernameValid($username);
125
        if (true !== $uValid) {
126
            $messages [] = $uValid;
127
        }
128
129
        $model->setVariable('password', $password);
130
        $pValid = $this->isPasswordValid($password);
131
        if (true !== $pValid) {
132
            $messages [] = $pValid;
133
        }
134
135
        if (true === $uValid && true === $pValid) {
136
            if (true === $this->getHtpasswdService()->userExists($username)) {
137
                $this->getHtpasswdService()->updateUser($username, $password);
138
                $messages [] = "Benutzer '{$username}' wurde aktualisiert.";
139
            } else {
140
                $this->getHtpasswdService()->addUser($username, $password);
141
                $messages [] = "Benutzer '{$username}' wurde hinzugefügt.";
142
            }
143
144
            $model = $this->indexAction();
145
            $model->setTemplate('htpasswd-manager/user/index');
146
        }
147
148
        $model->setVariable('messages', $messages);
149
150
        return $model;
151
    }
152
153
    /**
154
     * Returns true if valid, of not it returns a string with information about the reason
155
     *
156
     * @param string $username
157
     * @return boolean string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
158
     */
159
    private function isUsernameValid($username) {
160
        if (strlen($username) <= 2)
161
            return "Benutzername ist zu kurz.";
162
        else if (preg_match('~[a-zäöo][a-zäöu_0-9-]+~i', $username) !== 1)
163
            return "Benutzername enthält ungültige Zeichen";
164
        else if (strpos($username, ' ') !== false)
165
            return "Leerzeichen sind im Benutzernamen nicht erlaubt";
166
167
        return true;
168
    }
169
170
    /**
171
     * Returns true if valid, of not it returns a string with information about the reason
172
     *
173
     * @param string $password
174
     * @return boolean string
175
     */
176
    private function isPasswordValid($password) {
177
        return true;
178
    }
179
180
    private function getHtpasswdService() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
181
        if ($this->htpasswdService === NULL) {
182
            $sl                    = $this->getServiceLocator();
183
            $this->htpasswdService = $sl->get('HtpasswdManager\Service\HtpasswdService');
184
        }
185
186
        return $this->htpasswdService;
187
    }
188
189
    private function getUserService() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
190
        if ($this->userService === NULL) {
191
            $sl                = $this->getServiceLocator();
192
            $this->userService = $sl->get('HtpasswdManager\Service\UserService');
193
        }
194
195
        return $this->userService;
196
    }
197
198
}
199