我得到了使用这个 PostgreSQL 代码创建的数据库“比基尼环礁”:
CREATE TABLE TESTA (
name character varying NOT NULL,
age integer NOT NULL,
PRIMARY KEY(name)
);
CREATE DOMAIN RANK AS character varying
CHECK (
VALUE = 'bigmac' OR
VALUE = 'smallmac'
);
CREATE TABLE TESTB (
name character varying NOT NULL,
rank character varying NOT NULL,
PRIMARY KEY(name)
);
CREATE OR REPLACE VIEW TESTAB AS
SELECT * FROM (TESTA
JOIN
TESTB
ON TESTA.name = TESTB.name);
CREATE OR REPLACE RULE TESTAB_INSERT
AS ON INSERT TO TESTAB
DO INSTEAD (
INSERT INTO TESTA (name,age) VALUES (NEW.name,NEW.age);
INSERT INTO TESTB (name,rank) VALUES (NEW.name,NEW.rank);
)
CREATE OR REPLACE FUNCTION testRankToAge() RETURNS TRIGGER AS $$
DECLARE
ageRes integer;
BEGIN
SELECT AGE INTO ageRes
FROM TESTA
WHERE name = NEW.name;
IF (ageRes < 42) AND (NEW.rank = 'bigmac')
THEN
RAISE EXCEPTION 'YOU CANNOT BE BIGMAC AT THAT AGE';
END IF;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE TRIGGER rankToAgeTrigger
AFTER INSERT
ON TESTB
FOR EACH ROW
EXECUTE PROCEDURE testRankToAge();
DROP ROLE IF EXISTS testUser;
CREATE ROLE testUser WITH PASSWORD '123456' LOGIN;
GRANT INSERT ON TESTAB TO testUser;
现在我想让我的(Common-LISP)程序做一个简单的插入:
(clsql:file-enable-sql-reader-syntax)
(clsql-sys:with-database (db (list "localhost" "bikini-atoll" "testuser" "123456") :database-type :postgresql)
(clsql-sys:insert-records :into [TESTAB]
:attributes '([NAME] [AGE] [RANK])
:values '("sir" 44 "bigmac")
:database db))
这基本上是一个:
INSERT INTO TESTAB (NAME,AGE,RANK) VALUES ('sir',44,'bigmac');
作为用户 testuser (我怀疑这是一个特定于程序语言的问题,以及我的一些糟糕的数据库/触发器设计)。
但是(出乎意料的)结果是:
Error 42501 / FEHLER: no permission for relation testa
CONTEXT: SQL-COMMAND „SELECT AGE FROM TESTA
WHERE name = NEW.name“
PL/pgSQL-Funktion testranktoage() LINE 5 AT SQL-Command
has occurred.
这表明 testuser 还需要SELECT
触发器可能在其上执行操作的任何表的权限,这似乎有点奇怪,因为我认为视图的主要原因之一是限制/过滤用户对表的访问,这将与授予SELECT
一致性触发器所需的任何内容相反。
如何修复/防止这种初露端倪的许可破坏?