def sudo_to_user(request): """Sudo to a user based on POST data""" sudo_account_id = request.POST.get('account') try: sudo_account = Account.objects.get(pk=sudo_account_id) except Account.DoesNotExist: messages.error(request, 'Account not found.') else: sudo(request, sudo_account) return HttpResponseRedirect(reverse('webfront-index'))
def account_detail(request, account_id=None): try: account = Account.objects.get(id=account_id) except Account.DoesNotExist: account = None account_form = AccountForm(instance=account) org_form = OrganizationAddForm(account) group_form = GroupAddForm(account) if request.method == 'POST': if 'submit_account' in request.POST: account_form = AccountForm(request.POST, instance=account) if account_form.is_valid(): account = account_form.save(commit=False) if 'password1' in account_form.cleaned_data and \ account_form.cleaned_data['password1'] and not account.ext_sync: account.set_password( account_form.cleaned_data['password1']) account.save() new_message(request, '"%s" has been saved.' % (account), type=Messages.SUCCESS) return HttpResponseRedirect( reverse('useradmin-account_detail', args=[account.id])) elif 'submit_org' in request.POST: org_form = OrganizationAddForm(account, request.POST) if org_form.is_valid(): organization = org_form.cleaned_data['organization'] try: account.organizations.get(id=organization.id) new_message(request, 'Organization was not added as it has already ' 'been added.', type=Messages.WARNING) except Organization.DoesNotExist: account.organizations.add(organization) new_message(request, 'Added organization "%s" to account "%s"' % (organization, account), type=Messages.SUCCESS) return HttpResponseRedirect( reverse('useradmin-account_detail', args=[account.id])) elif 'submit_group' in request.POST: group_form = GroupAddForm(account, request.POST) if group_form.is_valid(): group = group_form.cleaned_data['group'] if ((group.is_admin_group() or group.is_protected_group()) and account.is_default_account()): new_message(request, 'Default user may not be added to "%s" ' 'group.' % group, type=Messages.ERROR) else: try: account.accountgroup_set.get(id=group.id) new_message(request, 'Group was not added as it has already ' 'been added.', type=Messages.WARNING) except AccountGroup.DoesNotExist: account.accountgroup_set.add(group) new_message(request, 'Added "%s" to group "%s"' % (account, group), type=Messages.SUCCESS) return HttpResponseRedirect( reverse('useradmin-account_detail', args=[account.id])) elif 'submit_sudo' in request.POST: sudo_account_id = request.POST.get('account') try: sudo_account = Account.objects.get(pk=sudo_account_id) except Account.DoesNotExist: new_message(request, 'Account not found.', type=Message.ERROR) else: sudo(request, sudo_account) return HttpResponseRedirect(reverse('webfront-index')) if account: active = {'account_detail': True} current_user = get_account(request) else: active = {'account_new': True} return render_to_response( 'useradmin/account_detail.html', { 'active': active, 'account': account, 'account_form': account_form, 'org_form': org_form, 'group_form': group_form, }, UserAdminContext(request))
def account_detail(request, account_id=None): try: account = Account.objects.get(id=account_id) except Account.DoesNotExist: account = None account_form = AccountForm(instance=account) org_form = OrganizationAddForm(account) group_form = GroupAddForm(account) if request.method == 'POST': if 'submit_account' in request.POST: account_form = AccountForm(request.POST, instance=account) if account_form.is_valid(): account = account_form.save(commit=False) if 'password1' in account_form.cleaned_data and \ account_form.cleaned_data['password1'] and not account.ext_sync: account.set_password(account_form.cleaned_data['password1']) account.save() new_message(request, '"%s" has been saved.' % (account), type=Messages.SUCCESS) return HttpResponseRedirect(reverse('useradmin-account_detail', args=[account.id])) elif 'submit_org' in request.POST: org_form = OrganizationAddForm(account, request.POST) if org_form.is_valid(): organization = org_form.cleaned_data['organization'] try: account.organizations.get(id=organization.id) new_message(request, 'Organization was not added as it has already ' 'been added.', type=Messages.WARNING) except Organization.DoesNotExist: account.organizations.add(organization) new_message(request, 'Added organization "%s" to account "%s"' % (organization, account), type=Messages.SUCCESS) return HttpResponseRedirect(reverse('useradmin-account_detail', args=[account.id])) elif 'submit_group' in request.POST: group_form = GroupAddForm(account, request.POST) if group_form.is_valid(): group = group_form.cleaned_data['group'] if ((group.is_admin_group() or group.is_protected_group()) and account.is_default_account()): new_message(request, 'Default user may not be added to "%s" ' 'group.' % group, type=Messages.ERROR) else: try: account.accountgroup_set.get(id=group.id) new_message(request, 'Group was not added as it has already ' 'been added.', type=Messages.WARNING) except AccountGroup.DoesNotExist: account.accountgroup_set.add(group) new_message(request, 'Added "%s" to group "%s"' % (account, group), type=Messages.SUCCESS) return HttpResponseRedirect(reverse('useradmin-account_detail', args=[account.id])) elif 'submit_sudo' in request.POST: sudo_account_id = request.POST.get('account') try: sudo_account = Account.objects.get(pk=sudo_account_id) except Account.DoesNotExist: new_message(request, 'Account not found.', type=Message.ERROR) else: sudo(request, sudo_account) return HttpResponseRedirect(reverse('webfront-index')) if account: active = {'account_detail': True} current_user = get_account(request) else: active = {'account_new': True} return render_to_response('useradmin/account_detail.html', { 'active': active, 'account': account, 'account_form': account_form, 'org_form': org_form, 'group_form': group_form, }, UserAdminContext(request))