# Path: ldapcherry/exceptions.py
# class UserDoesntExist(Exception):
#     def __init__(self, user, backend):
#         self.user = user
#         self.bakend = backend
#         self.log = \
#             "user '" + user + "'" \
#             " does not exist" \
#             " in backend '" + backend + "'"
# 
# class GroupDoesntExist(Exception):
#     def __init__(self, group, backend):
#         self.group = group
#         self.bakend = backend
#         self.log = \
#             "group '" + group + "'" \
#             " does not exist" \
#             " in backend '" + backend + "'"
# 
# class UserAlreadyExists(Exception):
#     def __init__(self, user, backend):
#         self.user = user
#         self.bakend = backend
#         self.log = \
#             "user '" + user + "'" \
#             " already exists" \
#             " in backend '" + backend + "'"


import cherrypy
import ldap
import ldap.modlist as modlist
import ldap.filter
import logging
import ldapcherry.backend
import sys
import os
import re
from ldapcherry.exceptions import UserDoesntExist, \
    GroupDoesntExist, \
    UserAlreadyExists
    from sets import Set as set
                       )
                try:
                    ldap_client.modify_s(group, ldif)
                # if already member, not a big deal, just log it and continue
                except (ldap.TYPE_OR_VALUE_EXISTS, ldap.ALREADY_EXISTS) as e:
                    self._logger(
                        severity=logging.INFO,
                        msg="%(backend)s: user '%(user)s'"
                            " already member of group '%(group)s'"
                            " (attribute '%(attr)s')" % {
                                'user': username,
                                'group': self._uni(group),
                                'attr': attr,
                                'backend': self.backend_name
                                }
                    )
                except ldap.NO_SUCH_OBJECT as e:
                    raise GroupDoesntExist(group, self.backend_name)
                except Exception as e:
                    ldap_client.unbind_s()
                    self._exception_handler(e)
        ldap_client.unbind_s()

    def del_from_groups(self, username, groups):
        """Delete user from groups"""
        # it follows the same logic than add_to_groups
        # but with MOD_DELETE
        ldap_client = self._bind()
        tmp = self._get_user(self._byte_p2(username), ALL_ATTRS)
        if tmp is None:

# Path: ldapcherry/exceptions.py
# class UserDoesntExist(Exception):
#     def __init__(self, user, backend):
#         self.user = user
#         self.bakend = backend
#         self.log = \
#             "user '" + user + "'" \
#             " does not exist" \
#             " in backend '" + backend + "'"
# 
# class GroupDoesntExist(Exception):
#     def __init__(self, group, backend):
#         self.group = group
#         self.bakend = backend
#         self.log = \
#             "group '" + group + "'" \
#             " does not exist" \
#             " in backend '" + backend + "'"
# 
# class UserAlreadyExists(Exception):
#     def __init__(self, user, backend):
#         self.user = user
#         self.bakend = backend
#         self.log = \
#             "user '" + user + "'" \
#             " already exists" \
#             " in backend '" + backend + "'"


# Path: ldapcherry/backend/backendLdap.py
import cherrypy
import ldap
import ldap.modlist as modlist
import ldap.filter
import logging
import ldapcherry.backend
import sys
import os
import re
from ldapcherry.exceptions import UserDoesntExist, \
    GroupDoesntExist, \
    UserAlreadyExists
    from sets import Set as set

                       )
                try:
                    ldap_client.modify_s(group, ldif)
                # if already member, not a big deal, just log it and continue
                except (ldap.TYPE_OR_VALUE_EXISTS, ldap.ALREADY_EXISTS) as e:
                    self._logger(
                        severity=logging.INFO,
                        msg="%(backend)s: user '%(user)s'"
                            " already member of group '%(group)s'"
                            " (attribute '%(attr)s')" % {
                                'user': username,
                                'group': self._uni(group),
                                'attr': attr,
                                'backend': self.backend_name
                                }
                    )
                except ldap.NO_SUCH_OBJECT as e:
                    raise GroupDoesntExist(group, self.backend_name)
                except Exception as e:
                    ldap_client.unbind_s()
                    self._exception_handler(e)
        ldap_client.unbind_s()

    def del_from_groups(self, username, groups):
        """Delete user from groups"""
        # it follows the same logic than add_to_groups
        # but with MOD_DELETE
        ldap_client = self._bind()
        tmp = self._get_user(self._byte_p2(username), ALL_ATTRS)
        if tmp is None:

            raise UserDoesntExist(username, self.backend_name)