Skip to content

Commit e47cbb3

Browse files
committed
Add has_tablespace_privilege().
Christopher Kings-Lynne
1 parent 1a0f3e4 commit e47cbb3

File tree

5 files changed

+254
-6
lines changed

5 files changed

+254
-6
lines changed

doc/src/sgml/func.sgml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.213 2004/07/02 22:49:45 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.214 2004/07/12 20:23:47 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -6980,6 +6980,21 @@ SELECT set_config('log_statement_stats', 'off', false);
69806980
<entry><type>boolean</type></entry>
69816981
<entry>does current user have privilege for schema</entry>
69826982
</row>
6983+
<row>
6984+
<entry><literal><function>has_tablespace_privilege</function>(<parameter>user</parameter>,
6985+
<parameter>tablespace</parameter>,
6986+
<parameter>privilege</parameter>)</literal>
6987+
</entry>
6988+
<entry><type>boolean</type></entry>
6989+
<entry>does user have privilege for tablespace</entry>
6990+
</row>
6991+
<row>
6992+
<entry><literal><function>has_tablespace_privilege</function>(<parameter>tablespace</parameter>,
6993+
<parameter>privilege</parameter>)</literal>
6994+
</entry>
6995+
<entry><type>boolean</type></entry>
6996+
<entry>does current user have privilege for tablespace</entry>
6997+
</row>
69836998
</tbody>
69846999
</tgroup>
69857000
</table>
@@ -6999,6 +7014,9 @@ SELECT set_config('log_statement_stats', 'off', false);
69997014
<indexterm zone="functions-misc">
70007015
<primary>has_schema_privilege</primary>
70017016
</indexterm>
7017+
<indexterm zone="functions-misc">
7018+
<primary>has_tablespace_privilege</primary>
7019+
</indexterm>
70027020

70037021
<para>
70047022
<function>has_table_privilege</function> checks whether a user
@@ -7064,6 +7082,14 @@ SELECT has_function_privilege('joeuser', 'myfunc(int, text)', 'execute');
70647082
<literal>USAGE</literal>.
70657083
</para>
70667084

7085+
<para>
7086+
<function>has_tablespace_privilege</function> checks whether a user
7087+
can access a tablespace in a particular way. The possibilities for its
7088+
arguments are analogous to <function>has_table_privilege</function>.
7089+
The desired access privilege type must evaluate to
7090+
<literal>CREATE</literal>.
7091+
</para>
7092+
70677093
<para>
70687094
To evaluate whether a user holds a grant option on the privilege,
70697095
append <literal> WITH GRANT OPTION</literal> to the privilege key

src/backend/utils/adt/acl.c

Lines changed: 205 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/acl.c,v 1.106 2004/06/18 06:13:49 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/acl.c,v 1.107 2004/07/12 20:23:50 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -21,6 +21,7 @@
2121
#include "catalog/pg_shadow.h"
2222
#include "catalog/pg_type.h"
2323
#include "commands/dbcommands.h"
24+
#include "commands/tablespace.h"
2425
#include "miscadmin.h"
2526
#include "utils/acl.h"
2627
#include "utils/builtins.h"
@@ -54,6 +55,8 @@ static Oid convert_language_name(text *languagename);
5455
static AclMode convert_language_priv_string(text *priv_type_text);
5556
static Oid convert_schema_name(text *schemaname);
5657
static AclMode convert_schema_priv_string(text *priv_type_text);
58+
static Oid convert_tablespace_name(text *tablespacename);
59+
static AclMode convert_tablespace_priv_string(text *priv_type_text);
5760

5861

5962
/*
@@ -2207,3 +2210,204 @@ convert_schema_priv_string(text *priv_type_text)
22072210
errmsg("unrecognized privilege type: \"%s\"", priv_type)));
22082211
return ACL_NO_RIGHTS; /* keep compiler quiet */
22092212
}
2213+
2214+
/*
2215+
* has_tablespace_privilege variants
2216+
* These are all named "has_tablespace_privilege" at the SQL level.
2217+
* They take various combinations of tablespace name, tablespace OID,
2218+
* user name, user sysid, or implicit user = current_user.
2219+
*
2220+
* The result is a boolean value: true if user has the indicated
2221+
* privilege, false if not.
2222+
*/
2223+
2224+
/*
2225+
* has_tablespace_privilege_name_name
2226+
* Check user privileges on a tablespace given
2227+
* name username, text tablespacename, and text priv name.
2228+
*/
2229+
Datum
2230+
has_tablespace_privilege_name_name(PG_FUNCTION_ARGS)
2231+
{
2232+
Name username = PG_GETARG_NAME(0);
2233+
text *tablespacename = PG_GETARG_TEXT_P(1);
2234+
text *priv_type_text = PG_GETARG_TEXT_P(2);
2235+
int32 usesysid;
2236+
Oid tablespaceoid;
2237+
AclMode mode;
2238+
AclResult aclresult;
2239+
2240+
usesysid = get_usesysid(NameStr(*username));
2241+
tablespaceoid = convert_tablespace_name(tablespacename);
2242+
mode = convert_tablespace_priv_string(priv_type_text);
2243+
2244+
aclresult = pg_tablespace_aclcheck(tablespaceoid, usesysid, mode);
2245+
2246+
PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2247+
}
2248+
2249+
/*
2250+
* has_tablespace_privilege_name
2251+
* Check user privileges on a tablespace given
2252+
* text tablespacename and text priv name.
2253+
* current_user is assumed
2254+
*/
2255+
Datum
2256+
has_tablespace_privilege_name(PG_FUNCTION_ARGS)
2257+
{
2258+
text *tablespacename = PG_GETARG_TEXT_P(0);
2259+
text *priv_type_text = PG_GETARG_TEXT_P(1);
2260+
AclId usesysid;
2261+
Oid tablespaceoid;
2262+
AclMode mode;
2263+
AclResult aclresult;
2264+
2265+
usesysid = GetUserId();
2266+
tablespaceoid = convert_tablespace_name(tablespacename);
2267+
mode = convert_tablespace_priv_string(priv_type_text);
2268+
2269+
aclresult = pg_tablespace_aclcheck(tablespaceoid, usesysid, mode);
2270+
2271+
PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2272+
}
2273+
2274+
/*
2275+
* has_tablespace_privilege_name_id
2276+
* Check user privileges on a tablespace given
2277+
* name usename, tablespace oid, and text priv name.
2278+
*/
2279+
Datum
2280+
has_tablespace_privilege_name_id(PG_FUNCTION_ARGS)
2281+
{
2282+
Name username = PG_GETARG_NAME(0);
2283+
Oid tablespaceoid = PG_GETARG_OID(1);
2284+
text *priv_type_text = PG_GETARG_TEXT_P(2);
2285+
int32 usesysid;
2286+
AclMode mode;
2287+
AclResult aclresult;
2288+
2289+
usesysid = get_usesysid(NameStr(*username));
2290+
mode = convert_tablespace_priv_string(priv_type_text);
2291+
2292+
aclresult = pg_tablespace_aclcheck(tablespaceoid, usesysid, mode);
2293+
2294+
PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2295+
}
2296+
2297+
/*
2298+
* has_tablespace_privilege_id
2299+
* Check user privileges on a tablespace given
2300+
* tablespace oid, and text priv name.
2301+
* current_user is assumed
2302+
*/
2303+
Datum
2304+
has_tablespace_privilege_id(PG_FUNCTION_ARGS)
2305+
{
2306+
Oid tablespaceoid = PG_GETARG_OID(0);
2307+
text *priv_type_text = PG_GETARG_TEXT_P(1);
2308+
AclId usesysid;
2309+
AclMode mode;
2310+
AclResult aclresult;
2311+
2312+
usesysid = GetUserId();
2313+
mode = convert_tablespace_priv_string(priv_type_text);
2314+
2315+
aclresult = pg_tablespace_aclcheck(tablespaceoid, usesysid, mode);
2316+
2317+
PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2318+
}
2319+
2320+
/*
2321+
* has_tablespace_privilege_id_name
2322+
* Check user privileges on a tablespace given
2323+
* usesysid, text tablespacename, and text priv name.
2324+
*/
2325+
Datum
2326+
has_tablespace_privilege_id_name(PG_FUNCTION_ARGS)
2327+
{
2328+
int32 usesysid = PG_GETARG_INT32(0);
2329+
text *tablespacename = PG_GETARG_TEXT_P(1);
2330+
text *priv_type_text = PG_GETARG_TEXT_P(2);
2331+
Oid tablespaceoid;
2332+
AclMode mode;
2333+
AclResult aclresult;
2334+
2335+
tablespaceoid = convert_tablespace_name(tablespacename);
2336+
mode = convert_tablespace_priv_string(priv_type_text);
2337+
2338+
aclresult = pg_tablespace_aclcheck(tablespaceoid, usesysid, mode);
2339+
2340+
PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2341+
}
2342+
2343+
/*
2344+
* has_tablespace_privilege_id_id
2345+
* Check user privileges on a tablespace given
2346+
* usesysid, tablespace oid, and text priv name.
2347+
*/
2348+
Datum
2349+
has_tablespace_privilege_id_id(PG_FUNCTION_ARGS)
2350+
{
2351+
int32 usesysid = PG_GETARG_INT32(0);
2352+
Oid tablespaceoid = PG_GETARG_OID(1);
2353+
text *priv_type_text = PG_GETARG_TEXT_P(2);
2354+
AclMode mode;
2355+
AclResult aclresult;
2356+
2357+
mode = convert_tablespace_priv_string(priv_type_text);
2358+
2359+
aclresult = pg_tablespace_aclcheck(tablespaceoid, usesysid, mode);
2360+
2361+
PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
2362+
}
2363+
2364+
/*
2365+
* Support routines for has_tablespace_privilege family.
2366+
*/
2367+
2368+
/*
2369+
* Given a tablespace name expressed as a string, look it up and return Oid
2370+
*/
2371+
static Oid
2372+
convert_tablespace_name(text *tablespacename)
2373+
{
2374+
char *spcname;
2375+
Oid oid;
2376+
2377+
spcname = DatumGetCString(DirectFunctionCall1(textout,
2378+
PointerGetDatum(tablespacename)));
2379+
oid = get_tablespace_oid(spcname);
2380+
2381+
if (!OidIsValid(oid))
2382+
ereport(ERROR,
2383+
(errcode(ERRCODE_UNDEFINED_OBJECT),
2384+
errmsg("tablespace \"%s\" does not exist", spcname)));
2385+
2386+
return oid;
2387+
}
2388+
2389+
/*
2390+
* convert_tablespace_priv_string
2391+
* Convert text string to AclMode value.
2392+
*/
2393+
static AclMode
2394+
convert_tablespace_priv_string(text *priv_type_text)
2395+
{
2396+
char *priv_type;
2397+
2398+
priv_type = DatumGetCString(DirectFunctionCall1(textout,
2399+
PointerGetDatum(priv_type_text)));
2400+
2401+
/*
2402+
* Return mode from priv_type string
2403+
*/
2404+
if (pg_strcasecmp(priv_type, "CREATE") == 0)
2405+
return ACL_CREATE;
2406+
if (pg_strcasecmp(priv_type, "CREATE WITH GRANT OPTION") == 0)
2407+
return ACL_GRANT_OPTION_FOR(ACL_CREATE);
2408+
2409+
ereport(ERROR,
2410+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2411+
errmsg("unrecognized privilege type: \"%s\"", priv_type)));
2412+
return ACL_NO_RIGHTS; /* keep compiler quiet */
2413+
}

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.243 2004/07/02 22:49:48 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.244 2004/07/12 20:23:51 momjian Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200407022
56+
#define CATALOG_VERSION_NO 200407121
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.341 2004/07/02 22:49:48 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.342 2004/07/12 20:23:53 momjian Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -3181,6 +3181,18 @@ DESCR("current user privilege on schema by schema name");
31813181
DATA(insert OID = 2273 ( has_schema_privilege PGNSP PGUID 12 f f t f s 2 16 "26 25" _null_ has_schema_privilege_id - _null_ ));
31823182
DESCR("current user privilege on schema by schema oid");
31833183

3184+
DATA(insert OID = 2390 ( has_tablespace_privilege PGNSP PGUID 12 f f t f s 3 16 "19 25 25" _null_ has_tablespace_privilege_name_name - _null_ ));
3185+
DESCR("user privilege on tablespace by username, tablespace name");
3186+
DATA(insert OID = 2391 ( has_tablespace_privilege PGNSP PGUID 12 f f t f s 3 16 "19 26 25" _null_ has_tablespace_privilege_name_id - _null_ ));
3187+
DESCR("user privilege on tablespace by username, tablespace oid");
3188+
DATA(insert OID = 2392 ( has_tablespace_privilege PGNSP PGUID 12 f f t f s 3 16 "23 25 25" _null_ has_tablespace_privilege_id_name - _null_ ));
3189+
DESCR("user privilege on tablespace by usesysid, tablespace name");
3190+
DATA(insert OID = 2393 ( has_tablespace_privilege PGNSP PGUID 12 f f t f s 3 16 "23 26 25" _null_ has_tablespace_privilege_id_id - _null_ ));
3191+
DESCR("user privilege on tablespace by usesysid, tablespace oid");
3192+
DATA(insert OID = 2394 ( has_tablespace_privilege PGNSP PGUID 12 f f t f s 2 16 "25 25" _null_ has_tablespace_privilege_name - _null_ ));
3193+
DESCR("current user privilege on tablespace by tablespace name");
3194+
DATA(insert OID = 2395 ( has_tablespace_privilege PGNSP PGUID 12 f f t f s 2 16 "26 25" _null_ has_tablespace_privilege_id - _null_ ));
3195+
DESCR("current user privilege on tablespace by tablespace oid");
31843196

31853197
DATA(insert OID = 2290 ( record_in PGNSP PGUID 12 f f t f v 2 2249 "2275 26" _null_ record_in - _null_ ));
31863198
DESCR("I/O");

src/include/utils/builtins.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.245 2004/07/02 18:59:25 joe Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.246 2004/07/12 20:23:59 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -52,6 +52,12 @@ extern Datum has_schema_privilege_id_name(PG_FUNCTION_ARGS);
5252
extern Datum has_schema_privilege_id_id(PG_FUNCTION_ARGS);
5353
extern Datum has_schema_privilege_name(PG_FUNCTION_ARGS);
5454
extern Datum has_schema_privilege_id(PG_FUNCTION_ARGS);
55+
extern Datum has_tablespace_privilege_name_name(PG_FUNCTION_ARGS);
56+
extern Datum has_tablespace_privilege_name_id(PG_FUNCTION_ARGS);
57+
extern Datum has_tablespace_privilege_id_name(PG_FUNCTION_ARGS);
58+
extern Datum has_tablespace_privilege_id_id(PG_FUNCTION_ARGS);
59+
extern Datum has_tablespace_privilege_name(PG_FUNCTION_ARGS);
60+
extern Datum has_tablespace_privilege_id(PG_FUNCTION_ARGS);
5561

5662
/* bool.c */
5763
extern Datum boolin(PG_FUNCTION_ARGS);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy