def profile(request): # If users are editable in this setup, go to the editable profile view if settings.ALLOW_USER_EDITS: return edit(request) user = request.user user_profile = UserProfile.objects.get(user=user) title = _("Your profile (%s)") % user if request.method == "POST": form = ApiKeyForm(request.POST) userprofileform = UserProfileForm(request.POST, instance=user_profile) if form.is_valid() and userprofileform.is_valid(): if form["regenerate_api_key"] != "": generate_api_key(user) userprofileform.save() return redirect("profile") else: form = ApiKeyForm() userprofileform = UserProfileForm(instance=user_profile) return render( request, "accounts/profile.html", { "form": form, "userprofileform": userprofileform, "title": title }, )
def create_user(self, user_info): user = super(CustomOIDCBackend, self).create_user(user_info) for attr, value in user_info.items(): setattr(user, attr, value) user.save() generate_api_key(user) return user
def edit(request, id=None): # Forbidden if user isn't an admin and is trying to edit another user if str(request.user.id) != str(id) and id is not None: if request.user.is_superuser is False: return redirect('main.views.forbidden') # Load user if id is None: user = request.user title = 'Edit your profile (%s)' % user else: user = get_object_or_404(User, pk=id) title = 'Edit user %s' % user # Form if request.method == 'POST': form = UserChangeForm(request.POST, instance=user) if form.is_valid(): user = form.save(commit=False) # change password if requested password = request.POST.get('password', '') if password != '': user.set_password(password) # prevent non-admin from self-promotion if not request.user.is_superuser: user.is_superuser = False user.save() # regenerate API key if requested regenerate_api_key = request.POST.get('regenerate_api_key', '') if regenerate_api_key != '': generate_api_key(user) # determine where to redirect to if request.user.is_superuser: return_view = 'components.accounts.views.list' else: return_view = 'profile' messages.info(request, _('Saved.')) return redirect(return_view) else: suppress_administrator_toggle = True if request.user.is_superuser: suppress_administrator_toggle = False form = UserChangeForm( instance=user, suppress_administrator_toggle=suppress_administrator_toggle) return render(request, 'accounts/edit.html', { 'form': form, 'user': user, 'title': title })
def edit(request, id=None): # Forbidden if user isn't an admin and is trying to edit another user if str(request.user.id) != str(id) and id is not None: if request.user.is_superuser is False: return redirect("main.views.forbidden") # Load user if id is None: user = request.user title = "Edit your profile (%s)" % user else: user = get_object_or_404(User, pk=id) title = "Edit user %s" % user # Form if request.method == "POST": form = UserChangeForm(request.POST, instance=user) if form.is_valid(): user = form.save(commit=False) # change password if requested password = request.POST.get("password", "") if password != "": user.set_password(password) # prevent non-admin from self-promotion if not request.user.is_superuser: user.is_superuser = False user.save() # regenerate API key if requested regenerate_api_key = request.POST.get("regenerate_api_key", "") if regenerate_api_key != "": generate_api_key(user) # determine where to redirect to if request.user.is_superuser: return_view = "components.accounts.views.list" else: return_view = "profile" messages.info(request, _("Saved.")) return redirect(return_view) else: suppress_administrator_toggle = True if request.user.is_superuser: suppress_administrator_toggle = False form = UserChangeForm( instance=user, suppress_administrator_toggle=suppress_administrator_toggle) return render(request, "accounts/edit.html", { "form": form, "user": user, "title": title })
def configure_user(self, user): generate_api_key(user) # If CAS_AUTOCONFIGURE_EMAIL and CAS_EMAIL_DOMAIN settings are # configured, add an email address for this user, using rule # username@domain. if settings.CAS_AUTOCONFIGURE_EMAIL and settings.CAS_EMAIL_DOMAIN: user.email = "{0}@{1}".format(user.username, settings.CAS_EMAIL_DOMAIN) user.save() return user
def test_api_authenticates_via_key(self): user = get_user_model().objects.get(pk=1) generate_api_key(user) key = ApiKey.objects.get(user=user).key for url in self.API_URLS: response = self.client.get( url, HTTP_AUTHORIZATION="ApiKey test:{}".format(key), follow=False) self.assertEqual(response.status_code, 200)
def profile(request): # If users are editable in this setup, go to the editable profile view if settings.ALLOW_USER_EDITS: return edit(request) user = request.user title = _('Your profile (%s)') % user if request.method == 'POST': form = ApiKeyForm(request.POST) if form.is_valid(): if form['regenerate_api_key'] != '': generate_api_key(user) return redirect('profile') else: form = ApiKeyForm() return render(request, 'accounts/profile.html', { 'form': form, 'title': title })
def profile(request): # If users are editable in this setup, go to the editable profile view if settings.ALLOW_USER_EDITS: return edit(request) user = request.user title = _("Your profile (%s)") % user if request.method == "POST": form = ApiKeyForm(request.POST) if form.is_valid(): if form["regenerate_api_key"] != "": generate_api_key(user) return redirect("profile") else: form = ApiKeyForm() return render(request, "accounts/profile.html", { "form": form, "title": title })
def ldap_populate_user(sender, user, ldap_user, **kwargs): if user.pk is None: user.save() generate_api_key(user)
def configure_user(self, user): generate_api_key(user) return user
def ldap_populate_user(sender, user, ldap_user, **kwargs): generate_api_key(user)