Ignore:
Timestamp:
6 Apr 2017, 10:37:01 (8 years ago)
Author:
uli
Message:

flake8.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/authentication.py

    r14667 r14669  
    3030from zope.pluggableauth.plugins.session import SessionCredentialsPlugin
    3131from zope.pluggableauth.plugins.httpplugins import (
    32         HTTPBasicAuthCredentialsPlugin)
     32    HTTPBasicAuthCredentialsPlugin)
    3333from zope.pluggableauth.interfaces import (
    34         ICredentialsPlugin, IAuthenticatorPlugin,
    35         IAuthenticatedPrincipalFactory, AuthenticatedPrincipalCreated)
     34    ICredentialsPlugin, IAuthenticatorPlugin, IAuthenticatedPrincipalFactory,
     35    AuthenticatedPrincipalCreated)
    3636from zope.publisher.interfaces import IRequest
    3737from zope.password.interfaces import IPasswordManager
    3838from zope.securitypolicy.principalrole import principalRoleManager
    39 from waeup.kofa.interfaces import (ILocalRoleSetEvent,
    40     IUserAccount, IAuthPluginUtility, IPasswordValidator,
    41     IKofaPrincipal, IKofaPrincipalInfo, IKofaPluggable,
    42     IBatchProcessor, IGNORE_MARKER, IFailedLoginInfo)
     39from waeup.kofa.interfaces import (
     40    ILocalRoleSetEvent, IUserAccount, IAuthPluginUtility, IPasswordValidator,
     41    IKofaPrincipal, IKofaPrincipalInfo, IKofaPluggable, IBatchProcessor,
     42    IGNORE_MARKER, IFailedLoginInfo)
    4343from waeup.kofa.utils.batching import BatchProcessor
    4444from waeup.kofa.permissions import get_all_roles
    4545
     46
    4647def setup_authentication(pau):
    4748    """Set up plugguble authentication utility.
     
    5354    """
    5455    pau.credentialsPlugins = (
    55             'No Challenge if Authenticated',
    56             'xmlrpc-credentials',
    57             'credentials')
     56        'No Challenge if Authenticated',
     57        'xmlrpc-credentials',
     58        'credentials')
    5859    pau.authenticatorPlugins = ('users',)
    5960
     
    6364        util.register(pau)
    6465
     66
    6567def get_principal_role_manager():
    6668    """Get a role manager for principals.
     
    7476    return principalRoleManager
    7577
    76 class KofaSessionCredentialsPlugin(grok.GlobalUtility,
    77                                     SessionCredentialsPlugin):
     78
     79class KofaSessionCredentialsPlugin(
     80        grok.GlobalUtility, SessionCredentialsPlugin):
    7881    """Session plugin that picks usernames/passwords from fields in webforms.
    7982    """
     
    143146                getattr(self, name) == getattr(obj, name, default))
    144147        return False not in result
     148
    145149
    146150class KofaPrincipal(Principal):
     
    168172    def __repr__(self):
    169173        return 'KofaPrincipal(%r)' % self.id
     174
    170175
    171176class AuthenticatedKofaPrincipalFactory(grok.MultiAdapter):
     
    200205        return principal
    201206
     207
    202208class FailedLoginInfo(grok.Model):
    203209    grok.implements(IFailedLoginInfo)
     
    224230        pass
    225231
     232
    226233class Account(grok.Model):
    227234    """Kofa user accounts store infos about a user.
     
    237244
    238245    def __init__(self, name, password, title=None, description=None,
    239                  email=None, phone=None, public_name=None, roles = []):
     246                 email=None, phone=None, public_name=None, roles=[]):
    240247        self.name = name
    241248        if title is None:
     
    270277            # unset/empty passwords do never match
    271278            return False
    272         if self.suspended == True:
     279        if self.suspended:
    273280            return False
    274281        passwordmanager = getUtility(IPasswordManager, 'SSHA')
     
    312319        self._p_changed = True
    313320        return
     321
    314322
    315323class UserAuthenticatorPlugin(grok.GlobalUtility):
     
    387395        return site['users']
    388396
     397
    389398class PasswordValidator(grok.GlobalUtility):
    390399
    391   grok.implements(IPasswordValidator)
    392 
    393   def validate_password(self, pw, pw_repeat):
    394        errors = []
    395        if len(pw) < 3:
    396          errors.append('Password must have at least 3 chars.')
    397        if pw != pw_repeat:
    398          errors.append('Passwords do not match.')
    399        return errors
     400    grok.implements(IPasswordValidator)
     401
     402    def validate_password(self, pw, pw_repeat):
     403        errors = []
     404        if len(pw) < 3:
     405            errors.append('Password must have at least 3 chars.')
     406        if pw != pw_repeat:
     407            errors.append('Passwords do not match.')
     408        return errors
     409
    400410
    401411class LocalRoleSetEvent(object):
     
    408418        self.principal_id = principal_id
    409419        self.granted = granted
     420
    410421
    411422@grok.subscribe(IUserAccount, grok.IObjectRemovedEvent)
     
    430441        role_manager.unsetRoleForPrincipal(role_id, principal)
    431442    return
     443
    432444
    433445@grok.subscribe(IUserAccount, grok.IObjectAddedEvent)
     
    451463    return
    452464
     465
    453466@grok.subscribe(Interface, ILocalRoleSetEvent)
    454467def handle_local_role_changed(obj, event):
     
    464477    user.notifyLocalRoleChanged(event.object, event.role_id, event.granted)
    465478    return
     479
    466480
    467481@grok.subscribe(Interface, grok.IObjectRemovedEvent)
     
    474488    for local_role, user_name, setting in role_map.getPrincipalsAndRoles():
    475489        notify(LocalRoleSetEvent(
    476                 obj, local_role, user_name, granted=False))
     490            obj, local_role, user_name, granted=False))
    477491    return
     492
    478493
    479494class UserAccountFactory(grok.GlobalUtility):
     
    493508        return implementedBy(Account)
    494509
     510
    495511class UserProcessor(BatchProcessor):
    496     """The User Processor processes user accounts, i.e. `Account` objects in the
    497     ``users`` container.
     512    """The User Processor processes user accounts, i.e. `Account` objects in
     513    the ``users`` container.
    498514
    499515    The `roles` columns must contain Python list
     
    512528    iface = IUserAccount
    513529
    514     location_fields = ['name',]
     530    location_fields = ['name', ]
    515531    factory_name = 'waeup.UserAccount'
    516532
     
    541557        if user is not None:
    542558            parent = self.getParent(row, site)
    543             grok.getSite().logger.info('%s - %s - User removed'
    544                 % (self.name, row['name']))
     559            grok.getSite().logger.info(
     560                '%s - %s - User removed' % (self.name, row['name']))
    545561            del parent[user.name]
    546562        pass
     
    551567        changed = []
    552568        for key, value in row.items():
    553             if  key == 'roles':
     569            if key == 'roles':
    554570                # We cannot simply set the roles attribute here because
    555571                # we can't assure that the name attribute is set before
     
    571587        # Log actions...
    572588        items_changed = ', '.join(changed)
    573         grok.getSite().logger.info('%s - %s - %s - updated: %s'
    574             % (self.name, filename, row['name'], items_changed))
     589        grok.getSite().logger.info(
     590            '%s - %s - %s - updated: %s' % (
     591                self.name, filename, row['name'], items_changed))
    575592        return
    576593
     
    587604            for role in evalvalue:
    588605                if role not in all_roles:
    589                     errs.append(('roles','invalid role'))
     606                    errs.append(('roles', 'invalid role'))
    590607        return errs, inv_errs, conv_dict
     608
    591609
    592610class UsersPlugin(grok.GlobalUtility):
     
    607625            # Add new attributes
    608626            for i in items:
    609                 if not hasattr(user,i[0]):
    610                     setattr(user,i[0],i[1].missing_value)
     627                if not hasattr(user, i[0]):
     628                    setattr(user, i[0], i[1].missing_value)
    611629                    logger.info(
    612630                        'UsersPlugin: %s attribute %s added.' % (
    613                         user.name,i[0]))
     631                            user.name, i[0]))
    614632            if not hasattr(user, 'failed_logins'):
    615633                # add attribute `failed_logins`...
     
    620638            for i in self.deprecated_attributes:
    621639                try:
    622                     delattr(user,i)
     640                    delattr(user, i)
    623641                    logger.info(
    624642                        'UsersPlugin: %s attribute %s deleted.' % (
    625                         user.name,i))
     643                            user.name, i))
    626644                except AttributeError:
    627645                    pass
Note: See TracChangeset for help on using the changeset viewer.