简体   繁体   English

在 Django 中创建用户和发送密码重置

[英]Creating Users & Sending Password Resets in Django

I apologize if there has been a substantial answer on this already, I have searched for quite a while and can't find any helpful answers.如果对此已经有实质性的答案,我深表歉意,我已经搜索了很长时间,但找不到任何有用的答案。

I have a django project that has varying levels of account access.我有一个 Django 项目,它具有不同级别的帐户访问权限。 I have a group of 'Managers' and I want to allow them to manage user accounts.我有一组“经理”,我想让他们管理用户帐户。

I want the Managers to be able to create the user account objects for the users.我希望经理能够为用户创建用户帐户对象。 However, I don't want them to have to deal with creating their passwords and sending them to the users (for obvious reasons).但是,我不希望他们必须处理创建密码并将其发送给用户(出于显而易见的原因)。 This way, managers can impersonate users to get work done on their behalf, and users maintain password control and ownership of their data.通过这种方式,管理人员可以冒充用户代表他们完成工作,并且用户可以维护密码控制和数据所有权。

The idea is that managers create the account, and when the account is created Users will be sent a password reset form (same as django's out-of-box auth) which will allow them to set their passwords.这个想法是经理创建帐户,当创建帐户时,用户将收到一个密码重置表单(与 django 的开箱即用身份验证相同),这将允许他们设置密码。

My code looks similar to below (omitted non-imperative stuff)我的代码看起来类似于下面(省略了非必要的东西)

from django.contrib.auth.models import User
from django.contrib.auth.forms import PasswordResetForm

@manager_required
def manager_add_users(request):
    add_user_form = manager_add_user_form(request.POST)
    new_user_name = add_user_form.cleaned_data['user_name']
    new_user_email = add_user_form.cleaned_data['user_email']

    new_user = User.objects.create_user(
        username = new_user_name, 
        email = new_user_email, 
        )
    new_user.save()

    set_password_form = PasswordResetForm({'email': new_user.email })
            if set_password_form.is_valid():
                print 'Reset Form Is Valid'
                set_password_form.save(
                    request= request,
                    use_https=True,
                    from_email="support@org.com",
                    email_template_name='registration/password_reset_email.html')

The account creates properly and everything runs without error.该帐户创建正确,一切正常运行。 The issue is that although the form is valid (reset form is valid statement prints) it is not actually sending the reset form.问题是,尽管表单有效(重置表单是有效的语句打印),但它实际上并未发送重置表单。 There are no form errors.没有形式错误。

However, in testing when I initialize the form with an address that already exists in the system like this:但是,在测试中,当我使用系统中已存在的地址初始化表单时,如下所示:

set_password_form = PasswordResetForm({'email':'existing_address@example.com'})

The password reset form sends without error.密码重置表单发送没有错误。 So it only works with existing user email addresses in the system, but although the user has been created and the .save() method called, it's still not catching it (The users are marked as 'Active' upon creation, so they should be able to be found)所以它只适用于系统中现有的用户电子邮件地址,但是尽管用户已经创建并调用了 .save() 方法,它仍然没有捕捉到它(用户在创建时被标记为“活动”,所以他们应该可以找到)

I'm a bit at a loss.我有点不知所措。 I can think of a few ways that I could get around this issue, but this seems the most direct way of doing it and I'm really not entirely sure why it doesn't work.我可以想到一些方法来解决这个问题,但这似乎是最直接的方法,我真的不完全确定为什么它不起作用。

Yes, I am able to send messages.是的,我可以发送消息。 I am using django's backend mail for testing:我正在使用 django 的后端邮件进行测试:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

Glancing at the source code , it looks like Django is ignoring the request because the password is blank.看了一下源码,好像是因为密码为空,Django 忽略了这个请求。

Try setting a temporary password (using, say, django.utils.crypto.get_random_string() ) before saving the user.在保存用户之前尝试设置一个临时密码(例如使用django.utils.crypto.get_random_string() )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM