priv.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*!
  2. \file db/driver/postgres/priv.c
  3. \brief DBMI - Low Level PostgreSQL database driver - privilages
  4. This program is free software under the GNU General Public License
  5. (>=v2). Read the file COPYING that comes with GRASS for details.
  6. \author Radim Blazek
  7. */
  8. #include <grass/dbmi.h>
  9. #include <grass/glocale.h>
  10. #include "globals.h"
  11. #include "proto.h"
  12. int db__driver_grant_on_table(dbString * tableName, int priv, int to)
  13. {
  14. PGresult *res;
  15. dbString sql;
  16. dbConnection connection;
  17. G_debug(3, "db__driver_grant_on_table()");
  18. db_get_connection(&connection);
  19. db_init_string(&sql);
  20. db_set_string(&sql, "grant ");
  21. if (priv | DB_PRIV_SELECT)
  22. db_append_string(&sql, "select ");
  23. db_append_string(&sql, "on ");
  24. db_append_string(&sql, db_get_string(tableName));
  25. db_append_string(&sql, " to ");
  26. if (to | DB_GROUP && connection.group) {
  27. db_append_string(&sql, "group ");
  28. db_append_string(&sql, connection.group);
  29. if (to | DB_PUBLIC)
  30. db_append_string(&sql, ", ");
  31. }
  32. if (to | DB_PUBLIC)
  33. db_append_string(&sql, "public");
  34. G_debug(3, " SQL: %s", db_get_string(&sql));
  35. res = PQexec(pg_conn, db_get_string(&sql));
  36. if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) {
  37. db_d_append_error("%s\n%s\n%s",
  38. _("Unable grant on table:"),
  39. db_get_string(&sql),
  40. PQerrorMessage(pg_conn));
  41. db_d_report_error();
  42. PQclear(res);
  43. db_free_string(&sql);
  44. return DB_FAILED;
  45. }
  46. PQclear(res);
  47. db_free_string(&sql);
  48. return DB_OK;
  49. }