summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <code@mail.matthias.benkard.de>2012-08-18 17:49:46 +0200
committerMatthias Andreas Benkard <code@mail.matthias.benkard.de>2012-08-18 17:49:46 +0200
commit28c046e169c5874ce066228e7f3658d02317c216 (patch)
treedba7d84bbfbf7173632b70c9e89fc80282ebe3d6
parent573113dfb99e4d6eb6086b53dee1afcb845c18a9 (diff)
Schema: Genericize access control.
-rw-r--r--migrations/6_genericize_access_control.sql14
-rw-r--r--schema.sql24
-rw-r--r--src/mulk/benki/lazychat.clj8
3 files changed, 32 insertions, 14 deletions
diff --git a/migrations/6_genericize_access_control.sql b/migrations/6_genericize_access_control.sql
new file mode 100644
index 0000000..b415d5b
--- /dev/null
+++ b/migrations/6_genericize_access_control.sql
@@ -0,0 +1,14 @@
+ALTER TABLE lazychat_targets RENAME TO post_targets;
+
+CREATE VIEW user_visible_posts AS
+ SELECT eur.user, t.message
+ FROM effective_user_roles eur, post_targets t
+ WHERE t.target = eur.role
+ UNION
+ SELECT m.owner, m.message
+ FROM posts m;
+
+CREATE OR REPLACE VIEW user_visible_lazychat_messages AS
+ SELECT uvp.user, uvp.message
+ FROM user_visible_posts uvp
+ INNER JOIN lazychat_messages lm ON lm.id = uvp.message;
diff --git a/schema.sql b/schema.sql
index c555166..ea5110f 100644
--- a/schema.sql
+++ b/schema.sql
@@ -146,15 +146,14 @@ CREATE TABLE lazychat_messages INHERITS posts (
content VARCHAR,
format VARCHAR NOT NULL,
PRIMARY KEY(id),
- FOREIGN KEY(author) REFERENCES users,
CHECK (format IN ('markdown'))
);
-CREATE TABLE lazychat_targets(
+CREATE TABLE post_targets(
message INTEGER NOT NULL,
- target INTEGER NOT NULL,
+ post INTEGER NOT NULL,
PRIMARY KEY(message, target),
- FOREIGN KEY(message) REFERENCES lazychat_messages,
+ --FOREIGN KEY(message) REFERENCES posts AND CHILDREN,
FOREIGN KEY(target) REFERENCES roles
);
@@ -162,8 +161,8 @@ CREATE TABLE lazychat_references(
referrer INTEGER NOT NULL,
referee INTEGER NOT NULL,
PRIMARY KEY(referrer, referee),
- FOREIGN KEY(referrer) REFERENCES lazychat_messages,
- FOREIGN KEY(referee) REFERENCES lazychat_messages
+ --FOREIGN KEY(referee) REFERENCES posts AND CHILDREN,
+ FOREIGN KEY(referrer) REFERENCES lazychat_messages
);
CREATE TABLE user_default_target(
@@ -239,13 +238,18 @@ CREATE TRIGGER new_user_create_user_role
FOR EACH ROW
EXECUTE PROCEDURE new_user_create_user_role();
-CREATE VIEW user_visible_lazychat_messages AS
+CREATE VIEW user_visible_posts AS
SELECT eur.user, t.message
- FROM effective_user_roles eur, lazychat_targets t
+ FROM effective_user_roles eur, post_targets t
WHERE t.target = eur.role
UNION
- SELECT m.author, m.message
- FROM lazychat_messages m;
+ SELECT m.owner, m.message
+ FROM posts m;
+
+CREATE VIEW user_visible_lazychat_messages AS
+ SELECT uvp.user, uvp.message
+ FROM user_visible_posts uvp
+ INNER JOIN lazychat_messages lm ON lm.id = uvp.message;
ROLLBACK;
--COMMIT;
diff --git a/src/mulk/benki/lazychat.clj b/src/mulk/benki/lazychat.clj
index 75b0b16..fefb487 100644
--- a/src/mulk/benki/lazychat.clj
+++ b/src/mulk/benki/lazychat.clj
@@ -62,18 +62,18 @@
[:referrer :referee]
[id (int referee)]))
(doseq [target targets]
- (sql/insert-values :lazychat_targets
+ (sql/insert-values :post_targets
[:message :target]
[id (int target)]))
(case visibility
("public")
(sql/do-prepared
- "INSERT INTO lazychat_targets
+ "INSERT INTO post_targets
SELECT ?, role FROM role_tags WHERE tag = 'world'"
[id])
("protected")
(sql/do-prepared
- "INSERT INTO lazychat_targets
+ "INSERT INTO post_targets
SELECT ?, target FROM user_default_target WHERE (\"user\" = ?)"
[id user])
("private")
@@ -113,7 +113,7 @@
WHERE id = ?"
id)
referees (map :referee (query "SELECT referee FROM lazychat_references WHERE referrer = ?" id))
- targets (map :target (query "SELECT target FROM lazychat_targets WHERE message = ?" id))]
+ targets (map :target (query "SELECT target FROM post_targets WHERE message = ?" id))]
(and message
(assoc message
:referees referees