UsersController.php 8.37 KB
Newer Older
1
2
3
4
5
6
<?php


namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
Jacob Priddy's avatar
Jacob Priddy committed
7
use Source\Authorization\Permissions;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Source\Exceptions\DeleteFailedException;
Jacob Priddy's avatar
Jacob Priddy committed
9
use Source\UseCases\Users\GetUser\GetUserUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
10
use Source\UseCases\Users\CreateUser\CreateUserUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
11
use Source\UseCases\Users\DeleteUser\DeleteUserUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
12
use Source\UseCases\Users\UpdateUser\UpdateUserUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
13
use Source\UseCases\Users\GetAllUsers\GetAllUsersUseCase;
14
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
15
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
16
17
use Source\UseCases\Users\GetUser\APIPresenter as GetUserAPIPresenter;
use Source\UseCases\Users\GetAllUsers\APIPresenter as AllUsersAPIPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
18
use Source\UseCases\Users\CreateUser\APIPresenter as CreateUserAPIPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
19
20
use Source\UseCases\Users\DeleteUser\APIPresenter as DeleteUserAPIPresenter;
use Source\UseCases\Users\UpdateUser\APIPresenter as UpdateUserAPIPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
21
use Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCase;
22
use Source\UseCases\GroupUser\GetUserGroups\APIPresenter as GetUserGroupsAPIPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
23
24
use Source\UseCases\GroupUser\AddUserToGroup\APIPresenter as AddUserToGroupAPIPresenter;
use Source\UseCases\GroupUser\RemoveUserFromGroup\APIPresenter as RemoveUserFromGroupAPIPresenter;
25

Jacob Priddy's avatar
Jacob Priddy committed
26
27
class UsersController extends ApiController
{
Jacob Priddy's avatar
Jacob Priddy committed
28
    /**
Jacob Priddy's avatar
Jacob Priddy committed
29
30
31
32
     * @param \Source\UseCases\Users\GetAllUsers\GetAllUsersUseCase $getAllUsers
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
Jacob Priddy's avatar
Jacob Priddy committed
33
     */
Jacob Priddy's avatar
Jacob Priddy committed
34
35
    public function index(GetAllUsersUseCase $getAllUsers): JsonResponse
    {
Jacob Priddy's avatar
Jacob Priddy committed
36
37
        $this->authorizer->protectAll([Permissions::MANAGE_USERS]);

Jacob Priddy's avatar
Jacob Priddy committed
38
        $presenter = new AllUsersAPIPresenter();
Jacob Priddy's avatar
Jacob Priddy committed
39
40
41
42

        $getAllUsers->all($presenter);

        return $this->respondWithData($presenter->getViewModel());
43
44
    }

Jacob Priddy's avatar
Jacob Priddy committed
45
    /**
Jacob Priddy's avatar
Jacob Priddy committed
46
47
48
49
50
     * @param \Source\UseCases\Users\GetUser\GetUserUseCase $getUser
     * @param string                                        $userId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\EntityNotFoundException
     * @throws \Source\Exceptions\AuthorizationException
Jacob Priddy's avatar
Jacob Priddy committed
51
     */
Jacob Priddy's avatar
Jacob Priddy committed
52
53
    public function get(GetUserUseCase $getUser, string $userId): JsonResponse
    {
Jacob Priddy's avatar
Jacob Priddy committed
54
55
        $this->authorizer->protectAll([Permissions::MANAGE_USERS]);

Jacob Priddy's avatar
Jacob Priddy committed
56
57
58
59
60
        $presenter = new GetUserAPIPresenter();

        $getUser->get($userId, $presenter);

        return $this->respondWithData($presenter->getViewModel());
61
62
    }

Jacob Priddy's avatar
Jacob Priddy committed
63
    /**
Jacob Priddy's avatar
Jacob Priddy committed
64
65
66
67
68
69
     * @param \Source\UseCases\Users\CreateUser\CreateUserUseCase $createUser
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityExistsException
     * @throws \Source\Exceptions\EntityNotFoundException
Jacob Priddy's avatar
Jacob Priddy committed
70
     */
Jacob Priddy's avatar
Jacob Priddy committed
71
72
    public function store(CreateUserUseCase $createUser): JsonResponse
    {
Jacob Priddy's avatar
Jacob Priddy committed
73
74
        $this->authorizer->protectAll([Permissions::MANAGE_USERS]);

Jacob Priddy's avatar
Jacob Priddy committed
75
76
77
78
79
80
81
82
83
84
        $this->validate($this->request, [
            'first_name' => 'required|string|max:255',
            'last_name' => 'required|string|max:255',
            'display_name' => 'required|string|max:255',
            'emplid' => 'nullable|string|max:7|min:6',
            'email' => 'required|email|max:255',
            'password' => 'nullable|string|min:15|max:255',
            'doorcode' => 'required|string|numeric|digits_between:4,255',
            'expires_at' => 'nullable|string|date|max:255',
        ]);
Jacob Priddy's avatar
Jacob Priddy committed
85
86
87

        $presenter = new CreateUserAPIPresenter();

Jacob Priddy's avatar
Jacob Priddy committed
88
89
90
91
92
        $attributes = $this->request->all();

        $attributes['salt'] = config('app.key');

        $createUser->create($attributes, $presenter);
Jacob Priddy's avatar
Jacob Priddy committed
93
94

        return $this->respondWithData($presenter->getViewModel());
95
96
    }

Jacob Priddy's avatar
Jacob Priddy committed
97
    /**
Jacob Priddy's avatar
Jacob Priddy committed
98
99
100
101
102
103
     * @param \Source\UseCases\Users\UpdateUser\UpdateUserUseCase $updateUser
     * @param string                                              $userId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\EntityNotFoundException
     * @throws \Source\Exceptions\AuthorizationException
Jacob Priddy's avatar
Jacob Priddy committed
104
     */
Jacob Priddy's avatar
Jacob Priddy committed
105
106
    public function update(UpdateUserUseCase $updateUser, string $userId): JsonResponse
    {
Jacob Priddy's avatar
Jacob Priddy committed
107
108
        $this->authorizer->protectAll([Permissions::MANAGE_USERS]);

Jacob Priddy's avatar
Jacob Priddy committed
109
        $this->validate($this->request, [
110
111
112
            'first_name' => 'string|max:255',
            'last_name' => 'string|max:255',
            'display_name' => 'string|max:255',
Jacob Priddy's avatar
Jacob Priddy committed
113
            'emplid' => 'nullable|string|max:7|min:6',
114
            'email' => 'email|max:255',
Jacob Priddy's avatar
Jacob Priddy committed
115
116
117
118
            'password' => 'nullable|string|max:255',
            'doorcode' => 'nullable|string|numeric|digits_between:4,255',
            'expires_at' => 'nullable|string|date|max:255',
        ]);
Jacob Priddy's avatar
Jacob Priddy committed
119
120
121

        $presenter = new UpdateUserAPIPresenter();

Jacob Priddy's avatar
Jacob Priddy committed
122
123
124
125
126
        $attributes = $this->request->all();

        $attributes['salt'] = config('app.key');

        $updateUser->update($userId, $attributes, $presenter);
Jacob Priddy's avatar
Jacob Priddy committed
127

Jacob Priddy's avatar
Jacob Priddy committed
128
129
130
131
        if ($presenter->hasError()) {
            return $this->respondWithError($presenter->getViewModel()['message']);
        }

Jacob Priddy's avatar
Jacob Priddy committed
132
        return $this->respondWithData($presenter->getViewModel());
133
134
    }

Jacob Priddy's avatar
Jacob Priddy committed
135
    /**
Jacob Priddy's avatar
Jacob Priddy committed
136
137
138
139
140
     * @param \Source\UseCases\Users\DeleteUser\DeleteUserUseCase $deleteUser
     * @param string                                              $userId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
Jacob Priddy's avatar
Jacob Priddy committed
141
     */
Jacob Priddy's avatar
Jacob Priddy committed
142
143
    public function delete(DeleteUserUseCase $deleteUser, string $userId): JsonResponse
    {
Jacob Priddy's avatar
Jacob Priddy committed
144
145
        $this->authorizer->protectAll([Permissions::MANAGE_USERS]);

Jacob Priddy's avatar
Jacob Priddy committed
146
147
148
149
150
        $presenter = new DeleteUserAPIPresenter();

        try {
            $deleteUser->delete($userId, $presenter);
        } catch (DeleteFailedException $e) {
Jacob Priddy's avatar
Jacob Priddy committed
151
            $this->setStatusCode(400);
Jacob Priddy's avatar
Jacob Priddy committed
152
            return $this->respondWithError($e->getMessage());
Jacob Priddy's avatar
Jacob Priddy committed
153
154
155
        }

        return $this->respondWithData($presenter->getViewModel());
156
    }
Jacob Priddy's avatar
Jacob Priddy committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

    /**
     * @param \Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCase $useCase
     * @param string                                                          $userId
     * @param string                                                          $groupId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function addUserToGroup(AddUserToGroupUseCase $useCase, string $userId, string $groupId): JsonResponse
    {
        $this->authorizer->protectAll([Permissions::MANAGE_USERS]);

        $presenter = new AddUserToGroupAPIPresenter();

        $useCase->addUserToGroup($userId, $groupId, $presenter);

        return $this->respondWithMessage($presenter->getViewModel()['message']);
    }

    /**
     * @param \Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCase $useCase
     * @param string                                                                    $userId
     * @param string                                                                    $groupId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function removeUserFromGroup(RemoveUserFromGroupUseCase $useCase, string $userId, string $groupId): JsonResponse
    {
        $this->authorizer->protectAll([Permissions::MANAGE_USERS]);

        $presenter = new RemoveUserFromGroupAPIPresenter();

        $useCase->removeUserFromGroup($userId, $groupId, $presenter);

        return $this->respondWithMessage($presenter->getViewModel()['message']);
    }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

    /**
     * @param \Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase $useCase
     * @param string                                                        $userId
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\AuthorizationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function getGroupsForUser(GetUserGroupsUseCase $useCase, string $userId): JsonResponse
    {
        $this->authorizer->protectAll([Permissions::MANAGE_USERS]);

        $presenter = new GetUserGroupsAPIPresenter();

        $useCase->getGroupsForUser($userId, $presenter);

        return $this->respondWithData($presenter->getViewModel());
    }
213
}