def updateusername(userid: int) -> Dict[str, Any]: # Cast the userID. userid = UserID(userid) username = request.get_json()['username'] user = g.data.local.user.get_user(userid) # Make sure the user ID is valid if user is None: raise Exception('Cannot find user to update!') if not valid_username(username): raise Exception('Invalid username!') # Make sure this user ID isn't taken potential_userid = g.data.local.user.from_username(username) if potential_userid is not None and potential_userid != userid: raise Exception('That username is already taken!') # Update the user user.username = username g.data.local.user.put_user(user) return { 'username': username, }
def register() -> Response: card_number = request.form['card_number'] pin = request.form['pin'] username = request.form['username'] email = request.form['email'] password1 = request.form['password1'] password2 = request.form['password2'] # First, try to convert the card to a valid E004 ID try: cardid = CardCipher.decode(card_number) except CardCipherException: error('Invalid card number!') return register_display(card_number, username, email) # Now, see if this card ID exists already userid = g.data.local.user.from_cardid(cardid) if userid is None: error('This card has not been used on the network yet!') return register_display(card_number, username, email) # Now, make sure this user doesn't already have an account user = g.data.local.user.get_user(userid) if user.username is not None or user.email is not None: error('This card is already in use!') return register_display(card_number, username, email) # Now, see if the pin is correct if not g.data.local.user.validate_pin(userid, pin): error('The entered PIN does not match the PIN on the card!') return register_display(card_number, username, email) # Now, see if the username is valid if not valid_username(username): error('Invalid username!') return register_display(card_number, username, email) # Now, check whether the username is already in use if g.data.local.user.from_username(username) is not None: error('The chosen username is already in use!') return register_display(card_number, username, email) # Now, see if the email address is valid if not valid_email(email): error('Invalid email address!') return register_display(card_number, username, email) # Now, make sure that the passwords match if password1 != password2: error('Passwords do not match each other!') return register_display(card_number, username, email) # Now, make sure passwords are long enough if len(password1) < 6: error('Password is not long enough!') return register_display(card_number, username, email) # Now, create the account. user.username = username user.email = email g.data.local.user.put_user(user) g.data.local.user.update_password(userid, password1) # Now, log them into that created account! aes = AESCipher(g.config['secret_key']) sessionID = g.data.local.user.create_session(userid) success('Successfully registered account!') response = make_response(redirect(url_for('home_pages.viewhome'))) response.set_cookie('SessionID', aes.encrypt(sessionID)) return response