Changeset 990

Show
Ignore:
Timestamp:
07/02/06 16:22:11 (2 years ago)
Author:
coderanger
Message:

AccountManagerPlugin:

Adding some more storage manipulators

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • accountmanagerplugin/sandbox/modular/acct_mgr/api.py

    r988 r990  
    1515class AccountError(TracError): 
    1616    """ A generic account error. """ 
     17     
     18class AccountActionError(AccountError): 
     19    """ An account action error. Generally raised on an invalid or unknown action. """ 
    1720 
    1821class UnknownUserError(AccountError): 
     
    3134         
    3235    def add_user(self, user): 
    33         """Create a new user. 
    34         """ 
     36        """Create a new user. Optional function.""" 
    3537 
    3638    def delete_user(self, user): 
    37         """Deletes the user account.""" 
     39        """Deletes the user account. Optional function""" 
    3840 
    3941class IPasswordStore(Interface): 
     
    119121 
    120122    def get_users(self): 
     123        """Enumerate all users in all account stores.""" 
    121124        for store in self.account_stores: 
    122125            for user in store.get_users(): 
     
    124127 
    125128    def has_user(self, user): 
    126         for store in self.account_stores: 
    127             if store.has_user(user): 
    128                 return True 
    129         return False 
     129        """Check if the username is found in any store.""" 
     130        return any(self.account_stores, lambda s: s.has_user(user)) 
     131         
     132    # Account store functions 
     133    def can_change_password(self): 
     134        """Check if any password stores are writable.""" 
     135        return any(self.password_stores, lambda s: hasattr(s, 'set_password')) 
     136         
     137    def can_change_account(self): 
     138        """Check if any account stores are writable.""" 
     139        return any(self.account_stores, lambda s: hasattr(s, 'add_account')) 
     140         
     141    def get_mutable_stores(self): 
     142        """Find mutable stores. 
     143        Returns a tuple of (account_store, password_store), where None means no mutable stores found""" 
     144        acct   = find(self.account_stores , lambda s: hasattr(s, 'add_account')) 
     145        passwd = find(self.password_stores, lambda s: hasattr(s, 'set_password')) 
     146        return (acct,passwd) 
     147    mutable_stores = property(get_mutable_stores) 
    130148         
    131149    # IAccountChangeListener methods 
  • accountmanagerplugin/sandbox/modular/acct_mgr/model.py

    r988 r990  
    2828            else: 
    2929                raise AccountError, "Non-new account that doesn't exist in any store" 
     30        else: 
     31            assert not AccountSystem(self.env).has_user(self.username) 
     32            self.store, _ = AccountSystem(self.env).mutable_stores 
    3033 
    3134    def save(self, db = None): 
     
    3740        old_status = cursor.fetchone() 
    3841        if not old_status: 
    39             cursor.execute('INSERT INTO account_status (user, status) VALUES (%s, %s)',(self.username, self.status)) 
     42            cursor.execute('INSERT INTO account_status (user, status) VALUES (%s, %s)', (self.username, self.status)) 
    4043        elif old_status == self.status: 
    4144            return 
    4245        else: 
    43             cursor.execute(' 
     46            cursor.execute('UPDATE account_status SET status=%s WHERE user=%s', (self.status, self.username)) 
     47             
     48        if handle_commit: 
     49            db.commit() 
     50            
     51    def create(self, password=None): 
     52        """Create this account.""" 
     53        assert self.status == 'new', 'Cannot create an existing account' 
     54        if self.store: 
     55            self.store.add_account(self.username) 
     56        else: 
     57            raise AccountActionError, 'No mutable stores available' 
     58             
     59        self.status = 'created' 
     60        self.save() 
     61             
     62        if password: 
     63            self.set_password(password) 
    4464 
    4565    def _get_db(db)