server/sources/ldapuser.py
changeset 5174 78438ad513ca
parent 5153 3684ccae5cdc
child 5323 329b4f6d18b4
equal deleted inserted replaced
5173:73760bbb66bd 5174:78438ad513ca
   228                                         'X eid %(x)s, U eid %(u)s',
   228                                         'X eid %(x)s, U eid %(u)s',
   229                                         {'x': emailrset[0][0], 'u': eid})
   229                                         {'x': emailrset[0][0], 'u': eid})
   230                             elif rset:
   230                             elif rset:
   231                                 if not execute('SET X address %(addr)s WHERE '
   231                                 if not execute('SET X address %(addr)s WHERE '
   232                                                'U primary_email X, U eid %(u)s',
   232                                                'U primary_email X, U eid %(u)s',
   233                                                {'addr': ldapemailaddr, 'u': eid}, 'u'):
   233                                                {'addr': ldapemailaddr, 'u': eid}):
   234                                     execute('SET X address %(addr)s WHERE '
   234                                     execute('SET X address %(addr)s WHERE '
   235                                             'X eid %(x)s',
   235                                             'X eid %(x)s',
   236                                             {'addr': ldapemailaddr, 'x': rset[0][0]}, 'x')
   236                                             {'addr': ldapemailaddr, 'x': rset[0][0]})
   237                             else:
   237                             else:
   238                                 # no email found, create it
   238                                 # no email found, create it
   239                                 _insert_email(session, ldapemailaddr, eid)
   239                                 _insert_email(session, ldapemailaddr, eid)
   240         finally:
   240         finally:
   241             session.commit()
   241             session.commit()
   544         inserted in the system table.
   544         inserted in the system table.
   545         """
   545         """
   546         super(LDAPUserSource, self).after_entity_insertion(session, dn, entity)
   546         super(LDAPUserSource, self).after_entity_insertion(session, dn, entity)
   547         for group in self.user_default_groups:
   547         for group in self.user_default_groups:
   548             session.execute('SET X in_group G WHERE X eid %(x)s, G name %(group)s',
   548             session.execute('SET X in_group G WHERE X eid %(x)s, G name %(group)s',
   549                             {'x': entity.eid, 'group': group}, 'x')
   549                             {'x': entity.eid, 'group': group})
   550         # search for existant email first
   550         # search for existant email first
   551         try:
   551         try:
   552             emailaddr = self._cache[dn][self.user_rev_attrs['email']]
   552             emailaddr = self._cache[dn][self.user_rev_attrs['email']]
   553         except KeyError:
   553         except KeyError:
   554             return
   554             return
   555         rset = session.execute('EmailAddress X WHERE X address %(addr)s',
   555         rset = session.execute('EmailAddress X WHERE X address %(addr)s',
   556                                {'addr': emailaddr})
   556                                {'addr': emailaddr})
   557         if rset:
   557         if rset:
   558             session.execute('SET U primary_email X WHERE U eid %(u)s, X eid %(x)s',
   558             session.execute('SET U primary_email X WHERE U eid %(u)s, X eid %(x)s',
   559                             {'x': rset[0][0], 'u': entity.eid}, 'u')
   559                             {'x': rset[0][0], 'u': entity.eid})
   560         else:
   560         else:
   561             # not found, create it
   561             # not found, create it
   562             _insert_email(session, emailaddr, entity.eid)
   562             _insert_email(session, emailaddr, entity.eid)
   563 
   563 
   564     def update_entity(self, session, entity):
   564     def update_entity(self, session, entity):
   569         """delete an entity from the source"""
   569         """delete an entity from the source"""
   570         raise RepositoryError('this source is read only')
   570         raise RepositoryError('this source is read only')
   571 
   571 
   572 def _insert_email(session, emailaddr, ueid):
   572 def _insert_email(session, emailaddr, ueid):
   573     session.execute('INSERT EmailAddress X: X address %(addr)s, U primary_email X '
   573     session.execute('INSERT EmailAddress X: X address %(addr)s, U primary_email X '
   574                     'WHERE U eid %(x)s', {'addr': emailaddr, 'x': ueid}, 'x')
   574                     'WHERE U eid %(x)s', {'addr': emailaddr, 'x': ueid})
   575 
   575 
   576 class GotDN(Exception):
   576 class GotDN(Exception):
   577     """exception used when a dn localizing the searched user has been found"""
   577     """exception used when a dn localizing the searched user has been found"""
   578     def __init__(self, dn):
   578     def __init__(self, dn):
   579         self.dn = dn
   579         self.dn = dn