Skip to content

Commit c8afa8d

Browse files
authored
Merge pull request #68 from topcoder-platform/issues-468
Add endpoints to get user permissions
2 parents a8ff602 + c2edfbf commit c8afa8d

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
use Garden\Web\Exception\ClientException;
4+
use Garden\Schema\Schema;
5+
use Vanilla\Utility\InstanceValidatorSchema;
6+
use Garden\Web\Data;
7+
use Garden\Web\Exception\NotFoundException;
8+
use Garden\Web\Exception\ServerException;
9+
use Vanilla\ApiUtils;
10+
11+
/**
12+
* Permission API Controller for the `/permission` resource.
13+
*/
14+
class PermissionApiController extends AbstractApiController {
15+
16+
/**
17+
* Get default user permissions
18+
* @param $userID
19+
* @return Data
20+
* @throws \Garden\Web\Exception\HttpException
21+
* @throws \Vanilla\Exception\PermissionException
22+
*/
23+
public function index($userID) {
24+
$this->permission('Garden.Settings.Manage');
25+
if (!Gdn::userModel()->getID($userID)) {
26+
throw notFoundException('User');
27+
}
28+
$userPermissions = Gdn::userModel()->getPermissions($userID);
29+
$data = [
30+
'userPermissions' => $userPermissions,
31+
];
32+
return $data;
33+
}
34+
35+
/**
36+
* Get user permissions for a category
37+
* @param $userID
38+
* @param $categoryID
39+
* @return Data
40+
* @throws \Garden\Web\Exception\HttpException
41+
* @throws \Vanilla\Exception\PermissionException
42+
*/
43+
public function get($userID, $categoryID) {
44+
$this->permission('Garden.Settings.Manage');
45+
46+
if (!Gdn::userModel()->getID($userID)) {
47+
throw notFoundException('User');
48+
}
49+
50+
$category = CategoryModel::categories($categoryID);
51+
if (!$category) {
52+
throw notFoundException('Category');
53+
}
54+
$groupID = val('GroupID', $category, null);
55+
$data = [
56+
'GroupID' => $groupID,
57+
'PermsGroupView' => $groupID? GroupModel::getGroupRoleFor($userID, $groupID) : null,
58+
'PermsDiscussionsView' => CategoryModel::checkPermission($category, 'Vanilla.Discussions.View', true, $userID),
59+
'PermsDiscussionsAdd' => CategoryModel::checkPermission($category, 'Vanilla.Discussions.Add', true, $userID),
60+
'PermsDiscussionsEdit' => CategoryModel::checkPermission($category, 'Vanilla.Discussions.Edit', true, $userID),
61+
'PermsCommentsAdd' => CategoryModel::checkPermission($category, 'Vanilla.Comments.Add', true, $userID),
62+
'PermsDiscussionsUploads' => CategoryModel::checkPermission($category, 'Vanilla.Discussions.Uploads', true, $userID),
63+
'PermsCommentsUploads' => CategoryModel::checkPermission($category, 'Vanilla.Comments.Uploads', true, $userID)
64+
];
65+
return $data;
66+
}
67+
}

DebugPlugin/openapi/permission.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
openapi: 3.0.2
2+
info: Vanilla Permission API
3+
paths:
4+
/permission/{userID}/{categoryID}:
5+
get:
6+
parameters:
7+
- description: UserID to check.
8+
in: path
9+
name: userID
10+
schema:
11+
type: integer
12+
- description: CategoryID to check.
13+
in: path
14+
name: categoryID
15+
schema:
16+
type: integer
17+
responses:
18+
'200':
19+
content:
20+
'application/json':
21+
schema:
22+
items:
23+
$ref: '#/components/schemas/Records'
24+
type: array
25+
description: Success
26+
tags:
27+
- Data
28+
summary: List records.
29+
/permission/{userID}:
30+
get:
31+
parameters:
32+
- description: UserID to check.
33+
in: path
34+
name: userID
35+
schema:
36+
type: integer
37+
responses:
38+
'200':
39+
content:
40+
'application/json':
41+
schema:
42+
items:
43+
$ref: '#/components/schemas/Records'
44+
type: array
45+
description: Success
46+
tags:
47+
- Data
48+
summary: List records.
49+
components:
50+
schemas:
51+
Records:
52+
type: object

0 commit comments

Comments
 (0)