aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/db/users.py
blob: 7e32902cd06ad18adabd0daecf8253228cd22557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# This file is part of Buildbot.  Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

import sqlalchemy as sa
from sqlalchemy.sql.expression import and_

from buildbot.db import base

class UsDict(dict):
    pass

class UsersConnectorComponent(base.DBConnectorComponent):
    # Documentation is in developer/database.rst

    def findUserByAttr(self, identifier, attr_type, attr_data, _race_hook=None):
        def thd(conn, no_recurse=False):
            tbl = self.db.model.users
            tbl_info = self.db.model.users_info

            self.check_length(tbl.c.identifier, identifier)
            self.check_length(tbl_info.c.attr_type, attr_type)
            self.check_length(tbl_info.c.attr_data, attr_data)

            # try to find the user
            q = sa.select([ tbl_info.c.uid ],
                        whereclause=and_(tbl_info.c.attr_type == attr_type,
                                tbl_info.c.attr_data == attr_data))
            rows = conn.execute(q).fetchall()

            if rows:
                return rows[0].uid

            _race_hook and _race_hook(conn)

            # try to do both of these inserts in a transaction, so that both
            # the new user and the corresponding attributes appear at the same
            # time from the perspective of other masters.
            transaction = conn.begin()
            try:
                r = conn.execute(tbl.insert(), dict(identifier=identifier))
                uid = r.inserted_primary_key[0]

                conn.execute(tbl_info.insert(),
                        dict(uid=uid, attr_type=attr_type,
                             attr_data=attr_data))

                transaction.commit()
            except (sa.exc.IntegrityError, sa.exc.ProgrammingError):
                transaction.rollback()

                # try it all over again, in case there was an overlapping,
                # identical call to findUserByAttr, but only retry once.
                if no_recurse:
                    raise
                return thd(conn, no_recurse=True)

            return uid
        d = self.db.pool.do(thd)
        return d

    @base.cached("usdicts")
    def getUser(self, uid):
        def thd(conn):
            tbl = self.db.model.users
            tbl_info = self.db.model.users_info

            q = tbl.select(whereclause=(tbl.c.uid == uid))
            users_row = conn.execute(q).fetchone()

            if not users_row:
                return None

            # make UsDict to return
            usdict = UsDict()

            # gather all attr_type and attr_data entries from users_info table
            q = tbl_info.select(whereclause=(tbl_info.c.uid == uid))
            rows = conn.execute(q).fetchall()
            for row in rows:
                usdict[row.attr_type] = row.attr_data

            # add the users_row data *after* the attributes in case attr_type
            # matches one of these keys.
            usdict['uid'] = users_row.uid
            usdict['identifier'] = users_row.identifier
            usdict['bb_username'] = users_row.bb_username
            usdict['bb_password'] = users_row.bb_password

            return usdict
        d = self.db.pool.do(thd)
        return d

    def getUserByUsername(self, username):
        def thd(conn):
            tbl = self.db.model.users
            tbl_info = self.db.model.users_info

            q = tbl.select(whereclause=(tbl.c.bb_username == username))
            users_row = conn.execute(q).fetchone()

            if not users_row:
                return None

            # make UsDict to return
            usdict = UsDict()

            # gather all attr_type and attr_data entries from users_info table
            q = tbl_info.select(whereclause=(tbl_info.c.uid == users_row.uid))
            rows = conn.execute(q).fetchall()
            for row in rows:
                usdict[row.attr_type] = row.attr_data

            # add the users_row data *after* the attributes in case attr_type
            # matches one of these keys.
            usdict['uid'] = users_row.uid
            usdict['identifier'] = users_row.identifier
            usdict['bb_username'] = users_row.bb_username
            usdict['bb_password'] = users_row.bb_password

            return usdict
        d = self.db.pool.do(thd)
        return d

    def getUsers(self):
        def thd(conn):
            tbl = self.db.model.users
            rows = conn.execute(tbl.select()).fetchall()

            dicts = []
            if rows:
                for row in rows:
                    ud = dict(uid=row.uid, identifier=row.identifier)
                    dicts.append(ud)
            return dicts
        d = self.db.pool.do(thd)
        return d

    def updateUser(self, uid=None, identifier=None, bb_username=None,
                   bb_password=None, attr_type=None, attr_data=None,
                   _race_hook=None):
        def thd(conn):
            transaction = conn.begin()
            tbl = self.db.model.users
            tbl_info = self.db.model.users_info
            update_dict = {}

            # first, add the identifier is it exists
            if identifier is not None:
                self.check_length(tbl.c.identifier, identifier)
                update_dict['identifier'] = identifier

            # then, add the creds if they exist
            if bb_username is not None:
                assert bb_password is not None
                self.check_length(tbl.c.bb_username, bb_username)
                self.check_length(tbl.c.bb_password, bb_password)
                update_dict['bb_username'] = bb_username
                update_dict['bb_password'] = bb_password

            # update the users table if it needs to be updated
            if update_dict:
                q = tbl.update(whereclause=(tbl.c.uid == uid))
                res = conn.execute(q, update_dict)

            # then, update the attributes, carefully handling the potential
            # update-or-insert race condition.
            if attr_type is not None:
                assert attr_data is not None

                self.check_length(tbl_info.c.attr_type, attr_type)
                self.check_length(tbl_info.c.attr_data, attr_data)

                # first update, then insert
                q = tbl_info.update(
                        whereclause=(tbl_info.c.uid == uid)
                                    & (tbl_info.c.attr_type == attr_type))
                res = conn.execute(q, attr_data=attr_data)
                if res.rowcount == 0:
                    _race_hook and _race_hook(conn)

                    # the update hit 0 rows, so try inserting a new one
                    try:
                        q = tbl_info.insert()
                        res = conn.execute(q,
                                uid=uid,
                                attr_type=attr_type,
                                attr_data=attr_data)
                    except (sa.exc.IntegrityError, sa.exc.ProgrammingError):
                        # someone else beat us to the punch inserting this row;
                        # let them win.
                        transaction.rollback()
                        return

            transaction.commit()
        d = self.db.pool.do(thd)
        return d

    def removeUser(self, uid):
        def thd(conn):
            # delete from dependent tables first, followed by 'users'
            for tbl in [
                    self.db.model.change_users,
                    self.db.model.users_info,
                    self.db.model.users,
                    ]:
                conn.execute(tbl.delete(whereclause=(tbl.c.uid==uid)))
        d = self.db.pool.do(thd)
        return d

    def identifierToUid(self, identifier):
        def thd(conn):
            tbl = self.db.model.users

            q = tbl.select(whereclause=(tbl.c.identifier == identifier))
            row = conn.execute(q).fetchone()
            if not row:
                return None

            return row.uid
        d = self.db.pool.do(thd)
        return d