Skip to content

Commit

Permalink
feat: allow turning off pages from Payload (#266)
Browse files Browse the repository at this point in the history
* feat: allow turning off pages from Payload

* fix: cms startup in e2e setup

* fix: use healthchecks and stuff to order startups

* fix: playwright serving report on fail

* fix: mock discord in e2e

* fix: allow reading features logged out

* fix: missing / wrong env variables on e2e setup

* fix: logout in dev

* chore: unify package name in e2e

* fix: revert hardcoded image in e2e setup

* fix: lint scripts

* fix: add missing migration
  • Loading branch information
geisterfurz007 authored Jan 7, 2024
1 parent 3a78aaf commit 3cbe281
Show file tree
Hide file tree
Showing 29 changed files with 869 additions and 61 deletions.
35 changes: 27 additions & 8 deletions docker-compose.e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ services:
POSTGRES_USER: ytfe2e
POSTGRES_PASSWORD: ytfe2e
POSTGRES_DB: ytfe2e
healthcheck:
test: ["CMD-SHELL", "pg_isready -d ytfe2e"]
start_period: 2s
interval: 1s

server:
image: ghcr.io/yes-theory-fam/yestheory-family-server:COMMIT_HASH
Expand All @@ -22,14 +26,20 @@ services:
DISCORD_BOT_TOKEN: skipped
DISCORD_TARGET_GUILD: skipped

DISCORD_ADMIN_ID: skipped
YESBOT_API_TOKEN: yesbot-api-token

DISCORD_CLIENT_ID: skipped
DISCORD_CLIENT_SECRET: skipped
DISCORD_SCOPES: skipped

CMS_ENDPOINT: "http://cms:3001/api/graphql"
CMS_API_KEY: e2e-api-key
depends_on:
- cms
- postgres
cms:
condition: 'service_healthy'
postgres:
condition: 'service_healthy'

web:
image: ghcr.io/yes-theory-fam/yestheory-family-web:COMMIT_HASH
Expand All @@ -44,19 +54,28 @@ services:
cms:
image: ghcr.io/yes-theory-fam/yestheory-family-cms:COMMIT_HASH
environment:
DATABASE_URI: "postgresql://ytfe2e:ytfe2e@postgres:5432/payload?schema=public"
ENABLE_DATASEEDER: "true"
PAYLOAD_SECRET: asdfghjkl
INITIAL_ADMIN_MAIL: [email protected]
INITIAL_ADMIN_PASSWORD: example
DATABASE_URI: "postgresql://ytfe2e:ytfe2e@postgres:5432/payload?schema=public"
TYPESENSE_API_KEY: 1234567890
TYPESENSE_API_URL: http://typesense:8108
SERVER_URL: http://localhost:3001
FRONTEND_URL: http://web:3000
BACKEND_URL: http://server:5000
INTERNAL_BACKEND_URL: http://server:5001
YESBOT_API_AUTH_TOKEN: yesbot-api-token
INITIAL_ADMIN_ID: skipped
PAYLOAD_API_KEY: e2e-api-key
healthcheck:
test: ["CMD-SHELL", "curl --silent --fail http://localhost:3001/admin > /dev/null || exit 1"]
start_period: 5s
interval: 1s
retries: 10
ports:
- "3001:3001"
depends_on:
- typesense
postgres:
condition: 'service_healthy'
typesense:
condition: 'service_started'

typesense:
image: typesense/typesense:0.25.1
Expand Down
2 changes: 2 additions & 0 deletions packages/cms/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ RUN yarn workspaces focus && \

FROM base as runtime

RUN apk add curl --no-cache

ENV NODE_ENV=production
ENV PAYLOAD_CONFIG_PATH=packages/cms/dist/payload.config.js

Expand Down
4 changes: 2 additions & 2 deletions packages/cms/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env sh

node node_modules/.bin/payload migrate

if [ "$ENABLE_DATASEEDER" = "true" ]; then node ./packages/cms/dist/dataseeder/main.js ; fi

node node_modules/.bin/payload migrate

exec "$@"
2 changes: 1 addition & 1 deletion packages/cms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"copyfiles": "copyfiles -u 1 \"src/**/*.{html,css,scss,ttf,woff,woff2,eot,svg,jpg,png}\" dist/",
"generate:types": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:types",
"generate:graphQLSchema": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:graphQLSchema",
"lint": "yarn run lint:fix && yarn run prettier:fix",
"lint": "yarn run eslint:fix && yarn run prettier:fix",
"eslint:check": "eslint . --report-unused-disable-directives --max-warnings 0",
"eslint:fix": "eslint . --fix --report-unused-disable-directives --max-warnings 0",
"prettier:check": "prettier . --check",
Expand Down
10 changes: 10 additions & 0 deletions packages/cms/src/access/hidden-unless-owner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {type User} from 'payload/auth';
import {type SessionUser} from '../collections/users';

export const hiddenUnlessOwner = ({user}: {user: User}) => {
const sessionUser = user as unknown as SessionUser;
const roles =
'roles' in sessionUser ? sessionUser.roles : sessionUser.user.roles;

return !roles.includes('owner');
};
40 changes: 40 additions & 0 deletions packages/cms/src/collections/features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {type CollectionConfig} from 'payload/types';
import {hiddenUnlessOwner} from '../access/hidden-unless-owner';
import {requireOneOf} from '../access/require-one-of';

export const Features: CollectionConfig = {
slug: 'feature',
access: {
read: () => true,
create: requireOneOf(),
update: requireOneOf(),
delete: requireOneOf(),
},
admin: {
useAsTitle: 'name',
hidden: hiddenUnlessOwner,
},
fields: [
{
name: 'name',
type: 'text',
required: true,
},
{
name: 'pathPrefix',
type: 'text',
required: true,
},
{
name: 'navPath',
type: 'text',
hooks: {
beforeValidate: [({value, data}) => value ?? data.pathPrefix],
},
},
{
name: 'enabled',
type: 'checkbox',
},
],
};
10 changes: 4 additions & 6 deletions packages/cms/src/collections/users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {type GeneratedTypes} from 'payload';
import parseCookies from 'payload/dist/utilities/parseCookies';
import {type CollectionConfig} from 'payload/types';
import {hiddenUnlessOwner} from '../access/hidden-unless-owner';
import {requireOneOf} from '../access/require-one-of';
import {YtfAuthStrategy} from '../lib/auth-strategy';
import {getUserIdFromRequest} from '../lib/get-user-id-from-request';
Expand Down Expand Up @@ -49,22 +50,19 @@ export const Users: CollectionConfig = {
create: requireOneOf(),
update: requireOneOf(),
delete: requireOneOf(),
read: ({req, id}) => {
read: ({req}) => {
const {user} = req;
if (!user?.user) return false;

const {roles} = user.user;
if (roles.includes('owner')) return true;

// Setting it up this way, hides the users collection from the UI while still allowing the /me endpoint to
// function.
if (id) return user.user.id === id;

return false;
return true;
},
},
admin: {
useAsTitle: 'id',
hidden: hiddenUnlessOwner,
},
endpoints: [
{
Expand Down
Loading

0 comments on commit 3cbe281

Please sign in to comment.