2015-04-16 22:07:45 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:set expandtab tabstop=4 shiftwidth=4:
|
|
|
|
#
|
|
|
|
# The MIT License (MIT)
|
|
|
|
# LdapCherry
|
|
|
|
# Copyright (c) 2014 Carpentier Pierre-Francois
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2015-07-14 11:21:33 +02:00
|
|
|
import re
|
2015-04-16 22:07:45 +02:00
|
|
|
|
2015-05-17 12:24:29 +02:00
|
|
|
from ldapcherry.pyyamlwrapper import loadNoDump
|
|
|
|
from ldapcherry.pyyamlwrapper import DumplicatedKey
|
2015-07-11 22:03:58 +02:00
|
|
|
from ldapcherry.exceptions import *
|
2015-05-17 12:24:29 +02:00
|
|
|
from sets import Set
|
|
|
|
import yaml
|
|
|
|
|
2015-07-14 11:21:33 +02:00
|
|
|
# List of available types for form
|
2015-05-17 12:24:29 +02:00
|
|
|
types = ['string', 'email', 'int', 'stringlist', 'fix', 'password']
|
2015-04-16 22:07:45 +02:00
|
|
|
|
2015-07-10 21:06:28 +02:00
|
|
|
|
2015-04-16 22:07:45 +02:00
|
|
|
class Attributes:
|
|
|
|
|
|
|
|
def __init__(self, attributes_file):
|
2015-05-17 12:24:29 +02:00
|
|
|
self.attributes_file = attributes_file
|
|
|
|
self.backends = Set([])
|
2015-06-06 22:23:21 +02:00
|
|
|
self.self_attributes = {}
|
2015-05-22 09:49:50 +02:00
|
|
|
self.backend_attributes = {}
|
2015-05-31 19:38:31 +02:00
|
|
|
self.displayed_attributes = {}
|
2015-05-31 18:07:46 +02:00
|
|
|
self.key = None
|
2015-05-17 12:24:29 +02:00
|
|
|
try:
|
|
|
|
stream = open(attributes_file, 'r')
|
|
|
|
except:
|
|
|
|
raise MissingAttributesFile(attributes_file)
|
|
|
|
try:
|
|
|
|
self.attributes = loadNoDump(stream)
|
|
|
|
except DumplicatedKey as e:
|
|
|
|
raise DumplicateAttributesKey(e.key)
|
2015-04-16 22:07:45 +02:00
|
|
|
|
2015-05-17 19:22:11 +02:00
|
|
|
for attrid in self.attributes:
|
|
|
|
self._mandatory_check(attrid)
|
2015-05-17 19:49:13 +02:00
|
|
|
attr = self.attributes[attrid]
|
|
|
|
if not attr['type'] in types:
|
2015-07-13 09:10:36 +02:00
|
|
|
raise WrongAttributeType(
|
|
|
|
attr['type'],
|
|
|
|
attrid,
|
|
|
|
attributes_file
|
|
|
|
)
|
|
|
|
if attr['type'] == 'password':
|
|
|
|
if attrid + '1' in self.attributes or \
|
|
|
|
attrid + '2' in self.attributes:
|
|
|
|
raise PasswordAttributesCollision(attrid)
|
2015-05-18 12:30:51 +02:00
|
|
|
if 'self' in attr and attr['self']:
|
2015-05-31 23:27:04 +02:00
|
|
|
self.self_attributes[attrid] = attr
|
2015-05-31 18:07:46 +02:00
|
|
|
if 'key' in attr and attr['key']:
|
2015-07-11 22:03:58 +02:00
|
|
|
if self.key is not None:
|
2015-05-31 18:07:46 +02:00
|
|
|
raise DumplicateUserKey(attrid, self.key)
|
|
|
|
self.key = attrid
|
2015-05-18 12:30:51 +02:00
|
|
|
for b in attr['backends']:
|
|
|
|
self.backends.add(b)
|
2015-05-22 09:49:50 +02:00
|
|
|
if b not in self.backend_attributes:
|
2015-06-15 23:03:47 +02:00
|
|
|
self.backend_attributes[b] = {}
|
|
|
|
self.backend_attributes[b][attr['backends'][b]] = attrid
|
2015-05-31 18:07:46 +02:00
|
|
|
if 'search_displayed' in attr and attr['search_displayed']:
|
2015-05-31 23:27:04 +02:00
|
|
|
self.displayed_attributes[attrid] = attr
|
2015-05-31 18:07:46 +02:00
|
|
|
|
|
|
|
if self.key is None:
|
|
|
|
raise MissingUserKey()
|
|
|
|
|
2015-07-14 11:21:33 +02:00
|
|
|
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
|
|
|
|
|
2015-05-31 18:07:46 +02:00
|
|
|
def get_search_attributes(self):
|
|
|
|
return self.displayed_attributes
|
|
|
|
|
|
|
|
def get_key(self):
|
|
|
|
return self.key
|
2015-05-17 19:22:11 +02:00
|
|
|
|
|
|
|
def _mandatory_check(self, attr):
|
2015-05-31 23:27:04 +02:00
|
|
|
for m in ['description', 'display_name', 'type', 'backends', 'weight']:
|
2015-05-17 19:22:11 +02:00
|
|
|
if m not in self.attributes[attr]:
|
|
|
|
raise MissingKey(m, attr, self.attributes_file)
|
|
|
|
|
2015-04-16 22:07:45 +02:00
|
|
|
def get_selfattributes(self):
|
|
|
|
"""get the list of groups from roles"""
|
2015-05-18 12:30:51 +02:00
|
|
|
return self.self_attributes
|
|
|
|
|
|
|
|
def get_backends(self):
|
|
|
|
"""return the list of backends in roles file"""
|
|
|
|
return self.backends
|
2015-04-16 22:07:45 +02:00
|
|
|
|
2015-05-22 09:49:50 +02:00
|
|
|
def get_backend_attributes(self, backend):
|
|
|
|
if backend not in self.backends:
|
|
|
|
raise WrongBackend(backend)
|
2015-06-15 23:03:47 +02:00
|
|
|
return self.backend_attributes[backend].keys()
|
2015-05-22 09:49:50 +02:00
|
|
|
|
2015-05-31 18:40:35 +02:00
|
|
|
def get_backend_key(self, backend):
|
|
|
|
if backend not in self.backends:
|
|
|
|
raise WrongBackend(backend)
|
|
|
|
return self.attributes[self.key]['backends'][backend]
|
|
|
|
|
2015-05-18 15:04:36 +02:00
|
|
|
def get_attributes(self):
|
2015-04-16 22:07:45 +02:00
|
|
|
"""get the list of groups from roles"""
|
2015-06-15 21:30:19 +02:00
|
|
|
return self.attributes.keys()
|
|
|
|
|
|
|
|
def get_backends_attributes(self, attribute):
|
|
|
|
return self.attributes[attribute]['backends']
|