Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f986173

Browse files
committed
Add a \z command to psql that lists off grant/revoke permissions
- if someone can pick a better \? for this, plesae let me know...all the good ones seem taken :(
1 parent 8148952 commit f986173

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/bin/psql/psql.c

+53-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.58 1997/03/12 21:19:14 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.59 1997/04/10 11:54:29 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -82,6 +82,7 @@ handleCopyIn(PGresult * res, const bool mustprompt,
8282
FILE * copystream);
8383
static int tableList(PsqlSettings * ps, bool deep_tablelist);
8484
static int tableDesc(PsqlSettings * ps, char *table);
85+
static int rightsList(PsqlSettings * ps);
8586
static void prompt_for_password(char *username, char *password);
8687
static char * make_connect_string(char *host, char *port, char *dbname,
8788
char *username, char *password);
@@ -173,6 +174,7 @@ slashUsage(PsqlSettings * ps)
173174
fprintf(stderr, " \\t -- toggle table headings and row count (currently %s)\n", on(ps->opt.header));
174175
fprintf(stderr, " \\T [<html>] -- set html3.0 <table ...> options (currently '%s')\n", ps->opt.tableOpt ? ps->opt.tableOpt : "");
175176
fprintf(stderr, " \\x -- toggle expanded output (currently %s)\n", on(ps->opt.expanded));
177+
fprintf(stderr, " \\z -- list current grant/revoke permissions\n");
176178
fprintf(stderr, " \\! [<cmd>] -- shell escape or command\n");
177179
}
178180

@@ -301,6 +303,53 @@ tableList(PsqlSettings * ps, bool deep_tablelist)
301303
}
302304
}
303305

306+
/*
307+
* List Tables Grant/Revoke Permissions returns 0 if all went well
308+
*
309+
*/
310+
int
311+
rightsList(PsqlSettings * ps)
312+
{
313+
char listbuf[256];
314+
int nColumns;
315+
int i;
316+
317+
PGresult *res;
318+
319+
listbuf[0] = '\0';
320+
strcat(listbuf, "SELECT relname, relacl");
321+
strcat(listbuf, " FROM pg_class, pg_user ");
322+
strcat(listbuf, "WHERE ( relkind = 'r' OR relkind = 'i') ");
323+
strcat(listbuf, " and relname !~ '^pg_'");
324+
strcat(listbuf, " and relname !~ '^Inv[0-9]+'");
325+
strcat(listbuf, " and usesysid = relowner");
326+
strcat(listbuf, " ORDER BY relname ");
327+
if (!(res = PSQLexec(ps, listbuf)))
328+
return -1;
329+
330+
nColumns = PQntuples(res);
331+
if(nColumns > 0) {
332+
/* Display the information */
333+
334+
printf("\nDatabase = %s\n", PQdb(ps->db));
335+
printf(" +------------------+----------------------------------------------------+\n");
336+
printf(" | Relation | Grant/Revoke Permissions |\n");
337+
printf(" +------------------+----------------------------------------------------+\n");
338+
339+
/* next, print out the instances */
340+
for (i = 0; i < PQntuples(res); i++) {
341+
printf(" | %-16.16s", PQgetvalue(res, i, 0));
342+
printf(" | %-50.50s | ", PQgetvalue(res, i, 1));
343+
printf("\n");
344+
}
345+
printf(" +------------------+----------------------------------------------------+\n");
346+
PQclear(res);
347+
return (0);
348+
} else {
349+
fprintf(stderr, "Couldn't find any tables!\n");
350+
return (-1);
351+
}
352+
}
304353
/*
305354
* Describe a table
306355
*
@@ -1193,6 +1242,9 @@ HandleSlashCmds(PsqlSettings * settings,
11931242
fprintf(stderr, "field separater changed to '%s'\n", settings->opt.fieldSep);
11941243
}
11951244
break;
1245+
case 'z': /* list table rights (grant/revoke) */
1246+
rightsList(settings);
1247+
break;
11961248
case 't': /* toggle headers */
11971249
toggle(settings, &settings->opt.header, "output headings and row count");
11981250
break;

0 commit comments

Comments
 (0)