Hi,

this is refactoring of the patch for ticket 1259 (handling of boolean for nsaccountlock in LDAP).


Now it is possible to just work with True/False on Python side and JavaScript side also gets true/false via JSON marshalling. At the same time, TRUE/FALSE is provided towards LDAP storage and correctly handled when returned back.

Tested with command line tools, WebUI, and make-test.
--
/ Alexander Bokovoy
From a9c9eeb9e64a0b91190d6f0a3abd84caf18034bd Mon Sep 17 00:00:00 2001
From: Alexander Bokovoy <aboko...@redhat.com>
Date: Thu, 7 Jul 2011 18:58:18 +0300
Subject: [PATCH] Convert nsaccountlock to always work as bool towards Python
 code

https://fedorahosted.org/freeipa/ticket/1259

Python code will see nsaccountlock as bool. JavaScript code will also see it as 
bool.

This allows native boolean operations with the lock field. Passes both CLI and 
WebUI tests.
---
 install/ui/user.js             |   11 +++++++++--
 ipalib/parameters.py           |    4 ++--
 ipalib/plugins/user.py         |   25 +++++++++++++++----------
 ipaserver/ipaldap.py           |    2 +-
 tests/test_xmlrpc/test_attr.py |   14 +++++++-------
 5 files changed, 34 insertions(+), 22 deletions(-)

diff --git a/install/ui/user.js b/install/ui/user.js
index 
9fd14d69ddf447dd3afd194b4e4b611292bdfecc..fb43916f1dd86fda2aac21ae8bcf50596ac9c166
 100644
--- a/install/ui/user.js
+++ b/install/ui/user.js
@@ -200,9 +200,16 @@ IPA.user_status_widget = function(spec) {
         if (!that.record) return;
 
         var lock_field = 'nsaccountlock';
+        var locked_field = that.record[lock_field];
+        var locked = false;
 
-        var locked = that.record[lock_field] &&
-            that.record[lock_field][0].toLowerCase() === 'true';
+        if (typeof locked_field === 'array') locked_field = locked_field[0];
+
+        if (typeof locked_field === 'boolean') {
+            locked = locked_field;
+        } else {
+            locked = locked_field && locked_field.toLowerCase() === 'true';
+        }
 
         var status;
         var action;
diff --git a/ipalib/parameters.py b/ipalib/parameters.py
index 
3d9f208d2f49e2f02bfbfacef9911bdb124961e8..56ce056a6a66585a3b1b6c7366056d852f615665
 100644
--- a/ipalib/parameters.py
+++ b/ipalib/parameters.py
@@ -903,8 +903,8 @@ class Bool(Param):
     # FIXME: This my quick hack to get some UI stuff working, change these 
defaults
     #   --jderose 2009-08-28
     kwargs = Param.kwargs + (
-        ('truths', frozenset, frozenset([1, u'1', u'true', u'TRUE'])),
-        ('falsehoods', frozenset, frozenset([0, u'0', u'false', u'FALSE'])),
+        ('truths', frozenset, frozenset([1, u'1', True, u'true', u'TRUE'])),
+        ('falsehoods', frozenset, frozenset([0, u'0', False, u'false', 
u'FALSE'])),
     )
 
     def _convert_scalar(self, value, index=None):
diff --git a/ipalib/plugins/user.py b/ipalib/plugins/user.py
index 
1f85238daeccb67349cd53242356f19c9e6b81ea..7d67bddd5730e4ed8bdc546972ef6dbba58d6847
 100644
--- a/ipalib/plugins/user.py
+++ b/ipalib/plugins/user.py
@@ -69,11 +69,19 @@ NO_UPG_MAGIC = '__no_upg__'
 
 def validate_nsaccountlock(entry_attrs):
     if 'nsaccountlock' in entry_attrs:
-        if not isinstance(entry_attrs['nsaccountlock'], basestring):
-            raise errors.OnlyOneValueAllowed(attr='nsaccountlock')
-        if entry_attrs['nsaccountlock'].lower() not in ('true','false'):
-            raise errors.ValidationError(name='nsaccountlock', error='must be 
TRUE or FALSE')
+        nsaccountlock = entry_attrs['nsaccountlock']
+        if not isinstance(nsaccountlock, (bool, Bool)):
+            if not isinstance(nsaccountlock, basestring):
+                raise errors.OnlyOneValueAllowed(attr='nsaccountlock')
+            if nsaccountlock.lower() not in ('true','false'):
+                raise errors.ValidationError(name='nsaccountlock', error='must 
be TRUE or FALSE')
 
+def convert_nsaccountlock(entry_attrs):
+    if not 'nsaccountlock' in entry_attrs:
+        entry_attrs['nsaccountlock'] = False
+    else:
+        nsaccountlock = Bool('temp')
+        entry_attrs['nsaccountlock'] = 
nsaccountlock.convert(entry_attrs['nsaccountlock'][0])
 
 class user(LDAPObject):
     """
@@ -428,8 +436,7 @@ class user_mod(LDAPUpdate):
         return dn
 
     def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
-        if not 'nsaccountlock' in entry_attrs:
-            entry_attrs['nsaccountlock'] = [u'False']
+        convert_nsaccountlock(entry_attrs)
         self.obj._convert_manager(entry_attrs, **options)
         return dn
 
@@ -460,8 +467,7 @@ class user_find(LDAPSearch):
         for entry in entries:
             (dn, attrs) = entry
             self.obj._convert_manager(attrs, **options)
-            if not 'nsaccountlock' in attrs:
-                attrs['nsaccountlock'] = [u'False']
+            convert_nsaccountlock(attrs)
 
     msg_summary = ngettext(
         '%(count)d user matched', '%(count)d users matched', 0
@@ -475,8 +481,7 @@ class user_show(LDAPRetrieve):
     Display information about a user.
     """
     def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
-        if not 'nsaccountlock' in entry_attrs:
-            entry_attrs['nsaccountlock'] = [u'False']
+        convert_nsaccountlock(entry_attrs)
         self.obj._convert_manager(entry_attrs, **options)
         return dn
 
diff --git a/ipaserver/ipaldap.py b/ipaserver/ipaldap.py
index 
cf76d6222089f9195d304e748f4ffa24978c8f36..77f5b289a5dd663fe0713d00262f788361fb0cd3
 100644
--- a/ipaserver/ipaldap.py
+++ b/ipaserver/ipaldap.py
@@ -567,7 +567,7 @@ class IPAdmin(SimpleLDAPObject):
         else:
             operation = ldap.MOD_ADD
 
-        modlist.append((operation, "nsAccountlock", "true"))
+        modlist.append((operation, "nsAccountlock", "TRUE"))
 
         try:
             if sctrl is not None:
diff --git a/tests/test_xmlrpc/test_attr.py b/tests/test_xmlrpc/test_attr.py
index 
198251fcd0401518420fff41514d403e2f8efd9c..9f1b1c51db3f800aef98e49eb5a22e7416d99e73
 100644
--- a/tests/test_xmlrpc/test_attr.py
+++ b/tests/test_xmlrpc/test_attr.py
@@ -83,7 +83,7 @@ class test_attr(Declarative):
                     gidnumber=[fuzzy_digits],
                     mail=[u't...@example.com'],
                     memberof_group=[u'ipausers'],
-                    nsaccountlock=[u'False'],
+                    nsaccountlock=False,
                 ),
                 summary=u'Modified user "tuser1"',
                 value=user1,
@@ -107,7 +107,7 @@ class test_attr(Declarative):
                     gidnumber=[fuzzy_digits],
                     mail=[u't...@example.com', u'te...@example.com'],
                     memberof_group=[u'ipausers'],
-                    nsaccountlock=[u'False'],
+                    nsaccountlock=False,
                 ),
                 summary=u'Modified user "tuser1"',
                 value=user1,
@@ -132,7 +132,7 @@ class test_attr(Declarative):
                     mail=[u't...@example.com', u'te...@example.com'],
                     memberof_group=[u'ipausers'],
                     telephonenumber=[u'410-555-1212', u'301-555-1212'],
-                    nsaccountlock=[u'False'],
+                    nsaccountlock=False,
                 ),
                 summary=u'Modified user "tuser1"',
                 value=user1,
@@ -157,7 +157,7 @@ class test_attr(Declarative):
                     mail=[u't...@example.com', u'te...@example.com'],
                     memberof_group=[u'ipausers'],
                     telephonenumber=[u'301-555-1212'],
-                    nsaccountlock=[u'False'],
+                    nsaccountlock=False,
                 ),
                 summary=u'Modified user "tuser1"',
                 value=user1,
@@ -182,7 +182,7 @@ class test_attr(Declarative):
                     mail=[u't...@example.com', u'te...@example.com'],
                     memberof_group=[u'ipausers'],
                     telephonenumber=[u'301-555-1212', u'202-888-9833', 
u'703-555-1212'],
-                    nsaccountlock=[u'False'],
+                    nsaccountlock=False,
                 ),
                 summary=u'Modified user "tuser1"',
                 value=user1,
@@ -225,7 +225,7 @@ class test_attr(Declarative):
                     mail=[u't...@example.com', u'te...@example.com'],
                     memberof_group=[u'ipausers'],
                     telephonenumber=[u'301-555-1212', u'202-888-9833', 
u'703-555-1212'],
-                    nsaccountlock=[u'False'],
+                    nsaccountlock=False,
                 ),
                 summary=u'Modified user "tuser1"',
                 value=user1,
@@ -250,7 +250,7 @@ class test_attr(Declarative):
                     mail=[u't...@example.com', u'te...@example.com'],
                     memberof_group=[u'ipausers'],
                     telephonenumber=[u'301-555-1212', u'202-888-9833', 
u'703-555-1212'],
-                    nsaccountlock=[u'False'],
+                    nsaccountlock=False,
                 ),
                 summary=u'Modified user "tuser1"',
                 value=user1,
-- 
1.7.6

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

Reply via email to