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

References AccessShareLock, ACL_ID_PUBLIC, addRangeTableEntryForRelation(), addRTEtoQuery(), AEXPR_OP, assign_expr_collations(), construct_array(), copyObject, EXPR_KIND_POLICY, ColumnRef::fields, RowSecurityPolicy::hassublinks, list_make1, list_make2, ColumnRef::location, 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().

68 {
69  List *policies = NIL;
70  RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
71  Datum role;
72  FuncCall *n;
73  Node *e;
74  ColumnRef *c;
75  ParseState *qual_pstate;
76  RangeTblEntry *rte;
77 
78  if (strcmp(RelationGetRelationName(relation), "rls_test_permissive") != 0 &&
79  strcmp(RelationGetRelationName(relation), "rls_test_both") != 0)
80  return NIL;
81 
82  qual_pstate = make_parsestate(NULL);
83 
84  rte = addRangeTableEntryForRelation(qual_pstate, relation, AccessShareLock,
85  NULL, false, false);
86  addRTEtoQuery(qual_pstate, rte, false, true, true);
87 
89 
90  policy->policy_name = pstrdup("extension policy");
91  policy->polcmd = '*';
92  policy->roles = construct_array(&role, 1, OIDOID, sizeof(Oid), true, 'i');
93 
94  /*
95  * policy->qual = (Expr *) makeConst(BOOLOID, -1, InvalidOid,
96  * sizeof(bool), BoolGetDatum(true), false, true);
97  */
98 
99  n = makeFuncCall(list_make2(makeString("pg_catalog"),
100  makeString("current_user")), NIL, 0);
101 
102  c = makeNode(ColumnRef);
103  c->fields = list_make1(makeString("username"));
104  c->location = 0;
105 
106  e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
107 
108  policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
110  "POLICY");
111  /* Fix up collation information */
112  assign_expr_collations(qual_pstate, (Node *) policy->qual);
113 
114  policy->with_check_qual = copyObject(policy->qual);
115  policy->hassublinks = false;
116 
117  policies = list_make1(policy);
118 
119  return policies;
120 }
#define list_make2(x1, x2)
Definition: pg_list.h:229
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
char * pstrdup(const char *in)
Definition: mcxt.c:1186
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3291
#define AccessShareLock
Definition: lockdefs.h:36
Definition: nodes.h:525
unsigned int Oid
Definition: postgres_ext.h:31
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
int location
Definition: parsenodes.h:236
#define list_make1(x1)
Definition: pg_list.h:227
void assign_expr_collations(ParseState *pstate, Node *expr)
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
char * c
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
Expr * with_check_qual
Definition: rowsecurity.h:27
#define RelationGetRelationName(relation)
Definition: rel.h:453
ArrayType * roles
Definition: rowsecurity.h:24
void addRTEtoQuery(ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
FuncCall * makeFuncCall(List *name, List *args, int location)
Definition: makefuncs.c:583
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
#define makeNode(_type_)
Definition: nodes.h:573
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
e
Definition: preproc-init.c:82
#define ACL_ID_PUBLIC
Definition: acl.h:46
#define copyObject(obj)
Definition: nodes.h:641
Definition: pg_list.h:50
List * fields
Definition: parsenodes.h:235

◆ test_rls_hooks_restrictive()

List* test_rls_hooks_restrictive ( CmdType  cmdtype,
Relation  relation 
)

Definition at line 131 of file test_rls_hooks.c.

References AccessShareLock, ACL_ID_PUBLIC, addRangeTableEntryForRelation(), addRTEtoQuery(), AEXPR_OP, assign_expr_collations(), construct_array(), copyObject, EXPR_KIND_POLICY, ColumnRef::fields, RowSecurityPolicy::hassublinks, list_make1, list_make2, ColumnRef::location, 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().

132 {
133  List *policies = NIL;
134  RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
135  Datum role;
136  FuncCall *n;
137  Node *e;
138  ColumnRef *c;
139  ParseState *qual_pstate;
140  RangeTblEntry *rte;
141 
142 
143  if (strcmp(RelationGetRelationName(relation), "rls_test_restrictive") != 0 &&
144  strcmp(RelationGetRelationName(relation), "rls_test_both") != 0)
145  return NIL;
146 
147  qual_pstate = make_parsestate(NULL);
148 
149  rte = addRangeTableEntryForRelation(qual_pstate, relation, AccessShareLock,
150  NULL, false, false);
151  addRTEtoQuery(qual_pstate, rte, false, true, true);
152 
154 
155  policy->policy_name = pstrdup("extension policy");
156  policy->polcmd = '*';
157  policy->roles = construct_array(&role, 1, OIDOID, sizeof(Oid), true, 'i');
158 
159  n = makeFuncCall(list_make2(makeString("pg_catalog"),
160  makeString("current_user")), NIL, 0);
161 
162  c = makeNode(ColumnRef);
163  c->fields = list_make1(makeString("supervisor"));
164  c->location = 0;
165 
166  e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
167 
168  policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
170  "POLICY");
171  /* Fix up collation information */
172  assign_expr_collations(qual_pstate, (Node *) policy->qual);
173 
174  policy->with_check_qual = copyObject(policy->qual);
175  policy->hassublinks = false;
176 
177  policies = list_make1(policy);
178 
179  return policies;
180 }
#define list_make2(x1, x2)
Definition: pg_list.h:229
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:65
char * pstrdup(const char *in)
Definition: mcxt.c:1186
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3291
#define AccessShareLock
Definition: lockdefs.h:36
Definition: nodes.h:525
unsigned int Oid
Definition: postgres_ext.h:31
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
int location
Definition: parsenodes.h:236
#define list_make1(x1)
Definition: pg_list.h:227
void assign_expr_collations(ParseState *pstate, Node *expr)
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
char * c
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
Expr * with_check_qual
Definition: rowsecurity.h:27
#define RelationGetRelationName(relation)
Definition: rel.h:453
ArrayType * roles
Definition: rowsecurity.h:24
void addRTEtoQuery(ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
FuncCall * makeFuncCall(List *name, List *args, int location)
Definition: makefuncs.c:583
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
#define makeNode(_type_)
Definition: nodes.h:573
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
e
Definition: preproc-init.c:82
#define ACL_ID_PUBLIC
Definition: acl.h:46
#define copyObject(obj)
Definition: nodes.h:641
Definition: pg_list.h:50
List * fields
Definition: parsenodes.h:235