Example #1
0
def password_reset(request):
    """
    A view for resetting a user's password
    """
    if request.method == 'POST': 
        form = ResetPasswordForm(request.POST) 
        if form.is_valid(): 
            email = form.cleaned_data.get('email')
            if email == '*****@*****.**':
              msg = 'The demo user cannot reset the password'
              messages.error(request, msg)
            else:
              user = form.get_user()
              # generate and set new password
              password = utils.user_password_reset(user)
              # send email 
              utils.send_reset_password_email(request, user, password)
              # message 
              msg = 'Your new password has been sent to your email address'
              messages.success(request, msg)
    else:
        form = ResetPasswordForm()

    # display information if user is already logged in 
    if request.user.is_authenticated():
        url = reverse('url_password_change')
        msg = '''You are currently logged in, you may instead want to
                 <a href="%s" class="alert-link">change your password</a>.''' % url
        messages.info(request, msg)

    return render_to_response('account/password_reset.html', 
                              { 'form' : form, 'request' : request }, 
                  context_instance=RequestContext(request)) 
Example #2
0
def password_reset(request):
    if request.method == 'POST':
        errors = []
        username = request.POST.get('username')
        try:
            user = User.objects.get(username__exact=username)
            #temp_pass = ''.join(random.choice(string.ascii_uppercase) for i in range(6))
            #user.set_password(temp_pass)
            #user.save()
            #core_services.ldap_change_user_password(user, temp_pass)

            uid = urlsafe_base64_encode(force_bytes(user.pk))
            token = default_token_generator.make_token(user)

            auth_utils.send_reset_password_email(user.email, user.id, uid,
                                                 token)
            return redirect('password_reset_success')

        except Exception as e:
            errors.append(_('User account does not exist') + ': ' + str(e))
            return render_to_response('password_reset.html',
                                      {'errors': errors},
                                      RequestContext(request))

    else:
        if 'AD' in GVSIGOL_LDAP and GVSIGOL_LDAP['AD'].__len__() > 0:
            return redirect('login_user')
        return render_to_response('password_reset.html', {},
                                  RequestContext(request))
Example #3
0
def password_reset(request):
    if request.method == 'POST':
        errors = []
        username = request.POST.get('username')
        try:
            user = User.objects.get(username__exact=username)

            uid = urlsafe_base64_encode(force_bytes(user.pk))
            token = default_token_generator.make_token(user)

            auth_utils.send_reset_password_email(user.email, user.id, uid,
                                                 token)
            return redirect('password_reset_success')
        except User.DoesNotExist:
            logger.exception("Error resetting password")
            errors.append(_('User account does not exist'))
            return render(request, 'password_reset.html', {'errors': errors})
        except Exception:
            logger.exception("Error resetting password")
            errors.append(_('That did not work'))
            return render(request, 'password_reset.html', {'errors': errors})

    else:
        if 'AD' in GVSIGOL_LDAP and GVSIGOL_LDAP['AD'].__len__() > 0:
            return redirect('login_user')
        return render(request, 'password_reset.html', {})
Example #4
0
def recover_password(email: str, db: Session = Depends(deps.get_db)) -> Any:
    """
    Password Recovery
    """
    user = crud.user.get_by_email(db, email=email)

    if not user:
        raise NotFoundException(
            "用户不存在"
        )
    password_reset_token = generate_password_reset_token(email=email)
    send_reset_password_email(
        email_to=user.email, email=email, token=password_reset_token
    )
    return resp_200({"msg": "Password recovery email sent"})
Example #5
0
def recover_password(email: str, db: Session = Depends(deps.get_db)) -> Any:
    """
    Password Recovery
    """
    user = crud.user.get_by_email(db, email=email)

    if not user:
        raise HTTPException(
            status_code=404,
            detail="The user with this username does not exist in the system.",
        )
    password_reset_token = generate_password_reset_token(email=email)
    send_reset_password_email(email_to=user.email,
                              email=email,
                              token=password_reset_token)
    return {"msg": "Password recovery email sent"}
Example #6
0
def recover_password(
    email: str, db: AsyncIOMotorClient = Depends(get_database)) -> Any:
    """
    Password Recovery
    """
    user = crud.user.get_by_email(db, email=email)

    if not user:
        raise HTTPException(
            status_code=404,
            detail="用户不存在!",
        )
    password_reset_token = generate_password_reset_token(email=email)
    send_reset_password_email(email_to=user.email,
                              email=email,
                              token=password_reset_token)
    return {"msg": "确认邮件已发出!"}
Example #7
0
def recover_password(username: str):
    """
    Password Recovery.
    """
    db = get_default_db()
    user = db.user.get(username=username)

    if not user:
        raise HTTPException(
            status_code=HTTP_404_NOT_FOUND,
            detail="The user with this username does not exist in the system.",
        )
    password_reset_token = generate_password_reset_token(username=username)
    send_reset_password_email(email_to=user.email,
                              username=username,
                              token=password_reset_token)
    return {"msg": "Password recovery email sent"}
Example #8
0
def recover_password(
    email: str, db: Session = Depends(deps.get_db)) -> JSONResponse:
    """
    Password Recovery
    """
    db_user = crud.get_user(db, email=email)

    if db_user is None:
        return JSONResponse(status_code=404,
                            content={
                                "message":
                                "The user with this email "
                                "does not exist in the system."
                            })

    password_reset_token = access_token.generate_password_reset_token(
        email=email)
    send_reset_password_email(email=email,
                              password_reset_token=password_reset_token)
    return JSONResponse(status_code=200, content={"message": "success"})
Example #9
0
 def send_email(self):
     user = self.cleaned_data['user']
     new_password = AuthUser.objects.make_random_password()
     user.set_password(new_password)
     user.save()
     send_reset_password_email(user, new_password)