PostgreSQL Source Code  git master
test_rls_hooks.h File Reference
Include dependency graph for test_rls_hooks.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

Listtest_rls_hooks_permissive (CmdType cmdtype, Relation relation)
 
Listtest_rls_hooks_restrictive (CmdType cmdtype, Relation relation)
 

Function Documentation

◆ test_rls_hooks_permissive()

List* test_rls_hooks_permissive ( CmdType  cmdtype,
Relation  relation 
)

Definition at line 45 of file test_rls_hooks.c.

46 {
47  List *policies = NIL;
48  RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
49  Datum role;
50  FuncCall *n;
51  Node *e;
52  ColumnRef *c;
53  ParseState *qual_pstate;
54  ParseNamespaceItem *nsitem;
55 
56  if (strcmp(RelationGetRelationName(relation), "rls_test_permissive") != 0 &&
57  strcmp(RelationGetRelationName(relation), "rls_test_both") != 0)
58  return NIL;
59 
60  qual_pstate = make_parsestate(NULL);
61 
62  nsitem = addRangeTableEntryForRelation(qual_pstate,
63  relation, AccessShareLock,
64  NULL, false, false);
65  addNSItemToQuery(qual_pstate, nsitem, false, true, true);
66 
68 
69  policy->policy_name = pstrdup("extension policy");
70  policy->polcmd = '*';
71  policy->roles = construct_array_builtin(&role, 1, OIDOID);
72 
73  /*
74  * policy->qual = (Expr *) makeConst(BOOLOID, -1, InvalidOid,
75  * sizeof(bool), BoolGetDatum(true), false, true);
76  */
77 
78  n = makeFuncCall(list_make2(makeString("pg_catalog"),
79  makeString("current_user")),
80  NIL,
82  -1);
83 
84  c = makeNode(ColumnRef);
85  c->fields = list_make1(makeString("username"));
86  c->location = 0;
87 
88  e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
89 
90  policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
92  "POLICY");
93  /* Fix up collation information */
94  assign_expr_collations(qual_pstate, (Node *) policy->qual);
95 
96  policy->with_check_qual = copyObject(policy->qual);
97  policy->hassublinks = false;
98 
99  policies = list_make1(policy);
100 
101  return policies;
102 }
#define ACL_ID_PUBLIC
Definition: acl.h:46
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3381
#define AccessShareLock
Definition: lockdefs.h:36
FuncCall * makeFuncCall(List *name, List *args, CoercionForm funcformat, int location)
Definition: makefuncs.c:603
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void * palloc0(Size size)
Definition: mcxt.c:1347
#define copyObject(obj)
Definition: nodes.h:224
#define makeNode(_type_)
Definition: nodes.h:155
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
void assign_expr_collations(ParseState *pstate, Node *expr)
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:39
@ EXPR_KIND_POLICY
Definition: parse_node.h:78
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
@ AEXPR_OP
Definition: parsenodes.h:315
#define NIL
Definition: pg_list.h:68
#define list_make1(x1)
Definition: pg_list.h:212
#define list_make2(x1, x2)
Definition: pg_list.h:214
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
char * c
e
Definition: preproc-init.c:82
@ COERCE_EXPLICIT_CALL
Definition: primnodes.h:734
#define RelationGetRelationName(relation)
Definition: rel.h:539
Definition: pg_list.h:54
Definition: nodes.h:129
ArrayType * roles
Definition: rowsecurity.h:24
Expr * with_check_qual
Definition: rowsecurity.h:27
String * makeString(char *str)
Definition: value.c:63

References AccessShareLock, ACL_ID_PUBLIC, addNSItemToQuery(), addRangeTableEntryForRelation(), AEXPR_OP, assign_expr_collations(), COERCE_EXPLICIT_CALL, construct_array_builtin(), copyObject, EXPR_KIND_POLICY, RowSecurityPolicy::hassublinks, list_make1, list_make2, make_parsestate(), makeFuncCall(), makeNode, makeSimpleA_Expr(), makeString(), NIL, ObjectIdGetDatum(), palloc0(), RowSecurityPolicy::polcmd, RowSecurityPolicy::policy_name, pstrdup(), RowSecurityPolicy::qual, RelationGetRelationName, RowSecurityPolicy::roles, transformWhereClause(), and RowSecurityPolicy::with_check_qual.

Referenced by _PG_init().

◆ test_rls_hooks_restrictive()

List* test_rls_hooks_restrictive ( CmdType  cmdtype,
Relation  relation 
)

Definition at line 113 of file test_rls_hooks.c.

114 {
115  List *policies = NIL;
116  RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
117  Datum role;
118  FuncCall *n;
119  Node *e;
120  ColumnRef *c;
121  ParseState *qual_pstate;
122  ParseNamespaceItem *nsitem;
123 
124  if (strcmp(RelationGetRelationName(relation), "rls_test_restrictive") != 0 &&
125  strcmp(RelationGetRelationName(relation), "rls_test_both") != 0)
126  return NIL;
127 
128  qual_pstate = make_parsestate(NULL);
129 
130  nsitem = addRangeTableEntryForRelation(qual_pstate,
131  relation, AccessShareLock,
132  NULL, false, false);
133  addNSItemToQuery(qual_pstate, nsitem, false, true, true);
134 
136 
137  policy->policy_name = pstrdup("extension policy");
138  policy->polcmd = '*';
139  policy->roles = construct_array_builtin(&role, 1, OIDOID);
140 
141  n = makeFuncCall(list_make2(makeString("pg_catalog"),
142  makeString("current_user")),
143  NIL,
145  -1);
146 
147  c = makeNode(ColumnRef);
148  c->fields = list_make1(makeString("supervisor"));
149  c->location = 0;
150 
151  e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
152 
153  policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
155  "POLICY");
156  /* Fix up collation information */
157  assign_expr_collations(qual_pstate, (Node *) policy->qual);
158 
159  policy->with_check_qual = copyObject(policy->qual);
160  policy->hassublinks = false;
161 
162  policies = list_make1(policy);
163 
164  return policies;
165 }

References AccessShareLock, ACL_ID_PUBLIC, addNSItemToQuery(), addRangeTableEntryForRelation(), AEXPR_OP, assign_expr_collations(), COERCE_EXPLICIT_CALL, construct_array_builtin(), copyObject, EXPR_KIND_POLICY, RowSecurityPolicy::hassublinks, list_make1, list_make2, make_parsestate(), makeFuncCall(), makeNode, makeSimpleA_Expr(), makeString(), NIL, ObjectIdGetDatum(), palloc0(), RowSecurityPolicy::polcmd, RowSecurityPolicy::policy_name, pstrdup(), RowSecurityPolicy::qual, RelationGetRelationName, RowSecurityPolicy::roles, transformWhereClause(), and RowSecurityPolicy::with_check_qual.

Referenced by _PG_init().