more fine-grained errors for invalid invitations
This commit is contained in:
@@ -15,7 +15,19 @@ class WrongUserError(Exception):
|
||||
|
||||
@property
|
||||
def message(self):
|
||||
return "User {} with DOD ID {} does not match expected DOD ID {} for invitation {}".format(self.user.id, self.user.dod_id, self.invite.user.dod_id, self.invite.id)
|
||||
return "User {} with DOD ID {} does not match expected DOD ID {} for invitation {}".format(
|
||||
self.user.id, self.user.dod_id, self.invite.user.dod_id, self.invite.id
|
||||
)
|
||||
|
||||
|
||||
class ExpiredError(Exception):
|
||||
def __init__(self, invite):
|
||||
self.invite = invite
|
||||
|
||||
@property
|
||||
def message(self):
|
||||
return "Invitation {} has expired.".format(self.invite.id)
|
||||
|
||||
|
||||
class InvitationError(Exception):
|
||||
def __init__(self, invite):
|
||||
@@ -60,23 +72,28 @@ class Invitations(object):
|
||||
if invite.user.dod_id != user.dod_id:
|
||||
raise WrongUserError(user, invite)
|
||||
|
||||
if invite.is_expired:
|
||||
invite.status = InvitationStatus.REJECTED
|
||||
elif invite.is_pending:
|
||||
invite.status = InvitationStatus.ACCEPTED
|
||||
elif invite.is_expired:
|
||||
Invitations._update_status(invite, InvitationStatus.REJECTED)
|
||||
raise ExpiredError(invite)
|
||||
|
||||
db.session.add(invite)
|
||||
db.session.commit()
|
||||
|
||||
if invite.is_revoked or invite.is_rejected:
|
||||
elif invite.is_accepted or invite.is_revoked or invite.is_rejected:
|
||||
raise InvitationError(invite)
|
||||
|
||||
WorkspaceUsers.enable(invite.workspace_role)
|
||||
|
||||
return invite
|
||||
elif invite.is_pending:
|
||||
Invitations._update_status(invite, InvitationStatus.ACCEPTED)
|
||||
WorkspaceUsers.enable(invite.workspace_role)
|
||||
return invite
|
||||
|
||||
@classmethod
|
||||
def current_expiration_time(cls):
|
||||
return datetime.datetime.now() + datetime.timedelta(
|
||||
minutes=Invitations.EXPIRATION_LIMIT_MINUTES
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _update_status(cls, invite, new_status):
|
||||
invite.status = new_status
|
||||
db.session.add(invite)
|
||||
db.session.commit()
|
||||
|
||||
return invite
|
||||
|
||||
@@ -3,27 +3,35 @@ from flask_wtf.csrf import CSRFError
|
||||
import werkzeug.exceptions as werkzeug_exceptions
|
||||
|
||||
import atst.domain.exceptions as exceptions
|
||||
from atst.domain.invitations import InvitationError, WrongUserError as InvitationWrongUserError
|
||||
from atst.domain.invitations import (
|
||||
InvitationError,
|
||||
ExpiredError as InvitationExpiredError,
|
||||
WrongUserError as InvitationWrongUserError,
|
||||
)
|
||||
|
||||
|
||||
def log_error(e):
|
||||
error_message = e.message if hasattr(e, "message") else str(e)
|
||||
current_app.logger.error(error_message)
|
||||
|
||||
|
||||
def handle_error(e, message="Not Found", code=404):
|
||||
log_error(e)
|
||||
return render_template("error.html", message=message), code
|
||||
|
||||
|
||||
def make_error_pages(app):
|
||||
def log_error(e):
|
||||
error_message = e.message if hasattr(e, "message") else str(e)
|
||||
app.logger.error(error_message)
|
||||
|
||||
@app.errorhandler(werkzeug_exceptions.NotFound)
|
||||
@app.errorhandler(exceptions.NotFoundError)
|
||||
@app.errorhandler(exceptions.UnauthorizedError)
|
||||
# pylint: disable=unused-variable
|
||||
def not_found(e):
|
||||
log_error(e)
|
||||
return render_template("error.html", message="Not Found"), 404
|
||||
return handle_error(e)
|
||||
|
||||
@app.errorhandler(exceptions.UnauthenticatedError)
|
||||
# pylint: disable=unused-variable
|
||||
def unauthorized(e):
|
||||
log_error(e)
|
||||
return render_template("error.html", message="Log in Failed"), 401
|
||||
return handle_error(e, message="Log in Failed", code=401)
|
||||
|
||||
@app.errorhandler(CSRFError)
|
||||
# pylint: disable=unused-variable
|
||||
@@ -46,12 +54,13 @@ def make_error_pages(app):
|
||||
@app.errorhandler(InvitationWrongUserError)
|
||||
# pylint: disable=unused-variable
|
||||
def invalid_invitation(e):
|
||||
log_error(e)
|
||||
return (
|
||||
render_template(
|
||||
"error.html", message="The link you followed is invalid."
|
||||
),
|
||||
404,
|
||||
return handle_error(e, message="The link you followed is invalid.", code=404)
|
||||
|
||||
@app.errorhandler(InvitationExpiredError)
|
||||
# pylint: disable=unused-variable
|
||||
def invalid_invitation(e):
|
||||
return handle_error(
|
||||
e, message="The invitation you followed has expired.", code=404
|
||||
)
|
||||
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user