-
Notifications
You must be signed in to change notification settings - Fork 1
/
interceptors.bal
24 lines (21 loc) · 923 Bytes
/
interceptors.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import ballerina/graphql;
readonly service class AccessGrant {
*graphql:Interceptor;
isolated remote function execute(graphql:Context context, graphql:Field 'field) returns anydata|error {
var scope = check context.get("scope");
if scope is string && !self.grantAccess(scope, 'field.getName()) {
return error("Access denied!");
}
return context.resolve('field);
}
isolated function grantAccess(string scope, string fieldName) returns boolean {
string[] restrictedForFriends = ["day", "month", "street"];
string[] restrictedForOther = ["day", "month", "year", "homeTown", "street", "mobile"];
if scope is FRIEND && restrictedForFriends.indexOf(fieldName) is int {
return false;
}else if scope is OTHER && restrictedForOther.indexOf(fieldName) is int {
return false;
}
return true;
}
}