Ejemplo n.º 1
0
def create_form_user(id_user, auth, groups, edit, ldap_grps=None):

    if edit:
        user = auth.get_clientFactory().create_usuario().get_by_id(
            id_user).get('usuario')

        if type(user['grupos']) == unicode:
            user['grupos'] = list([user['grupos'], 'one_group'])
        # Get LDAP user and the first group associated
        is_ldap = False
        usr_list = None
        ini_grp = None
        if user['user_ldap'] is not None:
            ldap = Ldap(auth)
            ldap_user_ = ldap.get_user(user['user_ldap'])

            users_grps = ldap.get_groups_user(ldap_user_['cn'])

            ldap_ini_grps = [grp[1] for grp in ldap_grps]
            for grp in users_grps:
                if grp in ldap_ini_grps:
                    ini_grp = ldap_grps[ldap_ini_grps.index(grp)][0]
                    usr_list = ldap.get_users_group(ini_grp)
                    is_ldap = True
                    break

        form_user = UserForm(groups,
                             ldap_group_list=ldap_grps,
                             ldap_user_list=usr_list,
                             initial={
                                 "name": user['nome'],
                                 "email": user['email'],
                                 "user": user['user'],
                                 "groups": user['grupos'],
                                 "password": user['pwd'],
                                 "active": user['ativo'],
                                 "is_ldap": is_ldap,
                                 "ldap_group": ini_grp,
                                 "ldap_user": user['user_ldap']
                             })
        #action = "user.edit %s 1" % (str(id_user))
        action = url("user.edit", id_user, 1)
    else:
        #action = "user.edit 0 0"
        action = url("user.edit", 0, 0)
        form_user = UserForm(groups, ldap_grps)

    return form_user, action
Ejemplo n.º 2
0
def create_form_user(id_user, auth, groups, edit, ldap_grps=None):

    if edit:
        user = auth.get_clientFactory().create_usuario().get_by_id(
            id_user).get('usuario')

        if type(user['grupos']) == unicode:
            user['grupos'] = list([user['grupos'], 'one_group'])
        # Get LDAP user and the first group associated
        is_ldap = False
        usr_list = None
        ini_grp = None
        if user['user_ldap'] is not None:
            ldap = Ldap(auth)
            ldap_user_ = ldap.get_user(user['user_ldap'])

            users_grps = ldap.get_groups_user(ldap_user_['cn'])

            ldap_ini_grps = [grp[1] for grp in ldap_grps]
            for grp in users_grps:
                if grp in ldap_ini_grps:
                    ini_grp = ldap_grps[ldap_ini_grps.index(grp)][0]
                    usr_list = ldap.get_users_group(ini_grp)
                    is_ldap = True
                    break

        form_user = UserForm(groups, ldap_group_list=ldap_grps, ldap_user_list=usr_list, initial={"name": user['nome'], "email": user['email'], "user": user[
                             'user'], "groups": user['grupos'], "password": user['pwd'], "active": user['ativo'], "is_ldap": is_ldap, "ldap_group": ini_grp, "ldap_user": user['user_ldap']})
        #action = "user.edit %s 1" % (str(id_user))
        action = url("user.edit", id_user, 1)
    else:
        #action = "user.edit 0 0"
        action = url("user.edit", 0, 0)
        form_user = UserForm(groups, ldap_grps)

    return form_user, action
Ejemplo n.º 3
0
def edit_user_form(request, pattern, cn):
    try:

        lists = dict()
        lists["pattern"] = pattern
        lists["action"] = reverse(
            "ldap.user.edit", kwargs={"pattern": pattern, "cn": cn})
        lists["edit"] = True

        ldap = Ldap(AuthSession(request.session).get_user().get_username())

        group_list = get_groups(ldap)
        policy_list = get_policies(ldap)

        if pattern == PATTERN_TYPES.INTERNAL:
            groupPattern = ldap.groupStandard
            usertype_list = CHOICES_GROUP

        elif pattern == PATTERN_TYPES.EXTERNAL:
            groupPattern = ldap.groupStandardExternal
            usertype_list = None

        if request.method == "POST":

            form = UserForm(group_list, policy_list, usertype_list, request.POST, initial={
                            'groupPattern': groupPattern})

            if form.is_valid():

                data = form.cleaned_data

                for e in data.keys():
                    if type(data[e]) == unicode or type(data[e]) == int:
                        data[e] = str(data[e])

                if valid_form_user(ldap, request, data['cn'], data['uidNumber'], data['employeeNumber'], data['mail'], edit=True):

                    ldap.edit_user(data['cn'], data['uidNumber'], data['groupPattern'], data['homeDirectory'], data['givenName'], data['initials'], data['sn'], data['mail'], data['homePhone'], data['mobile'], data['street'], data[
                                   'description'], data['employeeNumber'], data['employeeType'], data['loginShell'], data['shadowLastChange'], data['shadowMin'], data['shadowMax'], data['shadowWarning'], data['policy'], data['groups'])

                    client_user = AuthSession(
                        request.session).get_clientFactory().create_usuario()

                    try:
                        local_user = client_user.get_by_user_ldap(data['cn'])
                        local_user = local_user['usuario']
                        name = data['givenName'] + ' ' + \
                            data['initials'] + ' ' + data['sn']
                        client_user.alterar(local_user['id'], local_user['user'], local_user[
                                            'pwd'], name, local_user['ativo'], data['mail'], local_user['user_ldap'])
                    except UsuarioNaoExisteError:
                        pass

                    messages.add_message(
                        request, messages.SUCCESS, ldap_messages.get("success_edit_user"))
                    return HttpResponseRedirect(reverse('ldap.user.list', args=[pattern]))

        else:

            user = ldap.get_user(cn)
            user['groups'] = ldap.get_groups_user(cn)
            user['groupPattern'] = groupPattern

            form = UserForm(
                group_list, policy_list, usertype_list, initial=user)

    except LDAPNotFoundError, e:
        messages.add_message(
            request, messages.ERROR, ldap_messages.get("invalid_user") % cn)
        return HttpResponseRedirect(reverse('ldap.user.list', args=[pattern]))