<!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>[12244] twext/trunk/twext/who</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/12244">12244</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2014-01-06 15:22:43 -0800 (Mon, 06 Jan 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Handle invalid directory record fields.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#twexttrunktwextwhodirectorypy">twext/trunk/twext/who/directory.py</a></li>
<li><a href="#twexttrunktwextwhoidirectorypy">twext/trunk/twext/who/idirectory.py</a></li>
<li><a href="#twexttrunktwextwhoopendirectory_servicepy">twext/trunk/twext/who/opendirectory/_service.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="twexttrunktwextwhodirectorypy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/directory.py (12243 => 12244)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/directory.py        2014-01-06 22:03:14 UTC (rev 12243)
+++ twext/trunk/twext/who/directory.py        2014-01-06 23:22:43 UTC (rev 12244)
</span><span class="lines">@@ -31,6 +31,7 @@
</span><span class="cx"> from twisted.cred.credentials import DigestedCredentials
</span><span class="cx"> 
</span><span class="cx"> from .idirectory import (
</span><ins>+    InvalidDirectoryRecordError,
</ins><span class="cx">     QueryNotSupportedError, NotAllowedError,
</span><span class="cx">     FieldName, RecordType,
</span><span class="cx">     IDirectoryService, IDirectoryRecord,
</span><span class="lines">@@ -323,33 +324,40 @@
</span><span class="cx">     def __init__(self, service, fields):
</span><span class="cx">         for fieldName in self.requiredFields:
</span><span class="cx">             if fieldName not in fields or not fields[fieldName]:
</span><del>-                raise ValueError(&quot;{0} field is required.&quot;.format(fieldName))
</del><ins>+                raise InvalidDirectoryRecordError(
+                    &quot;{0} field is required.&quot;.format(fieldName),
+                    fields
+                )
</ins><span class="cx"> 
</span><span class="cx">             if service.fieldName.isMultiValue(fieldName):
</span><span class="cx">                 values = fields[fieldName]
</span><span class="cx">                 for value in values:
</span><span class="cx">                     if not value:
</span><del>-                        raise ValueError(
-                            &quot;{0} field must not be empty.&quot;.format(fieldName)
</del><ins>+                        raise InvalidDirectoryRecordError(
+                            &quot;{0} field must not be empty.&quot;.format(fieldName),
+                            fields
</ins><span class="cx">                         )
</span><span class="cx"> 
</span><span class="cx">         if (
</span><span class="cx">             fields[FieldName.recordType] not in
</span><span class="cx">             service.recordType.iterconstants()
</span><span class="cx">         ):
</span><del>-            raise ValueError(
</del><ins>+            raise InvalidDirectoryRecordError(
</ins><span class="cx">                 &quot;Unknown record type: {0!r} is not in {1!r}.&quot;.format(
</span><span class="cx">                     fields[FieldName.recordType],
</span><span class="cx">                     tuple(service.recordType.iterconstants()),
</span><del>-                )
</del><ins>+                ),
+                fields
</ins><span class="cx">             )
</span><span class="cx"> 
</span><span class="cx">         def checkType(name, value):
</span><span class="cx">             expectedType = service.fieldName.valueType(name)
</span><span class="cx">             if not isinstance(value, expectedType):
</span><del>-                raise TypeError(
-                    &quot;Value {0!r} for field {1} is not of type {2}&quot;
-                    .format(value, name, expectedType)
</del><ins>+                raise InvalidDirectoryRecordError(
+                    &quot;Value {0!r} for field {1} is not of type {2}&quot;.format(
+                        value, name, expectedType
+                    ),
+                    fields
</ins><span class="cx">                 )
</span><span class="cx"> 
</span><span class="cx">         # Normalize fields
</span></span></pre></div>
<a id="twexttrunktwextwhoidirectorypy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/idirectory.py (12243 => 12244)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/idirectory.py        2014-01-06 22:03:14 UTC (rev 12243)
+++ twext/trunk/twext/who/idirectory.py        2014-01-06 23:22:43 UTC (rev 12244)
</span><span class="lines">@@ -23,6 +23,7 @@
</span><span class="cx">     &quot;DirectoryServiceError&quot;,
</span><span class="cx">     &quot;DirectoryConfigurationError&quot;,
</span><span class="cx">     &quot;DirectoryAvailabilityError&quot;,
</span><ins>+    &quot;InvalidDirectoryRecordError&quot;,
</ins><span class="cx">     &quot;UnknownRecordTypeError&quot;,
</span><span class="cx">     &quot;QueryNotSupportedError&quot;,
</span><span class="cx">     &quot;NoSuchRecordError&quot;,
</span><span class="lines">@@ -68,6 +69,23 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+class InvalidDirectoryRecordError(DirectoryServiceError):
+    &quot;&quot;&quot;
+    Invalid directory record.
+    &quot;&quot;&quot;
+    def __init__(self, message, fields):
+        &quot;&quot;&quot;
+        @param message: An error message.
+        @type message: string
+
+        @param fields: The fields that were used to attempt to build a record.
+        @type fields: L{dict}
+        &quot;&quot;&quot;
+        super(InvalidDirectoryRecordError, self).__init__(message)
+        self.fields = fields
+
+
+
</ins><span class="cx"> class UnknownRecordTypeError(DirectoryServiceError):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Unknown record type.
</span></span></pre></div>
<a id="twexttrunktwextwhoopendirectory_servicepy"></a>
<div class="modfile"><h4>Modified: twext/trunk/twext/who/opendirectory/_service.py (12243 => 12244)</h4>
<pre class="diff"><span>
<span class="info">--- twext/trunk/twext/who/opendirectory/_service.py        2014-01-06 22:03:14 UTC (rev 12243)
+++ twext/trunk/twext/who/opendirectory/_service.py        2014-01-06 23:22:43 UTC (rev 12244)
</span><span class="lines">@@ -30,7 +30,8 @@
</span><span class="cx"> from twext.python.log import Logger
</span><span class="cx"> 
</span><span class="cx"> from ..idirectory import (
</span><del>-    DirectoryServiceError, QueryNotSupportedError,
</del><ins>+    DirectoryServiceError, DirectoryAvailabilityError,
+    InvalidDirectoryRecordError, QueryNotSupportedError,
</ins><span class="cx">     FieldName as BaseFieldName, RecordType as BaseRecordType,
</span><span class="cx">     IPlaintextPasswordVerifier, IHTTPDigestVerifier,
</span><span class="cx"> )
</span><span class="lines">@@ -66,13 +67,17 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class OpenDirectoryConnectionError(OpenDirectoryError):
</del><ins>+class OpenDirectoryConnectionError(DirectoryAvailabilityError):
</ins><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     OpenDirectory connection error.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><ins>+    def __init__(self, message, odError=None):
+        super(OpenDirectoryConnectionError, self).__init__(message)
+        self.odError = odError
</ins><span class="cx"> 
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx"> class OpenDirectoryQueryError(OpenDirectoryError):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     OpenDirectory query error.
</span><span class="lines">@@ -346,6 +351,7 @@
</span><span class="cx">             raise QueryNotSupportedError(
</span><span class="cx">                 &quot;Unknown match type: {0}&quot;.format(matchType)
</span><span class="cx">             )
</span><ins>+        matchType = matchType.value
</ins><span class="cx"> 
</span><span class="cx">         if MatchFlags.caseInsensitive in iterFlags(expression.flags):
</span><span class="cx">             caseInsensitive = 0x100
</span><span class="lines">@@ -356,21 +362,25 @@
</span><span class="cx">         maxResults = 0
</span><span class="cx"> 
</span><span class="cx">         if expression.fieldName is self.fieldName.recordType:
</span><del>-            recordTypes = ODRecordType.fromRecordType(expression.fieldValue).value
-            matchType = ODMatchType.all
</del><ins>+            recordTypes = ODRecordType.fromRecordType(
+                expression.fieldValue
+            ).value
+            matchType = ODMatchType.all.value
</ins><span class="cx">             queryAttribute = None
</span><span class="cx">             queryValue = None
</span><span class="cx"> 
</span><span class="cx">         else:
</span><span class="cx">             recordTypes = [t.value for t in ODRecordType.iterconstants()]
</span><del>-            queryAttribute = ODAttribute.fromFieldName(expression.fieldName).value
</del><ins>+            queryAttribute = ODAttribute.fromFieldName(
+                expression.fieldName
+            ).value
</ins><span class="cx">             queryValue = expression.fieldValue
</span><span class="cx"> 
</span><span class="cx">         query, error = ODQuery.queryWithNode_forRecordTypes_attribute_matchType_queryValues_returnAttributes_maximumResults_error_(
</span><span class="cx">             self.node,
</span><span class="cx">             recordTypes,
</span><span class="cx">             queryAttribute,
</span><del>-            matchType.value | caseInsensitive,
</del><ins>+            matchType | caseInsensitive,
</ins><span class="cx">             queryValue,
</span><span class="cx">             fetchAttributes,
</span><span class="cx">             maxResults,
</span><span class="lines">@@ -415,9 +425,23 @@
</span><span class="cx">                 &quot;Unable to execute OpenDirectory query&quot;, error
</span><span class="cx">             ))
</span><span class="cx"> 
</span><del>-        return succeed([DirectoryRecord(self, odr) for odr in odRecords])
</del><ins>+        result = []
+        for odRecord in odRecords:
+            try:
+                record = DirectoryRecord(self, odRecord)
+            except InvalidDirectoryRecordError as e:
+                self.log.error(
+                    &quot;Invalid OpenDirectory record ({error}).  &quot;
+                    &quot;Fields: {error.fields}&quot;,
+                    error=e
+                )
+                continue
</ins><span class="cx"> 
</span><ins>+            result.append(record)
</ins><span class="cx"> 
</span><ins>+        return succeed(result)
+
+
</ins><span class="cx">     def recordsFromNonCompoundExpression(self, expression, records=None):
</span><span class="cx">         if isinstance(expression, MatchExpression):
</span><span class="cx">             try:
</span><span class="lines">@@ -542,8 +566,15 @@
</span><span class="cx">                 fields[fieldName] = values[0]
</span><span class="cx"> 
</span><span class="cx">         # Set uid from guid
</span><del>-        fields[service.fieldName.uid] = unicode(fields[service.fieldName.guid])
</del><ins>+        try:
+            guid = fields[service.fieldName.guid]
+        except KeyError:
+            raise InvalidDirectoryRecordError(
+                &quot;GUID field is required.&quot;, fields
+            )
</ins><span class="cx"> 
</span><ins>+        fields[service.fieldName.uid] = unicode(guid)
+
</ins><span class="cx">         super(DirectoryRecord, self).__init__(service, fields)
</span><span class="cx">         self._odRecord = odRecord
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>