]> git.decadent.org.uk Git - dak.git/blob - dak/dakdb/update84.py
Use correct db_name for MD5 hash
[dak.git] / dak / dakdb / update84.py
1 #!/usr/bin/env python
2 # coding=utf8
3
4 """
5 add per-suite database permissions
6
7 @contact: Debian FTP Master <ftpmaster@debian.org>
8 @copyright: 2012 Ansgar Burchardt <ansgar@debian.org>
9 @license: GNU General Public License version 2 or later
10 """
11
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
26 ################################################################################
27
28 import psycopg2
29 from daklib.dak_exceptions import DBUpdateError
30 from daklib.config import Config
31
32 statements = [
33 """
34 CREATE TABLE suite_permission (
35   suite_id INT NOT NULL REFERENCES suite(id) ON DELETE CASCADE,
36   role TEXT NOT NULL,
37   PRIMARY KEY (suite_id, role)
38 )
39 """,
40
41 """
42 CREATE OR REPLACE FUNCTION has_suite_permission(action TEXT, suite_id INT)
43   RETURNS BOOLEAN
44   STABLE
45   STRICT
46   SET search_path = public, pg_temp
47   LANGUAGE plpgsql
48 AS $$
49 DECLARE
50   v_result BOOLEAN;
51 BEGIN
52
53   IF pg_has_role('ftpteam', 'USAGE') THEN
54     RETURN 't';
55   END IF;
56
57   SELECT BOOL_OR(pg_has_role(sp.role, 'USAGE')) INTO v_result
58     FROM suite_permission sp
59    WHERE sp.suite_id = has_suite_permission.suite_id
60    GROUP BY sp.suite_id;
61
62   IF v_result IS NULL THEN
63     v_result := 'f';
64   END IF;
65
66   RETURN v_result;
67
68 END;
69 $$
70 """,
71
72 """
73 CREATE OR REPLACE FUNCTION trigger_check_suite_permission() RETURNS TRIGGER
74 SET search_path = public, pg_temp
75 LANGUAGE plpgsql
76 AS $$
77 DECLARE
78   v_row RECORD;
79   v_suite_name suite.suite_name%TYPE;
80 BEGIN
81
82   CASE TG_OP
83     WHEN 'INSERT', 'UPDATE' THEN
84       v_row := NEW;
85     WHEN 'DELETE' THEN
86       v_row := OLD;
87     ELSE
88       RAISE EXCEPTION 'Unexpected TG_OP (%)', TG_OP;
89   END CASE;
90
91   IF TG_OP = 'UPDATE' AND OLD.suite != NEW.suite THEN
92     RAISE EXCEPTION 'Cannot change suite';
93   END IF;
94
95   IF NOT has_suite_permission(TG_OP, v_row.suite) THEN
96     SELECT suite_name INTO STRICT v_suite_name FROM suite WHERE id = v_row.suite;
97     RAISE EXCEPTION 'Not allowed to % in %', TG_OP, v_suite_name;
98   END IF;
99
100   RETURN v_row;
101
102 END;
103 $$
104 """,
105
106 """
107 CREATE CONSTRAINT TRIGGER trigger_override_permission
108   AFTER INSERT OR UPDATE OR DELETE
109   ON override
110   FOR EACH ROW
111   EXECUTE PROCEDURE trigger_check_suite_permission()
112 """,
113
114 """
115 CREATE CONSTRAINT TRIGGER trigger_src_associations_permission
116   AFTER INSERT OR UPDATE OR DELETE
117   ON src_associations
118   FOR EACH ROW
119   EXECUTE PROCEDURE trigger_check_suite_permission()
120 """,
121
122 """
123 CREATE CONSTRAINT TRIGGER trigger_bin_associations_permission
124   AFTER INSERT OR UPDATE OR DELETE
125   ON bin_associations
126   FOR EACH ROW
127   EXECUTE PROCEDURE trigger_check_suite_permission()
128 """,
129 ]
130
131 ################################################################################
132 def do_update(self):
133     print __doc__
134     try:
135         cnf = Config()
136
137         c = self.db.cursor()
138
139         for stmt in statements:
140             c.execute(stmt)
141
142         c.execute("UPDATE config SET value = '84' WHERE name = 'db_revision'")
143         self.db.commit()
144
145     except psycopg2.ProgrammingError as msg:
146         self.db.rollback()
147         raise DBUpdateError('Unable to apply sick update 84, rollback issued. Error message: {0}'.format(msg))