Skip to content

Commit 9b75689

Browse files
committed
FORMS-984: Updated permissions
1 parent 88f7b53 commit 9b75689

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ about writing changes to this log.
99
## [Unreleased]
1010

1111
- Added endpoint for getting all submissions on a webform.
12+
- **Updated** permissions such that users must be given access explicitly.
1213

1314
## [1.1.0]
1415

README.md

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,28 @@ vendor/bin/drush pm:enable os2forms_rest_api
1515
We use [Key auth](https://www.drupal.org/project/key_auth) for authenticating
1616
api users.
1717

18-
A user can access the Webforrm REST API if
18+
A user can access the Webform REST API if
1919

20-
1. it has the “OS2Form REST API user” (`os2forms_rest_api_user`) role and
21-
2. has a generated key (User > Edit > Key authentication; `/user/«user
20+
1. it has the “OS2Form REST API user” (`os2forms_rest_api_user`) role,
21+
2. has been granted access to the formular
22+
(see [Custom access control](#custom-access-control) )
23+
3. has a generated key (User > Edit > Key authentication; `/user/«user
2224
id»/key-auth`).
2325

24-
The “OS2Form REST API user” role gives read-only access to the API. To get read
26+
The “OS2Form REST API user” role gives read-only access to the API. To get write
2527
access, a user must also have the “OS2Form REST API user (write)”
2628
(`os2forms_rest_api_user_write`) role.
2729

2830
## Endpoints
2931

30-
| Name | Path | Methods |
31-
|--------------------|------------------------------------------------|---------|
32-
| Webform Elements | `/webform_rest/{webform_id}/elements` | GET |
33-
| Webform Fields | `/webform_rest/{webform_id}/fields` | GET |
34-
| Webform Submission | `/webform_rest/{webform_id}/submission/{uuid}` | GET |
35-
| Webform Submit | `/webform_rest/submit` | POST |
36-
| File | `/entity/file/{file_id}` | GET |
32+
| Name | Path | Methods |
33+
|---------------------|------------------------------------------------|---------|
34+
| Webform Elements | `/webform_rest/{webform_id}/elements` | GET |
35+
| Webform Fields | `/webform_rest/{webform_id}/fields` | GET |
36+
| Webform Submission | `/webform_rest/{webform_id}/submission/{uuid}` | GET |
37+
| Webform Submissions | `/webform_rest/{webform_id}/submissions` | GET |
38+
| Webform Submit | `/webform_rest/submit` | POST |
39+
| File | `/entity/file/{file_id}` | GET |
3740

3841
## Examples
3942

@@ -125,14 +128,45 @@ Response:
125128

126129
(the `sid` value is a webform submission uuid).
127130

131+
### Webform submissions
132+
133+
You can filter results based on submission time by
134+
adding query parameters to the url:
135+
136+
| Name | Value | Example |
137+
|-------------|----------------------|--------------|
138+
| `starttime` | PHP DateTime formats | `yesterday` |
139+
| `endtime` | PHP DateTime formats | `2023-10-23` |
140+
141+
If left out, filtering upon the left out parameter will not be done.
142+
143+
The example beneath requests all submissions after October 1. 2023.
144+
145+
Request:
146+
147+
```sh
148+
> curl --silent --header 'api-key: …' 'https://127.0.0.1:8000/webform_rest/some_webform_id/submissions?starttime=2023-10-01'
149+
```
150+
151+
Response:
152+
153+
```json
154+
{"webform_id":"some_webform_id",
155+
"starttime":"2023-10-01",
156+
"submissions":{
157+
"123":"https:\/\/127.0.0.1:8000\/da\/webform_rest\/some_webform_id\/submission\/44b1fe1b-ee96-481e-b941-d1219d1dcb55",
158+
"124":"https:\/\/127.0.0.1:8000\/da\/webform_rest\/some_webform_id\/submission\/3652836d-3dab-4919-b880-e82cbbf3c24c"
159+
}
160+
}
161+
```
162+
128163
## Custom access control
129164

130-
To limit access to webforms, you can specify a list of API users that are
165+
To give access to webforms, you need to specify a list of API users that are
131166
allowed to access a webform's data via the API.
132167

133168
Go to Settings > General > Third party settings > OS2Forms > REST API to specify
134-
which users can access a webform's data. **If no users are specified, all API
135-
users can access the data.**
169+
which users can access a webform's data.
136170

137171
### Technical details
138172

src/WebformHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ public function webformThirdPartySettingsFormAlter(array &$form, FormStateInterf
143143
/** @var \Drupal\user\Entity\User $apiUser */
144144
$apiUser = $this->entityTypeManager->getStorage('user')->load($this->currentUser->id());
145145
// Don't show API data links if current user is not included in
146-
// (non-empty) list of allowed users.
147-
if (!empty($allowedUsers) && !isset($allowedUsers[$apiUser->id()])) {
146+
// list of allowed users.
147+
if (!isset($allowedUsers[$apiUser->id()])) {
148148
$apiUser = NULL;
149149
}
150-
$apiKey = $apiUser ? $apiUser->api_key->value : NULL;
150+
$apiKey = $apiUser?->api_key->value;
151151
if (!empty($apiKey)) {
152152
$form['third_party_settings']['os2forms']['os2forms_rest_api']['api_info']['endpoints_test'] = [
153153
'#type' => 'fieldset',
@@ -260,7 +260,7 @@ public function hasWebformAccess(WebformInterface $webform, $user): bool {
260260

261261
$allowedUsers = $this->getAllowedUsers($webform);
262262

263-
return empty($allowedUsers) || isset($allowedUsers[$userId]);
263+
return isset($allowedUsers[$userId]);
264264
}
265265

266266
/**

0 commit comments

Comments
 (0)