Completed
Push — master ( 0c2aa0...b1edbb )
by Michiel
03:00 queued 10s
created

RightToBeForgottenController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\ManagementBundle\Controller;
20
21
use DateTime;
22
use Rhumsaa\Uuid\Uuid;
23
use Surfnet\Stepup\Helper\JsonHelper;
24
use Surfnet\Stepup\Identity\Value\Institution;
25
use Surfnet\Stepup\Identity\Value\NameId;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\IdentityService;
27
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command;
28
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ForgetIdentityCommand;
29
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\TransactionAwarePipeline;
30
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
31
use Symfony\Component\HttpFoundation\JsonResponse;
32
use Symfony\Component\HttpFoundation\Request;
33
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
34
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
35
36
/**
37
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
38
 */
39
class RightToBeForgottenController extends Controller
40
{
41
    /**
42
     * @return TransactionAwarePipeline
43
     */
44
    private $pipeline;
45
46
    /**
47
     * @var IdentityService
48
     */
49
    private $identityService;
50
51
    public function __construct(TransactionAwarePipeline $pipeline, IdentityService $identityService)
52
    {
53
        $this->pipeline = $pipeline;
54
        $this->identityService = $identityService;
55
    }
56
57
    public function forgetIdentityAction(Request $request)
58
    {
59
        $this->denyAccessUnlessGranted(['ROLE_MANAGEMENT']);
60
61
        $payload = JsonHelper::decode($request->getContent());
62
63
        if (!isset($payload['name_id'])) {
64
            throw new BadRequestHttpException('Please specify a NameID in the property "name_id"');
65
        }
66
67
        if (!isset($payload['institution'])) {
68
            throw new BadRequestHttpException('Please specify an institution in the property "institution"');
69
        }
70
71
        $this->assertMayForget(new NameId($payload['name_id']), new Institution($payload['institution']));
72
73
        $command = new ForgetIdentityCommand();
74
        $command->UUID        = (string) Uuid::uuid4();
75
        $command->nameId      = $payload['name_id'];
76
        $command->institution = $payload['institution'];
77
78
        return $this->handleCommand($request, $command);
79
    }
80
81
    /**
82
     * @param Request $request
83
     * @param Command $command
84
     * @return JsonResponse
85
     */
86 View Code Duplication
    private function handleCommand(Request $request, Command $command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        $this->pipeline->process($command);
89
90
        $serverName = $request->server->get('SERVER_NAME') ?: $request->server->get('SERVER_ADDR');
91
        $response   = new JsonResponse([
92
            'status'       => 'OK',
93
            'processed_by' => $serverName,
94
            'applied_at'   => (new DateTime())->format(DateTime::ISO8601)
95
        ]);
96
97
        return $response;
98
    }
99
100
    /**
101
     * @param NameId      $nameId
102
     * @param Institution $institution
103
     * @throws ConflictHttpException
104
     */
105
    private function assertMayForget(NameId $nameId, Institution $institution)
106
    {
107
        $credentials =
108
            $this->identityService->findRegistrationAuthorityCredentialsByNameIdAndInstitution($nameId, $institution);
109
110
        if ($credentials === null) {
111
            return;
112
        }
113
114
        if ($credentials->isSraa()) {
115
            throw new ConflictHttpException(
116
                'Identity is currently configured to act as an SRAA. ' .
117
                'Remove its NameID from the configuration and try again.'
118
            );
119
        }
120
121
        if ($credentials->isRaa()) {
122
            $role = 'RAA';
123
        } else {
124
            $role = 'RA';
125
        }
126
127
        throw new ConflictHttpException(sprintf(
128
            'Identity is currently accredited as an %s. Retract the accreditation and try again.',
129
            $role
130
        ));
131
    }
132
}
133