<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[12145] twext/trunk/twext/who/opendirectory</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/12145">12145</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2013-12-19 11:10:14 -0800 (Thu, 19 Dec 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Get rid of _adaptODRecord() and just pass the odRecord object to DirectoryRecord's init.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunktwextwhoopendirectory__init__py">twext/trunk/twext/who/opendirectory/__init__.py</a></li>
<li><a href="#twexttrunktwextwhoopendirectoryservicepy">twext/trunk/twext/who/opendirectory/service.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="twexttrunktwextwhoopendirectory__init__py"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/opendirectory/__init__.py (12144 => 12145)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/opendirectory/__init__.py        2013-12-19 18:28:54 UTC (rev 12144)
+++ twext/trunk/twext/who/opendirectory/__init__.py        2013-12-19 19:10:14 UTC (rev 12145)
</span><span class="lines">@@ -22,11 +22,14 @@
</span><span class="cx">     &quot;OpenDirectoryError&quot;,
</span><span class="cx">     &quot;OpenDirectoryConnectionError&quot;,
</span><span class="cx">     &quot;OpenDirectoryQueryError&quot;,
</span><ins>+    &quot;OpenDirectoryDataError&quot;,
</ins><span class="cx">     &quot;DirectoryService&quot;,
</span><span class="cx">     &quot;DirectoryRecord&quot;,
</span><span class="cx"> ]
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-from .service import OpenDirectoryError
-from .service import OpenDirectoryConnectionError, OpenDirectoryQueryError
-from .service import DirectoryService, DirectoryRecord
</del><ins>+from .service import (
+    OpenDirectoryError, OpenDirectoryConnectionError, OpenDirectoryQueryError,
+    OpenDirectoryDataError,
+    DirectoryService, DirectoryRecord,
+)
</ins></span></pre></div>
<a id="twexttrunktwextwhoopendirectoryservicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/opendirectory/service.py (12144 => 12145)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/opendirectory/service.py        2013-12-19 18:28:54 UTC (rev 12144)
+++ twext/trunk/twext/who/opendirectory/service.py        2013-12-19 19:10:14 UTC (rev 12145)
</span><span class="lines">@@ -23,9 +23,19 @@
</span><span class="cx"> 
</span><span class="cx"> from odframework import ODSession, ODNode, ODQuery
</span><span class="cx"> 
</span><del>-from twisted.python.constants import Names, NamedConstant
-from twisted.python.constants import Values, ValueConstant
</del><ins>+from zope.interface import implements
</ins><span class="cx"> 
</span><ins>+from twisted.python.constants import (
+    Names, NamedConstant, Values, ValueConstant,
+)
+from twisted.internet.defer import succeed, fail
+from twisted.cred.checkers import ICredentialsChecker
+from twisted.cred.credentials import (
+    IUsernamePassword, IUsernameHashedPassword, DigestedCredentials,
+)
+from twisted.cred.error import UnauthorizedLogin
+# from twisted.web.guard import DigestCredentialFactory
+
</ins><span class="cx"> from twext.python.log import Logger
</span><span class="cx"> 
</span><span class="cx"> from ..idirectory import (
</span><span class="lines">@@ -41,18 +51,8 @@
</span><span class="cx"> )
</span><span class="cx"> from ..util import iterFlags, ConstantsContainer
</span><span class="cx"> 
</span><del>-from twisted.cred.checkers import ICredentialsChecker
-from twisted.cred.credentials import IUsernamePassword, IUsernameHashedPassword
-from twisted.cred.error import UnauthorizedLogin
</del><span class="cx"> 
</span><del>-from zope.interface import implements
-from twisted.internet.defer import succeed, fail
-from twisted.web.guard import DigestCredentialFactory
-from twisted.cred.credentials import DigestedCredentials
</del><span class="cx"> 
</span><del>-
-
-
</del><span class="cx"> #
</span><span class="cx"> # Exceptions
</span><span class="cx"> #
</span><span class="lines">@@ -81,7 +81,13 @@
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+class OpenDirectoryDataError(OpenDirectoryError):
+    &quot;&quot;&quot;
+    OpenDirectory data error.
+    &quot;&quot;&quot;
</ins><span class="cx"> 
</span><ins>+
+
</ins><span class="cx"> #
</span><span class="cx"> # Constants
</span><span class="cx"> #
</span><span class="lines">@@ -477,62 +483,6 @@
</span><span class="cx">         return query
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-    def _adaptODRecord(self, odRecord):
-        &quot;&quot;&quot;
-        Adapt a native OpenDirectory record to a L{DirectoryRecord}.
-
-        @param odRecord: A native OpenDirectory record.
-        @type odRecord: L{ODRecord}
-
-        @return: A directory record with the fields matching the attributes of
-            C{odRecord}.
-        @rtype: L{DirectoryRecord}
-        &quot;&quot;&quot;
-        details, error = odRecord.recordDetailsForAttributes_error_(None, None)
-
-        if error:
-            self.log.error(
-                &quot;Error while reading OpenDirectory record: {error}&quot;,
-                error=error
-            )
-            raise OpenDirectoryQueryError(error)
-
-        fields = {}
-        for name, values in details.iteritems():
-            if name == ODAttribute.metaRecordName.value:
-                # We get this field even though we did not ask for it...
-                continue
-
-            try:
-                attribute = ODAttribute.lookupByValue(name)
-            except ValueError:
-                self.log.debug(
-                    &quot;Unexpected OpenDirectory record attribute: {attribute}&quot;,
-                    attribute=name
-                )
-                continue
-            fieldName = attribute.fieldName
-
-            if type(values) is bytes:
-                values = (unicode(values),)
-            else:
-                values = [unicode(v) for v in values]
-
-            if BaseFieldName.isMultiValue(fieldName):
-                fields[fieldName] = values
-            else:
-                assert len(values) == 1
-
-                if fieldName is self.fieldName.recordType:
-                    fields[fieldName] = ODRecordType.lookupByValue(
-                        values[0]
-                    ).recordType
-                else:
-                    fields[fieldName] = values[0]
-
-        return DirectoryRecord(self, fields)
-
-
</del><span class="cx">     def _recordsFromQuery(self, query):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Executes a query and generates directory records from it.
</span><span class="lines">@@ -554,7 +504,7 @@
</span><span class="cx">             raise OpenDirectoryQueryError(error)
</span><span class="cx"> 
</span><span class="cx">         for odRecord in odRecords:
</span><del>-            yield self._adaptODRecord(odRecord)
</del><ins>+            yield DirectoryRecord(self, odRecord)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -573,11 +523,11 @@
</span><span class="cx">         try:
</span><span class="cx">             if isinstance(expression, CompoundExpression):
</span><span class="cx">                 query = self._queryFromCompoundExpression(expression)
</span><del>-                return self._recordsFromQuery(query)
</del><ins>+                return succeed(self._recordsFromQuery(query))
</ins><span class="cx"> 
</span><span class="cx">             elif isinstance(expression, MatchExpression):
</span><span class="cx">                 query = self._queryFromMatchExpression(expression)
</span><del>-                return self._recordsFromQuery(query)
</del><ins>+                return succeed(self._recordsFromQuery(query))
</ins><span class="cx"> 
</span><span class="cx">         except QueryNotSupportedError:
</span><span class="cx">             pass
</span><span class="lines">@@ -620,13 +570,13 @@
</span><span class="cx">         @raises: L{UnauthorizedLogin} if the credentials are not valid.
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-        record = self._getUserRecord(credentials.username)
</del><ins>+        odRecord = self._getUserRecord(credentials.username)
</ins><span class="cx"> 
</span><del>-        if record is None:
</del><ins>+        if odRecord is None:
</ins><span class="cx">             return fail(UnauthorizedLogin(&quot;No such user&quot;))
</span><span class="cx"> 
</span><span class="cx">         if IUsernamePassword.providedBy(credentials):
</span><del>-            result, error = record.verifyPassword_error_(
</del><ins>+            result, error = odRecord.verifyPassword_error_(
</ins><span class="cx">                 credentials.password, None
</span><span class="cx">             )
</span><span class="cx"> 
</span><span class="lines">@@ -634,7 +584,7 @@
</span><span class="cx">                 return fail(UnauthorizedLogin(error))
</span><span class="cx"> 
</span><span class="cx">             if result:
</span><del>-                return succeed(self._adaptODRecord(record))
</del><ins>+                return succeed(DirectoryRecord(self, odRecord))
</ins><span class="cx"> 
</span><span class="cx">         elif isinstance(credentials, DigestedCredentials):
</span><span class="cx">             try:
</span><span class="lines">@@ -655,7 +605,7 @@
</span><span class="cx">                 )
</span><span class="cx">                 return fail(UnauthorizedLogin(&quot;Invalid digest challenge&quot;))
</span><span class="cx"> 
</span><del>-            result, m1, m2, error = record.verifyExtendedWithAuthenticationType_authenticationItems_continueItems_context_error_(
</del><ins>+            result, m1, m2, error = odRecord.verifyExtendedWithAuthenticationType_authenticationItems_continueItems_context_error_(
</ins><span class="cx">                 &quot;dsAuthMethodStandard:dsAuthNodeDIGEST-MD5&quot;,
</span><span class="cx">                 [
</span><span class="cx">                     credentials.username,
</span><span class="lines">@@ -670,7 +620,7 @@
</span><span class="cx">                 return fail(UnauthorizedLogin(error))
</span><span class="cx"> 
</span><span class="cx">             if result:
</span><del>-                return succeed(self._adaptODRecord(record))
</del><ins>+                return succeed(DirectoryRecord(self, odRecord))
</ins><span class="cx"> 
</span><span class="cx">         else:
</span><span class="cx">             return fail(UnauthorizedLogin(
</span><span class="lines">@@ -680,24 +630,74 @@
</span><span class="cx">         return fail(UnauthorizedLogin(&quot;Unknown authorization failure&quot;))
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class CustomDigestCredentialFactory(DigestCredentialFactory):
-    &quot;&quot;&quot;
-    DigestCredentialFactory without qop, to interop with OD.
-    &quot;&quot;&quot;
</del><span class="cx"> 
</span><del>-    def getChallenge(self, address):
-        result = DigestCredentialFactory.getChallenge(self, address)
-        del result[&quot;qop&quot;]
-        return result
</del><ins>+# class CustomDigestCredentialFactory(DigestCredentialFactory):
+#     &quot;&quot;&quot;
+#     DigestCredentialFactory without qop, to interop with OD.
+#     &quot;&quot;&quot;
</ins><span class="cx"> 
</span><ins>+#     def getChallenge(self, address):
+#         result = DigestCredentialFactory.getChallenge(self, address)
+#         del result[&quot;qop&quot;]
+#         return result
</ins><span class="cx"> 
</span><ins>+
+
</ins><span class="cx"> class DirectoryRecord(BaseDirectoryRecord):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     OpenDirectory directory record.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-    def __init__(self, service, fields):
-         # Make sure that uid and guid are both set and equal
</del><ins>+    log = Logger()
+
+    # GUID is a required attribute for OD records.
+    requiredFields = BaseDirectoryRecord.requiredFields + (BaseFieldName.guid,)
+
+
+    def __init__(self, service, odRecord):
+        details, error = odRecord.recordDetailsForAttributes_error_(None, None)
+
+        if error:
+            self.log.error(
+                &quot;Error while reading OpenDirectory record: {error}&quot;,
+                error=error
+            )
+            raise OpenDirectoryDataError(error)
+
+        fields = {}
+        for name, values in details.iteritems():
+            if name == ODAttribute.metaRecordName.value:
+                # We get this field even though we did not ask for it...
+                continue
+
+            try:
+                attribute = ODAttribute.lookupByValue(name)
+            except ValueError:
+                self.log.debug(
+                    &quot;Unexpected OpenDirectory record attribute: {attribute}&quot;,
+                    attribute=name
+                )
+                continue
+            fieldName = attribute.fieldName
+
+            if type(values) is bytes:
+                values = (unicode(values),)
+            else:
+                values = [unicode(v) for v in values]
+
+            if BaseFieldName.isMultiValue(fieldName):
+                fields[fieldName] = values
+            else:
+                assert len(values) == 1
+
+                if fieldName is service.fieldName.recordType:
+                    fields[fieldName] = ODRecordType.lookupByValue(
+                        values[0]
+                    ).recordType
+                else:
+                    fields[fieldName] = values[0]
+
+        # Make sure that uid and guid are both set and equal
</ins><span class="cx">         uid = fields.get(service.fieldName.uid, None)
</span><span class="cx">         guid = fields.get(service.fieldName.guid, None)
</span><span class="cx"> 
</span><span class="lines">@@ -713,14 +713,10 @@
</span><span class="cx">             fields[service.fieldName.guid] = uid
</span><span class="cx"> 
</span><span class="cx">         super(DirectoryRecord, self).__init__(service, fields)
</span><ins>+        self._odRecord = odRecord
</ins><span class="cx"> 
</span><span class="cx"> 
</span><del>-    requiredFields = BaseDirectoryRecord.requiredFields + (BaseFieldName.guid,)
</del><span class="cx"> 
</span><del>-
-
-
-
</del><span class="cx"> if __name__ == &quot;__main__&quot;:
</span><span class="cx">     import sys
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>