def host_signup( email: str, password: str, name: str, primary_affiliation: int, reason: str = None ) -> Tuple[Optional['UserData'], Optional[str], bool]: with session_scope() as session: if PrimaryAffiliation.get_by_id(session, primary_affiliation) is not None: user = User.create( session, User(email=email, password=password, name=name, primary_affiliation=primary_affiliation)) if user is not None: code = None threshold = int(get_property('user.threshold')) if threshold > 0: code = UserVerification.add(session, user.id).code set_property('user.threshold', str(threshold - 1)) host_request = UserHostRequest( user=user.id, primary_affiliation=primary_affiliation, reason=reason) session.add(host_request) return UserData(user), code, True return None, None, True return None, None, False
def invite_next_users(): threshold = int(get_property('user.threshold')) with session_scope() as session: users = UserData.list(User.next_users_to_permit(session)) for user in users: if threshold < 1: break code = UserVerification.add(session, user.id).code send_verification_email(to=user.email, code=code) threshold -= 1 set_property('user.threshold', str(threshold))
def login(email: str, password: str) -> 'UserData': with session_scope() as session: if User.verify_credentials(session, email, password): user = User.get_by_email(session, email) if not user.active: verification = UserVerification.get_by_user(session, user.id) threshold = int(get_property('user.threshold')) if not verification and threshold > 0: verification = UserVerification.add(session, user_id=user.id) send_verification_email(to=email, code=verification.code) set_property('user.threshold', threshold - 1) user.login_count += 1 return UserData(user) return None
def post(self, path): if not self.has_admin_role(): self.write_error(403) raise Finish() prop = 'user.threshold' data = self.get_data() try: increment = int(data.get('increment', 0)) except ValueError: self.write_error(400, f"Error: invalid number") raise Finish() set_property(prop, str(int(get_property(prop)) + increment)) invite_next_users() self.success(status=201) self.finish()
def signup(email: str, password: str, name: str = None) -> Tuple[Optional['UserData'], Optional[str]]: """ Sign user up with :param email: :param password: :param name: :return: """ with session_scope() as session: user = User.create(session, User(email=email, password=password)) if user is not None: code = None threshold = int(get_property('user.threshold')) if threshold > 0: logging.info("passed threshold") code = UserVerification.add(session, user.id).code set_property('user.threshold', str(threshold - 1)) else: logging.info("failed threshold") return UserData(user), code return None, None
def get(self, path): user_id = self.get_user_id() try: user = get_user(user_id) threshold = int(get_property('user.threshold')) if user.active: logging.info(f"User {user_id} is already active") self.write_error(400, "Error: user already active") elif user.status == "REQUESTED" and (threshold > 0 or get_user_verification_code(user_id) is not None): code = get_user_verification(user_id) send_verification_email(to=user.email, code=code) set_property('user.threshold', str(threshold-1)) self.success(status=204) elif user.status == 'VERIFIED' or user.status == 'ACCEPTED': self.write_error(400, "User is already verified") else: self.write_error(403, "User has not yet been permitted") except Exception as e: logging.error("Failed to send verification email") logging.error(e) self.write_error(500, "Error: failed to send verification email") finally: self.finish()