示例#1
0
文件: views.py 项目: c-base/cbmi
def set_ldap_field(request, form_type, field_names, template_name):
    """
    Abstract view for each of the different forms.

    field_names contains the mapping of the field name in the form to
    """
    member = retrieve_member(request)
    initial = {}

    if request.method == 'POST':
        form = form_type(request.POST)
        if form.is_valid():

            for form_field, ldap_field in field_names:
                if form.cleaned_data[form_field] == '':
                    member.set(ldap_field, None)
                else:
                    member.set(ldap_field, form.cleaned_data[form_field])
                initial[form_field] = member.get(ldap_field)
            member.save()
            new_form = form_type(initial=initial)
            return render(request, template_name,
                    {'message': _('Your changes have been saved. Thank you!'),
                     'form': new_form, 'member': member.to_dict()})
        else:
            return render(request, template_name,
                    {'form': form, 'member': member.to_dict()})
    else:
        for form_field, ldap_field in field_names:
            initial[form_field] = member.get(ldap_field)
        form = form_type(initial=initial)
        return render(request, template_name,
                {'form': form, 'member': member.to_dict()})
示例#2
0
文件: views.py 项目: c-base/cbmi
def admin(request):
    admin_member = retrieve_member(request)
    if len(request.user.groups.filter(name__in=['ldap_admins'])) < 1:
        return render(request, 'access_denied.html')
    users = admin_member.list_users()
    if request.method == 'POST':
        form = AdminForm(request.POST, request=request, users=users)

        if form.is_valid():
            new_password = form.cleaned_data['password1']
            admin_member.admin_change_password(form.cleaned_data['username'], new_password)

            member = MemberValues(form.cleaned_data['username'], new_password)
            member.set('sambaLMPassword', smbpasswd.lmhash(new_password))
            member.set('sambaNTPassword', smbpasswd.nthash(new_password))
            member.save()

            new_form = AdminForm(request=request, users=users)
            return render(request, 'admin.html',
                {'message': _('The password for %s was changed. Thank you!' % form.cleaned_data['username']),
                 'form': new_form})
        else:
            return render(request, 'admin.html',
                {'form': form})
    else:
        form = AdminForm(request=request, users=users)
        return render(request, 'admin.html',
            {'form': form})
示例#3
0
文件: views.py 项目: c-base/cbmi
def set_hash_field(request, form_type, in_field, out_field, hash_func,
        template_name):
    """
    Abstract view for changing LDAP attributes that need to be hashed.
    Takes a function that converts the value into the hashed_value.
    """
    member = retrieve_member(request)
    initial = {}

    if request.method == 'POST':
        form = form_type(request.POST)
        if form.is_valid():
            hashed_value = hash_func(form.cleaned_data[in_field])
            member.set(out_field, hashed_value)
            member.save()
            new_form = form_type(initial=initial)
            return render(request, template_name,
                    {'message': _('Your changes have been saved. Thank you!'),
                     'form': new_form, 'member': member.to_dict()})
        else:
            return render(request, template_name,
                    {'form': form, 'member': member.to_dict()})
    else:
        form = form_type(initial=initial)
        return render(request, template_name,
                {'form': form, 'member': member.to_dict()})
示例#4
0
文件: views.py 项目: c-base/cbmi
def password(request):
    """
    View that changes the password on the LDAP server.
    """
    member = retrieve_member(request)

    if request.method == 'POST':
        form = PasswordForm(request.POST, request=request)

        if form.is_valid():
            new_password = form.cleaned_data['password1']

            # change the password for the Wifi
            member.set('sambaLMPassword', smbpasswd.lmhash(new_password))
            member.set('sambaNTPassword', smbpasswd.nthash(new_password))
            member.save()

            # change the LDAP password
            member.change_password(new_password)

            key = store_ldap_password(request, new_password)
            request.session.save()
            new_form = PasswordForm()
            response = render(request, 'password.html',
                {'message': _('Your password was changed. Thank you!'),
                 'form': new_form, 'member': member.to_dict()})
            response.set_cookie('sessionkey', key)
            return response
        else:
            return render(request, 'password.html',
                {'form': form, 'member': member.to_dict()})
    else:
        form = PasswordForm()
        return render(request, 'password.html',
            {'form': form, 'member': member.to_dict()})
示例#5
0
def admin(request):
    admin_member = retrieve_member(request)
    if len(request.user.groups.filter(name__in=['ldap_admins'])) < 1:
        return render(request, 'access_denied.html')
    users = admin_member.list_users()
    if request.method == 'POST':
        form = AdminForm(request.POST, request=request, users=users)

        if form.is_valid():
            new_password = form.cleaned_data['password1']
            admin_member.admin_change_password(form.cleaned_data['username'],
                                               new_password)

            member = MemberValues(form.cleaned_data['username'], new_password)
            member.set('sambaLMPassword', smbpasswd.lmhash(new_password))
            member.set('sambaNTPassword', smbpasswd.nthash(new_password))
            member.save()

            new_form = AdminForm(request=request, users=users)
            return render(request, 'admin.html',
                          {'message': _(
                              'The password for %s was changed. Thank you!' %
                              form.cleaned_data['username']),
                              'form': new_form})
        else:
            return render(request, 'admin.html',
                          {'form': form})
    else:
        form = AdminForm(request=request, users=users)
        return render(request, 'admin.html',
                      {'form': form})
示例#6
0
def set_ldap_field(request, form_type, field_names, template_name):
    """
    Abstract view for each of the different forms.

    field_names contains the mapping of the field name in the form to
    """
    member = retrieve_member(request)
    initial = {}

    if request.method == 'POST':
        form = form_type(request.POST)
        if form.is_valid():

            for form_field, ldap_field in field_names:
                if form.cleaned_data[form_field] == '':
                    member.set(ldap_field, None)
                else:
                    member.set(ldap_field, form.cleaned_data[form_field])
                initial[form_field] = member.get(ldap_field)
            member.save()
            new_form = form_type(initial=initial)
            return render(request, template_name,
                          {'message': _(
                              'Your changes have been saved. Thank you!'),
                              'form': new_form, 'member': member.to_dict()})
        else:
            return render(request, template_name,
                          {'form': form, 'member': member.to_dict()})
    else:
        for form_field, ldap_field in field_names:
            initial[form_field] = member.get(ldap_field)
        form = form_type(initial=initial)
        return render(request, template_name,
                      {'form': form, 'member': member.to_dict()})
示例#7
0
文件: views.py 项目: c-base/cbmi
def home(request):
    member = retrieve_member(request)
    number_of_members = member.get_number_of_members()
    password = get_ldap_password(request)
    username = request.user.username
    url = "https://vorstand.c-base.org/cteward-api/legacy/member/%s" % username
    cteward = None
    try:
        r = requests.get(url, verify=False, auth=(username, password))
        cteward = r.json()
    except Exception:
        pass
    context = {'member': member.to_dict(),
        'groups': list(request.user.groups.all().order_by('name')),
        'number_of_members': number_of_members,
        'cteward': cteward,
    }
    return render(request, 'home.html', context)
示例#8
0
def set_hash_field(request, form_type, in_field, out_field, hash_func,
                   template_name):
    """
    Abstract view for changing LDAP attributes that need to be hashed.
    Takes a function that converts the value into the hashed_value.
    """
    member = retrieve_member(request)
    initial = {}

    if request.method == 'POST':
        form = form_type(request.POST)
        if form.is_valid():
            hashed_value = hash_func(form.cleaned_data[in_field])
            member.set(out_field, hashed_value)
            member.save()
            new_form = form_type(initial=initial)
            return render(
                request,
                template_name,
                {
                    'message': _('Your changes have been saved. Thank you!'),
                    'form': new_form,
                    'member': member.to_dict()
                }
            )
        else:
            return render(
                request,
                template_name,
                {
                    'form': form,
                    'member': member.to_dict()
                }
            )
    else:
        form = form_type(initial=initial)
        return render(
            request,
            template_name,
            {
                'form': form,
                'member': member.to_dict()
            }
        )
示例#9
0
def home(request):
    member = retrieve_member(request)
    number_of_members = member.get_number_of_members()
    password = get_ldap_password(request)
    username = request.user.username
    url = "https://vorstand.c-base.org/cteward-api/legacy/member/%s" % username
    cteward = None
    try:
        r = requests.get(
            url,
            verify=False,
            auth=(username, password)
        )
        cteward = r.json()
    except Exception:
        pass
    context = {
        'member': member.to_dict(),
        'groups': list(request.user.groups.all().order_by('name')),
        'number_of_members': number_of_members,
        'cteward': cteward,
    }
    return render(request, 'home.html', context)
示例#10
0
def password(request):
    """
    View that changes the password on the LDAP server.
    """
    member = retrieve_member(request)

    if request.method == 'POST':
        form = PasswordForm(request.POST, request=request)

        if form.is_valid():
            new_password = form.cleaned_data['password1']

            # change the password for the Wifi
            member.set('sambaLMPassword', smbpasswd.lmhash(new_password))
            member.set('sambaNTPassword', smbpasswd.nthash(new_password))
            member.save()

            # change the LDAP password
            member.change_password(new_password)

            key = store_ldap_password(request, new_password)
            request.session.save()
            new_form = PasswordForm()
            response = render(request, 'password.html',
                              {'message': _(
                                  'Your password was changed. Thank you!'),
                                  'form': new_form, 'member': member.to_dict()})
            response.set_cookie('sessionkey', key)
            return response
        else:
            return render(request, 'password.html',
                          {'form': form, 'member': member.to_dict()})
    else:
        form = PasswordForm()
        return render(request, 'password.html',
                      {'form': form, 'member': member.to_dict()})