mirror of
https://github.com/thiloho/archtika.git
synced 2025-11-22 10:51:36 +01:00
Add user id field to article table
This commit is contained in:
@@ -71,12 +71,13 @@ CREATE TABLE internal.home (
|
|||||||
CREATE TABLE internal.article (
|
CREATE TABLE internal.article (
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
website_id UUID REFERENCES internal.website(id) ON DELETE CASCADE NOT NULL,
|
website_id UUID REFERENCES internal.website(id) ON DELETE CASCADE NOT NULL,
|
||||||
|
user_id UUID REFERENCES internal.user(id) ON DELETE SET NULL,
|
||||||
title VARCHAR(100) NOT NULL CHECK (trim(title) <> ''),
|
title VARCHAR(100) NOT NULL CHECK (trim(title) <> ''),
|
||||||
meta_description VARCHAR(250) NOT NULL CHECK (trim(meta_description) <> ''),
|
meta_description VARCHAR(250) CHECK (trim(meta_description) <> ''),
|
||||||
meta_author VARCHAR(100) NOT NULL CHECK (trim(meta_author) <> ''),
|
meta_author VARCHAR(100) CHECK (trim(meta_author) <> ''),
|
||||||
cover_image UUID REFERENCES internal.media(id) ON DELETE SET NULL,
|
cover_image UUID REFERENCES internal.media(id) ON DELETE SET NULL,
|
||||||
publication_date DATE NOT NULL DEFAULT CURRENT_DATE,
|
publication_date DATE NOT NULL DEFAULT CURRENT_DATE,
|
||||||
main_content TEXT NOT NULL CHECK (trim(main_content) <> ''),
|
main_content TEXT CHECK (trim(main_content) <> ''),
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CLOCK_TIMESTAMP(),
|
created_at TIMESTAMPTZ NOT NULL DEFAULT CLOCK_TIMESTAMP(),
|
||||||
last_modified_at TIMESTAMPTZ NOT NULL DEFAULT CLOCK_TIMESTAMP(),
|
last_modified_at TIMESTAMPTZ NOT NULL DEFAULT CLOCK_TIMESTAMP(),
|
||||||
last_modified_by UUID REFERENCES internal.user(id) ON DELETE SET NULL
|
last_modified_by UUID REFERENCES internal.user(id) ON DELETE SET NULL
|
||||||
|
|||||||
@@ -77,6 +77,7 @@ AS
|
|||||||
SELECT
|
SELECT
|
||||||
id,
|
id,
|
||||||
website_id,
|
website_id,
|
||||||
|
user_id,
|
||||||
title,
|
title,
|
||||||
meta_description,
|
meta_description,
|
||||||
meta_author,
|
meta_author,
|
||||||
@@ -126,7 +127,10 @@ CREATE FUNCTION
|
|||||||
api.create_website(content_type VARCHAR(10), title VARCHAR(50), OUT website_id UUID) AS $$
|
api.create_website(content_type VARCHAR(10), title VARCHAR(50), OUT website_id UUID) AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
_website_id UUID;
|
_website_id UUID;
|
||||||
|
_user_id UUID;
|
||||||
BEGIN
|
BEGIN
|
||||||
|
_user_id := (current_setting('request.jwt.claims', true)::json->>'user_id')::UUID;
|
||||||
|
|
||||||
INSERT INTO internal.website (content_type, title)
|
INSERT INTO internal.website (content_type, title)
|
||||||
VALUES (create_website.content_type, create_website.title)
|
VALUES (create_website.content_type, create_website.title)
|
||||||
RETURNING id INTO _website_id;
|
RETURNING id INTO _website_id;
|
||||||
@@ -141,10 +145,10 @@ BEGIN
|
|||||||
VALUES
|
VALUES
|
||||||
(_website_id, '## Main content comes in here');
|
(_website_id, '## Main content comes in here');
|
||||||
|
|
||||||
INSERT INTO internal.article (website_id, title, meta_description, meta_author, main_content)
|
INSERT INTO internal.article (website_id, user_id, title, meta_description, meta_author, main_content)
|
||||||
VALUES
|
VALUES
|
||||||
(_website_id, 'First article', 'This is the first sample article', 'Author Name', '## First article'),
|
(_website_id, _user_id, 'First article', 'This is the first sample article', 'Author Name', '## First article'),
|
||||||
(_website_id, 'Second article', 'This is the second sample article', 'Author Name', '## Second article');
|
(_website_id, _user_id, 'Second article', 'This is the second sample article', 'Author Name', '## Second article');
|
||||||
|
|
||||||
INSERT INTO internal.footer (website_id, additional_text)
|
INSERT INTO internal.footer (website_id, additional_text)
|
||||||
VALUES (_website_id, 'This website was created with archtika');
|
VALUES (_website_id, 'This website was created with archtika');
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ ALTER TABLE internal.header ENABLE ROW LEVEL SECURITY;
|
|||||||
ALTER TABLE internal.home ENABLE ROW LEVEL SECURITY;
|
ALTER TABLE internal.home ENABLE ROW LEVEL SECURITY;
|
||||||
ALTER TABLE internal.article ENABLE ROW LEVEL SECURITY;
|
ALTER TABLE internal.article ENABLE ROW LEVEL SECURITY;
|
||||||
ALTER TABLE internal.footer ENABLE ROW LEVEL SECURITY;
|
ALTER TABLE internal.footer ENABLE ROW LEVEL SECURITY;
|
||||||
|
ALTER TABLE internal.collab ENABLE ROW LEVEL SECURITY;
|
||||||
|
|
||||||
CREATE POLICY view_user ON internal.user
|
CREATE POLICY view_user ON internal.user
|
||||||
FOR SELECT
|
FOR SELECT
|
||||||
@@ -178,6 +179,51 @@ USING (
|
|||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
CREATE POLICY view_collaborations ON internal.collab
|
||||||
|
FOR SELECT
|
||||||
|
USING (
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM internal.website
|
||||||
|
WHERE internal.website.id = internal.collab.website_id
|
||||||
|
AND internal.website.owner_id = (current_setting('request.jwt.claims', true)::json->>'user_id')::UUID
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE POLICY insert_collaborations ON internal.collab
|
||||||
|
FOR INSERT
|
||||||
|
WITH CHECK (
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM internal.website
|
||||||
|
WHERE internal.website.id = internal.collab.website_id
|
||||||
|
AND internal.website.owner_id = (current_setting('request.jwt.claims', true)::json->>'user_id')::UUID
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE POLICY update_collaborations ON internal.collab
|
||||||
|
FOR UPDATE
|
||||||
|
USING (
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM internal.website
|
||||||
|
WHERE internal.website.id = internal.collab.website_id
|
||||||
|
AND internal.website.owner_id = (current_setting('request.jwt.claims', true)::json->>'user_id')::UUID
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE POLICY delete_collaborations ON internal.collab
|
||||||
|
FOR DELETE
|
||||||
|
USING (
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1
|
||||||
|
FROM internal.website
|
||||||
|
WHERE internal.website.id = internal.collab.website_id
|
||||||
|
AND internal.website.owner_id = (current_setting('request.jwt.claims', true)::json->>'user_id')::UUID
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
-- migrate:down
|
-- migrate:down
|
||||||
DROP POLICY view_user ON internal.user;
|
DROP POLICY view_user ON internal.user;
|
||||||
DROP POLICY view_own_websites ON internal.website;
|
DROP POLICY view_own_websites ON internal.website;
|
||||||
@@ -197,6 +243,10 @@ DROP POLICY delete_own_article ON internal.article;
|
|||||||
DROP POLICY insert_own_article ON internal.article;
|
DROP POLICY insert_own_article ON internal.article;
|
||||||
DROP POLICY view_own_footer ON internal.footer;
|
DROP POLICY view_own_footer ON internal.footer;
|
||||||
DROP POLICY update_own_footer ON internal.footer;
|
DROP POLICY update_own_footer ON internal.footer;
|
||||||
|
DROP POLICY view_collaborations ON internal.collab;
|
||||||
|
DROP POLICY insert_collaborations ON internal.collab;
|
||||||
|
DROP POLICY update_collaborations ON internal.collab;
|
||||||
|
DROP POLICY delete_collaborations ON internal.collab;
|
||||||
|
|
||||||
ALTER TABLE internal.user DISABLE ROW LEVEL SECURITY;
|
ALTER TABLE internal.user DISABLE ROW LEVEL SECURITY;
|
||||||
ALTER TABLE internal.website DISABLE ROW LEVEL SECURITY;
|
ALTER TABLE internal.website DISABLE ROW LEVEL SECURITY;
|
||||||
@@ -206,3 +256,4 @@ ALTER TABLE internal.header DISABLE ROW LEVEL SECURITY;
|
|||||||
ALTER TABLE internal.home DISABLE ROW LEVEL SECURITY;
|
ALTER TABLE internal.home DISABLE ROW LEVEL SECURITY;
|
||||||
ALTER TABLE internal.article DISABLE ROW LEVEL SECURITY;
|
ALTER TABLE internal.article DISABLE ROW LEVEL SECURITY;
|
||||||
ALTER TABLE internal.footer DISABLE ROW LEVEL SECURITY;
|
ALTER TABLE internal.footer DISABLE ROW LEVEL SECURITY;
|
||||||
|
ALTER TABLE internal.collab DISABLE ROW LEVEL SECURITY;
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
-- migrate:up
|
|
||||||
ALTER TABLE internal.collab ENABLE ROW LEVEL SECURITY;
|
|
||||||
|
|
||||||
CREATE POLICY view_collaborations ON internal.collab
|
|
||||||
FOR SELECT
|
|
||||||
USING (
|
|
||||||
EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM internal.website
|
|
||||||
WHERE internal.website.id = internal.collab.website_id
|
|
||||||
AND internal.website.owner_id = (current_setting('request.jwt.claims', true)::json->>'user_id')::UUID
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE POLICY insert_collaborations ON internal.collab
|
|
||||||
FOR INSERT
|
|
||||||
WITH CHECK (
|
|
||||||
EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM internal.website
|
|
||||||
WHERE internal.website.id = internal.collab.website_id
|
|
||||||
AND internal.website.owner_id = (current_setting('request.jwt.claims', true)::json->>'user_id')::UUID
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE POLICY update_collaborations ON internal.collab
|
|
||||||
FOR UPDATE
|
|
||||||
USING (
|
|
||||||
EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM internal.website
|
|
||||||
WHERE internal.website.id = internal.collab.website_id
|
|
||||||
AND internal.website.owner_id = (current_setting('request.jwt.claims', true)::json->>'user_id')::UUID
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE POLICY delete_collaborations ON internal.collab
|
|
||||||
FOR DELETE
|
|
||||||
USING (
|
|
||||||
EXISTS (
|
|
||||||
SELECT 1
|
|
||||||
FROM internal.website
|
|
||||||
WHERE internal.website.id = internal.collab.website_id
|
|
||||||
AND internal.website.owner_id = (current_setting('request.jwt.claims', true)::json->>'user_id')::UUID
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
-- migrate:down
|
|
||||||
DROP POLICY view_collaborations ON internal.collab;
|
|
||||||
DROP POLICY insert_collaborations ON internal.collab;
|
|
||||||
DROP POLICY update_collaborations ON internal.collab;
|
|
||||||
DROP POLICY delete_collaborations ON internal.collab;
|
|
||||||
|
|
||||||
ALTER TABLE internal.collab DISABLE ROW LEVEL SECURITY;
|
|
||||||
@@ -63,7 +63,7 @@ export const load: PageServerLoad = async ({ params, fetch, cookies, url, parent
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const actions: Actions = {
|
export const actions: Actions = {
|
||||||
createArticle: async ({ request, fetch, cookies, params }) => {
|
createArticle: async ({ request, fetch, cookies, params, locals }) => {
|
||||||
const data = await request.formData();
|
const data = await request.formData();
|
||||||
|
|
||||||
const res = await fetch("http://localhost:3000/article", {
|
const res = await fetch("http://localhost:3000/article", {
|
||||||
@@ -74,6 +74,7 @@ export const actions: Actions = {
|
|||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
website_id: params.websiteId,
|
website_id: params.websiteId,
|
||||||
|
user_id: locals.user.id,
|
||||||
title: data.get("title")
|
title: data.get("title")
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user