Issues (21)

src/Controller/UserController.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace App\Controller;
4
5
use App\Facades\Router;
6
use App\Facades\Security;
7
use App\Facades\Session;
8
use App\Facades\View;
9
use App\Model\User;
10
use Exception;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use RuntimeException;
14
15
/**
16
 * Controller for user related actions
17
 *
18
 * @author Ronan Chilvers <[email protected]>
19
 */
20
class UserController
21
{
22
    /**
23
     * Login action for users
24
     *
25
     * @author Ronan Chilvers <[email protected]>
26
     */
27
    public function login(
28
        ServerRequestInterface $request,
29
        ResponseInterface $response
30
    ) {
31
        $user = new User();
32
        if ('POST' == $request->getMethod()) {
33
            try {
34
                $data = $request->getParsedBody();
35
                if (!isset($data['email'], $data['password'])) {
36
                    throw new RuntimeException('Email / password are required');
37
                }
38
                $user = Security::login(
39
                    $data['email'],
40
                    $data['password']
41
                );
42
                if ($user instanceof User) {
43
                    return $response->withRedirect(
0 ignored issues
show
The method withRedirect() does not exist on Psr\Http\Message\ResponseInterface. It seems like you code against a sub-type of Psr\Http\Message\ResponseInterface such as Slim\Http\Response. ( Ignorable by Annotation )

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

43
                    return $response->/** @scrutinizer ignore-call */ withRedirect(
Loading history...
44
                        Router::pathFor('project.index')
45
                    );
46
                }
47
                throw new RuntimeException('Unable to log you in');
48
            } catch (Exception $ex) {
49
                Session::flash(
50
                    [
51
                        'heading' => $ex->getMessage(),
52
                    ],
53
                    'error'
54
                );
55
                return $response->withRedirect(
56
                    Router::pathFor('user.login')
57
                );
58
            }
59
        }
60
61
        return View::render(
62
            $response,
63
            'user/login.html.twig',
64
            [
65
                'user' => $user,
66
            ]
67
        );
68
    }
69
70
    /**
71
     * Logout action
72
     *
73
     * @author Ronan Chilvers <[email protected]>
74
     */
75
    public function logout(
76
        ServerRequestInterface $request,
77
        ResponseInterface $response
78
    ) {
79
        Security::logout();
80
81
        return $response->withRedirect(
82
            Router::pathFor('user.login')
83
        );
84
    }
85
86
    /**
87
     * Save the favourite deployments for a user
88
     *
89
     * @author Ronan Chilvers <[email protected]>
90
     */
91
    public function favourite(
92
        ServerRequestInterface $request,
93
        ResponseInterface $response,
94
        $args
95
    ) {
96
        $error    = false;
97
        $selected = false;
98
        $user     = Security::user();
99
        $project  = $args['project'];
100
        if (0 < $project) {
101
            $favourites = $user->preference('favourites', []);
102
            switch (isset($favourites[$project])) {
103
104
                // Not in favourites
105
                case false:
106
                    $favourites[$project] = $project;
107
                    $selected = true;
108
                    break;
109
110
                // In favourites
111
                default:
112
                    unset($favourites[$project]);
113
                    $selected = false;
114
                    break;
115
116
            }
117
            $favourites = array_filter($favourites);
118
            if (!$user->setPreference('favourites', $favourites)) {
119
                $error = true;
120
            }
121
        }
122
        $json = [
123
            'result' => ($error) ? 'error' : 'ok',
124
            'data' => [
125
                'project'    => $project,
126
                'selected'   => $selected,
127
                'favourites' => $user->preference('favourites', []),
128
            ]
129
        ];
130
131
        return $response->withJson($json);
0 ignored issues
show
The method withJson() does not exist on Psr\Http\Message\ResponseInterface. It seems like you code against a sub-type of Psr\Http\Message\ResponseInterface such as Slim\Http\Response. ( Ignorable by Annotation )

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

131
        return $response->/** @scrutinizer ignore-call */ withJson($json);
Loading history...
132
    }
133
134
    /**
135
     * User profile page
136
     *
137
     * @author Ronan Chilvers <[email protected]>
138
     */
139
    public function profile(
140
        ServerRequestInterface $request,
141
        ResponseInterface $response
142
    ) {
143
        $user = Security::user();
144
145
        if ('POST' == $request->getMethod()) {
146
            $data = $request->getParsedBody()['user'];
147
            $user->fromArray($data);
148
            if ($user->saveWithValidation()) {
149
                Security::refresh($user);
150
                Session::flash([
151
                    'heading' => 'Profile saved'
152
                ]);
153
                return $response->withRedirect(
154
                    Router::pathFor('user.profile')
155
                );
156
            }
157
        }
158
159
        return View::render(
160
            $response,
161
            'user/profile.html.twig',
162
            [
163
                'title'         => 'Profile',
164
                'current_route' => 'user.profile',
165
                'user'          => $user,
166
            ]
167
        );
168
    }
169
170
    /**
171
     * Security action for user passwords, etc
172
     *
173
     * @author Ronan Chilvers <[email protected]>
174
     */
175
    public function security(
176
        ServerRequestInterface $request,
177
        ResponseInterface $response
178
    ) {
179
        $user = Security::user();
180
181
        try {
182
            if ('POST' == $request->getMethod()) {
183
                $data = $request->getParsedBody()['user'];
184
                if (!$user->setNewPassword($data['password'], $data['password_new'], $data['password_confirm'])) {
185
                    throw new RuntimeException('Invalid input');
186
                }
187
                if (!$user->saveWithValidation('password')) {
188
                    throw new RuntimeException('Unable to save new password');
189
                }
190
                Session::flash([
191
                    'heading' => 'Profile saved'
192
                ]);
193
                return $response->withRedirect(
194
                    Router::pathFor('user.security')
195
                );
196
            }
197
        } catch (RuntimeException $ex) {
198
            Session::flash([
199
                    'heading' => 'Save failed',
200
                    'content' => $ex->getMessage()
201
                ],
202
                'error'
203
            );
204
        }
205
206
        return View::render(
207
            $response,
208
            'user/security.html.twig',
209
            [
210
                'title'         => 'Security',
211
                'current_route' => 'user.security',
212
                'user'          => $user,
213
            ]
214
        );
215
    }
216
}
217