This patch allows you do provide a Password as a two item tuple or list
(the password plus the password confirmation).  This is the most natural
way for this to work through the UI.
>From 8ecb97ca00600f05643f6844ab5c317d79857626 Mon Sep 17 00:00:00 2001
From: Jason Gerard DeRose <jder...@redhat.com>
Date: Fri, 16 Oct 2009 02:22:39 -0600
Subject: [PATCH] Change Password param so (password, confirm_password) can be passed to _convert_scalar()

---
 ipalib/errors.py                     |    9 +++++++++
 ipalib/parameters.py                 |    9 +++++++++
 tests/test_ipalib/test_parameters.py |   11 +++++++++++
 3 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/ipalib/errors.py b/ipalib/errors.py
index fb82062..1c358cd 100644
--- a/ipalib/errors.py
+++ b/ipalib/errors.py
@@ -736,6 +736,15 @@ class NoSuchNamespaceError(InvocationError):
     format = _('api has no such namespace: %(name)r')
 
 
+class PasswordMismatch(InvocationError):
+    """
+    **3011** Raise when password and password confirmation don't match.
+    """
+
+    errno = 3011
+    format = _('Passwords do not match')
+
+
 ##############################################################################
 # 4000 - 4999: Execution errors
 
diff --git a/ipalib/parameters.py b/ipalib/parameters.py
index bf6588b..819a158 100644
--- a/ipalib/parameters.py
+++ b/ipalib/parameters.py
@@ -35,6 +35,7 @@ from util import make_repr
 from request import ugettext
 from plugable import ReadOnly, lock, check_name
 from errors import ConversionError, RequirementError, ValidationError
+from errors import PasswordMismatch
 from constants import NULLS, TYPE_ERROR, CALLABLE_ERROR
 import csv
 
@@ -1145,6 +1146,14 @@ class Password(Str):
     A parameter for passwords (stored in the ``unicode`` type).
     """
 
+    def _convert_scalar(self, value, index=None):
+        if isinstance(value, (tuple, list)) and len(value) == 2:
+            (p1, p2) = value
+            if p1 != p2:
+                raise PasswordMismatch(name=self.name, index=index)
+            value = p1
+        return super(Password, self)._convert_scalar(value, index)
+
 
 class Enum(Param):
     """
diff --git a/tests/test_ipalib/test_parameters.py b/tests/test_ipalib/test_parameters.py
index f43fa32..44286ac 100644
--- a/tests/test_ipalib/test_parameters.py
+++ b/tests/test_ipalib/test_parameters.py
@@ -1065,6 +1065,17 @@ class test_Password(ClassChecker):
         assert o.pattern is None
         assert o.password is True
 
+    def test_convert_scalar(self):
+        """
+        Test the `ipalib.parameters.Password._convert_scalar` method.
+        """
+        o = self.cls('my_password')
+        e = raises(errors.PasswordMismatch, o._convert_scalar, [u'one', u'two'])
+        assert e.name == 'my_password'
+        assert e.index is None
+        assert o._convert_scalar([u'one', u'one']) == u'one'
+        assert o._convert_scalar(u'one') == u'one'
+
 
 class test_StrEnum(ClassChecker):
     """
-- 
1.6.3.3

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to