Completed
Push — master ( a65844...019578 )
by Alberto
15s queued 13s
created

PermissionsTrait::objectPermissionsIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2023 Atlas Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
namespace App\Utility;
16
17
use App\Controller\Admin\RolesController;
18
use BEdita\WebTools\ApiClientProvider;
19
use Cake\Cache\Cache;
20
use Cake\Utility\Hash;
21
22
/**
23
 * Save and delete permissions via API
24
 */
25
trait PermissionsTrait
26
{
27
    /**
28
     * Save permissions for single object.
29
     *
30
     * @param string $objectId The object ID
31
     * @param array $schema The object type schema
32
     * @param array $newPermissions The permissions to save
33
     * @return bool
34
     */
35
    public function savePermissions(string $objectId, array $schema, array $newPermissions): bool
36
    {
37
        if (!in_array('Permissions', (array)Hash::get($schema, 'associations'))) {
38
            return false;
39
        }
40
        $query = ['filter' => ['object_id' => $objectId], 'page_size' => 100];
41
        $objectPermissions = (array)ApiClientProvider::getApiClient()->getObjects('object_permissions', $query);
42
        $oldPermissions = (array)Hash::extract($objectPermissions, 'data.{n}.attributes.role_id');
43
        $oldPermissions = $this->setupPermissionsRoles($oldPermissions);
44
        $newPermissions = $this->setupPermissionsRoles($newPermissions);
45
        $toRemove = array_keys(array_diff($oldPermissions, $newPermissions));
46
        $toAdd = array_keys(array_diff($newPermissions, $oldPermissions));
47
        $toRemove = $this->objectPermissionsIds($objectPermissions, $toRemove);
48
        $this->removePermissions($toRemove);
49
        $this->addPermissions($objectId, $toAdd);
50
51
        return true;
52
    }
53
54
    /**
55
     * Add permissions per object by ID
56
     *
57
     * @param string $objectId The object ID
58
     * @param array $roleIds The role IDs
59
     * @return void
60
     */
61
    public function addPermissions(string $objectId, array $roleIds): void
62
    {
63
        foreach ($roleIds as $roleId) {
64
            ApiClientProvider::getApiClient()->save(
65
                'object_permissions',
66
                ['object_id' => $objectId, 'role_id' => $roleId]
67
            );
68
        }
69
    }
70
71
    /**
72
     * Remove permissions by object permission IDs
73
     *
74
     * @param array $objectPermissionIds The object permission IDs
75
     * @return void
76
     */
77
    public function removePermissions(array $objectPermissionIds): void
78
    {
79
        foreach ($objectPermissionIds as $id) {
80
            ApiClientProvider::getApiClient()->deleteObject($id, 'object_permissions');
81
        }
82
    }
83
84
    /**
85
     * Object permissions IDs per role IDs.
86
     *
87
     * @param array $objectPermissions The object permissions
88
     * @param array $roleIds The role IDs
89
     * @return array
90
     */
91
    public function objectPermissionsIds(array $objectPermissions, array $roleIds): array
92
    {
93
        $objectPermissions = (array)Hash::combine($objectPermissions, 'data.{n}.attributes.role_id', 'data.{n}.id');
94
95
        return array_map(function ($roleId) use ($objectPermissions) {
96
            return $objectPermissions[$roleId];
97
        }, $roleIds);
98
    }
99
100
    /**
101
     * Setup permission roles
102
     *
103
     * @param array $permissions The permissions
104
     * @return array
105
     */
106
    public function setupPermissionsRoles(array $permissions): array
107
    {
108
        $roles = Cache::remember(RolesController::CACHE_KEY_ROLES, function () {
109
            return Hash::combine(
110
                (array)ApiClientProvider::getApiClient()->get('/roles'),
111
                'data.{n}.id',
112
                'data.{n}.attributes.name'
113
            );
114
        });
115
        $result = [];
116
        foreach ($permissions as $roleId) {
117
            $result[$roleId] = (string)Hash::get($roles, $roleId);
118
        }
119
120
        return $result;
121
    }
122
}
123