Skip to content

Commit 0996c33

Browse files
committed
save
1 parent 0f425ac commit 0996c33

File tree

10 files changed

+272
-69
lines changed

10 files changed

+272
-69
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ vendor
44
.phpunit.result.cache
55
# test.php
66
# test*.php
7-
tests/config/test.yaml
7+
# tests/config/test.yaml
88
# tmp

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BaseClient.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ abstract class BaseClient
1616
protected $userPoolId;
1717
protected $appId;
1818

19-
// private $host = 'https://core.authing.cn';
20-
private $host = 'http://authing-server.natapp1.cc';
19+
private $host = 'https://core.authing.cn';
2120

2221

2322
private $_type = "SDK";

src/Mgmt/AclManagementClient.php

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
<?php
22

3-
43
namespace Authing\Mgmt;
54

65
use Authing\Types\AllowParam;
6+
use Authing\Types\AuthorizedResourcesParam;
77
use Authing\Types\CommonMessage;
88
use Authing\Types\IsActionAllowedParam;
9-
use Authing\Types\PolicyAssignmentTargetType;
10-
use Authing\Types\AuthorizedResourcesParam;
11-
129
use Exception;
1310
use stdClass;
1411

15-
function formatAuthorizedResources($obj) {
12+
function formatAuthorizedResources($obj)
13+
{
1614
// $authorizedResources = $obj->authorizedResources;
1715
$list = $obj->list;
1816
$total = $obj->totalCount;
19-
array_map(function($_){
20-
foreach($_ as $key => $value) {
21-
if(!$_->$key) {
17+
array_map(function ($_) {
18+
foreach ($_ as $key => $value) {
19+
if (!$_->$key) {
2220
unset($_->$key);
2321
}
2422
}
2523
return $_;
26-
}, (array)$list);
24+
}, (array) $list);
2725
$res = new stdClass;
2826
$res->list = $list;
2927
$res->totalCount = $total;
@@ -56,7 +54,8 @@ public function __construct($client)
5654
* @return CommonMessage
5755
* @throws Exception
5856
*/
59-
public function allow($resource, $action, $userId=null, $role=null) {
57+
public function allow($resource, $action, $userId = null, $role = null)
58+
{
6059
$param = (new AllowParam($resource, $action))->withUserId($userId)->withRoleCode($role);
6160
return $this->client->request($param->createRequest());
6261
}
@@ -70,22 +69,64 @@ public function allow($resource, $action, $userId=null, $role=null) {
7069
* @return bool
7170
* @throws Exception
7271
*/
73-
public function isAllowed($userId, $action, $resource) {
72+
public function isAllowed($userId, $action, $resource)
73+
{
7474
$param = new IsActionAllowedParam($resource, $action, $userId);
7575
return $this->client->request($param->createRequest());
7676
}
7777

7878
// targetType: PolicyAssignmentTargetType,
7979
// targetIdentifier: string,
8080
// namespace: string,
81-
function listAuthorizedResources($targetType, string $targetIdentifier, string $namespace, $ops = []) {
81+
public function listAuthorizedResources($targetType, string $targetIdentifier, string $namespace, $ops = [])
82+
{
8283
$resourceType = null;
83-
if(count($ops) > 0) {
84+
if (count($ops) > 0) {
8485
$resourceType = $ops['resourceType'];
8586
}
8687
$param = (new AuthorizedResourcesParam())->withTargetType($targetType)->withTargetIdentifier($targetIdentifier)->withNamespace($namespace)->withResourceType($resourceType);
8788
$data = formatAuthorizedResources($this->client->request($param->createRequest()));
8889
return $data;
8990
}
9091

92+
public function getResources(array $options)
93+
{
94+
extract($options, EXTR_OVERWRITE);
95+
$array = [
96+
'namespaceCode' => $namespaceCode,
97+
'type' => $type,
98+
'limit' => $limit ?? 10,
99+
'page' => $page ?? 1,
100+
];
101+
$params = http_build_query($array);
102+
$data = $this->client->httpGet("/api/v2/resources?$params");
103+
}
104+
105+
public function createResource(array $options)
106+
{
107+
if (!isset($options['code'])) {
108+
throw new Error('请为资源设定一个资源标识符');
109+
}
110+
if (!isset($options['actions']) || count($options['actions']) === 0) {
111+
throw new Error('请至少定义一个资源操作');
112+
}
113+
if (!isset($options['namespace'])) {
114+
throw new Error('请传入权限分组标识符');
115+
}
116+
$data = $this->client->httpPost('/api/v2/resources', $options);
117+
118+
return $data;
119+
}
120+
121+
public function updateResource(string $code, array $options)
122+
{
123+
$data = $this->client->httpPost("/api/v2/resources/$code", $options);
124+
return $data;
125+
}
126+
127+
public function deleteResource(string $code, string $namespaceCode)
128+
{
129+
$data = $this->client->httpDelete("/api/v2/resources/$code?namespace=$namespaceCode");
130+
return true;
131+
}
91132
}

src/Mgmt/OrgManagementClient.php

Lines changed: 100 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace Authing\Mgmt;
54

65
use Authing\Types\ChildrenNodesParam;
@@ -9,15 +8,21 @@
98
use Authing\Types\DeleteNodeParam;
109
use Authing\Types\DeleteOrgParam;
1110
use Authing\Types\IsRootNodeParam;
11+
use Authing\Types\ListNodeByIdAuthorizedResourcesParam;
1212
use Authing\Types\MoveNodeParam;
1313
use Authing\Types\Node;
14+
use Authing\Types\NodeByIdParam;
1415
use Authing\Types\NodeByIdWithMembersParam;
1516
use Authing\Types\Org;
1617
use Authing\Types\OrgParam;
1718
use Authing\Types\OrgsParam;
1819
use Authing\Types\PaginatedOrgs;
1920
use Authing\Types\PaginatedUsers;
2021
use Authing\Types\RootNodeParam;
22+
use Authing\Types\SetMainDepartmentParam;
23+
use Authing\Types\ListNodeByCodeAuthorizedResourcesParam;
24+
25+
2126
use Exception;
2227
use stdClass;
2328

@@ -46,7 +51,8 @@ public function __construct($client)
4651
* @return Org
4752
* @throws Exception
4853
*/
49-
public function create($name, $description = null, $code = null) {
54+
public function create($name, $description = null, $code = null)
55+
{
5056
$param = (new CreateOrgParam($name))->withCode($code)->withDescription($description);
5157
return $this->client->request($param->createRequest());
5258
}
@@ -58,7 +64,8 @@ public function create($name, $description = null, $code = null) {
5864
* @return CommonMessage
5965
* @throws Exception
6066
*/
61-
public function deleteById($id) {
67+
public function deleteById($id)
68+
{
6269
$param = new DeleteOrgParam($id);
6370
return $this->client->request($param->createRequest());
6471
}
@@ -77,13 +84,14 @@ private function buildTree($org)
7784
* @return PaginatedOrgs
7885
* @throws Exception
7986
*/
80-
public function paginate($page = 1, $limit = 10) {
87+
public function paginate($page = 1, $limit = 10)
88+
{
8189
$param = (new OrgsParam())->withPage($page)->withLimit($limit);
8290
// TODO: buildTree
8391
$data = $this->client->request($param->createRequest());
8492
$orgs = $data->orgs;
8593
$list = $orgs->list;
86-
array_map(function($org) {
94+
array_map(function ($org) {
8795
$this->buildTree($org);
8896
}, $list);
8997
$totalCount = $orgs->totalCount;
@@ -93,11 +101,13 @@ public function paginate($page = 1, $limit = 10) {
93101
return $_;
94102
}
95103

96-
public function addNode($orgId, $parentNodeId, $data) {
104+
public function addNode($orgId, $parentNodeId, $data)
105+
{
97106

98107
}
99108

100-
public function updateNode($id, $updates) {
109+
public function updateNode($id, $updates)
110+
{
101111

102112
}
103113

@@ -108,7 +118,8 @@ public function updateNode($id, $updates) {
108118
* @return Org
109119
* @throws Exception
110120
*/
111-
public function findById($id) {
121+
public function findById($id)
122+
{
112123
$param = new OrgParam($id);
113124
// TODO: buildTree
114125
return $this->client->request($param->createRequest());
@@ -122,7 +133,8 @@ public function findById($id) {
122133
* @return CommonMessage
123134
* @throws Exception
124135
*/
125-
public function deleteNode($orgId, $nodeId) {
136+
public function deleteNode($orgId, $nodeId)
137+
{
126138
$param = new DeleteNodeParam($orgId, $nodeId);
127139
return $this->client->request($param->createRequest());
128140
}
@@ -136,7 +148,8 @@ public function deleteNode($orgId, $nodeId) {
136148
* @return Org
137149
* @throws Exception
138150
*/
139-
public function moveNode($orgId, $nodeId, $targetParentId) {
151+
public function moveNode($orgId, $nodeId, $targetParentId)
152+
{
140153
$param = new MoveNodeParam($orgId, $nodeId, $targetParentId);
141154
// TODO: buildTree
142155
return $this->client->request($param->createRequest());
@@ -150,7 +163,8 @@ public function moveNode($orgId, $nodeId, $targetParentId) {
150163
* @return boolean
151164
* @throws Exception
152165
*/
153-
public function isRootNode($orgId, $nodeId) {
166+
public function isRootNode($orgId, $nodeId)
167+
{
154168
$param = new IsRootNodeParam($orgId, $nodeId);
155169
return $this->client->request($param->createRequest());
156170
}
@@ -163,7 +177,8 @@ public function isRootNode($orgId, $nodeId) {
163177
* @return Node[]
164178
* @throws Exception
165179
*/
166-
public function listChildren($orgId, $nodeId) {
180+
public function listChildren($orgId, $nodeId)
181+
{
167182
$param = new ChildrenNodesParam($orgId, $nodeId);
168183
return $this->client->request($param->createRequest());
169184
}
@@ -175,16 +190,19 @@ public function listChildren($orgId, $nodeId) {
175190
* @return Node[]
176191
* @throws Exception
177192
*/
178-
public function rootNode($orgId) {
193+
public function rootNode($orgId)
194+
{
179195
$param = new RootNodeParam($orgId);
180196
return $this->client->request($param->createRequest());
181197
}
182198

183-
public function importByJson($json) {
199+
public function importByJson($json)
200+
{
184201

185202
}
186203

187-
public function addMembers($nodeId, $userIds) {
204+
public function addMembers($nodeId, $userIds)
205+
{
188206

189207
}
190208

@@ -195,11 +213,75 @@ public function addMembers($nodeId, $userIds) {
195213
* @return PaginatedUsers
196214
* @throws Exception
197215
*/
198-
public function listMembers($param) {
216+
public function listMembers($param)
217+
{
199218
return $this->client->request($param->createRequest())->users;
200219
}
201220

202-
public function removeMembers($nodeId, $userIds) {
221+
public function removeMembers($nodeId, $userIds)
222+
{
223+
224+
}
225+
226+
public function getNodeById(string $nodeId)
227+
{
228+
$param = new NodeByIdParam($nodeId);
229+
$node = $this->client->request($param->createRequest())->nodeById;
230+
return $node;
231+
}
232+
233+
public function exportAll()
234+
{
235+
$data = $this->client->httpGet('/api/v2/orgs/export');
236+
return $data;
237+
}
238+
239+
public function setMainDepartment(string $userId, string $departmentId)
240+
{
241+
$param = (new SetMainDepartmentParam($userId))->withDepartmentId($departmentId);
242+
$data = $this->client->request($param->createRequest())->setMainDepartment;
243+
return $data;
244+
}
245+
246+
public function exportByOrgId(string $orgId)
247+
{
248+
$data = $this->client->httpGet("/api/v2/orgs/export?org_id=$orgId");
249+
return $data;
250+
}
251+
252+
public function listAuthorizedResourcesByNodeId(string $nodeId, string $namespace, array $options = [])
253+
{
254+
$resourceType = $options['resourceType'] ?? new stdClass;
255+
$param = (new ListNodeByIdAuthorizedResourcesParam($nodeId))->withNamespace($namespace)->withResourceType($resourceType);
256+
$node = $this->client->request($param->createRequest())->nodeById;
257+
if (!$node) {
258+
throw new Error('组织机构节点不存在');
259+
}
260+
$list = $node->authorizedResources->list;
261+
$totalCount = $node->authorizedResources->totalCount;
203262

263+
$list = formatAuthorizedResources($list);
264+
$_ = new stdClass;
265+
$_->list = $list;
266+
$_->totalCount = $totalCount;
267+
return $_;
268+
}
269+
270+
public function listAuthorizedResourcesByNodeCode(string $orgId, string $code, string $namespace, array $options = [])
271+
{
272+
$resourceType = $options['resourceType'] ?? new stdClass;
273+
$param = (new ListNodeByCodeAuthorizedResourcesParam($orgId, $code))->withNamespace($namespace)->withResourceType($resourceType);
274+
$node = $this->client->request($param->createRequest())->nodeById;
275+
if (!$node) {
276+
throw new Error('组织机构节点不存在');
277+
}
278+
$list = $node->authorizedResources->list;
279+
$totalCount = $node->authorizedResources->totalCount;
280+
281+
$list = formatAuthorizedResources($list);
282+
$_ = new stdClass;
283+
$_->list = $list;
284+
$_->totalCount = $totalCount;
285+
return $_;
204286
}
205-
}
287+
}

0 commit comments

Comments
 (0)