parameters: (self, con, username, group_mapping={})
return_statement: return True
docstring: Manage user groups
  
          Get all user's groups from ldap and refresh the already stored
          ones in py4web's application database or create new groups
          according to ldap.
docstring_summary: Manage user groups
docstring_tokens: Manage user groups
function: def do_manage_groups(self, con, username, group_mapping={}):
          """
          Manage user groups
  
          Get all user's groups from ldap and refresh the already stored
          ones in py4web's application database or create new groups
          according to ldap.
          """
          server = self.server
          port = self.port
          base_dn = self.base_dn
          mode = self.mode
          secure = self.secure
          self_signed_certificate = self.self_signed_certificate
          cert_path = self.cert_path
          cert_file = self.cert_file
          cacert_path = self.cacert_path
          cacert_file = self.cacert_file
          key_file = self.key_file
          bind_dn = self.bind_dn
          bind_pw = self.bind_pw
          filterstr = self.filterstr
          username_attrib = self.username_attrib
          custom_scope = self.custom_scope
          allowed_groups = self.allowed_groups
          manage_user = self.manage_user
          user_firstname_attrib = self.user_firstname_attrib
          user_lastname_attrib = self.user_lastname_attrib
          user_mail_attrib = self.user_mail_attrib
          manage_groups = self.manage_groups
          manage_groups_callback = self.manage_groups_callback
          db = self.db
          group_dn = self.db
          group_name_attrib = self.group_name_attrib
          group_member_attrib = self.group_member_attrib
          group_filterstr = self.group_filterstr
          group_mapping = self.group_mapping
          tls = self.tls
          logger = self.logger
          groups = self.groups
  
          logger.info("[%s] Manage user groups" % str(username))
          try:
              #
              # Get all group name where the user is in actually in ldap
              # #########################################################
              ldap_groups_of_the_user = self.get_user_groups_from_ldap(con, username)
  
              if group_mapping != {}:
                  l = []
                  for group in ldap_groups_of_the_user:
                      if group in group_mapping:
                          l.append(group_mapping[group])
                  ldap_groups_of_the_user = l
                  logger.info("User groups after remapping: %s" % str(l))
  
              #
              # Get all group name where the user is in actually in local db
              # #############################################################
              try:
                  db_user_id = (
                      db(db.auth_user.username == username)
                      .select(db.auth_user.id)
                      .first()
                      .id
                  )
              except:
                  try:
                      db_user_id = (
                          db(db.auth_user.email == username)
                          .select(db.auth_user.id)
                          .first()
                          .id
                      )
                  except AttributeError as e:
                      #
                      # There is no user in local db
                      # We create one
                      # ##############################
                      try:
                          db_user_id = db.auth_user.insert(
                              username=username, first_name=username
                          )
                      except AttributeError as e:
                          db_user_id = db.auth_user.insert(
                              email=username, first_name=username
                          )
              if not db_user_id:
                  logger.error("There is no username or email for %s!" % username)
                  raise
              db_groups_of_the_user = groups.get(db_user_id)
  
              auth_membership_changed = False
              #
              # Delete user membership from groups where user is not anymore
              # #############################################################
              for group_to_del in db_groups_of_the_user:
                  if ldap_groups_of_the_user.count(group_to_del) == 0:
                      groups.remove(db_user_id, group_to_del)
                      auth_membership_changed = True
  
              #
              # Create user membership in groups where user is not in already
              # ##############################################################
              for group_to_add in ldap_groups_of_the_user:
                  if db_groups_of_the_user.count(group_to_add) == 0:
                      groups.add(db_user_id, group_to_add)
                      auth_membership_changed = True
  
              if auth_membership_changed:
                  for callback in manage_groups_callback:
                      callback()
          except:
              logger.warning("[%s] Groups are not managed successfully!" % str(username))
              import traceback
  
              logger.debug(traceback.format_exc())
              return False
          return True
function_tokens: def do_manage_groups ( self , con , username , group_mapping = { } ) : server = self . server port = self . port base_dn = self . base_dn mode = self . mode secure = self . secure self_signed_certificate = self . self_signed_certificate cert_path = self . cert_path cert_file = self . cert_file cacert_path = self . cacert_path cacert_file = self . cacert_file key_file = self . key_file bind_dn = self . bind_dn bind_pw = self . bind_pw filterstr = self . filterstr username_attrib = self . username_attrib custom_scope = self . custom_scope allowed_groups = self . allowed_groups manage_user = self . manage_user user_firstname_attrib = self . user_firstname_attrib user_lastname_attrib = self . user_lastname_attrib user_mail_attrib = self . user_mail_attrib manage_groups = self . manage_groups manage_groups_callback = self . manage_groups_callback db = self . db group_dn = self . db group_name_attrib = self . group_name_attrib group_member_attrib = self . group_member_attrib group_filterstr = self . group_filterstr group_mapping = self . group_mapping tls = self . tls logger = self . logger groups = self . groups logger . info ( "[%s] Manage user groups" % str ( username ) ) try : # # Get all group name where the user is in actually in ldap # ######################################################### ldap_groups_of_the_user = self . get_user_groups_from_ldap ( con , username ) if group_mapping != { } : l = [ ] for group in ldap_groups_of_the_user : if group in group_mapping : l . append ( group_mapping [ group ] ) ldap_groups_of_the_user = l logger . info ( "User groups after remapping: %s" % str ( l ) ) # # Get all group name where the user is in actually in local db # ############################################################# try : db_user_id = ( db ( db . auth_user . username == username ) . select ( db . auth_user . id ) . first ( ) . id ) except : try : db_user_id = ( db ( db . auth_user . email == username ) . select ( db . auth_user . id ) . first ( ) . id ) except AttributeError as e : # # There is no user in local db # We create one # ############################## try : db_user_id = db . auth_user . insert ( username = username , first_name = username ) except AttributeError as e : db_user_id = db . auth_user . insert ( email = username , first_name = username ) if not db_user_id : logger . error ( "There is no username or email for %s!" % username ) raise db_groups_of_the_user = groups . get ( db_user_id ) auth_membership_changed = False # # Delete user membership from groups where user is not anymore # ############################################################# for group_to_del in db_groups_of_the_user : if ldap_groups_of_the_user . count ( group_to_del ) == 0 : groups . remove ( db_user_id , group_to_del ) auth_membership_changed = True # # Create user membership in groups where user is not in already # ############################################################## for group_to_add in ldap_groups_of_the_user : if db_groups_of_the_user . count ( group_to_add ) == 0 : groups . add ( db_user_id , group_to_add ) auth_membership_changed = True if auth_membership_changed : for callback in manage_groups_callback : callback ( ) except : logger . warning ( "[%s] Groups are not managed successfully!" % str ( username ) ) import traceback logger . debug ( traceback . format_exc ( ) ) return False return True