[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services/AuthenticationService - AuthenticationService.py:1.1 IUser.py:1.1 User.py:1.1 __init__.py:1.1 configure.zcml:1.1

Stephan Richter srichter@cbu.edu
Sat, 13 Jul 2002 12:52:59 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Services/AuthenticationService
In directory cvs.zope.org:/tmp/cvs-serv9375/AuthenticationService

Added Files:
	AuthenticationService.py IUser.py User.py __init__.py 
	configure.zcml 
Log Message:
Okay, this is a truely cheesy Authentication Service. You are not able to
specify multiple or different data sources, there are no groups and I
cannot specify roles on the user yet, since Issue 19 has not been
implemented.


=== Added File Zope3/lib/python/Zope/App/OFS/Services/AuthenticationService/AuthenticationService.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""
$Id: AuthenticationService.py,v 1.1 2002/07/13 16:52:57 srichter Exp $
"""
from types import TupleType

from Persistence import PersistentMapping
from Zope.Exceptions import NotFoundError
from Zope.ComponentArchitecture import getAdapter, queryAdapter

from Zope.App.OFS.Container.IContainer import IHomogenousContainer, IContainer
from Zope.App.OFS.Container.BTreeContainer import BTreeContainer

from Zope.App.Security.ILoginPassword import ILoginPassword
from Zope.App.Security.IAuthenticationService import IAuthenticationService

from Zope.App.OFS.Services.AuthenticationService.IUser import IUser

class DuplicateLogin(Exception): pass
class DuplicateId(Exception): pass

class ILocalAuthenticationService(IAuthenticationService, IContainer,
                                  IHomogenousContainer):
    """TTW manageable authentication service"""


class AuthenticationService(BTreeContainer):

    __implements__ = ILocalAuthenticationService

    def __init__(self):
        super(AuthenticationService, self).__init__()

    def getPrincipalByLogin(self, login):
        for p in self.values():
            if p.getLogin() == login:
                return p
        return None

    ############################################################
    # Implementation methods for interface
    # Zope.App.OFS.Services.AuthenticationService.AuthenticationService.

    ######################################
    # from: Zope.App.Security.IAuthenticationService.IAuthenticationService

    def authenticate(self, request):
        'See Zope.App.Security.IAuthenticationService.IAuthenticationService'
        a = queryAdapter(request, ILoginPassword, None)
        if a is not None:
            login = a.getLogin()
            if login is not None:
                p = self.getPrincipalByLogin(login)
                if p is not None:
                    password = a.getPassword()
                    if p.validate(password):
                        return p.getId()
        return None

    def unauthenticatedPrincipal(self):
        'See Zope.App.Security.IAuthenticationService.IAuthenticationService'
        return None

    def unauthorized(self, id, request):
        'See Zope.App.Security.IAuthenticationService.IAuthenticationService'
        # XXX This is a mess. request has no place here!
        if id is None:
            a = getAdapter(request, ILoginPassword)
            a.needLogin(realm="zope")

    def getPrincipal(self, id):
        'See Zope.App.Security.IAuthenticationService.IAuthenticationService'
        r = self.get(id)
        return r

    def getPrincipals(self, name):
        'See Zope.App.Security.IAuthenticationService.IAuthenticationService'
        name = name.lower()
        return [p for p in self.values()
                  if p.getTitle().lower().startswith(name) or
                     p.getLogin().lower().startswith(name)]

    ######################################
    # from: Zope.App.OFS.Container.IContainer.IHomogenousContainer

    def isAddable(self, interfaces):
        'See Zope.App.OFS.Container.IContainer.IHomogenousContainer'
        if type(interfaces) != TupleType:
            interfaces = (interfaces,)
        if IUser in interfaces:
            return 1
        return 0

    #
    ############################################################


=== Added File Zope3/lib/python/Zope/App/OFS/Services/AuthenticationService/IUser.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""
$Id: IUser.py,v 1.1 2002/07/13 16:52:57 srichter Exp $
"""
from Interface import Interface
from Zope.App.Security.IPrincipal import IPrincipal

class IReadUser(IPrincipal):
    """Read interface for a User."""

    def getLogin():
        """Get the login for the user."""

    def validate(pw):
        """Seee whether the password is valid."""


class IWriteUser(Interface):
    """Write interface for a User."""

    def setTitle(title):
        """Set title of User."""

    def setDescription(description):
        """Set description of User."""

    def setLogin(login):
        """Set login of User."""

    def setPassword(password):
        """Set password of User."""


class IUser(IReadUser, IWriteUser):
    """A user object for the Local Authentication Service."""


=== Added File Zope3/lib/python/Zope/App/OFS/Services/AuthenticationService/User.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""A persistent implementation of thr IPrincipal interface

$Id: User.py,v 1.1 2002/07/13 16:52:57 srichter Exp $
"""
from Persistence import Persistent
from Zope.App.OFS.Annotation.IAnnotatable import IAnnotatable
from Zope.App.OFS.Services.AuthenticationService.IUser import IUser

class User(Persistent):
    """A persistent implementation of the IUser interface """
    
    __implements__ =  IUser, IAnnotatable

    def __init__(self, id, title, description, login, pw):
        self.__id = id
        self.__title = title
        self.__description = description
        self.__login = login
        self.__pw = pw

    ############################################################
    # Implementation methods for interface
    # Zope.App.OFS.Services.AuthenticationService.IUser.

    ######################################
    # from: Zope.App.OFS.Services.AuthenticationService.IUser.IReadUser

    def getLogin(self):
        'See Zope.App.OFS.Services.AuthenticationService.IUser.IReadUser'
        return self.__login

    def validate(self, pw):
        'See Zope.App.OFS.Services.AuthenticationService.IUser.IReadUser'
        return pw == self.__pw

    ######################################
    # from: Zope.App.Security.IPrincipal.IPrincipal

    def getId(self):
        'See Zope.App.Security.IPrincipal.IPrincipal'
        return self.__id

    def getTitle(self):
        'See Zope.App.Security.IPrincipal.IPrincipal'
        return self.__title

    def getDescription(self):
        'See Zope.App.Security.IPrincipal.IPrincipal'
        return self.__description

    ######################################
    # from: Zope.App.OFS.Services.AuthenticationService.IUser.IWriteUser

    def setTitle(self, title):
        'See Zope.App.OFS.Services.AuthenticationService.IUser.IWriteUser'
        self.__title = title

    def setDescription(self, description):
        'See Zope.App.OFS.Services.AuthenticationService.IUser.IWriteUser'
        self.__description = description

    def setLogin(self, login):
        'See Zope.App.OFS.Services.AuthenticationService.IUser.IWriteUser'

    def setPassword(self, password):
        'See Zope.App.OFS.Services.AuthenticationService.IUser.IWriteUser'
        self.__pw = password
    #
    ############################################################


=== Added File Zope3/lib/python/Zope/App/OFS/Services/AuthenticationService/__init__.py ===


=== Added File Zope3/lib/python/Zope/App/OFS/Services/AuthenticationService/configure.zcml ===
<zopeConfigure
     xmlns='http://namespaces.zope.org/zope'>

<!-- Authentication Service Directives -->

<content class=".AuthenticationService.">
  <factory id="AuthenticationService" permission="Zope.ManageServices" />
    <require
        permission="Zope.View"
        interface="Zope.App.Security.IAuthenticationService." />
    <require
        permission="Zope.ManageServices"
        interface="Zope.App.OFS.Container.IContainer." />
</content>


<!-- User Directives -->

<content class=".User.">
  <factory id="User" permission="Zope.ManageServices" />
    <require
        permission="Zope.View"
        interface=".IUser.IReadUser" />
    <require
        permission="Zope.ManageContent"
        interface=".IUser.IWriteUser" />
</content>


<!-- Further directives -->

<include package=".Views" />

</zopeConfigure>