7
7
use Authing \Types \AuthorizeResourceParam ;
8
8
use Authing \Types \CommonMessage ;
9
9
use Authing \Types \IsActionAllowedParam ;
10
+ use Error ;
10
11
use Exception ;
11
12
use stdClass ;
12
13
@@ -37,7 +38,7 @@ function randomString(int $randomLenth = 32)
37
38
$ n = '' ;
38
39
39
40
for ($ i = 0 ; $ i < $ randomLenth ; $ i ++) {
40
- $ n .= t[rand (0 , $ a )];
41
+ $ n .= $ t [rand (0 , $ a - 1 )];
41
42
}
42
43
return $ n ;
43
44
}
@@ -114,6 +115,7 @@ public function getResources(array $options)
114
115
];
115
116
$ params = http_build_query ($ array );
116
117
$ data = $ this ->client ->httpGet ("/api/v2/resources? $ params " );
118
+ return $ data ;
117
119
}
118
120
119
121
public function createResource (array $ options )
@@ -158,10 +160,10 @@ public function createProgrammaticAccessAccount(string $appId, array $options =
158
160
159
161
public function disableProgrammaticAccessAccount (string $ programmaticAccessAccountId )
160
162
{
161
- $ data = new stdClass ();
162
- $ data -> id = $ programmaticAccessAccountId;
163
- $ data -> enabled = false ;
164
- $ res = $ this -> client -> httpPatch ( ' /api/v2/applications/programmatic-access-accounts ' , $ data );
163
+ $ res = $ this -> client -> httpPatch ( ' /api/v2/applications/programmatic-access-accounts ' , [
164
+ ' id ' => $ programmaticAccessAccountId,
165
+ ' enabled ' => false ,
166
+ ] );
165
167
return $ res ;
166
168
}
167
169
@@ -173,21 +175,22 @@ public function deleteProgrammaticAccessAccount(string $programmaticAccessAccoun
173
175
174
176
public function enableProgrammaticAccessAccount (string $ programmaticAccessAccountId )
175
177
{
176
- $ data = new stdClass ();
177
- $ data -> id = $ programmaticAccessAccountId;
178
- $ data -> enabled = true ;
179
- $ res = $ this -> client -> httpPatch ( " /api/v2/applications/programmatic-access-accounts " , $ data );
178
+ $ res = $ this -> client -> httpPatch ( " /api/v2/applications/programmatic-access-accounts " , [
179
+ ' id ' => $ programmaticAccessAccountId,
180
+ ' enabled ' => true ,
181
+ ] );
180
182
return $ res ;
181
183
}
182
184
183
185
public function refreshProgrammaticAccessAccountSecret (string $ programmaticAccessAccountId , string $ programmaticAccessAccountSecret = '' )
184
186
{
185
- $ programmaticAccessAccountSecret = randomString (32 );
186
- $ data = [
187
+ if (!isset ($ programmaticAccessAccountSecret ) || $ programmaticAccessAccountSecret === '' ) {
188
+ $ programmaticAccessAccountSecret = randomString (32 );
189
+ }
190
+ $ res = $ this ->client ->httpPatch ('/api/v2/applications/programmatic-access-accounts ' , [
187
191
'id ' => $ programmaticAccessAccountId ,
188
192
'secret ' => $ programmaticAccessAccountSecret ,
189
- ];
190
- $ res = $ this ->client ->httpPatch ('/api/v2/applications/programmatic-access-accounts ' , $ data );
193
+ ]);
191
194
return $ res ;
192
195
}
193
196
@@ -196,7 +199,7 @@ public function authorizeResource(array $params)
196
199
$ namespace = $ params ['namespace ' ];
197
200
$ resource = $ params ['resource ' ];
198
201
$ opts = $ params ['opts ' ];
199
- $ param = (new AuthorizeResourceParam ())->withNamespace ($ namespace )->withOpts ($ ops )->withResource ($ resource );
202
+ $ param = (new AuthorizeResourceParam ())->withNamespace ($ namespace )->withOpts ($ opts )->withResource ($ resource );
200
203
$ res = $ this ->client ->request ($ param ->createRequest ());
201
204
return $ res ;
202
205
}
@@ -208,9 +211,10 @@ public function listResourcePermissions()
208
211
209
212
public function getApplicationAccessPolicies (array $ options )
210
213
{
211
- if ($ options ['appId ' ]) {
214
+ if (! $ options ['appId ' ]) {
212
215
throw new Error ('请传入 appId ' );
213
216
}
217
+ $ options = (object ) $ options ;
214
218
$ appId = $ options ->appId ;
215
219
$ page = $ options ->page ?? 1 ;
216
220
$ limit = $ options ->limit ?? 10 ;
@@ -220,51 +224,52 @@ public function getApplicationAccessPolicies(array $options)
220
224
221
225
public function enableApplicationAccessPolicy (array $ options )
222
226
{
223
- if ($ options ['appId ' ]) {
227
+ if (! $ options ['appId ' ]) {
224
228
throw new Error ('请传入 appId ' );
225
229
}
226
- if ($ options ['targetType ' ]) {
230
+ if (! $ options ['targetType ' ]) {
227
231
throw new Error (
228
232
'请传入主体类型,可选值为 USER、ROLE、ORG、GROUP,含义为用户、角色、组织机构节点、用户分组 '
229
233
);
230
234
}
231
- if ($ options ['targetIdentifiers ' ]) {
235
+ if (! $ options ['targetIdentifiers ' ]) {
232
236
throw new Error ('请传入主体 id ' );
233
237
}
234
238
extract ($ options , EXTR_OVERWRITE );
235
239
$ data = [
236
240
'targetType ' => $ targetType ,
237
- 'namespace ' => $ namespace ,
241
+ 'namespace ' => $ namespace ?? null ,
238
242
'targetIdentifiers ' => $ targetIdentifiers ,
239
- 'inheritByChildren ' => $ inheritByChildren ,
243
+ 'inheritByChildren ' => $ inheritByChildren ?? null ,
240
244
];
241
245
$ this ->client ->httpPost ("/api/v2/applications/ $ appId/authorization/enable-effect " , $ data );
242
- $ _ = new stdClass ();
243
- $ _ -> code = 200 ;
244
- $ _ -> message = '启用应用访问控制策略成功 ' ;
245
- return $ _ ;
246
+ return ( object ) [
247
+ ' code ' => 200 ,
248
+ ' message ' => '启用应用访问控制策略成功 ' ,
249
+ ] ;
246
250
}
247
251
248
252
public function disableApplicationAccessPolicy (array $ options )
249
253
{
250
- if ($ options ['appId ' ]) {
254
+ if (! $ options ['appId ' ]) {
251
255
throw new Error ('请传入 appId ' );
252
256
}
253
- if ($ options ['targetType ' ]) {
257
+ if (! $ options ['targetType ' ]) {
254
258
throw new Error (
255
259
'请传入主体类型,可选值为 USER、ROLE、ORG、GROUP,含义为用户、角色、组织机构节点、用户分组 '
256
260
);
257
261
}
258
- if ($ options ['targetIdentifiers ' ]) {
262
+ if (! $ options ['targetIdentifiers ' ]) {
259
263
throw new Error ('请传入主体 id ' );
260
264
}
261
265
extract ($ options , EXTR_OVERWRITE );
262
266
$ data = [
263
267
'targetType ' => $ targetType ,
264
- 'namespace ' => $ namespace ,
268
+ 'namespace ' => $ namespace ?? null ,
265
269
'targetIdentifiers ' => $ targetIdentifiers ,
266
- 'inheritByChildren ' => $ inheritByChildren ,
270
+ 'inheritByChildren ' => $ inheritByChildren ?? null ,
267
271
];
272
+
268
273
$ this ->client ->httpPost ("/api/v2/applications/ $ appId/authorization/disable-effect " , $ data );
269
274
$ _ = new stdClass ();
270
275
$ _ ->code = 200 ;
@@ -274,24 +279,25 @@ public function disableApplicationAccessPolicy(array $options)
274
279
275
280
public function deleteApplicationAccessPolicy (array $ options )
276
281
{
277
- if ($ options ['appId ' ]) {
282
+ if (! $ options ['appId ' ]) {
278
283
throw new Error ('请传入 appId ' );
279
284
}
280
- if ($ options ['targetType ' ]) {
285
+ if (! $ options ['targetType ' ]) {
281
286
throw new Error (
282
287
'请传入主体类型,可选值为 USER、ROLE、ORG、GROUP,含义为用户、角色、组织机构节点、用户分组 '
283
288
);
284
289
}
285
- if ($ options ['targetIdentifiers ' ]) {
290
+ if (! $ options ['targetIdentifiers ' ]) {
286
291
throw new Error ('请传入主体 id ' );
287
292
}
288
293
extract ($ options , EXTR_OVERWRITE );
289
294
$ data = [
290
295
'targetType ' => $ targetType ,
291
- 'namespace ' => $ namespace ,
296
+ 'namespace ' => $ namespace ?? null ,
292
297
'targetIdentifiers ' => $ targetIdentifiers ,
293
- 'inheritByChildren ' => $ inheritByChildren ,
298
+ 'inheritByChildren ' => $ inheritByChildren ?? null ,
294
299
];
300
+
295
301
$ this ->client ->httpPost ("/api/v2/applications/ $ appId/authorization/revoke " , $ data );
296
302
$ _ = new stdClass ();
297
303
$ _ ->code = 200 ;
@@ -301,24 +307,25 @@ public function deleteApplicationAccessPolicy(array $options)
301
307
302
308
public function allowAccessApplication (array $ options )
303
309
{
304
- if ($ options ['appId ' ]) {
310
+ if (! $ options ['appId ' ]) {
305
311
throw new Error ('请传入 appId ' );
306
312
}
307
- if ($ options ['targetType ' ]) {
313
+ if (! $ options ['targetType ' ]) {
308
314
throw new Error (
309
315
'请传入主体类型,可选值为 USER、ROLE、ORG、GROUP,含义为用户、角色、组织机构节点、用户分组 '
310
316
);
311
317
}
312
- if ($ options ['targetIdentifiers ' ]) {
318
+ if (! $ options ['targetIdentifiers ' ]) {
313
319
throw new Error ('请传入主体 id ' );
314
320
}
315
321
extract ($ options , EXTR_OVERWRITE );
316
322
$ data = [
317
323
'targetType ' => $ targetType ,
318
- 'namespace ' => $ namespace ,
324
+ 'namespace ' => $ namespace ?? null ,
319
325
'targetIdentifiers ' => $ targetIdentifiers ,
320
- 'inheritByChildren ' => $ inheritByChildren ,
326
+ 'inheritByChildren ' => $ inheritByChildren ?? null ,
321
327
];
328
+
322
329
$ this ->client ->httpPost ("/api/v2/applications/ $ appId/authorization/allow " , $ data );
323
330
$ _ = new stdClass ();
324
331
$ _ ->code = 200 ;
@@ -328,24 +335,25 @@ public function allowAccessApplication(array $options)
328
335
329
336
public function denyAccessApplication (array $ options )
330
337
{
331
- if ($ options ['appId ' ]) {
338
+ if (! $ options ['appId ' ]) {
332
339
throw new Error ('请传入 appId ' );
333
340
}
334
- if ($ options ['targetType ' ]) {
341
+ if (! $ options ['targetType ' ]) {
335
342
throw new Error (
336
343
'请传入主体类型,可选值为 USER、ROLE、ORG、GROUP,含义为用户、角色、组织机构节点、用户分组 '
337
344
);
338
345
}
339
- if ($ options ['targetIdentifiers ' ]) {
346
+ if (! $ options ['targetIdentifiers ' ]) {
340
347
throw new Error ('请传入主体 id ' );
341
348
}
342
349
extract ($ options , EXTR_OVERWRITE );
343
350
$ data = [
344
351
'targetType ' => $ targetType ,
345
- 'namespace ' => $ namespace ,
352
+ 'namespace ' => $ namespace ?? null ,
346
353
'targetIdentifiers ' => $ targetIdentifiers ,
347
- 'inheritByChildren ' => $ inheritByChildren ,
354
+ 'inheritByChildren ' => $ inheritByChildren ?? null ,
348
355
];
356
+
349
357
$ this ->client ->httpPost ("/api/v2/applications/ $ appId/authorization/deny " , $ data );
350
358
$ _ = new stdClass ();
351
359
$ _ ->code = 200 ;
@@ -355,10 +363,10 @@ public function denyAccessApplication(array $options)
355
363
356
364
public function updateDefaultApplicationAccessPolicy (array $ options )
357
365
{
358
- if ($ options ['appId ' ]) {
366
+ if (! $ options ['appId ' ]) {
359
367
throw new Error ('请传入 appId ' );
360
368
}
361
- if ($ options ['defaultStrategy ' ]) {
369
+ if (! $ options ['defaultStrategy ' ]) {
362
370
throw new Error (
363
371
'请传入默认策略,可选值为 ALLOW_ALL、DENY_ALL,含义为默认允许所有用户登录应用、默认拒绝所有用户登录应用 '
364
372
);
@@ -371,5 +379,4 @@ public function updateDefaultApplicationAccessPolicy(array $options)
371
379
return $ res ;
372
380
}
373
381
374
-
375
382
}
0 commit comments