Exemplo n.º 1
0
    def mutate(self, info, **kwargs):
        with session_scope() as db_session:
            username = kwargs.get("username")
            email = kwargs.get("email")
            password = kwargs.get("password")
            validation.validate_registration(username, email, password)
            try:
                if current_user.is_authenticated:
                    raise GraphQLError(_("You are already logged in."))
                user = ModelUserAccount(
                    username=username,
                    email=email,
                    password=ModelUserAccount.generate_hash(password),
                    locale=str(get_locale()),
                )
                token = encode_token(user.email, verify_email_salt)
                verify_url = generate_url("verify_email.verify_email", token)
                template = template_env.get_template("verify_email.html")
                content = template.render(display_name=username,
                                          verify_url=verify_url)
                db_session.add(user)
                db_session.flush()
                q.enqueue(send_email, user.email,
                          _("Verify your DofusLab account"), content)
                login_user(user)
                save_custom_sets(db_session)
            except Exception as e:
                raise GraphQLError(_("An error occurred while registering."))

        return RegisterUser(user=user, ok=True)
def validate_email(email):
    regex = re.compile("[^@]+@[^@]+\.[^@]+")
    if not regex.fullmatch(email):
        raise GraphQLError(_("The email you entered is invalid."))
    db_user = ModelUserAccount.find_by_email(email)
    if db_user:
        raise GraphQLError(_("There already exists a user with that email."))
def validate_username(username):
    # regex from https://stackoverflow.com/questions/12018245/regular-expression-to-validate-username
    regex = re.compile("^(?=[a-zA-Z0-9._]{3,20}$)(?!.*[_.]{2})[^_.].*[^_.]$")
    if not regex.fullmatch(username):
        raise GraphQLError(_("The display name you entered is invalid."))
    db_user = ModelUserAccount.find_by_username(username)
    if db_user:
        raise GraphQLError(
            _("There already exists a user with that display name."))
Exemplo n.º 4
0
    def mutate(self, info, **kwargs):
        if current_user.is_authenticated:
            raise GraphQLError(_("You are already logged in."))
        token = kwargs.get("token")
        password = kwargs.get("password")
        email = decode_token(token, reset_password_salt)
        invalid_token_error = GraphQLError(
            _("The link is invalid or expired. Please request a new one."))
        if not email:
            raise invalid_token_error
        user = ModelUserAccount.find_by_email(email)

        if not user:
            raise GraphQLError(
                _("The link is invalid or expired. Please request a new one."))

        validation.validate_password(password)
        if user.check_password(password):
            raise GraphQLError(
                _("You must enter a password different from your current one.")
            )
        with session_scope() as db_session:
            user.password = ModelUserAccount.generate_hash(password)
            return ResetPassword(ok=True)
Exemplo n.º 5
0
 def mutate(self, info, **kwargs):
     if current_user.is_authenticated:
         raise GraphQLError(_("You are already logged in."))
     email = kwargs.get("email")
     password = kwargs.get("password")
     remember = kwargs.get("remember")
     user = ModelUserAccount.find_by_email(email)
     auth_error = GraphQLError(_("Invalid username or password."))
     if not user:
         raise auth_error
     if not user.check_password(password):
         raise auth_error
     login_user(user, remember=remember)
     refresh()
     with session_scope() as db_session:
         save_custom_sets(db_session)
     return LoginUser(user=user, ok=True)
Exemplo n.º 6
0
 def mutate(self, info, **kwargs):
     if not current_user.is_authenticated:
         raise GraphQLError(_("You are not logged in."))
     old_password = kwargs.get("old_password")
     new_password = kwargs.get("new_password")
     user = current_user._get_current_object()
     auth_error = GraphQLError(_("Incorrect password."))
     if not user:
         raise auth_error
     if not user.check_password(old_password):
         raise auth_error
     validation.validate_password(new_password)
     if old_password == new_password:
         raise GraphQLError(
             _("You must enter a password different from your current one.")
         )
     with session_scope():
         user.password = ModelUserAccount.generate_hash(new_password)
         return ChangePassword(ok=True)
Exemplo n.º 7
0
 def mutate(self, info, **kwargs):
     if current_user.is_authenticated:
         raise GraphQLError(_("You are already logged in."))
     email = kwargs.get("email")
     user = ModelUserAccount.find_by_email(email)
     auth_error = GraphQLError(
         _("We could not find an account with that email."))
     if not user:
         raise auth_error
     if not user.verified:
         raise GraphQLError(_("Please verify your email first."))
     token = encode_token(user.email, reset_password_salt)
     reset_password_url = "{}reset-password?token={}".format(
         base_url, token)
     template = template_env.get_template("reset_password.html")
     content = template.render(display_name=user.username,
                               reset_password_url=reset_password_url)
     q.enqueue(send_email, user.email, _("Reset your DofusLab password"),
               content)
     return RequestPasswordReset(ok=True)