Skip to content

Commit ec118e4

Browse files
committed
added ability to support no permission check
1 parent ebf58f8 commit ec118e4

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

Cpp/odin-views/permission.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ namespace {
2525
const fostlib::string &path,
2626
fostlib::http::server::request &req,
2727
const fostlib::host &host) {
28+
29+
if (not config.isobject()) {
30+
// If config is not an object, expecting this to be a view, meaning always go to this view without checking permission
31+
return fostlib::urlhandler::view::execute(
32+
config, path, req, host);
33+
}
34+
2835
const auto &permission = config["permission"];
2936
bool granted = false;
3037
if (odin::c_jwt_trust.value() && req.headers().exists("__jwt")) {
@@ -40,6 +47,7 @@ namespace {
4047
}
4148
}
4249
}
50+
4351
if (not granted) {
4452
fostlib::pg::connection cnx{fostgres::connection(config, req)};
4553
fostlib::json where;
@@ -61,8 +69,14 @@ namespace {
6169
return fostlib::urlhandler::view::execute(
6270
config["allowed"], path, req, host);
6371
} else {
64-
return fostlib::urlhandler::view::execute(
65-
config["forbidden"], path, req, host);
72+
if (config.has_key("forbidden")) {
73+
return fostlib::urlhandler::view::execute(
74+
config["forbidden"], path, req, host);
75+
} else {
76+
// Default forbidden view is fost.response.403
77+
return fostlib::urlhandler::view::execute(
78+
fostlib::json("fost.response.403"), path, req, host);
79+
}
6680
}
6781
}
6882

@@ -96,19 +110,13 @@ namespace {
96110
const fostlib::host &host) const {
97111

98112
auto method_config = fostlib::json();
99-
100113
if (req.method() == "HEAD" and config.has_key("GET")) {
101114
// HEAD method always use the same permission as GET
102115
method_config = config["GET"];
103116
} else {
104117
method_config = config[req.method()];
105118
}
106119

107-
// If forbidden is not defined, put default as 403 view
108-
if (not method_config.has_key("forbidden")) {
109-
fostlib::insert(method_config, "forbidden", "fost.response.403");
110-
}
111-
112120
return check_permission(method_config, config, path, req, host);
113121
}
114122
} c_permission_method;

tests/permissions-test-view.by-method.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,10 @@
3030
}
3131
}
3232
},
33-
"by-method-specified-otherwise/": {
33+
"by-method-no-permission-check/": {
3434
"view": "odin.permission.method",
3535
"configuration": {
36-
"PUT": {
37-
"permission": "write-permission",
38-
"allowed": "permission-required-view"
39-
},
40-
"otherwise": {
41-
"view": "fost.response.405"
42-
}
36+
"GET": "read-only-view"
4337
}
4438
}
4539
}

tests/permissions.by-method.fg

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ PUT test/permission /by-method/identity-1/ {"full_name": "Identity 1"} 403
4646
# Check other HTTP methods (HEAD is supported by default with GET)
4747
HEAD test/permission /by-method/identity-1/ 200
4848

49-
# Default for unspecified method returns forbidden 403
50-
POST test/permission /by-method/identity-1/ {"full_name": "Identity 1"} 403
51-
5249
# Set superuser, should have all permission
5350
odin.superuser read-user-1
5451
GET odin/api /me/permissions 200 {"columns": ["permission"],
@@ -120,7 +117,7 @@ odin.user.expire write-user-2
120117
GET odin/api /me/permissions 200 {"columns": ["permission"], "rows": []}
121118
PUT test/permission /by-method-specified-forbidden/identity-1/ {"full_name": "Identity 1"} 200 {}
122119

123-
## Test permission by method with specified otherwise
120+
## Test permission by method with no permission check
124121

125122
# Set up the read user account
126123
odin.user read-user-3 read-user-3 password1234
@@ -131,9 +128,9 @@ odin.jwt.authorization read-user-3 password1234
131128
GET odin/api /me/permissions 200 {"columns": ["permission"],
132129
"rows": [["read-permission"]]}
133130

134-
# Check that the web API checks the permissions for the methods
135-
GET test/permission /by-method/identity-1/ 200
136-
PUT test/permission /by-method/identity-1/ {"full_name": "Identity 1"} 403
131+
# Check the web API response to user with some permission
132+
GET test/permission /by-method-no-permission-check/identity-1/ 200
137133

138-
# Default for unspecified method returns forbidden 403
139-
POST test/permission /by-method/identity-1/ {"full_name": "Identity 1"} 405
134+
# Check the web API response to user with no permission
135+
odin.user.expire read-user-3
136+
GET test/permission /by-method-no-permission-check/identity-1/ 200

0 commit comments

Comments
 (0)