what

Protect the editing area and its data

Give the editor a private admin area, move the inventory table into it, and separate what visitors read from the records you will soon edit.

Allow 15–25 minutes. This is an editorial estimate. Continue in the same my-site folder, with the editor account from lesson 13.

Read

Start at L13. You can sign in, but nothing is private yet: every page is open to everyone, and the inventory table lists every record for any visitor. Before you can write drafts, two things must change.

First, authentication is not authorization. Signing in says who you are; a rule says what you may do. A whole folder can carry one rule: an application.what file in site/admin says that only the role editor may open anything inside it, page or partial. Hiding a link is not protection; the rule is.

Second, the records need two faces. The database keeps each page's working copy, which you will edit, and a published copy of the fields visitors may read. One line in what.toml, public, lists those fields. From then on every list, page, search and count a visitor can reach reads only published copies. On the next start the server publishes every existing page once, so your site looks exactly the same; a page you create later stays private until it is published.

Change

1. Keep a copy. Stop the dev server with Ctrl+C. Copy the whole my-site folder to a new folder such as my-site-before-L14.

2. Name the public fields. Replace what.toml. Only the public line and its comment are new.

what.toml
# What Project Configuration

[server]
port = 8085
host = "127.0.0.1"

[session]
enabled = true
cookie_name = "w_session"
max_age = 604800
database = "data/sessions.db"

# The database owns the pages now. On the first start after this change the
# server copies data/pages.json into the database once, keeps every id, and
# records that it did. It never reads the file again.
[import]
pages = "data/pages.json"

# Visitors may read the pages but not change them, and no two pages may share
# a slug: the database itself refuses the second one.
[collections.pages]
create = "none"
update = "none"
delete = "none"
unique = ["slug"]
# Visitors read published copies of these fields only. On the next start the
# server publishes every existing page once; a page created later stays
# private until it is published.
public = ["title", "slug", "summary", "body"]

# Sign-in. The site keeps its own editor accounts in data/app.db. You add one
# from the terminal with `run-what admin add`: there is no built-in account and
# no password in any file of the project.
[auth]
enabled = true
provider = "local"
login_path = "/login"
after_login = "/"

3. Protect the admin folder. Create site/admin/application.what. Every page in site/admin now uses the admin layout and needs the role editor, which your account has.

site/admin/application.what
layout = "components/admin-layout.html"
title = "Admin | My website"
description = "Editing area for My website."
auth = "editor"

4. Give the admin area its own layout. Create components/admin-layout.html. It looks like the site's layout, with an admin header so you always know where you are.

components/admin-layout.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="#description#">
  <title>#title#</title>
  <link rel="stylesheet" href="/static/styles.css">
</head>
<body class="site-body">
  <a class="skip-link" href="#main-content">Skip to content</a>
  <header class="site-header">
    <a class="site-name" href="/admin">My website admin</a>
    <nav class="site-nav" aria-label="Admin navigation">
      <a href="/admin">Admin home</a>
      <a href="/">View the site</a>
    </nav>
    <div class="site-account">
      <span>Signed in as #user.username#</span>
      <form method="post" action="/w-auth/logout">
        <button type="submit">Sign out</button>
      </form>
    </div>
  </header>
  <main id="main-content" class="site-main" tabindex="-1">
    <slot/>
  </main>
  <footer class="site-footer">
    <p>Only signed-in editors see these pages.</p>
  </footer>
</body>
</html>

5. Move the inventory into the admin area. Create site/admin/index.html, then delete site/inventory.html. The table was useful while everything was public; it is an editor's tool now.

site/admin/index.html
<what>
title = "Admin | My website"
description = "The editing area: every published page, one row each."
fetch.pages = "local:pages"
</what>

<h1>Admin</h1>
<p>Only a signed-in editor can open this page. The table shows the published copies that visitors read; editing them comes in the next lessons.</p>
<if pages>
<div class="table-wrap">
  <table class="inventory">
    <caption>Published pages</caption>
    <thead>
      <tr><th scope="col">Id</th><th scope="col">Title</th><th scope="col">Slug</th></tr>
    </thead>
    <tbody>
<loop data="#pages#" as="page">
      <tr><td>#page.id#</td><td><a href="/pages/#page.slug|urlencode#">#page.title#</a></td><td><code>#page.slug#</code></td></tr>
</loop>
    </tbody>
  </table>
</div>
<else/>
<p class="empty-state">No pages yet.</p>
</if>

6. Start the server again with run-what dev. Before the address it prints [collections.pages] public: published 8 existing records, once.

Check

  1. Signed out, open /admin. You are sent to the sign-in page. Sign in: you land back on /admin, with the table of eight published pages.
  2. Open /pages and a few pages. Everything visitors see is unchanged: the same eight pages, addresses and text.
  3. Open /inventory. It is gone: “Page not found”.

Exercise

Open a private window (signed out) and try /admin and /admin/index. Explain in one sentence why both send you to the sign-in page even though no link to them appears anywhere on the public site.

If something goes wrong

  • The server stops with “has published copies … Without [collections.pages] public”. The public line was removed after the first publication. Put it back: without it, visitors would see working copies.
  • The server stops with “already has records that were published or unpublished before”. The database holds a publication state this lesson did not create, and nothing was changed. Stop the server and keep a copy of the whole my-site folder before changing anything; to continue the course, use the checkpoint below.
  • /admin shows “You don't have permission”. You are signed in with an account without the editor role. Sign out and sign in as editor.
  • Start again from a known state. Download the L14 checkpoint into a new, empty folder; never unpack it over your project. It has no database and no account: run run-what admin add editor in that folder, then start it. Its first start imports the pages and publishes them once.

Next: L15: create your first draft.