def token_check(): token = request.args.get('token') if token is None or token == "": return error_view(400, "missing token") if UserPending.exists(token): return valid_view("valid token") else: return error_view(400, "invalid token")
def invite(): user_pending = None try: body = request.json if body is None: return error_view(400, "invalid JSON in body") email = body.get('email') if email is None: return error_view(400, "invalid email value") # check if mail is already used if User.exists_from_email(email): return error_view(500, f"email already used by an existing user") if UserRequest.exists(email): return error_view(500, f"a request for this email already exists") if UserPending.exists_from_email(email): return error_view( 500, f"a user with this email has already been invited") # create a new pending user in database user_pending = UserPending.new(email) user_pending.insert() # send a mail with the token default_channel = Channel.get(Channel.DEFAULT) template = INVITE_TEMPLATE template.set_format(token=user_pending.token) send(user_pending.email, default_channel, template) return user_pending_created_view(user_pending) except ObjectNotFound as o: return error_view(404, str(o)) except (MailSendingError, TelegramSendingError): # in case the mail cannot be sent, abort the invitation and delete the pending user in database if user_pending is not None: user_pending.delete() return error_view(500, f"error sending the invitation")
def request_access(): body = request.json if body is None: return error_view(400, "invalid JSON in body") email = body.get('email') if email is None: return error_view(400, "invalid email value") if User.exists_from_email(email): return error_view(500, "email unavailable") if UserPending.exists_from_email(email): return error_view(500, "an invitation has already been sent to this email") if UserRequest.exists(email): return error_view(500, "a request for this email has already been sent") user_request = UserRequest.new(email) user_request.insert() return user_request_created_view(user_request)
def register(): try: body = request.json if body is None: return error_view(400, "invalid JSON in body") username = body.get('username') password = body.get('password') token = request.args.get('token') if username is None or password is None or token is None: return error_view(400, "invalid parameters") if token == '' or username == '' or password == '': return error_view(400, "missing parameters") if User.exists(username): return error_view(500, f"user with username `{username}` already exists") user_pending = UserPending.get(token) created_user = User.new(username, password, user_pending.email) created_user.insert() user_pending.delete() default_channel = Channel.get(Channel.DEFAULT) user_channel = UserChannel.new( created_user.username, default_channel.name, created_user.email ) user_channel.verified = True user_channel.insert() return user_created_view(created_user) except ObjectNotFound as o: return error_view(404, str(o))
def user_pending_created_view(user_pending: UserPending): return jsonify({ "msg": f"user with mail {user_pending.email} invited", "user_pending": user_pending.safe_json() }), 201
def pending_list(): user_invite_list = UserPending.list() return user_pending_list_view(user_invite_list)