MeController.php 3.21 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
14
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase;
use Source\UseCases\Users\GetUser\APIPresenter as GetUserAPIPresenter;
15
use Source\UseCases\Users\UpdateUser\APIPresenter as UpdateUserAPIPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
16
17
18
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
use Source\UseCases\GroupUser\GetUserGroups\APIPresenter as GetUserGroupsAPIPresenter;

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());
    }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

    /**
     * @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',
        ]);

        $attributes = $this->request->all();
        $attributes['salt'] = config('app.key');

        $presenter = new UpdateUserAPIPresenter();
        $useCase->update($this->userId, $attributes, $presenter);

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

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