summaryrefslogtreecommitdiff
path: root/sql/modules/Session.sql
blob: e0547c2214e09d16088669343269d366690be609 (plain)
  1. CREATE OR REPLACE FUNCTION form_close(in_session_id int, in_form_id int)
  2. RETURNS BOOL AS
  3. $$
  4. BEGIN
  5. SELECT * FROM open_forms
  6. WHERE session_id = in_session_id AND id = in_form_id;
  7. IF FOUND THEN
  8. DELETE FROM open_forms
  9. WHERE session_id = in_session_id AND id = in_form_id;
  10. RETURN TRUE;
  11. ELSE RETURN FALSE;
  12. END IF;
  13. END;
  14. $$ language plpgsql;
  15. CREATE OR REPLACE FUNCTION form_open(in_session_id int)
  16. RETURNS INT AS
  17. $$
  18. BEGIN
  19. INSERT INTO open_forms (session_id) VALUES (in_session_id);
  20. RETURN currval('form_id_seq');
  21. END;
  22. $$ LANGUAGE PLPGSQL;
  23. CREATE OR REPLACE FUNCTION session_check(in_session_id int, in_token text)
  24. RETURNS session AS
  25. $$
  26. DECLARE out_row session%ROWTYPE;
  27. BEGIN
  28. UPDATE session
  29. SET last_used = now()
  30. WHERE session_id = in_session_id
  31. AND token = in_token
  32. AND last_used > now() - (SELECT value FROM defaults
  33. WHERE setting_key = 'timeout')::interval
  34. AND users_id = (select id from users
  35. where username = SESSION_USER);
  36. IF FOUND THEN
  37. SELECT * INTO out_row WHERE session_id = in_session_id;
  38. ELSE
  39. DELETE FROM SESSION
  40. WHERE users_id IN (select id from users
  41. where username = SESSION_USER);
  42. -- the above query also releases all discretionary locks by the
  43. -- session
  44. IF NOT FOUND THEN
  45. PERFORM id FROM users WHERE username = SESSION_USER;
  46. IF NOT FOUND THEN
  47. RAISE EXCEPTION 'User Not Known';
  48. END IF;
  49. END IF;
  50. INSERT INTO session(users_id, token, last_used, transaction_id)
  51. SELECT id, md5(random()::text), now(), 0
  52. FROM users WHERE username = SESSION_USER;
  53. -- TODO-- remove transaction_id field from session table
  54. SELECT * INTO out_row FROM session
  55. WHERE session_id = currval('session_session_id_seq');
  56. END IF;
  57. RETURN out_row;
  58. END;
  59. $$ LANGUAGE PLPGSQL;
  60. COMMENT ON FUNCTION session_check(int, text) IS
  61. $$ Return code is 0 for failure, 1 for success. $$;