summaryrefslogtreecommitdiff
path: root/sql/modules/security.sql
blob: 61828b64a4e79f55036e2047e148f52b56975d71 (plain)
  1. create table modules (
  2. id SERIAL PRIMARY KEY,
  3. mod_name TEXT UNIQUE NOT NULL,
  4. comments text default ''
  5. );
  6. comment on table modules is $$name may be used as an alternate key.
  7. Comments should be used to provide the admin of the system with an
  8. understanding of what the module does. Names and comments are also subject to
  9. string freezes since they may be translated by the application.$$;
  10. -- not adding comments to these because they are translated anyway.
  11. insert into modules (mod_name) values ('AR');
  12. insert into modules (mod_name) values ('AP');
  13. insert into modules (mod_name) values ('HR');
  14. insert into modules (mod_name) values ('Order Entry');
  15. insert into modules (mod_name) values ('Goods and Services');
  16. insert into modules (mod_name) values ('Recurring Transactions');
  17. insert into modules (mod_name) values ('System');
  18. create or replace function add_module (text, text) returns int AS $$
  19. insert into modules (mod_name, comments) values ($1, $2);
  20. select currval(modules_id_seq);
  21. $$ language sql;
  22. create or replace function get_all_modules () returns setof modules as $$
  23. select id, mod_name, comments from modules;
  24. $$ language sql;
  25. create or replace function get_module_by_id (int) returns modules as $$
  26. select id, modname, comments from modules where id = $1;
  27. $$ language sql;
  28. create or replace function get_module_by_name (text) returns modules as $$
  29. select id, modname, comments from modules where mod_name = $1;
  30. $$ language sql;
  31. create or replace function save_module (int, text, text) returns bool as $$
  32. update modules set mod_name = $2, comments=$3 where id = $1;
  33. $$ language sql;
  34. create table mod_relation (
  35. id serial primary key,
  36. mod_id int not null references modules(id),
  37. rel_name text NOT NULL,
  38. rel_type "char" CHECK IN ('t', 's')
  39. );
  40. comment on table mod_relation is $$reltype is 't' for tables or views and 's'
  41. for sequences. rel_name is the name of the table.$$;
  42. create or replace function register_table (text, text) returns int AS $$
  43. insert into mod_relation (mod_id, relname, reltype) values
  44. ((select id from modules where mod_name = $1), $2, 't');
  45. select 1;
  46. $$ language sql;
  47. create or replace function register_sequence (text, text) returns int as $$
  48. insert into module_relation (mod_id, relname, reltype) values
  49. ((select id from modules where mod_name = $1), $2, 's');
  50. select 1;
  51. $$ language sql;
  52. select register_table('System', 'modules');
  53. select register_table('System', 'mod_relation');
  54. select register_sequence('System', 'modules_id_seq');
  55. select register_sequence('System', 'mod_relation_id_seq');
  56. create or replace function change_my_password(text) returns bool as $$
  57. begin
  58. execute 'alter user ''' || session_user || ''' with encrypted password '''
  59. || $1 || '''';
  60. return true;
  61. end;
  62. $$ language plpgsql security definer;
  63. comment on function change_my_password is $$ This function must be created as a superuser to work!$$;
  64. create table db_users (
  65. id serial primary key,
  66. username text unique not null,
  67. active bool default true not null
  68. );
  69. comment on db_users is $$This is a list of users applicable to this
  70. dataset. Note that the user creation script must connect to the dataset to be
  71. used and add the username to this table. Otherwise the user will not be able
  72. to log in.$$;
  73. create table preferences
  74. (id integer primary key references db_users(id) deferrable initially deferred,
  75. employee_id integer references employees(id), deferrable initially deferred,
  76. countrycode text,
  77. currency text,
  78. dateformat text,
  79. menuwidth text,
  80. printer text,
  81. signature text,
  82. stylesheet text,
  83. templates text,
  84. timeout numeric,
  85. vclimit numeric
  86. );
  87. create or replace function add_user (text) returns bool as $$
  88. insert into dataset_users (username) values ($1);
  89. select true;
  90. $$ language sql;