summaryrefslogtreecommitdiffstats
path: root/ipalib/plugins/pwpolicy.py
diff options
context:
space:
mode:
authorPetr Viktorin <pviktori@redhat.com>2012-05-18 08:19:45 -0400
committerMartin Kosek <mkosek@redhat.com>2012-05-28 16:03:28 +0200
commitae1257517064362188b893a939cb336c48097256 (patch)
tree562cebc2f059b59584e57b30ab5e8dcac2d5b056 /ipalib/plugins/pwpolicy.py
parent74293426d9b88dad1fffa1762d2be83b1eb45d02 (diff)
downloadfreeipa-ae1257517064362188b893a939cb336c48097256.tar.gz
freeipa-ae1257517064362188b893a939cb336c48097256.tar.xz
freeipa-ae1257517064362188b893a939cb336c48097256.zip
Fix the pwpolicy_find post_callback
Always call convert_time_for_output so time gets reported correctly. That method has its own checks for whether the attributes are present; an additional check is unnecessary. Use a key function for sorting; cmp is deprecated, slower and more complicated. Add a test https://fedorahosted.org/freeipa/ticket/2726
Diffstat (limited to 'ipalib/plugins/pwpolicy.py')
-rw-r--r--ipalib/plugins/pwpolicy.py35
1 files changed, 18 insertions, 17 deletions
diff --git a/ipalib/plugins/pwpolicy.py b/ipalib/plugins/pwpolicy.py
index 330f9f7e5..7be590f2e 100644
--- a/ipalib/plugins/pwpolicy.py
+++ b/ipalib/plugins/pwpolicy.py
@@ -459,34 +459,36 @@ class pwpolicy_find(LDAPSearch):
# this command does custom sorting in post_callback
sort_result_entries = False
- def sort_priority(self,x,y):
+ def priority_sort_key(self, entry):
+ """Key for sorting password policies
+
+ returns a pair: (is_global, priority)
+ """
# global policy will be always last in the output
- if x[1]['cn'][0] == global_policy_name:
- return 1
- elif y[1]['cn'][0] == global_policy_name:
- return -1
+ if entry[1]['cn'][0] == global_policy_name:
+ return True, 0
else:
- # policies with higher priority will be at the beginning of the list
+ # policies with higher priority (lower number) will be at the
+ # beginning of the list
try:
- x = cmp(int(x[1]['cospriority'][0]), int(y[1]['cospriority'][0]))
+ cospriority = entry[1]['cospriority'][0]
except KeyError:
# if cospriority is not present in the entry, rather return 0
# than crash
- x = 0
- return x
+ cospriority = 0
+ return False, cospriority
def post_callback(self, ldap, entries, truncated, *args, **options):
for e in entries:
- # attribute rights are not allowed for pwpolicy_find
+ # When pkey_only flag is on, entries should contain only a cn.
+ # Add a cospriority attribute that will be used for sorting.
+ # Attribute rights are not allowed for pwpolicy_find.
self.obj.add_cospriority(e[1], e[1]['cn'][0], rights=False)
- if options.get('pkey_only', False):
- # when pkey_only flag is on, entries should contain only a cn
- # and a cospriority attribute that will be used for sorting
- # When the entries are sorted, cosentry is removed
- self.obj.convert_time_for_output(e[1], **options)
+
+ self.obj.convert_time_for_output(e[1], **options)
# do custom entry sorting by its cospriority
- entries.sort(self.sort_priority)
+ entries.sort(key=self.priority_sort_key)
if options.get('pkey_only', False):
# remove cospriority that was used for sorting
@@ -497,4 +499,3 @@ class pwpolicy_find(LDAPSearch):
pass
api.register(pwpolicy_find)
-