【问题标题】:flask-security - admin create user, force user to choose passwordflask-security - 管理员创建用户,强制用户选择密码
【发布时间】:2020-01-06 19:22:10
【问题描述】:

使用flask-security,我希望我的流程类似于以下内容:

  1. 管理员创建用户
  2. 管理员向用户发送他们已创建的通知电子邮件
  3. 用户使用通知邮件中的链接设置密码

我尝试在步骤 2 中使用管理员帐户的 forgot_password 视图,但 https://github.com/jwag956/flask-security/blob/743be9c979b558b4ecfb177dc8117c0bf55e38ed/flask_security/views.py#L464 需要匿名用户,因此会重定向。

我看到了https://stackoverflow.com/a/31228170/799921,但它有点模糊。这是最好的解决方案吗?

【问题讨论】:

  • 我在 SO 参考中看到您有一个解决方案。我不明白为什么 forgot_password 会是一个问题 - 实际上用户没有登录所以应该可以工作?你能提供一些细节吗?
  • 对不起,我不清楚。 forgot_password 的问题是我试图从登录的管理员帐户中使用 forgot_password 表单。我看到重定向到 SECURITY_POST_LOGIN_VIEW 视图。调试这个我确定/推断出包裹在 views.reset_password 周围的 anonymous_user_required 导致了重定向。
  • 从我的管理员创建用户视图调用 send_reset_password_instructions 也使创建用户的管理任务成为一个步骤,而不是我在 OP 中提到的两个步骤。

标签: flask-security


【解决方案1】:

我也在查看烧瓶安全性,并认为我找到了答案。我不确定我使用的函数是否旨在成为开放 api 的一部分,但它可以满足我的需要。

我创建了一个管理员创建用户视图,它将用户电子邮件(和姓名等)添加到数据库中。作为创建用户的一部分,视图调用通常在用户单击“忘记密码”时调用的函数,即send_reset_password_instructions。所以现在作为管理员创建用户过程的一部分,用户会收到一封电子邮件,其中包含用户可以设置自己的密码的链接。

from flask_security.recoverable import send_reset_password_instructions

# my code is maintains self.created_id after creating the user record
# this is due to some complex class involved which handles my crudapi stuff
# your code may vary
user = User.query.filter_by(id=self.created_id).one()
send_reset_password_instructions(user)

【讨论】:

    猜你喜欢
    • 2020-06-23
    • 1970-01-01
    • 2022-01-01
    • 2012-09-27
    • 2013-08-12
    • 1970-01-01
    • 2023-04-03
    • 2019-05-17
    • 1970-01-01
    相关资源
    最近更新 更多