From cee73ceb93fe4c8e6f4f76155af348c3f9e3df72 Mon Sep 17 00:00:00 2001 From: kakwa Date: Tue, 14 Jul 2015 11:21:33 +0200 Subject: [PATCH] adding check function to validate attributes type --- ldapcherry/attributes.py | 38 ++++++++++++++++++++++++++++++++++++++ ldapcherry/exceptions.py | 16 ++++++++++++++++ tests/cfg/attributes.yml | 1 - tests/test_Attributes.py | 17 +++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/ldapcherry/attributes.py b/ldapcherry/attributes.py index 1bf6602..15da584 100644 --- a/ldapcherry/attributes.py +++ b/ldapcherry/attributes.py @@ -7,6 +7,7 @@ import os import sys +import re from ldapcherry.pyyamlwrapper import loadNoDump from ldapcherry.pyyamlwrapper import DumplicatedKey @@ -14,6 +15,7 @@ from ldapcherry.exceptions import * from sets import Set import yaml +# List of available types for form types = ['string', 'email', 'int', 'stringlist', 'fix', 'password'] @@ -65,6 +67,42 @@ class Attributes: if self.key is None: raise MissingUserKey() + def _is_email(self, email): + pattern = '[\.\w]{1,}[@]\w+[.]\w+' + if re.match(pattern, email): + return True + else: + return False + + def check_attr(self, attr, value): + attrid = attr + if attrid not in self.attributes: + raise AttrNotDefined(attrid) + attr_type = self.attributes[attrid]['type'] + if attr_type == 'string': + return + elif attr_type == 'email': + if self._is_email(value): + return + else: + raise WrongAttrValue(attrid, attr_type) + elif attr_type == 'int': + try: + int(value) + return + except ValueError: + raise WrongAttrValue(attrid, attr_type) + elif attr_type == 'stringlist': + if value in self.attributes[attrid]['values']: + return + else: + raise WrongAttrValue(attrid, attr_type) + elif attr_type == 'fix': + if value != self.attributes[attrid]['value']: + raise WrongAttrValue(attrid, attr_type) + elif attr_type == 'password': + return + def get_search_attributes(self): return self.displayed_attributes diff --git a/ldapcherry/exceptions.py b/ldapcherry/exceptions.py index 3372b07..7dad9a4 100644 --- a/ldapcherry/exceptions.py +++ b/ldapcherry/exceptions.py @@ -158,3 +158,19 @@ class PasswordAttributesCollision(Exception): "key '" + key + "' type is password," \ " keys '" + key + "1' and '" + key + "2'"\ " are reserved and cannot be used" + + +class WrongAttrValue(Exception): + def __init__(self, attr, attrtype): + self.attr = attr + self.attrtype = attrtype + self.log = \ + "input for attribute '" + attr + "'" \ + " doesn't match type '" + attrtype + "'" + + +class AttrNotDefined(Exception): + def __init__(self, attr): + self.attr = attr + self.log = \ + "attribute '" + attr + "' is not defined in configuration" diff --git a/tests/cfg/attributes.yml b/tests/cfg/attributes.yml index 667fcf4..f068395 100644 --- a/tests/cfg/attributes.yml +++ b/tests/cfg/attributes.yml @@ -109,7 +109,6 @@ home: backends: ldap: home ad: Home - password: description: "Password of the user" display_name: "Password" diff --git a/tests/test_Attributes.py b/tests/test_Attributes.py index 073cded..af196bd 100644 --- a/tests/test_Attributes.py +++ b/tests/test_Attributes.py @@ -89,6 +89,23 @@ class TestError(object): else: raise AssertionError("expected an exception") + def testValidate(self): + inv = Attributes('./tests/cfg/attributes.yml') + attrs = {'cn': 'test', 'email': 'test@example.org', 'uidNumber': 4242, 'shell': '/bin/bash', 'logscript': 'login1.bat'} + for attrid in attrs: + inv.check_attr(attrid, attrs[attrid]) + + def testValidateError(self): + inv = Attributes('./tests/cfg/attributes.yml') + attrs = {'email': 'notamail', 'uidNumber': 'not an integer', 'shell': '/bin/not in list', 'logscript': 'not fixed'} + for attrid in attrs: + try: + inv.check_attr(attrid, attrs[attrid]) + except WrongAttrValue: + pass + else: + raise AssertionError("expected an exception") + # def testGetDisplayName(self): # inv = Attributes('./tests/cfg/attributes.yml') # res = inv.get_display_name('users')