[Zope3-checkins] CVS: zopeproducts/sqlauth - interfaces.py:1.2 __init__.py:1.3

Albertas Agejevas alga@codeworks.lt
Tue, 22 Jul 2003 12:44:27 -0400


Update of /cvs-repository/zopeproducts/sqlauth
In directory cvs.zope.org:/tmp/cvs-serv32730

Modified Files:
	interfaces.py __init__.py 
Log Message:
You can now provide a custom principal factory to SQLPrincipalSource.


=== zopeproducts/sqlauth/interfaces.py 1.1 => 1.2 ===
--- zopeproducts/sqlauth/interfaces.py:1.1	Fri Jul 18 09:31:13 2003
+++ zopeproducts/sqlauth/interfaces.py	Tue Jul 22 12:44:21 2003
@@ -16,7 +16,7 @@
 $Id$
 """
 import zope.schema
-from zope.interface import Interface
+from zope.interface import Interface, Attribute
 from zope.app.interfaces.services.pluggableauth \
      import IContainerPrincipalSource, ILoginPasswordPrincipalSource
 from zope.app.interfaces.container import IContainerNamesContainer
@@ -40,6 +40,13 @@
         """
         ),
         )
+
+    factory = Attribute(
+        """The factory for user instantiation.
+
+        Arguments for the factory are: a DBAPI2 connection, an id, and
+        a table name.
+        """)
 
 class ISQLPrincipalSource(ISQLPrincipalSourceData, IContainerPrincipalSource,
                           IContainerNamesContainer, ILoginPasswordPrincipalSource):


=== zopeproducts/sqlauth/__init__.py 1.2 => 1.3 ===
--- zopeproducts/sqlauth/__init__.py:1.2	Mon Jul 21 12:28:25 2003
+++ zopeproducts/sqlauth/__init__.py	Tue Jul 22 12:44:21 2003
@@ -31,6 +31,81 @@
 
 __metaclass__ = type
 
+
+class SQLPrincipal:
+    """This is a simple implementation of IUserSchemafied which issues
+    UPDATE SQL statements to the connection when the attributes are modified.
+    """
+
+    implements(IUserSchemafied)
+
+    def __init__(self, connection, id, table="users"):
+        """Arguments:
+             connection      an IDBIConnection object
+             id              the primary key of the user in the database
+             table           database table with the columns named as
+                             the attributes of IUserSchemafied
+        """
+        self._connection = connection
+        self._table = table
+        self._id = id
+        cursor = self._connection.cursor()
+        cursor.execute(
+            'SELECT login, password, title, description FROM "%s" WHERE id = %s'
+            % (table, id))
+        self._login, self._password, self._title, self._description = (
+            cursor.fetchone())
+
+    def validate(self, test_password):
+        "See zope.app.interfaces.services.pluggableauth.IUserSchemafied"
+        hash = base64.encodestring(sha(test_password).digest())[:-1]
+        return self._password == hash
+
+    def getId(self):
+        "See zope.app.interfaces.security.IPrincipal"
+        return self._id
+
+    def getTitle(self):
+        "See zope.app.interfaces.security.IPrincipal"
+        return self._title
+
+    def getDescription(self):
+        "See zope.app.interfaces.security.IPrincipal"
+        return self._description
+
+    def _getLogin(self): return self._login
+    def _getTitle(self): return self._title
+    def _getPassword(self): return self._password
+    def _getDescription(self): return self._description
+
+    def _setLogin(self, login):
+        self._set('login', login)
+
+    def _setTitle(self, title):
+        self._set('title', title)
+
+    def _setDescription(self, description):
+        self._set('description', description)
+
+    def _setPassword(self, password):
+        hash = base64.encodestring(sha(password).digest())[:-1]
+        self._set('password', hash)
+
+    def _set(self, attr, value):
+        setattr(self, '_' + attr, value)
+        self._connection.cursor().execute(
+            'UPDATE "%s" SET %s = \'%s\' WHERE id = %s' % (self._table, attr,
+                                                           value, self._id))
+    def __cmp__(self, other):
+        return cmp(self.id, other.id)
+
+    id = property(getId)
+    title = property(_getTitle, _setTitle)
+    login = property(_getLogin, _setLogin)
+    password = property(_getPassword, _setPassword)
+    description = property(_getDescription, _setDescription)
+
+
 class SQLPrincipalSource(Persistent):
     """SQL based principal source for the Pluggable Authentication Service.
 
@@ -47,13 +122,14 @@
     implements(ISQLPrincipalSource)
 
 
-    def __init__(self, connection=None, table="users"):
+    def __init__(self, connection=None, table="users", factory=SQLPrincipal):
         """
         connection is a registered SQL database connection name,
         table      is the name of the table the users are stored in.
         """
         self.connection = connection
         self.table = table
+        self.factory = factory
 
     def _getConnection(self):
         if not hasattr(self, '_v_connection'):
@@ -69,7 +145,7 @@
         "See zope.app.interfaces.services.pluggableauth.IPrincipalSource"
         conn = self._getConnection()
         try:
-            return SQLPrincipal(conn, int(id), self.table)
+            return self.factory(conn, int(id), self.table)
         except (IndexError, TypeError):
             raise NotFoundError
 
@@ -81,7 +157,7 @@
                        (self.table, name))
         result = []
         for row in cursor.fetchall():
-            result.append(SQLPrincipal(conn, row[0], self.table))
+            result.append(self.factory(conn, row[0], self.table))
 
         return result
 
@@ -92,7 +168,7 @@
         cursor.execute('SELECT id FROM "%s" WHERE login = \'%s\'' %
                        (self.table, key))
         id = cursor.fetchone()[0]
-        return SQLPrincipal(conn, id, self.table)
+        return self.factory(conn, id, self.table)
 
     def get(self, key, default=None):
         "See zope.interface.common.mapping.IReadMapping"
@@ -104,7 +180,7 @@
             cursor.execute('SELECT id FROM "%s" WHERE login = \'%s\'' %
                            (self.table, key))
             id = cursor.fetchone()[0]
-            return SQLPrincipal(conn, id, self.table)
+            return self.factory(conn, id, self.table)
 
             #return self[key]
         except (TypeError, IndexError):
@@ -127,7 +203,7 @@
         cursor = conn.cursor()
         cursor.execute('SELECT id FROM "%s"' % self.table)
         for id, in cursor.fetchall():
-            principal = SQLPrincipal(conn, id, self.table)
+            principal = self.factory(conn, id, self.table)
             yield principal.login
 
     def values(self):
@@ -137,7 +213,7 @@
         cursor.execute('SELECT id FROM "%s"' % self.table)
         result = []
         for id, in cursor.fetchall():
-            principal = SQLPrincipal(conn, id, self.table)
+            principal = self.factory(conn, id, self.table)
             result.append(principal)
         return tuple(result)
 
@@ -148,7 +224,7 @@
         cursor.execute('SELECT id FROM "%s"' % self.table)
         result = []
         for id, in cursor.fetchall():
-            principal = SQLPrincipal(conn, id, self.table)
+            principal = self.factory(conn, id, self.table)
             result.append((principal.login, principal))
         return tuple(result)
 
@@ -186,76 +262,3 @@
             return principal
         else:
             return None
-
-class SQLPrincipal:
-    """This is a simple implementation of IUserSchemafied which issues
-    UPDATE SQL statements to the connection when the attributes are modified.
-    """
-
-    implements(IUserSchemafied)
-
-    def __init__(self, connection, id, table="users"):
-        """Arguments:
-             connection      an IDBIConnection object
-             id              the primary key of the user in the database
-             table           database table with the columns named as
-                             the attributes of IUserSchemafied
-        """
-        self._connection = connection
-        self._table = table
-        self._id = id
-        cursor = self._connection.cursor()
-        cursor.execute(
-            'SELECT login, password, title, description FROM "%s" WHERE id = %s'
-            % (table, id))
-        self._login, self._password, self._title, self._description = (
-            cursor.fetchone())
-
-    def validate(self, test_password):
-        "See zope.app.interfaces.services.pluggableauth.IUserSchemafied"
-        hash = base64.encodestring(sha(test_password).digest())[:-1]
-        return self._password == hash
-
-    def getId(self):
-        "See zope.app.interfaces.security.IPrincipal"
-        return self._id
-
-    def getTitle(self):
-        "See zope.app.interfaces.security.IPrincipal"
-        return self._title
-
-    def getDescription(self):
-        "See zope.app.interfaces.security.IPrincipal"
-        return self._description
-
-    def _getLogin(self): return self._login
-    def _getTitle(self): return self._title
-    def _getPassword(self): return self._password
-    def _getDescription(self): return self._description
-
-    def _setLogin(self, login):
-        self._set('login', login)
-
-    def _setTitle(self, title):
-        self._set('title', title)
-
-    def _setDescription(self, description):
-        self._set('description', description)
-
-    def _setPassword(self, password):
-        hash = base64.encodestring(sha(password).digest())[:-1]
-        self._set('password', hash)
-
-    def _set(self, attr, value):
-        setattr(self, '_' + attr, value)
-        self._connection.cursor().execute(
-            'UPDATE "%s" SET %s = \'%s\' WHERE id = %s' % (self._table, attr,
-                                                           value, self._id))
-    def __cmp__(self, other):
-        return cmp(self.id, other.id)
-
-    id = property(getId)
-    title = property(_getTitle, _setTitle)
-    login = property(_getLogin, _setLogin)
-    password = property(_getPassword, _setPassword)
-    description = property(_getDescription, _setDescription)