1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 from twisted.python import log
20 from twisted.internet import defer
21 from twisted.application import service
22 from buildbot import pbutil
25 """
26 Base class for services that manage users manually. This takes care
27 of the service.MultiService work needed by all the services that
28 subclass it.
29 """
30
34
37
40
42 """
43 Perspective registered in buildbot.pbmanager and contains the real
44 workings of `buildbot user` by working with the database when
45 perspective_commandline is called.
46 """
47
50
98
99 @defer.deferredGenerator
101 """
102 This performs the requested operations from the `buildbot user`
103 call by calling the proper buildbot.db.users methods based on
104 the operation. It yields a deferred instance with the results
105 from the database methods.
106
107 @param op: operation to perform (add, remove, update, get)
108 @type op: string
109
110 @param bb_username: username portion of auth credentials
111 @type bb_username: string
112
113 @param bb_password: hashed password portion of auth credentials
114 @type bb_password: hashed string
115
116 @param ids: user identifiers used to find existing users
117 @type ids: list of strings or None
118
119 @param info: type/value pairs for each user that will be added
120 or updated in the database
121 @type info: list of dictionaries or None
122
123 @returns: results from db.users methods via deferred
124 """
125 log.msg("perspective_commandline called")
126 results = []
127
128 if ids:
129 for user in ids:
130
131
132 d = self.master.db.users.identifierToUid(identifier=user)
133 wfd = defer.waitForDeferred(d)
134 yield wfd
135 uid = wfd.getResult()
136
137 result = None
138 if op == 'remove':
139 if uid:
140 d = self.master.db.users.removeUser(uid)
141 wfd = defer.waitForDeferred(d)
142 yield wfd
143 wfd.getResult()
144 result = user
145 else:
146 log.msg("Unable to find uid for identifier %s" % user)
147 elif op == 'get':
148 if uid:
149 d = self.master.db.users.getUser(uid)
150 wfd = defer.waitForDeferred(d)
151 yield wfd
152 result = wfd.getResult()
153 else:
154 log.msg("Unable to find uid for identifier %s" % user)
155
156 results.append(result)
157 else:
158 for user in info:
159
160
161 ident = user.pop('identifier')
162 d = self.master.db.users.identifierToUid(identifier=ident)
163 wfd = defer.waitForDeferred(d)
164 yield wfd
165 uid = wfd.getResult()
166
167
168
169 if not user:
170 if uid:
171 d = self.master.db.users.updateUser(
172 uid=uid,
173 identifier=ident,
174 bb_username=bb_username,
175 bb_password=bb_password)
176 wfd = defer.waitForDeferred(d)
177 yield wfd
178 results.append(ident)
179 result = wfd.getResult()
180 else:
181 log.msg("Unable to find uid for identifier %s"
182 % user)
183 else:
184
185 once_through = False
186 for attr in user:
187 if op == 'update' or once_through:
188 if uid:
189 d = self.master.db.users.updateUser(
190 uid=uid,
191 identifier=ident,
192 bb_username=bb_username,
193 bb_password=bb_password,
194 attr_type=attr,
195 attr_data=user[attr])
196 else:
197 log.msg("Unable to find uid for identifier %s"
198 % user)
199 elif op == 'add':
200 d = self.master.db.users.findUserByAttr(
201 identifier=ident,
202 attr_type=attr,
203 attr_data=user[attr])
204 once_through = True
205 wfd = defer.waitForDeferred(d)
206 yield wfd
207 results.append(ident)
208 result = wfd.getResult()
209
210
211 if result:
212 results.append(result)
213 uid = result
214 results = self.formatResults(op, results)
215 yield results
216
218 """
219 Service that runs to set up and register CommandlineUserManagerPerspective
220 so `buildbot user` calls get to perspective_commandline.
221 """
222
223 - def __init__(self, username=None, passwd=None, port=None):
224 UsersBase.__init__(self)
225 assert username and passwd, ("A username and password pair must be given "
226 "to connect and use `buildbot user`")
227 self.username = username
228 self.passwd = passwd
229
230 assert port, "A port must be specified for a PB connection"
231 self.port = port
232 self.registration = None
233
239 self.registration = self.master.pbmanager.register(self.port,
240 self.username,
241 self.passwd,
242 factory)
243
249 d.addCallback(unreg)
250 return d
251