MeController.php 3.96 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
7
8
9
10
11
<?php

namespace App\Http\Controllers;

use App\Guards\ApiGuard;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\Auth\Guard;
use Source\Authorization\Authorizer;
use Source\Exceptions\AuthenticationException;
use Source\UseCases\Users\GetUser\GetUserUseCase;
12
use Source\UseCases\Users\UpdateUser\UpdateUserUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
13
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase;
14
use Source\UseCases\TokenUser\GetUserTokens\GetUserTokensUseCase;
Jacob Priddy's avatar
Jacob Priddy committed
15
use Source\UseCases\Users\GetUser\APIPresenter as GetUserAPIPresenter;
16
use Source\UseCases\Users\UpdateUser\APIPresenter as UpdateUserAPIPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
17
use Source\UseCases\GroupUser\GetUserGroups\APIPresenter as GetUserGroupsAPIPresenter;
18
use Source\UseCases\TokenUser\GetUserTokens\APIPresenter as GetUserTokensAPIPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

class MeController extends ApiController
{
    /**
     * @var \Illuminate\Contracts\Auth\Guard
     */
    protected Guard $guard;

    protected string $userId;

    /**
     * @param \Illuminate\Http\Request         $request
     * @param \Source\Authorization\Authorizer $authorizer
     * @param \App\Guards\ApiGuard             $guard
     * @throws \Source\Exceptions\AuthenticationException
     */
    public function __construct(Request $request, Authorizer $authorizer, ApiGuard $guard)
    {
        parent::__construct($request, $authorizer);
        $this->guard = $guard;

        $userId = $this->guard->id();

        if (!$userId) {
            throw new AuthenticationException();
        }

        $this->userId = $userId;
    }

    /**
     * @param \Source\UseCases\Users\GetUser\GetUserUseCase $useCase
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function index(GetUserUseCase $useCase): JsonResponse
    {
        $presenter = new GetUserAPIPresenter();

        $useCase->get($this->userId, $presenter);

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

    /**
     * @param \Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase $useCase
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function groups(GetUserGroupsUseCase $useCase): JsonResponse
    {
        $presenter = new GetUserGroupsAPIPresenter();

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

        return $this->respondWithData($presenter->getViewModel());
    }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

    /**
     * @param \Source\UseCases\Users\UpdateUser\UpdateUserUseCase $useCase
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function update(UpdateUserUseCase $useCase): JsonResponse
    {
        $this->validate($this->request, [
            'display_name' => 'string|max:255',
            'password' => 'string|min:20|max:255',
            'doorcode' => 'string|numeric|digits_between:11,255',
        ]);

        $presenter = new UpdateUserAPIPresenter();
92
        $useCase->update($this->userId, $this->request->all(), $presenter);
93
94
95
96
97
98
99

        if ($presenter->hasError()) {
            return $this->respondWithError($presenter->getViewModel()['message']);
        }

        return $this->respondWithData($presenter->getViewModel());
    }
100
101
102
103
104
105
106
107
108
109
110
111
112
113

    /**
     * @param \Source\UseCases\TokenUser\GetUserTokens\GetUserTokensUseCase $userTokens
     * @return \Illuminate\Http\JsonResponse
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function tokens(GetUserTokensUseCase $userTokens): JsonResponse
    {
        $presenter = new GetUserTokensAPIPresenter();

        $userTokens->getTokensForUser($this->userId, $presenter);

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

115
116
117
    /**
     * @return \Illuminate\Http\JsonResponse
     */
118
119
    public function permissions(): JsonResponse
    {
120
        return $this->respondWithData(['permissions' => $this->authorizer->getPermissions()]);
121
    }
Jacob Priddy's avatar
Jacob Priddy committed
122
}