1
0
mirror of https://github.com/kakwa/ldapcherry synced 2024-11-22 09:24:21 +01:00

adding check function to validate attributes type

This commit is contained in:
kakwa 2015-07-14 11:21:33 +02:00
parent dd5d7b9c19
commit cee73ceb93
4 changed files with 71 additions and 1 deletions

View File

@ -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

View File

@ -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"

View File

@ -109,7 +109,6 @@ home:
backends:
ldap: home
ad: Home
password:
description: "Password of the user"
display_name: "Password"

View File

@ -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')