Exemplo n.º 1
0
 def password():
     password_form = SettingResetPasswordForm()
     if request.method == 'POST':
         if password_form.validate_on_submit():
             usercheck_d = UserCheck()
             post_password_encrypt = usercheck_d.password_encrypt(
                 email=session['user']['email'],
                 password=password_form.beforepassword.data)
             this_user = User.objects(
                 email=session['user']['email']).first()
             if this_user.password == post_password_encrypt:
                 new_password_encrypt = usercheck_d.password_encrypt(
                     email=session['user']['email'],
                     password=password_form.newpassword.data)
                 this_user.password = new_password_encrypt
                 this_user.save()
                 flash(u"密码修改成功")
                 return redirect(
                     url_for('sign_module.setting_function',
                             setcate="password"))
             else:
                 flash(u"原密码错误,请重试")
                 return redirect(
                     url_for('sign_module.setting_function',
                             setcate="password"))
         else:
             flash(u"数据提交失败,请检查输入内容")
             return redirect(
                 url_for('sign_module.setting_function',
                         setcate="password"))
     return render_template('users/setting_password.html',
                            password_form=password_form)
Exemplo n.º 2
0
def resetpassword_function():
    forgetstring = request.args.get('forgetstring', '')
    resetform = ResetPswForm(forgetstring=forgetstring)
    reset_check = UserCheck()
    if request.method == 'POST':
        if resetform.validate_on_submit():
            user_count = User.objects(email=resetform.email.data.lower()).count()
            if user_count == 1:
                this_user = User.objects(
                    email=resetform.email.data.lower(),
                    ).first()
                if 'forget' in this_user and this_user.forget['string'] == resetform.forgetstring.data and (int((time.time())) - int(this_user.forget['time']) < 3600):
                    this_user.password = reset_check.password_encrypt(
                        email=resetform.email.data.lower(),
                        password=resetform.password.data)
                    this_user.forget = None
                    this_user.save()
                    flash(u"密码已经修改成功,去登陆吧")
                    return redirect(url_for('sign_module.login_function'))
                else:
                    flash(u"数据匹配失败,请核对你的信息")
                    return redirect(url_for('sign_module.resetpassword_function'))
            else:
                flash(u"邮件验证失败")
                return redirect(url_for('sign_module.resetpassword_function'))
        else:
            flash(u"信息核对失败,密码修改失败,请重新输入")
            return redirect(url_for('sign_module.resetpassword_function'))
    return render_template('users/resetpassword.html', resetform=resetform)
Exemplo n.º 3
0
def resetpassword_function():
    forgetstring = request.args.get('forgetstring', '')
    resetform = ResetPswForm(forgetstring=forgetstring)
    reset_check = UserCheck()
    if request.method == 'POST':
        if resetform.validate_on_submit():
            user_count = User.objects(
                email=resetform.email.data.lower()).count()
            if user_count == 1:
                this_user = User.objects(
                    email=resetform.email.data.lower(), ).first()
                if 'forget' in this_user and this_user.forget[
                        'string'] == resetform.forgetstring.data and (int(
                            (time.time())) - int(this_user.forget['time']) <
                                                                      3600):
                    this_user.password = reset_check.password_encrypt(
                        email=resetform.email.data.lower(),
                        password=resetform.password.data)
                    this_user.forget = None
                    this_user.save()
                    flash(u"密码已经修改成功,去登陆吧")
                    return redirect(url_for('sign_module.login_function'))
                else:
                    flash(u"数据匹配失败,请核对你的信息")
                    return redirect(
                        url_for('sign_module.resetpassword_function'))
            else:
                flash(u"邮件验证失败")
                return redirect(url_for('sign_module.resetpassword_function'))
        else:
            flash(u"信息核对失败,密码修改失败,请重新输入")
            return redirect(url_for('sign_module.resetpassword_function'))
    return render_template('users/resetpassword.html', resetform=resetform)
Exemplo n.º 4
0
def login_function():
    login = LoginForm()
    login_check = UserCheck()
    if request.method == 'POST':
        # POST
        if login.validate_on_submit():
            # Count the User of input information
            user_count = User.objects(
                email=login.email.data.lower(),
                password=login_check.password_encrypt(
                    email=login.email.data.lower(),
                    password=login.password.data)
            ).count()

            if user_count == 1:
                # Login successful

                # Add Session
                this_user = User.objects(
                    email=login.email.data,
                    ).first()
                session.permanent = True
                session['user'] = {
                    "username": this_user.username,
                    "email": this_user.email,
                    "email_md5": common.md5_encrypt(login.email.data),
                    "status": this_user.status,
                    "role": this_user.role,
                    "description": this_user.description
                }
                next_page = request.args.get('next', '')
                if next_page == '':
                    # Redirect to /me
                    flash(u"欢迎回来,亲。")
                    return redirect(url_for('sign_module.me_function'))
                else:
                    # Redirect to next page
                    return redirect(next_page)
            else:
                flash(u"用户名不存在或密码错误")
                return redirect(url_for('sign_module.login_function'))
        else:
            flash(u"数据提交失败,请检查输入内容")
            return redirect(url_for('sign_module.login_function'))

    return render_template('users/login.html', login=login)
Exemplo n.º 5
0
def register_function():
    register = RegisterForm()
    register_check = UserCheck()
    if request.method == 'POST':
        if register.validate_on_submit():
            # Count the User of input information
            user_count = User.objects(
                email=register.email.data.lower()).count()
            if user_count == 0:
                print register.username.data.lower()
                if User.objects(
                        _username=register.username.data.lower()).count() == 0:
                    # 注销入库
                    User(email=register.email.data.lower(),
                         username=register.username.data,
                         password=register_check.password_encrypt(
                             email=register.email.data.lower(),
                             password=register.password.data),
                         _username=register.username.data.lower()).save()
                    # Register Email
                    try:
                        mail_send(subject='恭喜你,小熊笔记账号注册成功!',
                                  recipients=[register.email.data.lower()],
                                  html_body=render_template(
                                      'mail/user_register.html',
                                      user={
                                          "name": register.username.data,
                                          "email": register.email.data.lower()
                                      }))
                    except:
                        flash(u"邮件发送失败")
                    flash(u"注册成功,请登录吧,亲")
                    return redirect(url_for('sign_module.login_function'))
                else:
                    flash(u"用户名已经被使用,请更换")
                    return redirect(url_for('sign_module.register_function'))

            else:
                flash(u"邮箱已经被使用,请尝试找回密码")
                return redirect(url_for('sign_module.register_function'))
        else:
            flash(u"填写的内容不完善,请重试")
            return redirect(url_for('sign_module.register_function'))
    return render_template("users/register.html", register=register)
Exemplo n.º 6
0
def login_function():
    login = LoginForm()
    login_check = UserCheck()
    if request.method == 'POST':
        # POST
        if login.validate_on_submit():
            # Count the User of input information
            user_count = User.objects(
                email=login.email.data.lower(),
                password=login_check.password_encrypt(
                    email=login.email.data.lower(),
                    password=login.password.data)).count()

            if user_count == 1:
                # Login successful

                # Add Session
                this_user = User.objects(email=login.email.data, ).first()
                session.permanent = True
                session['user'] = {
                    "username": this_user.username,
                    "email": this_user.email,
                    "email_md5": common.md5_encrypt(login.email.data),
                    "status": this_user.status,
                    "role": this_user.role,
                    "description": this_user.description
                }
                next_page = request.args.get('next', '')
                if next_page == '':
                    # Redirect to /me
                    flash(u"欢迎回来,亲。")
                    return redirect(url_for('sign_module.me_function'))
                else:
                    # Redirect to next page
                    return redirect(next_page)
            else:
                flash(u"用户名不存在或密码错误")
                return redirect(url_for('sign_module.login_function'))
        else:
            flash(u"数据提交失败,请检查输入内容")
            return redirect(url_for('sign_module.login_function'))

    return render_template('users/login.html', login=login)
Exemplo n.º 7
0
def register_function():
    register = RegisterForm()
    register_check = UserCheck()
    if request.method == 'POST':
        if register.validate_on_submit():
            # Count the User of input information
            user_count = User.objects(email=register.email.data.lower()).count()
            if user_count == 0:
                print register.username.data.lower()
                if User.objects(_username=register.username.data.lower()).count() == 0:
                    # 注销入库
                    User(
                        email=register.email.data.lower(),
                        username=register.username.data,
                        password=register_check.password_encrypt(
                            email=register.email.data.lower(),
                            password=register.password.data),
                        _username=register.username.data.lower()
                        ).save()
                    # Register Email
                    try:
                        mail_send(subject='恭喜你,小熊笔记账号注册成功!', recipients=[register.email.data.lower()], html_body=render_template('mail/user_register.html', user={"name": register.username.data, "email": register.email.data.lower()}))
                    except:
                        flash(u"邮件发送失败")
                    flash(u"注册成功,请登录吧,亲")
                    return redirect(url_for('sign_module.login_function'))
                else:
                    flash(u"用户名已经被使用,请更换")
                    return redirect(url_for('sign_module.register_function'))

            else:
                flash(u"邮箱已经被使用,请尝试找回密码")
                return redirect(url_for('sign_module.register_function'))
        else:
            flash(u"填写的内容不完善,请重试")
            return redirect(url_for('sign_module.register_function'))
    return render_template("users/register.html", register=register)
Exemplo n.º 8
0
 def password():
     password_form = SettingResetPasswordForm()
     if request.method == 'POST':
         if password_form.validate_on_submit():
             usercheck_d = UserCheck()
             post_password_encrypt = usercheck_d.password_encrypt(
                 email=session['user']['email'],
                 password=password_form.beforepassword.data)
             this_user = User.objects(email=session['user']['email']).first()
             if this_user.password == post_password_encrypt:
                 new_password_encrypt = usercheck_d.password_encrypt(
                     email=session['user']['email'],
                     password=password_form.newpassword.data)
                 this_user.password = new_password_encrypt
                 this_user.save()
                 flash(u"密码修改成功")
                 return redirect(url_for('sign_module.setting_function', setcate="password"))
             else:
                 flash(u"原密码错误,请重试")
                 return redirect(url_for('sign_module.setting_function', setcate="password"))
         else:
             flash(u"数据提交失败,请检查输入内容")
             return redirect(url_for('sign_module.setting_function', setcate="password"))
     return render_template('users/setting_password.html', password_form=password_form)
Exemplo n.º 9
0
def forgetpassword_function():
    forgetpsw = ForgetPswForm()
    forget_check = UserCheck()
    if request.method == 'POST':
        if forgetpsw.validate_on_submit():

            user_count = User.objects(
                email=forgetpsw.email.data.lower()).count()
            if user_count == 1:

                this_user = User.objects(
                    email=forgetpsw.email.data.lower(), ).first()
                now_time = int(time.time())

                if 'forget' not in this_user:

                    forgetstring = forget_check.forgetstring_encrypt(
                        email=forgetpsw.email.data.lower())
                    this_user.forget = {
                        'string': forgetstring,
                        'time': int(time.time())
                    }
                    this_user.save()
                    try:
                        mail_send(subject='你好,小熊笔记发来重置密码的密钥串',
                                  recipients=[this_user.email],
                                  html_body=render_template(
                                      'mail/user_forgetpassword.html',
                                      user={
                                          "name": this_user.username,
                                          "fstring": forgetstring
                                      }))
                    except:
                        flash(u"邮件发送失败")
                    flash(u"已发送密码重置邮件,请前往邮箱查收。邮件一小时内有效")
                    return redirect(
                        url_for('sign_module.forgetpassword_function'))

                else:
                    if now_time - int(this_user.forget['time']) > 3600:
                        # Overtime
                        forgetstring = forget_check.forgetstring_encrypt(
                            email=forgetpsw.email.data.lower())
                        this_user.forget = {
                            'string': forgetstring,
                            'time': int(time.time())
                        }
                        this_user.save()
                        # send mail
                        try:
                            mail_send(subject='你好,小熊笔记发来重置密码的密钥串',
                                      recipients=[this_user.email],
                                      html_body=render_template(
                                          'mail/user_forgetpassword.html',
                                          user={
                                              "name": this_user.username,
                                              "fstring": forgetstring
                                          }))
                        except:
                            flash(u"邮件发送失败")
                        flash(u"原密码重置邮件已失效,已重新生成并发送密码重置邮件,请前往邮箱查收")
                        return redirect(
                            url_for('sign_module.forgetpassword_function'))
                    elif (now_time - int(this_user.forget['time']) <
                          3600) and (now_time - int(this_user.forget['time']) >
                                     60):

                        # send mail
                        this_user.forget['time'] = now_time
                        this_user.save()
                        try:
                            mail_send(subject='你好,小熊笔记发来重置密码的密钥串',
                                      recipients=[this_user.email],
                                      html_body=render_template(
                                          'mail/user_forgetpassword.html',
                                          user={
                                              "name": this_user.username,
                                              "fstring":
                                              this_user.forget.string
                                          }))
                        except:
                            flash(u"邮件发送失败")
                        flash(u"密码重置邮件已重新发送")
                        return redirect(
                            url_for('sign_module.forgetpassword_function'))
                    elif now_time - int(this_user.forget['time']) < 60:
                        flash(u"密码重置邮件已发送,请勿频繁操作(邮件发送间隔为 1分钟)")
                        return redirect(
                            url_for('sign_module.forgetpassword_function'))

            else:
                flash(u"邮箱尚未注册,或者邮箱异常")
                return redirect(url_for('sign_module.forgetpassword_function'))
        else:
            flash(u"请填写正确的邮箱")
            return redirect(url_for('sign_module.forgetpassword_function'))
    return render_template('users/forgetpassword.html', forgetpsw=forgetpsw)
Exemplo n.º 10
0
def forgetpassword_function():
    forgetpsw = ForgetPswForm()
    forget_check = UserCheck()
    if request.method == 'POST':
        if forgetpsw.validate_on_submit():

            user_count = User.objects(email=forgetpsw.email.data.lower()).count()
            if user_count == 1:

                this_user = User.objects(
                    email=forgetpsw.email.data.lower(),
                    ).first()
                now_time = int(time.time())

                if 'forget' not in this_user:

                    forgetstring = forget_check.forgetstring_encrypt(email=forgetpsw.email.data.lower())
                    this_user.forget = {
                        'string': forgetstring,
                        'time': int(time.time())
                    }
                    this_user.save()
                    try:
                        mail_send(subject='你好,小熊笔记发来重置密码的密钥串', recipients=[this_user.email], html_body=render_template('mail/user_forgetpassword.html', user={"name": this_user.username, "fstring": forgetstring}))
                    except:
                        flash(u"邮件发送失败")
                    flash(u"已发送密码重置邮件,请前往邮箱查收。邮件一小时内有效")
                    return redirect(url_for('sign_module.forgetpassword_function'))

                else:
                    if now_time - int(this_user.forget['time']) > 3600:
                        # Overtime
                        forgetstring = forget_check.forgetstring_encrypt(email=forgetpsw.email.data.lower())
                        this_user.forget = {
                            'string': forgetstring,
                            'time': int(time.time())
                        }
                        this_user.save()
                        # send mail
                        try:
                            mail_send(subject='你好,小熊笔记发来重置密码的密钥串', recipients=[this_user.email], html_body=render_template('mail/user_forgetpassword.html', user={"name": this_user.username, "fstring": forgetstring}))
                        except:
                            flash(u"邮件发送失败")
                        flash(u"原密码重置邮件已失效,已重新生成并发送密码重置邮件,请前往邮箱查收")
                        return redirect(url_for('sign_module.forgetpassword_function'))
                    elif (now_time - int(this_user.forget['time']) < 3600) and (now_time - int(this_user.forget['time']) > 60):

                        # send mail
                        this_user.forget['time'] = now_time
                        this_user.save()
                        try:
                            mail_send(subject='你好,小熊笔记发来重置密码的密钥串', recipients=[this_user.email], html_body=render_template('mail/user_forgetpassword.html', user={"name": this_user.username, "fstring": this_user.forget.string}))
                        except:
                            flash(u"邮件发送失败")
                        flash(u"密码重置邮件已重新发送")
                        return redirect(url_for('sign_module.forgetpassword_function'))
                    elif now_time - int(this_user.forget['time']) < 60:
                        flash(u"密码重置邮件已发送,请勿频繁操作(邮件发送间隔为 1分钟)")
                        return redirect(url_for('sign_module.forgetpassword_function'))

            else:
                flash(u"邮箱尚未注册,或者邮箱异常")
                return redirect(url_for('sign_module.forgetpassword_function'))
        else:
            flash(u"请填写正确的邮箱")
            return redirect(url_for('sign_module.forgetpassword_function'))
    return render_template('users/forgetpassword.html', forgetpsw=forgetpsw)