Example #1
0
def activate(token):
    res = dfr(check_activate_token(token))
    if res["code"] == 0:
        data = res["data"]
        Action = data["Action"]

        if Action == "verifyEmail":
            username = data["username"]
            checkmail = data["email"]
            uk = rsp("account", username)
            usermail = g.rc.hget(uk, "email")
            success = False
            url = url_for("front.my") if g.signin else url_for("front.login")
            if checkmail == usermail:
                g.rc.hset(uk, "email_verified", 1)
                success = True
            return render_template("public/go.html", url=url, success=success)

        elif Action == "resetPassword":
            username = data["username"]
            return render_template("public/forgot.html",
                                   is_reset=True,
                                   token=token,
                                   user=username)

    else:
        name = res["msg"]
        if PY2 and not isinstance(name, text_type):
            name = name.decode("utf-8")
        return render_template("public/error.html",
                               code=res["code"],
                               name=name)
Example #2
0
def forgot():
    res = dict(code=1)
    Action = request.args.get("Action")
    username = request.form.get("username")
    if not username:
        res.update(msg="Parameter error")
        return res
    username = username.lower()
    ak = rsp("accounts")
    uk = rsp("account", username)

    #: 发送邮件
    if Action == "sending":
        if g.rc.sismember(ak, username):
            if is_true(int(g.rc.hget(uk, "email_verified") or 0)):
                html = make_email_tpl(
                    "activate_forgot.html",
                    activate_url=url_for(
                        "front.activate",
                        token=generate_activate_token(
                            dict(
                                Action="resetPassword",
                                username=username,
                            )),
                        _external=True,
                    ),
                    username=username,
                )
                res = sendmail(
                    subject="{}忘记密码".format(g.site_name),
                    message=html,
                    to=g.rc.hget(uk, "email"),
                )
            else:
                res.update(msg="The user has no authenticated mailbox")
        else:
            res.update(msg="No valid username found")

    #: 邮件验证通过,重置密码
    elif Action == "reset":
        token = request.form.get("token")
        password = request.form.get("password")
        if token and password:
            if len(password) < 6:
                res.update(msg="Password must be at least 6 characters")
            else:
                res = check_activate_token(token)
                if res["code"] == 0:
                    try:
                        g.rc.hset(uk, "password",
                                  generate_password_hash(password))
                    except RedisError:
                        res.update(code=1,
                                   msg="Program data storage service error")
                    else:
                        res.update(code=0)
        else:
            res.update(msg="Parameter error")

    return res