From f8b3df8b588c7fe1b1eca618e5b0e31bee9847ca Mon Sep 17 00:00:00 2001 From: kakwa Date: Sun, 31 May 2015 23:27:04 +0200 Subject: [PATCH] implementing weight and normalizing each kind of attributes --- conf/attributes.yml | 1 + ldapcherry/attributes.py | 8 ++++---- resources/templates/form.tmpl | 2 +- resources/templates/searchadmin.tmpl | 6 +++--- resources/templates/searchuser.tmpl | 6 +++--- tests/cfg/attributes.yml | 13 ++++++++++++- tests/cfg/attributes_wrong_type.yml | 2 ++ tests/test_Attributes.py | 4 ++-- 8 files changed, 28 insertions(+), 14 deletions(-) diff --git a/conf/attributes.yml b/conf/attributes.yml index 85e4cb5..667fcf4 100644 --- a/conf/attributes.yml +++ b/conf/attributes.yml @@ -122,6 +122,7 @@ password: logscript: description: "Windows login script" display_name: "Login script" + weight: 100 type: fix value: login1.bat backends: diff --git a/ldapcherry/attributes.py b/ldapcherry/attributes.py index ac78f36..3575131 100644 --- a/ldapcherry/attributes.py +++ b/ldapcherry/attributes.py @@ -21,7 +21,7 @@ class Attributes: def __init__(self, attributes_file): self.attributes_file = attributes_file self.backends = Set([]) - self.self_attributes = Set([]) + self.self_attributes = {} self.backend_attributes = {} self.displayed_attributes = {} self.key = None @@ -40,7 +40,7 @@ class Attributes: if not attr['type'] in types: raise WrongAttributeType(attr['type'], attrid, attributes_file) if 'self' in attr and attr['self']: - self.self_attributes.add(attrid) + self.self_attributes[attrid] = attr if 'key' in attr and attr['key']: if not self.key is None: raise DumplicateUserKey(attrid, self.key) @@ -51,7 +51,7 @@ class Attributes: self.backend_attributes[b] = [] self.backend_attributes[b].append(attr['backends'][b]) if 'search_displayed' in attr and attr['search_displayed']: - self.displayed_attributes[attrid] = attr['display_name'] + self.displayed_attributes[attrid] = attr if self.key is None: raise MissingUserKey() @@ -63,7 +63,7 @@ class Attributes: return self.key def _mandatory_check(self, attr): - for m in ['description', 'display_name', 'type', 'backends']: + for m in ['description', 'display_name', 'type', 'backends', 'weight']: if m not in self.attributes[attr]: raise MissingKey(m, attr, self.attributes_file) diff --git a/resources/templates/form.tmpl b/resources/templates/form.tmpl index bc9157b..6855840 100644 --- a/resources/templates/form.tmpl +++ b/resources/templates/form.tmpl @@ -1,4 +1,4 @@ -% for a in attributes: +% for a in sorted(attributes.keys(), key=lambda attr: attributes[attr]['weight']): <% attr = attributes[a] %>
diff --git a/resources/templates/searchadmin.tmpl b/resources/templates/searchadmin.tmpl index 901562f..79f2112 100644 --- a/resources/templates/searchadmin.tmpl +++ b/resources/templates/searchadmin.tmpl @@ -22,9 +22,9 @@ - %for attr in attrs_list: + %for attr in sorted(attrs_list.keys(), key=lambda attr: attrs_list[attr]['weight']): % endfor %for user in searchresult: - %for attr in attrs_list: + %for attr in sorted(attrs_list.keys(), key=lambda attr: attrs_list[attr]['weight']):
- ${attrs_list[attr]} + ${attrs_list[attr]['display_name']} @@ -38,7 +38,7 @@
% if attr in searchresult[user]: ${searchresult[user][attr]} diff --git a/resources/templates/searchuser.tmpl b/resources/templates/searchuser.tmpl index db3612f..ad17b52 100644 --- a/resources/templates/searchuser.tmpl +++ b/resources/templates/searchuser.tmpl @@ -22,9 +22,9 @@ - %for attr in attrs_list: + %for attr in sorted(attrs_list.keys(), key=lambda attr: attrs_list[attr]['weight']): % endfor @@ -32,7 +32,7 @@ %for user in searchresult: - %for attr in attrs_list: + %for attr in sorted(attrs_list.keys(), key=lambda attr: attrs_list[attr]['weight']):
- ${attrs_list[attr]} + ${attrs_list[attr]['display_name']}
% if attr in searchresult[user]: ${searchresult[user][attr]} diff --git a/tests/cfg/attributes.yml b/tests/cfg/attributes.yml index 834b851..667fcf4 100644 --- a/tests/cfg/attributes.yml +++ b/tests/cfg/attributes.yml @@ -3,6 +3,7 @@ cn: display_name: "Display Name" type: string search_displayed: True + weight: 30 autofill: function: cn args: @@ -11,11 +12,13 @@ cn: backends: ldap: cn ad: CN + first-name: description: "First name of the user" display_name: "First Name" search_displayed: True type: string + weight: 20 backends: ldap: givenName ad: givenName @@ -23,6 +26,7 @@ name: description: "Family name of the user" display_name: "Name" search_displayed: True + weight: 10 type: string backends: ldap: sn @@ -30,8 +34,8 @@ name: email: description: "Email of the user" display_name: "Name" - search_displayed: True type: email + weight: 40 autofill: function: email args: @@ -47,6 +51,7 @@ uid: search_displayed: True key: True type: string + weight: 50 autofill: function: uid args: @@ -58,6 +63,7 @@ uid: uidNumber: description: "User ID Number of the user" display_name: "UID Number" + weight: 60 type: int autofill: function: uidNumber @@ -70,6 +76,7 @@ uidNumber: gidNumber: description: "Group ID Number of the user" display_name: "GID Number" + weight: 70 type: int default: 10000 backends: @@ -78,6 +85,7 @@ gidNumber: shell: description: "Shell of the user" display_name: "Shell" + weight: 80 self: True type: stringlist values: @@ -90,6 +98,7 @@ shell: home: description: "Home user path" display_name: "Home" + weight: 90 type: string autofill: function: home @@ -104,6 +113,7 @@ home: password: description: "Password of the user" display_name: "Password" + weight: 31 self: True type: password backends: @@ -112,6 +122,7 @@ password: logscript: description: "Windows login script" display_name: "Login script" + weight: 100 type: fix value: login1.bat backends: diff --git a/tests/cfg/attributes_wrong_type.yml b/tests/cfg/attributes_wrong_type.yml index 4a276a9..2b39c21 100644 --- a/tests/cfg/attributes_wrong_type.yml +++ b/tests/cfg/attributes_wrong_type.yml @@ -3,6 +3,7 @@ cn: display_name: "Display Name" type: notatype key: True + weight: 10 autofill: function: cn args: @@ -16,6 +17,7 @@ first-name: description: "First name of the user" display_name: "First Name" type: string + weight: 10 backends: ldap: givenName ad: givenName diff --git a/tests/test_Attributes.py b/tests/test_Attributes.py index df9f720..acabfd0 100644 --- a/tests/test_Attributes.py +++ b/tests/test_Attributes.py @@ -21,7 +21,7 @@ class TestError(object): def testGetSelfAttributes(self): inv = Attributes('./tests/cfg/attributes.yml') ret = inv.get_selfattributes() - expected = Set(['password', 'shell']) + expected = {'password': {'backends': {'ad': 'userPassword', 'ldap': 'userPassword'}, 'display_name': 'Password', 'description': 'Password of the user', 'weight': 31, 'self': True, 'type': 'password'}, 'shell': {'backends': {'ad': 'SHELL', 'ldap': 'shell'}, 'display_name': 'Shell', 'description': 'Shell of the user', 'weight': 80, 'values': ['/bin/bash', '/bin/zsh', '/bin/sh'], 'self': True, 'type': 'stringlist'}} assert ret == expected def testGetSelfAttributes(self): @@ -33,7 +33,7 @@ class TestError(object): def testGetSearchAttributes(self): inv = Attributes('./tests/cfg/attributes.yml') ret = inv.get_search_attributes() - expected = {'first-name': 'First Name', 'cn': 'Display Name', 'name': 'Name', 'uid': 'UID', 'email': 'Name'} + expected = {'first-name': {'backends': {'ad': 'givenName', 'ldap': 'givenName'}, 'display_name': 'First Name', 'description': 'First name of the user', 'weight': 20, 'search_displayed': True, 'type': 'string'}, 'cn': {'autofill': {'function': 'cn', 'args': ['$first-name', '$name']}, 'backends': {'ad': 'CN', 'ldap': 'cn'}, 'display_name': 'Display Name', 'description': 'Firt Name and Display Name', 'weight': 30, 'search_displayed': True, 'type': 'string'}, 'name': {'backends': {'ad': 'sn', 'ldap': 'sn'}, 'display_name': 'Name', 'description': 'Family name of the user', 'weight': 10, 'search_displayed': True, 'type': 'string'}, 'uid': {'display_name': 'UID', 'description': 'UID of the user', 'weight': 50, 'autofill': {'function': 'uid', 'args': ['$first-name', '$last-name']}, 'backends': {'ad': 'UID', 'ldap': 'uid'}, 'key': True, 'search_displayed': True, 'type': 'string'}} assert ret == expected def testGetBackendAttributes(self):