考虑以下模式:授权邀请用户通过数据库触发器
Table "public.users"
Column | Type | Modifiers
----------------------+--------------------------+------------------------------------------------------------
uuid | uuid | not null default uuid_generate_v4()
email | character varying(254) | not null
name | text | not null
created_at | timestamp with time zone | not null default now()
Table "public.users_projects"
Column | Type | Modifiers
-----------------+--------------------------+--------------------------------------------
project_uuid | uuid | not null
user_uuid | uuid | not null
invitation_uuid | uuid |
membership_type | membership_type | not null default 'member'::membership_type
created_at | timestamp with time zone | not null default now()
Table "public.projects"
Column | Type | Modifiers
------------+--------------------------+-------------------------------------
uuid | uuid | not null default uuid_generate_v4()
name | character varying(100) | not null
created_at | timestamp with time zone | not null default now()
Table "public.invitations"
Column | Type | Modifiers
-----------------+--------------------------+--------------------------------------------
uuid | uuid | not null default uuid_generate_v4()
email | character varying(254) | not null
target_type | character varying(50) | not null
target_uuid | uuid | not null
membership_type | membership_type | not null default 'member'::membership_type
token | character varying(32) | not null default md5((random())::text)
creator_uuid | uuid | not null
也有一个枚举类型CREATE TYPE membership_type AS ENUM ('guest', 'member', 'manager', 'owner');
。
我想知道如果我可以使用触发器来验证用户不能创建一个资源的邀请,至少没有连接表上的成员类型manager
。
我的主要问题是:
- 我可以将该行插入“之前”使用触发器,
CREATE TRIGGER check_authorisation BEFORE INSERT OR UPDATE ON invitations FOR EACH ROW EXECUTE PROCEDURE check_authorisation();
- 我多少信息可以给回一条错误消息,因为对刚刚失败,因为数据库连接断开等
- 这是一个理智的方法吗?作为一个Web应用程序开发人员,我习惯于在应用程序代码中检查这个环境,但是我正在处理将由两个应用程序共享的数据库(Golang和Ruby)
- 我可以实现
REFERENCES
在多态字段(target_type
,target_uuid
)以某种方式?