def confirm_account(self, confirmation_token): ''' Confirms user in database ''' try: # Decode token jwt_manager = JWTConfirmEmailManagerFactory.new() brl_user = jwt_manager.get_confirmed_user(confirmation_token) user = self.store.read_user(brl_user) except NotInStoreException: raise NotFoundException("User '%s' doesn't exist" % brl_user) if user.confirmation_token == confirmation_token: if not user.active: # Do not re-send things if already activated try: register_signup(self.store, brl_user) except Exception as exc: logger.error("Can't register sign-up in background! %s" % str(exc)) user.active = True user.confirmation_date = datetime.datetime.now() self.store.update_user(user) jwt_auth_manager = JWTCredentialsManagerFactory.new(self.store) token = jwt_auth_manager.get_token_for(brl_user) return token, brl_user, user.ga_client_id else: raise NotFoundException("Invalid user or token")
def register(self, brl_user, email, plain_password, allow_mailing, provider=None, access_token=None, invited_by=None): ''' :param: user is a web_api.model.User ''' # Validate password if len(plain_password) < MIN_PASSWORD_LENGTH: logger.debug("Invalid password length for %s" % email) raise ControledErrorException("Password length must" " be %s characters min" % MIN_PASSWORD_LENGTH) # Search users with same email if self.store.read_user_by_email(email): logger.debug("Email '%s' already exists!" % email) raise ControledErrorException("Email '%s' already exists! Forgot password? " "Go to login and click on forgot password" % email) try: brl_user = BRLUser(brl_user) bii_user = User(brl_user) bii_user.password = plain_password except InvalidNameException as e: raise ControledErrorException(e) # Search invited_by user (by mail or login) friend = None if invited_by: if "@" in invited_by: # email address friend = self.store.read_user_by_email(invited_by) friend = friend.ID if friend else None else: # Login friend_object = self.store.exists_user_id_ignoring_case(invited_by) if friend_object and friend_object.active: friend = invited_by if not friend: raise ControledErrorException("User %s doesn't exist" % invited_by) bii_user.invited_by = friend # Check the existing of user name (User.ID), with case-insensitive if self.store.exists_user_id_ignoring_case(brl_user): logger.debug("User name '%s' already exists!" % brl_user) raise ControledErrorException("Username '%s' already exists! " "Choose other username" % brl_user) try: bii_user.email = email bii_user.allow_mailing = allow_mailing manager = JWTConfirmEmailManagerFactory.new() token = manager.get_token_for(brl_user) bii_user.confirmation_token = token bii_user.joined_date = datetime.datetime.now() bii_user.active = False oauth_service = get_oauth_service(self.store) oauth_user_info = oauth_service.get_user_info(provider, access_token) self.store.create_user(bii_user) if oauth_user_info: # If user has changed the oauth email, not confirm the account if oauth_user_info[1] == bii_user.email: bii_user.active = True try: register_signup(self.store, brl_user) except Exception as exc: logger.error("Can't register sign-up in background! %s" % str(exc)) bii_user.fill_user_oauth_token(provider, access_token) self.store.update_user(bii_user) return bii_user except Exception as e: logger.error("Error creating user at mongo: %s" % str(e)) logger.error(traceback.format_exc()) raise e