~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to lib/common/db.py

  • Committer: drtomc
  • Date: 2008-03-04 01:17:10 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:636
remakeuser: use the database, not the password file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
140
140
        Raises a DBException if the dictionary contains invalid fields.
141
141
        """
142
142
        if not DB.check_dict(dict, tablefields, disallowed):
143
 
            raise DBException("Supplied dictionary contains invalid fields.")
 
143
            extras = set(dict.keys()) - tablefields
 
144
            raise DBException("Supplied dictionary contains invalid fields. (%s)" % (repr(extras)))
144
145
        # Build two lists concurrently: field names and values, as SQL strings
145
146
        fieldnames = []
146
147
        values = []
172
173
        """
173
174
        if (not (DB.check_dict(primarydict, primary_keys, must=True)
174
175
            and DB.check_dict(updatedict, tablefields, disallowed_update))):
175
 
            raise DBException("Supplied dictionary contains invalid or "
176
 
                " missing fields.")
 
176
            raise DBException("Supplied dictionary contains invalid or missing fields (1).")
177
177
        # Make a list of SQL fragments of the form "field = 'new value'"
178
178
        # These fragments are ALREADY-ESCAPED
179
179
        setlist = []
199
199
        primarydict, tablename, primary_keys: See update.
200
200
        """
201
201
        if not DB.check_dict(primarydict, primary_keys, must=True):
202
 
            raise DBException("Supplied dictionary contains invalid or "
203
 
                " missing fields.")
 
202
            raise DBException("Supplied dictionary contains invalid or missing fields (2).")
204
203
        wherelist = []
205
204
        for k,v in primarydict.items():
206
205
            wherelist.append("%s = %s" % (k, _escape(v)))
225
224
            primary_keys is indeed the primary key).
226
225
        """
227
226
        if not DB.check_dict(primarydict, primary_keys, must=True):
228
 
            raise DBException("Supplied dictionary contains invalid or "
229
 
                " missing fields.")
 
227
            raise DBException("Supplied dictionary contains invalid or missing fields (3).")
230
228
        wherelist = []
231
229
        for k,v in primarydict.items():
232
230
            wherelist.append("%s = %s" % (k, _escape(v)))
289
287
    login_primary = frozenset(["login"])
290
288
    login_fields_list = [
291
289
        "login", "passhash", "state", "unixid", "email", "nick", "fullname",
292
 
        "rolenm", "studentid", "acct_exp", "pass_exp", "last_login"
 
290
        "rolenm", "studentid", "acct_exp", "pass_exp", "last_login", "svn_pass"
293
291
    ]
294
292
    login_fields = frozenset(login_fields_list)
295
 
    # Do not return passhash when reading from the DB
296
 
    login_getfields = login_fields - frozenset(["passhash"])
297
293
 
298
 
    def create_user(self, dry=False, **kwargs):
 
294
    def create_user(self, user_obj=None, dry=False, **kwargs):
299
295
        """Creates a user login entry in the database.
 
296
        Two ways to call this - passing a user object, or passing
 
297
        all fields as separate arguments.
 
298
 
 
299
        Either pass a "user_obj" as the first argument (in which case other
 
300
        fields will be ignored), or pass all fields as arguments.
 
301
 
300
302
        All user fields are to be passed as args. The argument names
301
303
        are the field names of the "login" table of the DB schema.
302
304
        However, instead of supplying a "passhash", you must supply a
307
309
        invalid keys or is missing required keys.
308
310
        """
309
311
        if 'passhash' in kwargs:
310
 
            raise DBException("Supplied arguments include passhash (invalid).")
 
312
            raise DBException("Supplied arguments include passhash (invalid) (1).")
311
313
        # Make a copy of the dict. Change password to passhash (hashing it),
312
314
        # and set 'state' to "no_agreement".
313
 
        kwargs = copy.copy(kwargs)
314
 
        if 'password' in kwargs:
315
 
            kwargs['passhash'] = _passhash(kwargs['password'])
316
 
            del kwargs['password']
317
 
        kwargs['state'] = "no_agreement"
 
315
        if user_obj is None:
 
316
            # Use the kwargs
 
317
            fields = copy.copy(kwargs)
 
318
        else:
 
319
            # Use the user object
 
320
            fields = dict(user_obj)
 
321
        if 'password' in fields:
 
322
            fields['passhash'] = _passhash(fields['password'])
 
323
            del fields['password']
 
324
        if 'role' in fields:
 
325
            # Convert role to rolenm
 
326
            fields['rolenm'] = str(user_obj.role)
 
327
            del fields['role']
 
328
        if user_obj is None:
 
329
            fields['state'] = "no_agreement"
 
330
            # else, we'll trust the user, but it SHOULD be "no_agreement"
 
331
            # (We can't change it because then the user object would not
 
332
            # reflect the DB).
 
333
        if 'local_password' in fields:
 
334
            del fields['local_password']
318
335
        # Execute the query.
319
 
        return self.insert(kwargs, "login", self.login_fields, dry=dry)
 
336
        return self.insert(fields, "login", self.login_fields, dry=dry)
320
337
 
321
338
    def update_user(self, login, dry=False, **kwargs):
322
339
        """Updates fields of a particular user. login is the name of the user
333
350
        with a new one.
334
351
        """
335
352
        if 'passhash' in kwargs:
336
 
            raise DBException("Supplied arguments include passhash (invalid).")
 
353
            raise DBException("Supplied arguments include passhash (invalid) (2).")
337
354
        if "password" in kwargs:
338
355
            kwargs = copy.copy(kwargs)
339
356
            kwargs['passhash'] = _passhash(kwargs['password'])
349
366
        Raises a DBException if the login is not found in the DB.
350
367
        """
351
368
        userdict = self.get_single({"login": login}, "login",
352
 
            self.login_getfields, self.login_primary,
 
369
            self.login_fields, self.login_primary,
353
370
            error_notfound="get_user: No user with that login name", dry=dry)
354
371
        if dry:
355
372
            return userdict     # Query string
359
376
    def get_users(self, dry=False):
360
377
        """Returns a list of all users in the DB, as User objects.
361
378
        """
362
 
        userdicts = self.get_all("login", self.login_getfields, dry=dry)
 
379
        userdicts = self.get_all("login", self.login_fields, dry=dry)
363
380
        if dry:
364
381
            return userdicts    # Query string
365
382
        # Package into User objects