EndpointPermissionsController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 65
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 11 3
A index() 0 17 1
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2022 Atlas Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace App\Controller\Admin;
14
15
use Cake\Http\Response;
16
use Cake\Utility\Hash;
17
18
/**
19
 * Permissions Controller
20
 *
21
 * @property \App\Controller\Component\PropertiesComponent $Properties
22
 */
23
class EndpointPermissionsController extends AdministrationBaseController
24
{
25
    /**
26
     * Resource type in use
27
     *
28
     * @var string
29
     */
30
    protected $resourceType = 'endpoint_permissions';
31
32
    /**
33
     * @inheritDoc
34
     */
35
    protected $readonly = false;
36
37
    /**
38
     * @inheritDoc
39
     */
40
    protected $properties = [
41
        'endpoint_id' => 'endpoints',
42
        'application_id' => 'applications',
43
        'role_id' => 'roles',
44
        'read' => 'bool',
45
        'write' => 'bool',
46
    ];
47
48
    /**
49
     * Index method
50
     *
51
     * @return \Cake\Http\Response|null
52
     */
53
    public function index(): ?Response
54
    {
55
        parent::index();
56
        $applications = $this->apiClient->get('/admin/applications', []);
57
        $applications = Hash::combine((array)$applications, 'data.{n}.id', 'data.{n}.attributes.name');
58
        $applications['-'] = '-';
59
        $this->set('applications', $applications);
60
        $endpoints = $this->apiClient->get('/admin/endpoints', []);
61
        $endpoints = Hash::combine((array)$endpoints, 'data.{n}.id', 'data.{n}.attributes.name');
62
        $endpoints['-'] = '-';
63
        $this->set('endpoints', $endpoints);
64
        $roles = $this->apiClient->get('/roles', []);
65
        $roles = Hash::combine((array)$roles, 'data.{n}.id', 'data.{n}.attributes.name');
66
        $roles['-'] = '-';
67
        $this->set('roles', $roles);
68
69
        return null;
70
    }
71
72
    /**
73
     * Save data
74
     *
75
     * @return \Cake\Http\Response|null
76
     */
77
    public function save(): ?Response
78
    {
79
        // check '-' values and set to null
80
        $data = $this->request->getData();
81
        foreach ($data as $key => $value) {
82
            if ($value === '-') {
83
                $data[$key] = null;
84
            }
85
        }
86
87
        return parent::save();
88
    }
89
}
90