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

« back to all changes in this revision

Viewing changes to lib/common/db.py

  • Committer: mattgiuca
  • Date: 2008-02-21 06:20:08 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:532
db.py: Augmented create_user.
    Now able to accept a User object instead of a set of fields.
    This will create a user in the DB from the fields of the User object
    instead.

Show diffs side-by-side

added added

removed removed

Lines of Context:
292
292
    # Do not return passhash when reading from the DB
293
293
    login_getfields = login_fields - frozenset(["passhash"])
294
294
 
295
 
    def create_user(self, dry=False, **kwargs):
 
295
    def create_user(self, user_obj=None, dry=False, **kwargs):
296
296
        """Creates a user login entry in the database.
 
297
        Two ways to call this - passing a user object, or passing
 
298
        all fields as separate arguments.
 
299
 
 
300
        Either pass a "user_obj" as the first argument (in which case other
 
301
        fields will be ignored), or pass all fields as arguments.
 
302
 
297
303
        All user fields are to be passed as args. The argument names
298
304
        are the field names of the "login" table of the DB schema.
299
305
        However, instead of supplying a "passhash", you must supply a
307
313
            raise DBException("Supplied arguments include passhash (invalid) (1).")
308
314
        # Make a copy of the dict. Change password to passhash (hashing it),
309
315
        # and set 'state' to "no_agreement".
310
 
        kwargs = copy.copy(kwargs)
311
 
        if 'password' in kwargs:
312
 
            kwargs['passhash'] = _passhash(kwargs['password'])
313
 
            del kwargs['password']
314
 
        kwargs['state'] = "no_agreement"
 
316
        if user_obj is None:
 
317
            # Use the kwargs
 
318
            fields = copy.copy(kwargs)
 
319
        else:
 
320
            # Use the user object
 
321
            fields = dict(user_obj)
 
322
        if 'password' in fields:
 
323
            fields['passhash'] = _passhash(fields['password'])
 
324
            del fields['password']
 
325
        if 'role' in fields:
 
326
            # Convert role to rolenm
 
327
            fields['rolenm'] = str(user_obj.role)
 
328
            del fields['role']
 
329
        if user_obj is None:
 
330
            fields['state'] = "no_agreement"
 
331
            # else, we'll trust the user, but it SHOULD be "no_agreement"
 
332
            # (We can't change it because then the user object would not
 
333
            # reflect the DB).
315
334
        # Execute the query.
316
 
        return self.insert(kwargs, "login", self.login_fields, dry=dry)
 
335
        return self.insert(fields, "login", self.login_fields, dry=dry)
317
336
 
318
337
    def update_user(self, login, dry=False, **kwargs):
319
338
        """Updates fields of a particular user. login is the name of the user