Skip to content

Commit 932335e

Browse files
authoredDec 21, 2022
Merge pull request #1 from OS2Forms/develop
Added module code
2 parents 00a951b + d8e06b6 commit 932335e

22 files changed

+1040
-0
lines changed
 

‎.github/workflows/pr.yaml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
on: pull_request
2+
name: PR Review
3+
jobs:
4+
test-composer-files:
5+
name: Validate composer
6+
runs-on: ubuntu-latest
7+
strategy:
8+
matrix:
9+
php-versions: [ '7.4', '8.0', '8.1' ]
10+
dependency-version: [ prefer-lowest, prefer-stable ]
11+
steps:
12+
- uses: actions/checkout@master
13+
- name: Setup PHP, with composer and extensions
14+
uses: shivammathur/setup-php@v2
15+
with:
16+
php-version: ${{ matrix.php-versions }}
17+
extensions: json
18+
coverage: none
19+
tools: composer:v2
20+
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
21+
- name: Get composer cache directory
22+
id: composer-cache
23+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
24+
- name: Cache dependencies
25+
uses: actions/cache@v2
26+
with:
27+
path: ${{ steps.composer-cache.outputs.dir }}
28+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
29+
restore-keys: ${{ runner.os }}-composer-
30+
- name: Validate composer files
31+
run: |
32+
composer validate --strict composer.json
33+
# Check that dependencies resolve.
34+
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
35+
36+
php-check-coding-standards:
37+
name: PHP - Check Coding Standards
38+
runs-on: ubuntu-latest
39+
strategy:
40+
matrix:
41+
php-versions: [ '7.4', '8.0', '8.1' ]
42+
dependency-version: [ prefer-lowest, prefer-stable ]
43+
steps:
44+
- uses: actions/checkout@master
45+
- name: Setup PHP, with composer and extensions
46+
uses: shivammathur/setup-php@v2
47+
with:
48+
php-version: ${{ matrix.php-versions }}
49+
extensions: json
50+
coverage: none
51+
tools: composer:v2
52+
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
53+
- name: Get composer cache directory
54+
id: composer-cache
55+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
56+
- name: Cache dependencies
57+
uses: actions/cache@v2
58+
with:
59+
path: ${{ steps.composer-cache.outputs.dir }}
60+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
61+
restore-keys: ${{ runner.os }}-composer-
62+
- name: Install Dependencies
63+
run: |
64+
composer install --no-interaction --no-progress
65+
- name: PHPCS
66+
run: |
67+
composer coding-standards-check/phpcs
68+
69+
php-code-analysis:
70+
name: PHP - Code analysis
71+
runs-on: ubuntu-latest
72+
strategy:
73+
matrix:
74+
php-versions: [ '7.4', '8.0', '8.1' ]
75+
dependency-version: [ prefer-lowest, prefer-stable ]
76+
steps:
77+
- uses: actions/checkout@master
78+
- name: Setup PHP, with composer and extensions
79+
uses: shivammathur/setup-php@v2
80+
with:
81+
php-version: ${{ matrix.php-versions }}
82+
extensions: json, gd
83+
coverage: none
84+
tools: composer:v2
85+
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
86+
- name: Get composer cache directory
87+
id: composer-cache
88+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
89+
- name: Cache dependencies
90+
uses: actions/cache@v2
91+
with:
92+
path: ${{ steps.composer-cache.outputs.dir }}
93+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
94+
restore-keys: ${{ runner.os }}-composer-
95+
- name: drupal-check
96+
run: |
97+
# We need a Drupal project to run drupal-check (cf. https://github.com/mglaman/drupal-check#usage)
98+
# Install Drupal
99+
composer --no-interaction create-project drupal/recommended-project:^9 --stability=dev drupal
100+
# Copy our module source code into the Drupal module folder.
101+
mkdir -p drupal/web/modules/contrib/os2forms_rest_api
102+
cp -r os2forms_rest_api.* composer.json src drupal/web/modules/contrib/os2forms_rest_api
103+
# Add our module as a composer repository.
104+
composer --no-interaction --working-dir=drupal config repositories.os2forms/os2forms_rest_api path web/modules/contrib/os2forms_rest_api
105+
# Restore Drupal composer repository.
106+
composer --no-interaction --working-dir=drupal config repositories.drupal composer https://packages.drupal.org/8
107+
108+
# Require our module.
109+
composer --no-interaction --working-dir=drupal require 'os2forms/os2forms_rest_api:*'
110+
111+
# Check code
112+
composer --no-interaction --working-dir=drupal require --dev drupal/core-dev
113+
cd drupal/web/modules/contrib/os2forms_rest_api
114+
# Remove our non-dev dependencies to prevent duplicated Drupal installation
115+
# PHP Fatal error: Cannot redeclare drupal_get_filename() (previously declared in /home/runner/work/os2forms_rest_api/os2forms_rest_api/drupal/web/modules/contrib/os2forms_rest_api/vendor/drupal/core/includes/bootstrap.inc:190) in /home/runner/work/os2forms_rest_api/os2forms_rest_api/drupal/web/core/includes/bootstrap.inc on line 190
116+
# Use sed to remove the "require" property in composer.json
117+
sed -i '/^\s*"require":/,/^\s*}/d' composer.json
118+
composer --no-interaction install
119+
composer code-analysis

‎.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

‎.php-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.4

‎README.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,146 @@
11
# OS2Forms REST API
22

3+
We use [Webform REST](https://www.drupal.org/project/webform_rest) to expose a
4+
number of API endpoints.
5+
6+
## Installation
7+
8+
```sh
9+
composer require os2forms/os2forms_rest_api
10+
vendor/bin/drush pm:enable os2forms_rest_api
11+
```
12+
13+
## Authentication
14+
15+
We use [Key auth](https://www.drupal.org/project/key_auth) for authenticating
16+
api users.
17+
18+
A user can access the Webforrm REST API if
19+
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
22+
id»/key-auth`).
23+
24+
The “OS2Form REST API user” role gives read-only access to the API. To get read
25+
access, a user must also have the “OS2Form REST API user (write)”
26+
(`os2forms_rest_api_user_write`) role.
27+
28+
## Endpoints
29+
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 |
37+
38+
## Examples
39+
40+
### Get file content from webform submission
41+
42+
Example uses `some_webform_id` as webform id, `some_submission_id` as submission
43+
id and `dokumenter` as the webform file element key.
44+
45+
Request:
46+
47+
```sh
48+
> curl --silent --header 'api-key: …' https://127.0.0.1:8000/webform_rest/some_webform_id/submission/some_submission_uuid
49+
```
50+
51+
Response:
52+
53+
```json
54+
{
55+
…,
56+
"data": {
57+
"navn": "Jack",
58+
"telefon": "12345678"
59+
"dokumenter": {
60+
"some_document_id",
61+
"some_other_docuent_id"
62+
}
63+
}
64+
}
65+
```
66+
67+
Use the file endpoint from above to get information on a file, substituting
68+
`{file_id}` with the actual file id (`some_document_id`) from the previous
69+
request.
70+
71+
Request:
72+
73+
```sh
74+
> curl --silent --header 'api-key: …' https://127.0.0.1:8000/webform_rest/entity/file/some_document_id
75+
```
76+
77+
Response:
78+
79+
```json
80+
{
81+
…,
82+
"uri": [
83+
{
84+
"value": "private:…",
85+
"url": "/system/files/webform/some_webform_id/…"
86+
}
87+
],
88+
89+
}
90+
```
91+
92+
Finally, you can get the actual file by combining the base url
93+
with the url from above response:
94+
95+
```sh
96+
> curl --silent --header 'api-key: …' http://127.0.0.1:8000/system/files/webform/some_webform_id/…
97+
```
98+
99+
Response:
100+
101+
The actual document content.
102+
103+
### Submit webform
104+
105+
Request:
106+
107+
```sh
108+
> curl --silent --location --header 'api-key: …' --header 'content-type: application/json' https://127.0.0.1:8000/webform_rest/submit --data @- <<'JSON'
109+
{
110+
"webform_id": "{webform_id}",
111+
"//": "Webform field values (cf. /webform_rest/{webform_id}/fields)",
112+
"navn_": "Mikkel",
113+
"adresse": "Livets landevej",
114+
"mail_": "mikkel@example.com",
115+
"telefonnummer_": "12345678"
116+
}
117+
JSON
118+
```
119+
120+
Response:
121+
122+
```json
123+
{"sid":"6d95afe9-18d1-4a7d-a1bf-fd38c58c7733"}
124+
```
125+
126+
(the `sid` value is a webform submission uuid).
127+
128+
## Custom access control
129+
130+
To limit access to webforms, you can specify a list of API users that are
131+
allowed to access a webform's data via the API.
132+
133+
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.**
136+
137+
### Technical details
138+
139+
The custom access check is implemented in an event subscriber listening on the
140+
`KernelEvents::REQUEST` event. See
141+
[EventSubscriber::onRequest](src/EventSubscriber/EventSubscriber.php) for
142+
details.
143+
144+
In order to make documents accessible for api users the Key auth
145+
`authentication_provider` service has been overwritten to be global. See
146+
[os2forms_rest_api.services](os2forms_rest_api.services.yml).

‎composer.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "os2forms/os2forms_rest_api",
3+
"description": "OS2Forms REST API",
4+
"type": "drupal-module",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Mikkel Ricky",
9+
"email": "rimi@aarhus.dk"
10+
}
11+
],
12+
"minimum-stability": "dev",
13+
"prefer-stable": true,
14+
"repositories": [
15+
{
16+
"type": "composer",
17+
"url": "https://packages.drupal.org/8"
18+
}
19+
],
20+
"require": {
21+
"drupal/key_auth": "^2.0",
22+
"drupal/webform_rest": "^4.0"
23+
},
24+
"require-dev": {
25+
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.2",
26+
"drupal/coder": "^8.3",
27+
"mglaman/drupal-check": "^1.4"
28+
},
29+
"scripts": {
30+
"code-analysis/drupal-check": [
31+
"vendor/bin/drupal-check --deprecations --analysis --exclude-dir=vendor *.* src"
32+
],
33+
"code-analysis": [
34+
"@code-analysis/drupal-check"
35+
],
36+
"coding-standards-check/phpcs": [
37+
"vendor/bin/phpcs --standard=phpcs.xml.dist"
38+
],
39+
"coding-standards-check": [
40+
"@coding-standards-check/phpcs"
41+
],
42+
"coding-standards-apply/phpcs": [
43+
"vendor/bin/phpcbf --standard=phpcs.xml.dist"
44+
],
45+
"coding-standards-apply": [
46+
"@coding-standards-apply/phpcs"
47+
]
48+
},
49+
"config": {
50+
"sort-packages": true,
51+
"allow-plugins": {
52+
"dealerdirect/phpcodesniffer-composer-installer": true
53+
}
54+
}
55+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- file
6+
- os2forms_rest_api
7+
- serialization
8+
enforced:
9+
module:
10+
- os2forms_rest_api
11+
id: entity.file
12+
plugin_id: 'entity:file'
13+
granularity: resource
14+
configuration:
15+
methods:
16+
- GET
17+
formats:
18+
- json
19+
authentication:
20+
- key_auth
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
langcode: da
2+
status: true
3+
dependencies:
4+
module:
5+
- key_auth
6+
- os2forms_rest_api
7+
- serialization
8+
- webform_rest
9+
enforced:
10+
module:
11+
- os2forms_rest_api
12+
id: webform_rest_elements
13+
plugin_id: webform_rest_elements
14+
granularity: resource
15+
configuration:
16+
methods:
17+
- GET
18+
formats:
19+
- json
20+
authentication:
21+
- key_auth
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
langcode: da
2+
status: true
3+
dependencies:
4+
module:
5+
- key_auth
6+
- os2forms_rest_api
7+
- serialization
8+
- webform_rest
9+
enforced:
10+
module:
11+
- os2forms_rest_api
12+
id: webform_rest_fields
13+
plugin_id: webform_rest_fields
14+
granularity: resource
15+
configuration:
16+
methods:
17+
- GET
18+
formats:
19+
- json
20+
authentication:
21+
- key_auth

0 commit comments

Comments
 (0)