Пример #1
0
def get_usergroup(usergroup, auto_create=None):
    """
    usergroup can be int(treated as id) or string(treated as groupname)
    auto_create = True or False or None
        when set to None,will use the value of settings.LDAP.group_auto_create
    """
    if isinstance(usergroup, int):
        UserGroup = get_model('usergroup')
        return UserGroup.get(usergroup)
    elif isinstance(usergroup, (str, unicode)):
        attr_dict = ldapauth_handler.get_group(groupname=usergroup)
        if attr_dict:
            groupname = attr_dict.get('name')
            if groupname:
                changed = False

                UserGroup = get_model('usergroup')
                usergroup_obj = UserGroup.get(UserGroup.c.name == groupname)
                if not usergroup_obj:
                    if auto_create == None:
                        auto_create = settings.LDAP.group_auto_create
                    if auto_create:
                        usergroup_obj = UserGroup(
                            name=groupname,
                            auth_type=settings.AUTH.AUTH_TYPE_LDAP)
                        changed = True
                        log.info("UserGroup '%s' auto create" % (groupname))

                #update usergroup attributes
                for k, v in settings.LDAP.server_param.get(
                        'group_attributes', {}).items():
                    if isinstance(v, dict):
                        to_group = v.get('to_group')
                        if to_group:
                            #k is setting attribute name, to_group is UserGroup attribute name
                            attr = attr_dict.get(k)
                            if attr != getattr(usergroup_obj, to_group):
                                setattr(usergroup_obj, to_group, attr)
                                changed = True

                if changed:
                    usergroup_obj.save()
                return usergroup_obj
Пример #2
0
def get_usergroup(usergroup,auto_create=None):
    """
    usergroup can be int(treated as id) or string(treated as groupname)
    auto_create = True or False or None
        when set to None,will use the value of settings.LDAP.group_auto_create
    """
    if isinstance(usergroup,int):
        UserGroup = get_model('usergroup')
        return UserGroup.get(usergroup)
    elif isinstance(usergroup,(str,unicode)):
        attr_dict = ldapauth_handler.get_group(groupname=usergroup)
        if attr_dict:
            groupname = attr_dict.get('name')
            if groupname:
                changed = False

                UserGroup = get_model('usergroup')
                usergroup_obj = UserGroup.get(UserGroup.c.name==groupname)
                if not usergroup_obj:
                    if auto_create==None:
                        auto_create = settings.LDAP.group_auto_create
                    if auto_create:
                        usergroup_obj = UserGroup(name=groupname, auth_type=settings.AUTH.AUTH_TYPE_LDAP)
                        changed = True
                        log.info("UserGroup '%s' auto create"%(groupname))

                #update usergroup attributes
                for k,v in settings.LDAP.server_param.get('group_attributes',{}).items():
                    if isinstance(v,dict):
                        to_group = v.get('to_group')
                        if to_group:
                            #k is setting attribute name, to_group is UserGroup attribute name
                            attr = attr_dict.get(k)
                            if attr!=getattr(usergroup_obj,to_group):
                                setattr(usergroup_obj,to_group,attr)
                                changed = True

                if changed:
                    usergroup_obj.save()
                return usergroup_obj
Пример #3
0
def ldap_get_usergroup(groupname):
    return ldapauth_handler.get_group(groupname=groupname)
Пример #4
0
def ldap_get_usergroup(groupname):
    return ldapauth_handler.get_group(groupname=groupname)