forked from NeoRazorX/facturascripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiAccess.php
More file actions
158 lines (137 loc) · 3.77 KB
/
ApiAccess.php
File metadata and controls
158 lines (137 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2018-2022 Carlos García Gómez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FacturaScripts\Core\Model;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Dinamic\Model\ApiKey as DinApiKey;
/**
* Defines the individual permissions for each resrouce within an api key.
*
* @author Carlos Garcia Gomez <carlos@facturascripts.com>
* @author Francesc Pineda Segarra <francesc.pineda@x-netdigital.com>
*/
class ApiAccess extends Base\ModelClass
{
use Base\ModelTrait;
/**
* Permission to delete.
*
* @var bool
*/
public $allowdelete;
/**
* Permission to get.
*
* @var bool
*/
public $allowget;
/**
* Permission to post.
*
* @var bool
*/
public $allowpost;
/**
* Permission to put.
*
* @var bool
*/
public $allowput;
/**
* Identifier of API key.
*
* @var int
*/
public $idapikey;
/**
* Identifier.
*
* @var int
*/
public $id;
/**
* Name of the resource.
*
* @var string
*/
public $resource;
/**
* Add the indicated resource list to the Role group
*
* @param int $idApiKey
* @param array $resources
* @param bool $state
*
* @return bool
*/
public static function addResourcesToApiKey(int $idApiKey, array $resources, bool $state = false): bool
{
$apiAccess = new static();
foreach ($resources as $resource) {
$where = [
new DataBaseWhere('idapikey', $idApiKey),
new DataBaseWhere('resource', $resource)
];
if ($apiAccess->loadFromCode('', $where)) {
continue;
}
$apiAccess->idapikey = $idApiKey;
$apiAccess->resource = $resource;
$apiAccess->allowdelete = $state;
$apiAccess->allowget = $state;
$apiAccess->allowpost = $state;
$apiAccess->allowput = $state;
if (false === $apiAccess->save()) {
return false;
}
}
return true;
}
public function install(): string
{
// needed dependencies
new DinApiKey();
return parent::install();
}
public static function primaryColumn(): string
{
return 'id';
}
/**
* Update HTTP method permissions for this API resource and save the changes.
*
* @param bool $get Whether GET is allowed.
* @param bool $post Whether POST is allowed.
* @param bool $put Whether PUT is allowed.
* @param bool $delete Whether DELETE is allowed.
*
* @return bool True if saved successfully, false otherwise.
*/
public function setAllowed(bool $get, bool $post, bool $put, bool $delete): bool
{
$this->allowget = $get;
$this->allowpost = $post;
$this->allowput = $put;
$this->allowdelete = $delete;
return $this->save();
}
public static function tableName(): string
{
return 'api_access';
}
}