Eliminate a lot of the if cases in create_user by separating them out. Add a limit to the number of users which can be created based on the license. Add support for creating and loading licenses.

This commit is contained in:
Jake Moshenko 2014-05-28 13:51:52 -04:00
parent 0ef1902957
commit 33b43b75c0
9 changed files with 103 additions and 26 deletions

View file

@ -64,7 +64,33 @@ class InvalidBuildTriggerException(DataModelException):
pass
def create_user(username, password, email, add_change_pw_notification=True):
class TooManyUsersException(DataModelException):
pass
def is_create_user_allowed():
return get_active_user_count() < config.app_config['LICENSE_USER_LIMIT']
def create_user(username, password, email):
""" Creates a regular user, if allowed. """
if not validate_password(password):
raise InvalidPasswordException(INVALID_PASSWORD_MESSAGE)
if not is_create_user_allowed():
raise TooManyUsersException()
created = _create_user(username, email)
# Store the password hash
pw_hash = bcrypt.hashpw(password, bcrypt.gensalt())
created.password_hash = pw_hash
created.save()
return created
def _create_user(username, email):
if not validate_email(email):
raise InvalidEmailAddressException('Invalid email address: %s' % email)
@ -72,10 +98,6 @@ def create_user(username, password, email, add_change_pw_notification=True):
if not username_valid:
raise InvalidUsernameException('Invalid username %s: %s' % (username, username_issue))
# We allow password none for the federated login case.
if password is not None and not validate_password(password):
raise InvalidPasswordException(INVALID_PASSWORD_MESSAGE)
try:
existing = User.get((User.username == username) | (User.email == email))
@ -94,18 +116,7 @@ def create_user(username, password, email, add_change_pw_notification=True):
pass
try:
pw_hash = None
if password is not None:
pw_hash = bcrypt.hashpw(password, bcrypt.gensalt())
new_user = User.create(username=username, password_hash=pw_hash,
email=email)
# If the password is None, then add a notification for the user to change
# their password ASAP.
if not pw_hash and add_change_pw_notification:
create_notification('password_required', new_user)
new_user = User.create(username=username, email=email)
return new_user
except Exception as ex:
raise DataModelException(ex.message)
@ -122,7 +133,7 @@ def is_username_unique(test_username):
def create_organization(name, email, creating_user):
try:
# Create the org
new_org = create_user(name, None, email, add_change_pw_notification=False)
new_org = _create_user(name, email)
new_org.organization = True
new_org.save()
@ -335,8 +346,11 @@ def set_team_org_permission(team, team_role_name, set_by_username):
return team
def create_federated_user(username, email, service_name, service_id):
new_user = create_user(username, None, email)
def create_federated_user(username, email, service_name, service_id, set_password_notification):
if not is_create_user_allowed():
raise TooManyUsersException()
new_user = _create_user(username, email)
new_user.verified = True
new_user.save()
@ -344,6 +358,9 @@ def create_federated_user(username, email, service_name, service_id):
FederatedLogin.create(user=new_user, service=service,
service_ident=service_id)
if set_password_notification:
create_notification('password_required', new_user)
return new_user