def register(self, firstname: str, lastname: str, username: str, emailaddress: str, password: str, companyname: str) -> dict: errors = {} # Validate all arguments # First Name if firstname == "": errors['firstname'] = "First name cannot be empty" elif not validate("firstname", firstname): errors['firstname'] = "First name contains invalid characters and/or it should be more than 2 and less then 20 characters long." # Last Name if lastname == "": errors['lastname'] = "Last name cannot be empty" elif not validate("lastname", lastname): errors['lastname'] = "Last name contains invalid characters and/or it should be more than 2 and less then 20 characters long." # password if password == "": errors['password'] = "******" elif not validate("password", password): errors['password'] = "******" # Username if username == "": errors['username'] = "******" elif not validate("username", username): errors[ 'username'] = "******" elif self.username_exists(username): errors['username'] = "******" # Email if emailaddress == "": errors['emailaddress'] = "Email Address cannot be empty." elif not validate("email", emailaddress): errors['emailaddress'] = "Please provide a valid email address." elif self.emailaddress_exists(emailaddress): errors['emailaddress'] = "Email Address already registered." # Comapny Name if companyname == "": errors['companyname'] = "Compnay name cannot be empty." elif not validate("companyname", companyname): errors['companyname'] = "Company name contains invalid characters and/or it should be more than 2 and less then 64 characters long." # if there are errors return if errors != {}: return {'success': False, 'errors': errors} # All validation tests passed now create a user in database user = User(firstName=firstname, lastName=lastname, companyName=companyname, password=APIUtils.encrypt_password(password), username=username, emailAddress=emailaddress, emailVerified=False ) user.save() # TODO check for error returned by generateRecoveryCode self.generateRecoveryCode(user, "verifyEmail") # return with successful message return {'success': True, 'message': "Your account has successfully been created"}
def change_password(self, username: str, new_password: str) -> dict: if not self.username_exists(username): return {'success': False, 'message': 'Invalid Username!'} user: User = User.objects(username=username)[0] if not validate("password", new_password): return {'success': False, 'error': "Password must contain 8 or more character with at least 1 lowercase, uppercase, numeric and special symbol character each."} user.password = APIUtils.encrypt_password(new_password) user.save() return {'success': True, 'message': 'Password updated successfully!'}
def recover_account(self, username: str, recovery_code: int, new_password: str): users = User.objects(username=username) if users.count() == 0: return {"success": False, "error": "Username does not exist."} elif validate("password", new_password): return {"success": False, "error": "Password does not meet the given criteria"} else: user: User = users[0] if user.codeFor is not None and user.recoveryCode is not None and user.recoveryCode == recovery_code and user.codeFor == "resetPassword": user.codeFor = None user.recoveryCode = None user.password = APIUtils.encrypt_password(new_password) user.save() return {"success": True, "message": "Password changed Successfully!"} return {"success": False, "error": "Invalid Code!"}