summaryrefslogtreecommitdiff
path: root/sql/Pg-central.sql
blob: 804bf9653b4e7b4e56ab6ad5eb8ce8123f902e41 (plain)
  1. -- Central DB structure
  2. -- This is the central database stuff which is used across all datasets
  3. -- in the ledger-smb.conf it is called 'ledgersmb' by default, but obviously
  4. -- can be named anything.
  5. -- USERS stuff --
  6. CREATE TABLE users (id serial UNIQUE, username varchar(30) primary key);
  7. COMMENT ON TABLE users IS $$username is the actual primary key here because we do not want duplicate users$$;
  8. CREATE TABLE users_conf(id integer primary key references users(id) deferrable initially deferred,
  9. acs text,
  10. address text,
  11. businessnumber text,
  12. company text,
  13. countrycode text,
  14. currency text,
  15. dateformat text,
  16. dbconnect text,
  17. dbdriver text default 'Pg',
  18. dbhost text default 'localhost',
  19. dbname text,
  20. dboptions text,
  21. dbpasswd text,
  22. dbport text,
  23. dbuser text,
  24. email text,
  25. fax text,
  26. menuwidth text,
  27. name text,
  28. numberformat text,
  29. password varchar(32) check(length(password) = 32),
  30. print text,
  31. printer text,
  32. role text,
  33. sid text,
  34. signature text,
  35. stylesheet text,
  36. tel text,
  37. templates text,
  38. timeout numeric,
  39. vclimit numeric);
  40. COMMENT ON TABLE users_conf IS 'This is a completely dumb table that is a place holder to get usersconf into the database. Next major release will have a much more sane implementation';
  41. COMMENT ON COLUMN users_conf.id IS 'Yes primary key with a FOREIGN KEY to users(id) is correct';
  42. COMMENT ON COLUMN users_conf.password IS 'This means we have to get rid of the current password stuff and move to presumably md5()';
  43. -- Per conversation with ChrisM, if the admin user has a null password a couple of things happen.
  44. -- 1. It is implicit that this is an initial install
  45. -- 2. If the admin password does not match the ledger-smb.conf admin password, we throw a hijack alert
  46. -- The two below statements must be run from a single session
  47. INSERT INTO users(username) VALUES ('admin');
  48. INSERT INTO users_conf(id,password) VALUES (currval('users_id_seq'),NULL);
  49. CREATE OR REPLACE FUNCTION create_user(text) RETURNS bigint AS $$
  50. INSERT INTO users(username) VALUES ($1);
  51. SELECT currval('users_id_seq');
  52. $$ LANGUAGE 'SQL';
  53. COMMENT ON FUNCTION create_user(text) IS $$ Function to create user. Returns users.id if successful, else it is an error. $$;
  54. CREATE OR REPLACE FUNCTION update_user(int4,text) RETURNS int4 AS $$
  55. UPDATE users SET username = $2 WHERE id = $1;
  56. SELECT 1;
  57. $$ LANGUAGE 'SQL';
  58. COMMENT ON FUNCTION update_user(int4,text) IS $$ Takes int4 which is users.id and text which is username. Will update username based on id. Username is unique $$;
  59. -- Session tracking table
  60. CREATE TABLE session(
  61. session_id serial PRIMARY KEY,
  62. sl_login VARCHAR(50),
  63. token VARCHAR(32) CHECK(length(token) = 32),
  64. last_used TIMESTAMP default now(),
  65. users_id INTEGER -- NOT NULL references users(id)
  66. );