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 47 of file test_rls_hooks.c.

48 {
49  List *policies = NIL;
50  RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
51  Datum role;
52  FuncCall *n;
53  Node *e;
54  ColumnRef *c;
55  ParseState *qual_pstate;
56  ParseNamespaceItem *nsitem;
57 
58  if (strcmp(RelationGetRelationName(relation), "rls_test_permissive") != 0 &&
59  strcmp(RelationGetRelationName(relation), "rls_test_both") != 0)
60  return NIL;
61 
62  qual_pstate = make_parsestate(NULL);
63 
64  nsitem = addRangeTableEntryForRelation(qual_pstate,
65  relation, AccessShareLock,
66  NULL, false, false);
67  addNSItemToQuery(qual_pstate, nsitem, false, true, true);
68 
70 
71  policy->policy_name = pstrdup("extension policy");
72  policy->polcmd = '*';
73  policy->roles = construct_array(&role, 1, OIDOID, sizeof(Oid), true, TYPALIGN_INT);
74 
75  /*
76  * policy->qual = (Expr *) makeConst(BOOLOID, -1, InvalidOid,
77  * sizeof(bool), BoolGetDatum(true), false, true);
78  */
79 
80  n = makeFuncCall(list_make2(makeString("pg_catalog"),
81  makeString("current_user")),
82  NIL,
84  -1);
85 
86  c = makeNode(ColumnRef);
87  c->fields = list_make1(makeString("username"));
88  c->location = 0;
89 
90  e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
91 
92  policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
94  "POLICY");
95  /* Fix up collation information */
96  assign_expr_collations(qual_pstate, (Node *) policy->qual);
97 
98  policy->with_check_qual = copyObject(policy->qual);
99  policy->hassublinks = false;
100 
101  policies = list_make1(policy);
102 
103  return policies;
104 }
#define ACL_ID_PUBLIC
Definition: acl.h:46
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3319
#define AccessShareLock
Definition: lockdefs.h:36
FuncCall * makeFuncCall(List *name, List *args, CoercionForm funcformat, int location)
Definition: makefuncs.c:586
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:49
char * pstrdup(const char *in)
Definition: mcxt.c:1305
void * palloc0(Size size)
Definition: mcxt.c:1099
#define copyObject(obj)
Definition: nodes.h:689
#define makeNode(_type_)
Definition: nodes.h:621
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:43
@ EXPR_KIND_POLICY
Definition: parse_node.h:77
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:271
#define NIL
Definition: pg_list.h:65
#define list_make1(x1)
Definition: pg_list.h:206
#define list_make2(x1, x2)
Definition: pg_list.h:208
uintptr_t Datum
Definition: postgres.h:411
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
unsigned int Oid
Definition: postgres_ext.h:31
char * c
e
Definition: preproc-init.c:82
@ COERCE_EXPLICIT_CALL
Definition: primnodes.h:492
#define RelationGetRelationName(relation)
Definition: rel.h:522
Definition: pg_list.h:51
Definition: nodes.h:574
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(), 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 115 of file test_rls_hooks.c.

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

References AccessShareLock, ACL_ID_PUBLIC, addNSItemToQuery(), addRangeTableEntryForRelation(), AEXPR_OP, assign_expr_collations(), COERCE_EXPLICIT_CALL, construct_array(), 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().