-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
71 lines (57 loc) · 2.11 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import typing
import pydantic
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import declarative_base, relationship
from sqlalchemy.sql import column
from rls.register_rls import register_rls
from rls.schemas import Command, ConditionArg, Permissive
Base: typing.Any = register_rls(declarative_base())
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True, index=True)
__rls_policies__ = [
Permissive(
condition_args=[
ConditionArg(comparator_name="account_id", type=Integer),
],
cmd=[Command.select, Command.update],
custom_expr=lambda x: column("id") == x,
custom_policy_name="equal_to_accountId_policy",
),
]
class Item(Base):
__tablename__ = "items"
id = Column(Integer, primary_key=True, index=True)
title = Column(String, index=True)
description = Column(String)
owner_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"))
owner = relationship("User")
__rls_policies__ = [
Permissive(
condition_args=[
ConditionArg(comparator_name="account_id", type=Integer),
],
cmd=[Command.select, Command.update],
custom_expr=lambda x: column("owner_id") == x,
custom_policy_name="equal_to_accountId_policy",
),
Permissive(
condition_args=[
ConditionArg(comparator_name="account_id", type=Integer),
],
cmd=[Command.select],
custom_expr=lambda x: column("owner_id") > x,
custom_policy_name="greater_than_accountId_policy",
),
Permissive(
condition_args=[
ConditionArg(comparator_name="account_id", type=Integer),
],
cmd=[Command.all],
custom_expr=lambda x: column("owner_id") <= x,
custom_policy_name="smaller_than_or_equal_accountId_policy",
),
]
class SampleRlsContext(pydantic.BaseModel):
account_id: int | None