A guard that redirects
Task: send visitors who are not signed in to the sign-in page with a message, and make sure they cannot post to the page's actions either.
yaml
# Recipe: a guard -- a top-level q:if with q:redirect -- protects the page and
# its actions.
paths:
components: ./components
migrations: ./migrations
datasources:
db:
driver: sqlite
database: ./data/notes.db1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
sql
CREATE TABLE notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
author TEXT NOT NULL,
body TEXT NOT NULL
);1
2
3
4
5
2
3
4
5
A q:if at the top of the page whose branch has a q:redirect is a guard. It runs before the page and before each of its actions, so a post sent straight to add is stopped too:
xml
<q:component name="Notes">
<!-- The guard: it runs before the page AND before each of its actions. -->
<q:if condition="not session.authenticated">
<q:redirect url="/login" flash="Sign in to write notes." />
</q:if>
<q:action name="add" method="POST">
<q:param name="body" required="true" minlength="2" />
<q:query name="added" datasource="db">
INSERT INTO notes (author, body) VALUES (:author, :body)
<q:param name="author" value="{session.userName}" type="string" />
<q:param name="body" value="{body}" type="string" />
</q:query>
<q:redirect url="/" flash="Saved." />
</q:action>
<q:query name="notes" datasource="db">
SELECT author, body FROM notes ORDER BY id DESC
</q:query>
<h1>Notes</h1>
<q:if condition="flash"><p>{flash}</p></q:if>
<form method="POST"><input name="body" /><button>Add</button></form>
<q:loop query="notes"><p>{notes.author}: {notes.body}</p></q:loop>
</q:component>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
xml
<q:component name="Login">
<q:if condition="flash"><p>{flash}</p></q:if>
<h1>Sign in</h1>
</q:component>1
2
3
4
2
3
4
The second test posts to the action without a session and checks that no row was written:
xml
<q:test name="the guard sends a visitor to sign in" page="/">
<test:visit />
<test:expect status="302" redirect="/login" />
<test:expect text="Sign in to write notes." />
</q:test>
<q:test name="the guard also stops the action: nothing is written" page="/">
<test:submit action="add" body="Sneaky note" />
<test:expect status="302" redirect="/login" />
<test:expect table="notes" count="0" />
</q:test>
<q:test name="a signed-in user writes a note" page="/">
<test:as user="ana" role="member" />
<test:submit action="add" body="Buy coffee" />
<test:expect redirect="/" flash="Saved." />
<test:expect table="notes" count="1" where="author = 'ana' AND body = 'Buy coffee'" />
<test:expect text="ana: Buy coffee" />
</q:test>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
text
tests/guard.test.q
PASS the guard sends a visitor to sign in
PASS the guard also stops the action: nothing is written
PASS a signed-in user writes a note
3 passed, 0 failed1
2
3
4
5
2
3
4
5
A guard can check anything the session holds. To only ask for a signed-in user or a role, require_auth and require_role say it in one attribute (A page for one role only).