mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Add current_schema() and current_schemas() inquiry functions.
Update has_table_privilege functions to cope with schema-qualified names in the same way as nextval() and others.
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/acl.c,v 1.71 2002/04/21 00:26:43 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/acl.c,v 1.72 2002/04/26 01:24:08 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -35,12 +35,7 @@ static bool aclitemeq(const AclItem *a1, const AclItem *a2);
|
||||
static bool aclitemgt(const AclItem *a1, const AclItem *a2);
|
||||
|
||||
static AclMode convert_priv_string(text *priv_type_text);
|
||||
static bool has_table_privilege_cname_cname(char *username, char *relname,
|
||||
text *priv_type_text);
|
||||
static bool has_table_privilege_cname_id(char *username, Oid reloid,
|
||||
text *priv_type_text);
|
||||
static bool has_table_privilege_id_cname(int32 usesysid, char *relname,
|
||||
text *priv_type_text);
|
||||
static Oid convert_rel_name(text *relname);
|
||||
|
||||
|
||||
/*
|
||||
@@ -653,7 +648,7 @@ aclcontains(PG_FUNCTION_ARGS)
|
||||
/*
|
||||
* has_table_privilege_name_name
|
||||
* Check user privileges on a relation given
|
||||
* name username, name relname, and text priv name.
|
||||
* name username, text relname, and text priv name.
|
||||
*
|
||||
* RETURNS
|
||||
* a boolean value
|
||||
@@ -664,22 +659,41 @@ Datum
|
||||
has_table_privilege_name_name(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Name username = PG_GETARG_NAME(0);
|
||||
Name relname = PG_GETARG_NAME(1);
|
||||
text *relname = PG_GETARG_TEXT_P(1);
|
||||
text *priv_type_text = PG_GETARG_TEXT_P(2);
|
||||
bool result;
|
||||
int32 usesysid;
|
||||
Oid reloid;
|
||||
AclMode mode;
|
||||
int32 aclresult;
|
||||
|
||||
result = has_table_privilege_cname_cname(NameStr(*username),
|
||||
NameStr(*relname),
|
||||
priv_type_text);
|
||||
/*
|
||||
* Lookup userid based on username
|
||||
*/
|
||||
usesysid = get_usesysid(NameStr(*username));
|
||||
|
||||
PG_RETURN_BOOL(result);
|
||||
/*
|
||||
* Lookup rel OID based on relname
|
||||
*/
|
||||
reloid = convert_rel_name(relname);
|
||||
|
||||
/*
|
||||
* Convert priv_type_text to an AclMode
|
||||
*/
|
||||
mode = convert_priv_string(priv_type_text);
|
||||
|
||||
/*
|
||||
* Check for the privilege
|
||||
*/
|
||||
aclresult = pg_class_aclcheck(reloid, usesysid, mode);
|
||||
|
||||
PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* has_table_privilege_name
|
||||
* Check user privileges on a relation given
|
||||
* name relname and text priv name.
|
||||
* text relname and text priv name.
|
||||
* current_user is assumed
|
||||
*
|
||||
* RETURNS
|
||||
@@ -690,18 +704,31 @@ has_table_privilege_name_name(PG_FUNCTION_ARGS)
|
||||
Datum
|
||||
has_table_privilege_name(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Name relname = PG_GETARG_NAME(0);
|
||||
text *relname = PG_GETARG_TEXT_P(0);
|
||||
text *priv_type_text = PG_GETARG_TEXT_P(1);
|
||||
int32 usesysid;
|
||||
bool result;
|
||||
Oid reloid;
|
||||
AclMode mode;
|
||||
int32 aclresult;
|
||||
|
||||
usesysid = GetUserId();
|
||||
|
||||
result = has_table_privilege_id_cname(usesysid,
|
||||
NameStr(*relname),
|
||||
priv_type_text);
|
||||
/*
|
||||
* Lookup rel OID based on relname
|
||||
*/
|
||||
reloid = convert_rel_name(relname);
|
||||
|
||||
PG_RETURN_BOOL(result);
|
||||
/*
|
||||
* Convert priv_type_text to an AclMode
|
||||
*/
|
||||
mode = convert_priv_string(priv_type_text);
|
||||
|
||||
/*
|
||||
* Check for the privilege
|
||||
*/
|
||||
aclresult = pg_class_aclcheck(reloid, usesysid, mode);
|
||||
|
||||
PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
|
||||
}
|
||||
|
||||
|
||||
@@ -721,13 +748,26 @@ has_table_privilege_name_id(PG_FUNCTION_ARGS)
|
||||
Name username = PG_GETARG_NAME(0);
|
||||
Oid reloid = PG_GETARG_OID(1);
|
||||
text *priv_type_text = PG_GETARG_TEXT_P(2);
|
||||
bool result;
|
||||
int32 usesysid;
|
||||
AclMode mode;
|
||||
int32 aclresult;
|
||||
|
||||
result = has_table_privilege_cname_id(NameStr(*username),
|
||||
reloid,
|
||||
priv_type_text);
|
||||
/*
|
||||
* Lookup userid based on username
|
||||
*/
|
||||
usesysid = get_usesysid(NameStr(*username));
|
||||
|
||||
PG_RETURN_BOOL(result);
|
||||
/*
|
||||
* Convert priv_type_text to an AclMode
|
||||
*/
|
||||
mode = convert_priv_string(priv_type_text);
|
||||
|
||||
/*
|
||||
* Check for the privilege
|
||||
*/
|
||||
aclresult = pg_class_aclcheck(reloid, usesysid, mode);
|
||||
|
||||
PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
|
||||
}
|
||||
|
||||
|
||||
@@ -749,7 +789,7 @@ has_table_privilege_id(PG_FUNCTION_ARGS)
|
||||
text *priv_type_text = PG_GETARG_TEXT_P(1);
|
||||
int32 usesysid;
|
||||
AclMode mode;
|
||||
int32 result;
|
||||
int32 aclresult;
|
||||
|
||||
usesysid = GetUserId();
|
||||
|
||||
@@ -761,19 +801,16 @@ has_table_privilege_id(PG_FUNCTION_ARGS)
|
||||
/*
|
||||
* Check for the privilege
|
||||
*/
|
||||
result = pg_class_aclcheck(reloid, usesysid, mode);
|
||||
aclresult = pg_class_aclcheck(reloid, usesysid, mode);
|
||||
|
||||
if (result == ACLCHECK_OK)
|
||||
PG_RETURN_BOOL(true);
|
||||
else
|
||||
PG_RETURN_BOOL(false);
|
||||
PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* has_table_privilege_id_name
|
||||
* Check user privileges on a relation given
|
||||
* usesysid, name relname, and priv name.
|
||||
* usesysid, text relname, and priv name.
|
||||
*
|
||||
* RETURNS
|
||||
* a boolean value
|
||||
@@ -784,15 +821,28 @@ Datum
|
||||
has_table_privilege_id_name(PG_FUNCTION_ARGS)
|
||||
{
|
||||
int32 usesysid = PG_GETARG_INT32(0);
|
||||
Name relname = PG_GETARG_NAME(1);
|
||||
text *relname = PG_GETARG_TEXT_P(1);
|
||||
text *priv_type_text = PG_GETARG_TEXT_P(2);
|
||||
bool result;
|
||||
Oid reloid;
|
||||
AclMode mode;
|
||||
int32 aclresult;
|
||||
|
||||
result = has_table_privilege_id_cname(usesysid,
|
||||
NameStr(*relname),
|
||||
priv_type_text);
|
||||
/*
|
||||
* Lookup rel OID based on relname
|
||||
*/
|
||||
reloid = convert_rel_name(relname);
|
||||
|
||||
PG_RETURN_BOOL(result);
|
||||
/*
|
||||
* Convert priv_type_text to an AclMode
|
||||
*/
|
||||
mode = convert_priv_string(priv_type_text);
|
||||
|
||||
/*
|
||||
* Check for the privilege
|
||||
*/
|
||||
aclresult = pg_class_aclcheck(reloid, usesysid, mode);
|
||||
|
||||
PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
|
||||
}
|
||||
|
||||
|
||||
@@ -813,7 +863,7 @@ has_table_privilege_id_id(PG_FUNCTION_ARGS)
|
||||
Oid reloid = PG_GETARG_OID(1);
|
||||
text *priv_type_text = PG_GETARG_TEXT_P(2);
|
||||
AclMode mode;
|
||||
int32 result;
|
||||
int32 aclresult;
|
||||
|
||||
/*
|
||||
* Convert priv_type_text to an AclMode
|
||||
@@ -823,18 +873,29 @@ has_table_privilege_id_id(PG_FUNCTION_ARGS)
|
||||
/*
|
||||
* Check for the privilege
|
||||
*/
|
||||
result = pg_class_aclcheck(reloid, usesysid, mode);
|
||||
aclresult = pg_class_aclcheck(reloid, usesysid, mode);
|
||||
|
||||
if (result == ACLCHECK_OK)
|
||||
PG_RETURN_BOOL(true);
|
||||
else
|
||||
PG_RETURN_BOOL(false);
|
||||
PG_RETURN_BOOL(aclresult == ACLCHECK_OK);
|
||||
}
|
||||
|
||||
/*
|
||||
* Internal functions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Given a relation name expressed as a string, look it up and return Oid
|
||||
*/
|
||||
static Oid
|
||||
convert_rel_name(text *relname)
|
||||
{
|
||||
RangeVar *relrv;
|
||||
|
||||
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname,
|
||||
"has_table_privilege"));
|
||||
|
||||
return RangeVarGetRelid(relrv, false);
|
||||
}
|
||||
|
||||
/*
|
||||
* convert_priv_string
|
||||
* Internal function.
|
||||
@@ -843,11 +904,13 @@ has_table_privilege_id_id(PG_FUNCTION_ARGS)
|
||||
* RETURNS
|
||||
* AclMode
|
||||
*/
|
||||
|
||||
static AclMode
|
||||
convert_priv_string(text *priv_type_text)
|
||||
{
|
||||
char *priv_type = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(priv_type_text)));
|
||||
char *priv_type;
|
||||
|
||||
priv_type = DatumGetCString(DirectFunctionCall1(textout,
|
||||
PointerGetDatum(priv_type_text)));
|
||||
|
||||
/*
|
||||
* Return mode from priv_type string
|
||||
@@ -880,114 +943,3 @@ convert_priv_string(text *priv_type_text)
|
||||
*/
|
||||
return ACL_NO_RIGHTS;
|
||||
}
|
||||
|
||||
/*
|
||||
* has_table_privilege_cname_cname
|
||||
* Check user privileges on a relation given
|
||||
* char *usename, char *relname, and text priv name.
|
||||
*
|
||||
* RETURNS
|
||||
* a boolean value
|
||||
* 't' indicating user has the privilege
|
||||
* 'f' indicating user does not have the privilege
|
||||
*/
|
||||
static bool
|
||||
has_table_privilege_cname_cname(char *username, char *relname,
|
||||
text *priv_type_text)
|
||||
{
|
||||
int32 usesysid;
|
||||
|
||||
/*
|
||||
* Lookup userid based on username
|
||||
*/
|
||||
usesysid = get_usesysid(username);
|
||||
|
||||
/*
|
||||
* Make use of has_table_privilege_id_cname. It accepts the arguments
|
||||
* we now have.
|
||||
*/
|
||||
return has_table_privilege_id_cname(usesysid, relname, priv_type_text);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* has_table_privilege_cname_id
|
||||
* Check user privileges on a relation given
|
||||
* char *usename, rel oid, and text priv name.
|
||||
*
|
||||
* RETURNS
|
||||
* a boolean value
|
||||
* 't' indicating user has the privilege
|
||||
* 'f' indicating user does not have the privilege
|
||||
*/
|
||||
static bool
|
||||
has_table_privilege_cname_id(char *username, Oid reloid,
|
||||
text *priv_type_text)
|
||||
{
|
||||
int32 usesysid;
|
||||
AclMode mode;
|
||||
int32 result;
|
||||
|
||||
/*
|
||||
* Lookup userid based on username
|
||||
*/
|
||||
usesysid = get_usesysid(username);
|
||||
|
||||
/*
|
||||
* Convert priv_type_text to an AclMode
|
||||
*/
|
||||
mode = convert_priv_string(priv_type_text);
|
||||
|
||||
/*
|
||||
* Finally, check for the privilege
|
||||
*/
|
||||
result = pg_class_aclcheck(reloid, usesysid, mode);
|
||||
|
||||
if (result == ACLCHECK_OK)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* has_table_privilege_id_cname
|
||||
* Check user privileges on a relation given
|
||||
* usesysid, char *relname, and text priv name.
|
||||
*
|
||||
* RETURNS
|
||||
* a boolean value
|
||||
* 't' indicating user has the privilege
|
||||
* 'f' indicating user does not have the privilege
|
||||
*/
|
||||
static bool
|
||||
has_table_privilege_id_cname(int32 usesysid, char *relname,
|
||||
text *priv_type_text)
|
||||
{
|
||||
Oid reloid;
|
||||
AclMode mode;
|
||||
int32 result;
|
||||
|
||||
/*
|
||||
* Convert relname to rel OID.
|
||||
*/
|
||||
reloid = RelnameGetRelid(relname);
|
||||
if (!OidIsValid(reloid))
|
||||
elog(ERROR, "has_table_privilege: relation \"%s\" does not exist",
|
||||
relname);
|
||||
|
||||
/*
|
||||
* Convert priv_type_text to an AclMode
|
||||
*/
|
||||
mode = convert_priv_string(priv_type_text);
|
||||
|
||||
/*
|
||||
* Finally, check for the privilege
|
||||
*/
|
||||
result = pg_class_aclcheck(reloid, usesysid, mode);
|
||||
|
||||
if (result == ACLCHECK_OK)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -12,14 +12,18 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.33 2001/10/28 06:25:52 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/name.c,v 1.34 2002/04/26 01:24:08 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#include "postgres.h"
|
||||
|
||||
#include "catalog/namespace.h"
|
||||
#include "miscadmin.h"
|
||||
#include "utils/array.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/lsyscache.h"
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* USER I/O ROUTINES (none) *
|
||||
@@ -209,7 +213,9 @@ namestrcmp(Name name, const char *str)
|
||||
}
|
||||
|
||||
|
||||
/* SQL-functions CURRENT_USER and SESSION_USER */
|
||||
/*
|
||||
* SQL-functions CURRENT_USER, SESSION_USER
|
||||
*/
|
||||
Datum
|
||||
current_user(PG_FUNCTION_ARGS)
|
||||
{
|
||||
@@ -223,6 +229,52 @@ session_user(PG_FUNCTION_ARGS)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* SQL-functions CURRENT_SCHEMA, CURRENT_SCHEMAS
|
||||
*/
|
||||
Datum
|
||||
current_schema(PG_FUNCTION_ARGS)
|
||||
{
|
||||
List *search_path = fetch_search_path();
|
||||
char *nspname;
|
||||
|
||||
if (search_path == NIL)
|
||||
PG_RETURN_NULL();
|
||||
nspname = get_namespace_name((Oid) lfirsti(search_path));
|
||||
PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(nspname)));
|
||||
}
|
||||
|
||||
Datum
|
||||
current_schemas(PG_FUNCTION_ARGS)
|
||||
{
|
||||
List *search_path = fetch_search_path();
|
||||
int nnames = length(search_path);
|
||||
Datum *names;
|
||||
int i;
|
||||
ArrayType *array;
|
||||
|
||||
/* +1 here is just to avoid palloc(0) error */
|
||||
names = (Datum *) palloc((nnames + 1) * sizeof(Datum));
|
||||
i = 0;
|
||||
while (search_path)
|
||||
{
|
||||
char *nspname;
|
||||
|
||||
nspname = get_namespace_name((Oid) lfirsti(search_path));
|
||||
names[i] = DirectFunctionCall1(namein, CStringGetDatum(nspname));
|
||||
i++;
|
||||
search_path = lnext(search_path);
|
||||
}
|
||||
|
||||
array = construct_array(names, nnames,
|
||||
false, /* Name is not by-val */
|
||||
NAMEDATALEN, /* sizeof(Name) */
|
||||
'i'); /* alignment of Name */
|
||||
|
||||
PG_RETURN_POINTER(array);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* PRIVATE ROUTINES *
|
||||
*****************************************************************************/
|
||||
|
||||
Reference in New Issue
Block a user