2015-07-15 21:25:41 +00:00
|
|
|
import bcrypt
|
|
|
|
import logging
|
|
|
|
import json
|
2016-09-08 16:24:47 +00:00
|
|
|
import uuid
|
2017-04-05 03:29:22 +00:00
|
|
|
from flask_login import UserMixin
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2018-04-06 17:48:01 +00:00
|
|
|
from peewee import JOIN, IntegrityError, fn
|
2015-07-15 21:25:41 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
from datetime import datetime, timedelta
|
2016-11-04 21:57:55 +00:00
|
|
|
from enum import Enum
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
from data.database import (User, LoginService, FederatedLogin, RepositoryPermission, TeamMember,
|
|
|
|
Team, Repository, TupleSelector, TeamRole, Namespace, Visibility,
|
2015-06-28 10:29:22 +00:00
|
|
|
EmailConfirmation, Role, db_for_update, random_string_generator,
|
2016-12-08 18:53:52 +00:00
|
|
|
UserRegion, ImageStorageLocation,
|
2016-09-08 22:43:50 +00:00
|
|
|
ServiceKeyApproval, OAuthApplication, RepositoryBuildTrigger,
|
2018-03-09 20:55:51 +00:00
|
|
|
UserPromptKind, UserPrompt, UserPromptTypes, DeletedNamespace,
|
|
|
|
RobotAccountMetadata)
|
2015-07-15 21:25:41 +00:00
|
|
|
from data.model import (DataModelException, InvalidPasswordException, InvalidRobotException,
|
|
|
|
InvalidUsernameException, InvalidEmailAddressException,
|
2016-10-20 18:58:35 +00:00
|
|
|
TooManyLoginAttemptsException, db_transaction,
|
2015-07-15 21:25:41 +00:00
|
|
|
notification, config, repository, _basequery)
|
2017-01-11 19:53:14 +00:00
|
|
|
from data.text import prefix_search
|
2015-07-15 21:25:41 +00:00
|
|
|
from util.names import format_robot_username, parse_robot_username
|
|
|
|
from util.validation import (validate_username, validate_email, validate_password,
|
|
|
|
INVALID_PASSWORD_MESSAGE)
|
|
|
|
from util.backoff import exponential_backoff
|
2017-04-05 18:01:55 +00:00
|
|
|
from util.timedeltastring import convert_to_timedelta
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
EXPONENTIAL_BACKOFF_SCALE = timedelta(seconds=1)
|
|
|
|
|
|
|
|
def hash_password(password, salt=None):
|
|
|
|
salt = salt or bcrypt.gensalt()
|
|
|
|
return bcrypt.hashpw(password.encode('utf-8'), salt)
|
|
|
|
|
2018-04-20 15:01:05 +00:00
|
|
|
def create_user(username, password, email, auto_verify=False, email_required=True, prompts=tuple(),
|
|
|
|
is_possible_abuser=False):
|
2015-07-15 21:25:41 +00:00
|
|
|
""" Creates a regular user, if allowed. """
|
|
|
|
if not validate_password(password):
|
|
|
|
raise InvalidPasswordException(INVALID_PASSWORD_MESSAGE)
|
|
|
|
|
2018-04-20 15:01:05 +00:00
|
|
|
created = create_user_noverify(username, email, email_required=email_required, prompts=prompts,
|
|
|
|
is_possible_abuser=is_possible_abuser)
|
2015-07-15 21:25:41 +00:00
|
|
|
created.password_hash = hash_password(password)
|
|
|
|
created.verified = auto_verify
|
|
|
|
created.save()
|
|
|
|
|
|
|
|
return created
|
|
|
|
|
|
|
|
|
2018-04-20 15:01:05 +00:00
|
|
|
def create_user_noverify(username, email, email_required=True, prompts=tuple(),
|
|
|
|
is_possible_abuser=False):
|
2016-09-08 16:24:47 +00:00
|
|
|
if email_required:
|
|
|
|
if not validate_email(email):
|
|
|
|
raise InvalidEmailAddressException('Invalid email address: %s' % email)
|
|
|
|
else:
|
|
|
|
# If email addresses are not required and none was specified, then we just use a unique
|
|
|
|
# ID to ensure that the database consistency check remains intact.
|
|
|
|
email = email or str(uuid.uuid4())
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
(username_valid, username_issue) = validate_username(username)
|
|
|
|
if not username_valid:
|
2017-01-17 22:31:24 +00:00
|
|
|
raise InvalidUsernameException('Invalid namespace %s: %s' % (username, username_issue))
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
existing = User.get((User.username == username) | (User.email == email))
|
|
|
|
logger.info('Existing user with same username or email.')
|
|
|
|
|
|
|
|
# A user already exists with either the same username or email
|
|
|
|
if existing.username == username:
|
|
|
|
raise InvalidUsernameException('Username has already been taken: %s' %
|
|
|
|
username)
|
|
|
|
raise InvalidEmailAddressException('Email has already been used: %s' %
|
|
|
|
email)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
# This is actually the happy path
|
|
|
|
logger.debug('Email and username are unique!')
|
|
|
|
|
2018-05-09 11:52:28 +00:00
|
|
|
# Create the user.
|
2015-07-15 21:25:41 +00:00
|
|
|
try:
|
2017-04-05 18:01:55 +00:00
|
|
|
default_expr_s = _convert_to_s(config.app_config['DEFAULT_TAG_EXPIRATION'])
|
2018-02-20 18:58:14 +00:00
|
|
|
default_max_builds = config.app_config.get('DEFAULT_NAMESPACE_MAXIMUM_BUILD_COUNT')
|
2018-04-20 15:01:05 +00:00
|
|
|
threat_max_builds = config.app_config.get('THREAT_NAMESPACE_MAXIMUM_BUILD_COUNT')
|
|
|
|
|
|
|
|
if is_possible_abuser and threat_max_builds is not None:
|
|
|
|
default_max_builds = threat_max_builds
|
|
|
|
|
2018-02-20 18:58:14 +00:00
|
|
|
new_user = User.create(username=username, email=email, removed_tag_expiration_s=default_expr_s,
|
|
|
|
maximum_queued_builds_count=default_max_builds)
|
2016-11-04 21:57:55 +00:00
|
|
|
for prompt in prompts:
|
|
|
|
create_user_prompt(new_user, prompt)
|
|
|
|
|
|
|
|
return new_user
|
2015-07-15 21:25:41 +00:00
|
|
|
except Exception as ex:
|
|
|
|
raise DataModelException(ex.message)
|
|
|
|
|
2018-02-20 18:58:14 +00:00
|
|
|
def increase_maximum_build_count(user, maximum_queued_builds_count):
|
|
|
|
""" Increases the maximum number of allowed builds on the namespace, if greater than that
|
|
|
|
already present.
|
|
|
|
"""
|
|
|
|
if (user.maximum_queued_builds_count is not None and
|
|
|
|
maximum_queued_builds_count > user.maximum_queued_builds_count):
|
|
|
|
user.maximum_queued_builds_count = maximum_queued_builds_count
|
|
|
|
user.save()
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
def is_username_unique(test_username):
|
|
|
|
try:
|
|
|
|
User.get((User.username == test_username))
|
|
|
|
return False
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def change_password(user, new_password):
|
|
|
|
if not validate_password(new_password):
|
|
|
|
raise InvalidPasswordException(INVALID_PASSWORD_MESSAGE)
|
|
|
|
|
|
|
|
pw_hash = hash_password(new_password)
|
|
|
|
user.invalid_login_attempts = 0
|
|
|
|
user.password_hash = pw_hash
|
2017-12-07 16:39:27 +00:00
|
|
|
invalidate_all_sessions(user)
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
# Remove any password required notifications for the user.
|
|
|
|
notification.delete_notifications_by_kind(user, 'password_required')
|
|
|
|
|
|
|
|
|
2016-11-04 21:57:55 +00:00
|
|
|
def get_default_user_prompts(features):
|
|
|
|
prompts = set()
|
|
|
|
if features.USER_METADATA:
|
|
|
|
prompts.add(UserPromptTypes.ENTER_NAME)
|
|
|
|
prompts.add(UserPromptTypes.ENTER_COMPANY)
|
|
|
|
|
|
|
|
return prompts
|
|
|
|
|
|
|
|
|
2016-09-08 22:43:50 +00:00
|
|
|
def has_user_prompts(user):
|
|
|
|
try:
|
|
|
|
UserPrompt.select().where(UserPrompt.user == user).get()
|
|
|
|
return True
|
|
|
|
except UserPrompt.DoesNotExist:
|
|
|
|
return False
|
|
|
|
|
2016-11-04 21:57:55 +00:00
|
|
|
|
2016-09-08 22:43:50 +00:00
|
|
|
def has_user_prompt(user, prompt_name):
|
|
|
|
prompt_kind = UserPromptKind.get(name=prompt_name)
|
|
|
|
|
|
|
|
try:
|
|
|
|
UserPrompt.get(user=user, kind=prompt_kind)
|
|
|
|
return True
|
|
|
|
except UserPrompt.DoesNotExist:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def create_user_prompt(user, prompt_name):
|
|
|
|
prompt_kind = UserPromptKind.get(name=prompt_name)
|
|
|
|
return UserPrompt.create(user=user, kind=prompt_kind)
|
|
|
|
|
|
|
|
|
|
|
|
def remove_user_prompt(user, prompt_name):
|
|
|
|
prompt_kind = UserPromptKind.get(name=prompt_name)
|
|
|
|
UserPrompt.delete().where(UserPrompt.user == user, UserPrompt.kind == prompt_kind).execute()
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_prompts(user):
|
|
|
|
query = UserPrompt.select().where(UserPrompt.user == user).join(UserPromptKind)
|
|
|
|
return [prompt.kind.name for prompt in query]
|
|
|
|
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def change_username(user_id, new_username):
|
|
|
|
(username_valid, username_issue) = validate_username(new_username)
|
|
|
|
if not username_valid:
|
|
|
|
raise InvalidUsernameException('Invalid username %s: %s' % (new_username, username_issue))
|
|
|
|
|
|
|
|
with db_transaction():
|
|
|
|
# Reload the user for update
|
|
|
|
user = db_for_update(User.select().where(User.id == user_id)).get()
|
|
|
|
|
|
|
|
# Rename the robots
|
2018-03-13 00:30:19 +00:00
|
|
|
for robot in db_for_update(_list_entity_robots(user.username, include_metadata=False)):
|
2015-07-15 21:25:41 +00:00
|
|
|
_, robot_shortname = parse_robot_username(robot.username)
|
|
|
|
new_robot_name = format_robot_username(new_username, robot_shortname)
|
|
|
|
robot.username = new_robot_name
|
|
|
|
robot.save()
|
|
|
|
|
|
|
|
# Rename the user
|
|
|
|
user.username = new_username
|
|
|
|
user.save()
|
2016-09-08 22:43:50 +00:00
|
|
|
|
|
|
|
# Remove any prompts for username.
|
|
|
|
remove_user_prompt(user, 'confirm_username')
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
return user
|
|
|
|
|
|
|
|
|
2015-12-28 18:59:50 +00:00
|
|
|
def change_invoice_email_address(user, invoice_email_address):
|
|
|
|
# Note: We null out the address if it is an empty string.
|
|
|
|
user.invoice_email_address = invoice_email_address or None
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
|
|
|
|
def change_send_invoice_email(user, invoice_email):
|
2015-07-15 21:25:41 +00:00
|
|
|
user.invoice_email = invoice_email
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
|
2017-04-05 18:01:55 +00:00
|
|
|
def _convert_to_s(timespan_string):
|
|
|
|
""" Returns the given timespan string (e.g. `2w` or `45s`) into seconds. """
|
|
|
|
return convert_to_timedelta(timespan_string).total_seconds()
|
|
|
|
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def change_user_tag_expiration(user, tag_expiration_s):
|
2017-04-05 18:01:55 +00:00
|
|
|
""" Changes the tag expiration on the given user/org. Note that the specified expiration must
|
|
|
|
be within the configured TAG_EXPIRATION_OPTIONS or this method will raise a
|
|
|
|
DataModelException.
|
|
|
|
"""
|
|
|
|
allowed_options = [_convert_to_s(o) for o in config.app_config['TAG_EXPIRATION_OPTIONS']]
|
|
|
|
if tag_expiration_s not in allowed_options:
|
|
|
|
raise DataModelException('Invalid tag expiration option')
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
user.removed_tag_expiration_s = tag_expiration_s
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
|
|
|
|
def update_email(user, new_email, auto_verify=False):
|
|
|
|
try:
|
|
|
|
user.email = new_email
|
|
|
|
user.verified = auto_verify
|
|
|
|
user.save()
|
|
|
|
except IntegrityError:
|
|
|
|
raise DataModelException('E-mail address already used')
|
|
|
|
|
|
|
|
|
2017-07-31 15:25:56 +00:00
|
|
|
def update_enabled(user, set_enabled):
|
|
|
|
user.enabled = set_enabled
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
|
2018-03-09 20:55:51 +00:00
|
|
|
def create_robot(robot_shortname, parent, description='', unstructured_metadata=None):
|
2015-07-15 21:25:41 +00:00
|
|
|
(username_valid, username_issue) = validate_username(robot_shortname)
|
|
|
|
if not username_valid:
|
|
|
|
raise InvalidRobotException('The name for the robot \'%s\' is invalid: %s' %
|
|
|
|
(robot_shortname, username_issue))
|
|
|
|
|
|
|
|
username = format_robot_username(parent.username, robot_shortname)
|
|
|
|
|
|
|
|
try:
|
|
|
|
User.get(User.username == username)
|
|
|
|
|
|
|
|
msg = 'Existing robot with name: %s' % username
|
|
|
|
logger.info(msg)
|
|
|
|
raise InvalidRobotException(msg)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
2018-03-09 20:55:51 +00:00
|
|
|
service = LoginService.get(name='quayrobot')
|
2015-07-15 21:25:41 +00:00
|
|
|
try:
|
2018-03-09 20:55:51 +00:00
|
|
|
with db_transaction():
|
|
|
|
created = User.create(username=username, robot=True)
|
|
|
|
password = created.email
|
|
|
|
|
|
|
|
FederatedLogin.create(user=created, service=service, service_ident=password)
|
|
|
|
RobotAccountMetadata.create(robot_account=created, description=description[0:255],
|
|
|
|
unstructured_json=unstructured_metadata or {})
|
|
|
|
return created, password
|
2015-07-15 21:25:41 +00:00
|
|
|
except Exception as ex:
|
|
|
|
raise DataModelException(ex.message)
|
|
|
|
|
|
|
|
|
2018-03-09 20:55:51 +00:00
|
|
|
def get_or_create_robot_metadata(robot):
|
2018-03-27 21:29:55 +00:00
|
|
|
defaults = dict(description='', unstructured_json={})
|
2018-03-13 00:30:19 +00:00
|
|
|
metadata, _ = RobotAccountMetadata.get_or_create(robot_account=robot, defaults=defaults)
|
|
|
|
return metadata
|
|
|
|
|
2018-03-09 20:55:51 +00:00
|
|
|
|
|
|
|
def update_robot_metadata(robot, description='', unstructured_json=None):
|
|
|
|
""" Updates the description and user-specified unstructured metadata associated
|
|
|
|
with a robot account to that specified. """
|
|
|
|
metadata = get_or_create_robot_metadata(robot)
|
|
|
|
metadata.description = description
|
|
|
|
metadata.unstructured_json = unstructured_json or metadata.unstructured_json or {}
|
|
|
|
metadata.save()
|
2018-03-13 00:30:19 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2018-03-09 20:55:51 +00:00
|
|
|
def get_robot_and_metadata(robot_shortname, parent):
|
|
|
|
robot_username = format_robot_username(parent.username, robot_shortname)
|
|
|
|
robot, metadata = lookup_robot_and_metadata(robot_username)
|
|
|
|
return robot, robot.email, metadata
|
|
|
|
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def lookup_robot(robot_username):
|
|
|
|
try:
|
|
|
|
return (User
|
2018-03-09 20:55:51 +00:00
|
|
|
.select(User, FederatedLogin)
|
2015-07-15 21:25:41 +00:00
|
|
|
.join(FederatedLogin)
|
|
|
|
.join(LoginService)
|
|
|
|
.where(LoginService.name == 'quayrobot', User.username == robot_username,
|
|
|
|
User.robot == True)
|
|
|
|
.get())
|
|
|
|
except User.DoesNotExist:
|
|
|
|
raise InvalidRobotException('Could not find robot with username: %s' % robot_username)
|
|
|
|
|
|
|
|
|
2018-03-09 20:55:51 +00:00
|
|
|
def lookup_robot_and_metadata(robot_username):
|
|
|
|
robot = lookup_robot(robot_username)
|
|
|
|
return robot, get_or_create_robot_metadata(robot)
|
|
|
|
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def get_matching_robots(name_prefix, username, limit=10):
|
|
|
|
admined_orgs = (_basequery.get_user_organizations(username)
|
|
|
|
.switch(Team)
|
|
|
|
.join(TeamRole)
|
|
|
|
.where(TeamRole.name == 'admin'))
|
|
|
|
|
|
|
|
prefix_checks = False
|
|
|
|
|
|
|
|
for org in admined_orgs:
|
2017-01-11 19:53:14 +00:00
|
|
|
org_search = prefix_search(User.username, org.username + '+' + name_prefix)
|
2015-10-14 16:18:04 +00:00
|
|
|
prefix_checks = prefix_checks | org_search
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2017-01-11 19:53:14 +00:00
|
|
|
user_search = prefix_search(User.username, username + '+' + name_prefix)
|
2015-10-14 16:18:04 +00:00
|
|
|
prefix_checks = prefix_checks | user_search
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
return User.select().where(prefix_checks).limit(limit)
|
|
|
|
|
|
|
|
|
|
|
|
def verify_robot(robot_username, password):
|
|
|
|
result = parse_robot_username(robot_username)
|
|
|
|
if result is None:
|
|
|
|
raise InvalidRobotException('%s is an invalid robot name' % robot_username)
|
|
|
|
|
|
|
|
# Find the matching robot.
|
|
|
|
query = (User
|
|
|
|
.select()
|
|
|
|
.join(FederatedLogin)
|
|
|
|
.join(LoginService)
|
|
|
|
.where(FederatedLogin.service_ident == password, LoginService.name == 'quayrobot',
|
|
|
|
User.username == robot_username))
|
|
|
|
|
|
|
|
try:
|
|
|
|
robot = query.get()
|
|
|
|
except User.DoesNotExist:
|
|
|
|
msg = ('Could not find robot with username: %s and supplied password.' %
|
|
|
|
robot_username)
|
|
|
|
raise InvalidRobotException(msg)
|
|
|
|
|
|
|
|
# Find the owner user and ensure it is not disabled.
|
|
|
|
try:
|
|
|
|
owner = User.get(User.username == result[0])
|
|
|
|
except User.DoesNotExist:
|
|
|
|
raise InvalidRobotException('Robot %s owner does not exist' % robot_username)
|
|
|
|
|
|
|
|
if not owner.enabled:
|
|
|
|
raise InvalidRobotException('This user has been disabled. Please contact your administrator.')
|
|
|
|
|
2018-03-13 00:30:19 +00:00
|
|
|
# Mark that the robot was accessed.
|
|
|
|
_basequery.update_last_accessed(robot)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
return robot
|
|
|
|
|
|
|
|
def regenerate_robot_token(robot_shortname, parent):
|
|
|
|
robot_username = format_robot_username(parent.username, robot_shortname)
|
|
|
|
|
2018-03-09 20:55:51 +00:00
|
|
|
robot, metadata = lookup_robot_and_metadata(robot_username)
|
2015-07-15 21:25:41 +00:00
|
|
|
password = random_string_generator(length=64)()
|
|
|
|
robot.email = password
|
2015-12-31 18:57:43 +00:00
|
|
|
robot.uuid = str(uuid4())
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
service = LoginService.get(name='quayrobot')
|
|
|
|
login = FederatedLogin.get(FederatedLogin.user == robot, FederatedLogin.service == service)
|
|
|
|
login.service_ident = password
|
|
|
|
|
|
|
|
login.save()
|
|
|
|
robot.save()
|
|
|
|
|
2018-03-09 20:55:51 +00:00
|
|
|
return robot, password, metadata
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
def delete_robot(robot_username):
|
|
|
|
try:
|
|
|
|
robot = User.get(username=robot_username, robot=True)
|
|
|
|
robot.delete_instance(recursive=True, delete_nullable=True)
|
|
|
|
|
|
|
|
except User.DoesNotExist:
|
|
|
|
raise InvalidRobotException('Could not find robot with username: %s' %
|
|
|
|
robot_username)
|
|
|
|
|
|
|
|
|
2016-09-27 14:52:34 +00:00
|
|
|
def list_namespace_robots(namespace):
|
|
|
|
""" Returns all the robots found under the given namespace. """
|
|
|
|
return _list_entity_robots(namespace)
|
|
|
|
|
|
|
|
|
2018-03-13 00:30:19 +00:00
|
|
|
def _list_entity_robots(entity_name, include_metadata=True):
|
2015-07-15 21:25:41 +00:00
|
|
|
""" Return the list of robots for the specified entity. This MUST return a query, not a
|
|
|
|
materialized list so that callers can use db_for_update.
|
2018-03-13 00:30:19 +00:00
|
|
|
"""
|
|
|
|
if include_metadata:
|
2018-04-06 17:48:01 +00:00
|
|
|
query = (User
|
|
|
|
.select(User, FederatedLogin, RobotAccountMetadata)
|
|
|
|
.join(FederatedLogin)
|
|
|
|
.switch(User)
|
|
|
|
.join(RobotAccountMetadata, JOIN.LEFT_OUTER)
|
|
|
|
.where(User.robot == True, User.username ** (entity_name + '+%')))
|
|
|
|
else:
|
|
|
|
query = (User
|
|
|
|
.select(User, FederatedLogin)
|
|
|
|
.join(FederatedLogin)
|
|
|
|
.where(User.robot == True, User.username ** (entity_name + '+%')))
|
2018-03-13 00:30:19 +00:00
|
|
|
|
|
|
|
return query
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
|
2018-05-14 20:27:16 +00:00
|
|
|
def list_entity_robot_permission_teams(entity_name, limit=None, include_permissions=False):
|
2015-07-15 21:25:41 +00:00
|
|
|
query = (_list_entity_robots(entity_name))
|
|
|
|
|
2018-03-13 00:30:19 +00:00
|
|
|
fields = [User.username, User.creation_date, User.last_accessed, FederatedLogin.service_ident,
|
2018-03-09 20:55:51 +00:00
|
|
|
RobotAccountMetadata.description, RobotAccountMetadata.unstructured_json]
|
2015-07-15 21:25:41 +00:00
|
|
|
if include_permissions:
|
|
|
|
query = (query
|
2018-04-06 17:48:01 +00:00
|
|
|
.join(RepositoryPermission, JOIN.LEFT_OUTER,
|
2015-07-15 21:25:41 +00:00
|
|
|
on=(RepositoryPermission.user == FederatedLogin.user))
|
2018-04-06 17:48:01 +00:00
|
|
|
.join(Repository, JOIN.LEFT_OUTER)
|
2015-07-15 21:25:41 +00:00
|
|
|
.switch(User)
|
2018-04-06 17:48:01 +00:00
|
|
|
.join(TeamMember, JOIN.LEFT_OUTER)
|
|
|
|
.join(Team, JOIN.LEFT_OUTER))
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
fields.append(Repository.name)
|
|
|
|
fields.append(Team.name)
|
|
|
|
|
2018-05-14 20:27:16 +00:00
|
|
|
query = query.limit(limit).order_by(User.last_accessed.desc())
|
2015-07-15 21:25:41 +00:00
|
|
|
return TupleSelector(query, fields)
|
|
|
|
|
|
|
|
|
2018-02-02 22:56:50 +00:00
|
|
|
def update_user_metadata(user, metadata=None):
|
2016-11-04 21:57:55 +00:00
|
|
|
""" Updates the metadata associated with the user, including his/her name and company. """
|
2018-02-02 22:56:50 +00:00
|
|
|
metadata = metadata if metadata is not None else {}
|
|
|
|
|
2016-11-04 21:57:55 +00:00
|
|
|
with db_transaction():
|
2018-06-26 20:07:59 +00:00
|
|
|
if 'given_name' in metadata:
|
|
|
|
user.given_name = metadata['given_name']
|
|
|
|
|
|
|
|
if 'family_name' in metadata:
|
|
|
|
user.family_name = metadata['family_name']
|
|
|
|
|
|
|
|
if 'company' in metadata:
|
|
|
|
user.company = metadata['company']
|
|
|
|
|
|
|
|
if 'location' in metadata:
|
|
|
|
user.location = metadata['location']
|
|
|
|
|
2016-11-04 21:57:55 +00:00
|
|
|
user.save()
|
|
|
|
|
|
|
|
# Remove any prompts associated with the user's metadata being needed.
|
|
|
|
remove_user_prompt(user, UserPromptTypes.ENTER_NAME)
|
|
|
|
remove_user_prompt(user, UserPromptTypes.ENTER_COMPANY)
|
|
|
|
|
|
|
|
|
2017-02-09 20:52:44 +00:00
|
|
|
def _get_login_service(service_id):
|
|
|
|
try:
|
|
|
|
return LoginService.get(LoginService.name == service_id)
|
|
|
|
except LoginService.DoesNotExist:
|
|
|
|
return LoginService.create(name=service_id)
|
|
|
|
|
|
|
|
|
2017-01-23 22:53:34 +00:00
|
|
|
def create_federated_user(username, email, service_id, service_ident,
|
2016-09-08 22:43:50 +00:00
|
|
|
set_password_notification, metadata={},
|
2018-06-01 17:24:29 +00:00
|
|
|
email_required=True, confirm_username=True,
|
|
|
|
prompts=tuple()):
|
2016-11-04 21:57:55 +00:00
|
|
|
prompts = set(prompts)
|
2018-06-01 17:24:29 +00:00
|
|
|
|
|
|
|
if confirm_username:
|
|
|
|
prompts.add(UserPromptTypes.CONFIRM_USERNAME)
|
2016-11-04 21:57:55 +00:00
|
|
|
|
|
|
|
new_user = create_user_noverify(username, email, email_required=email_required, prompts=prompts)
|
2015-07-15 21:25:41 +00:00
|
|
|
new_user.verified = True
|
|
|
|
new_user.save()
|
|
|
|
|
2017-02-09 20:52:44 +00:00
|
|
|
FederatedLogin.create(user=new_user, service=_get_login_service(service_id),
|
2016-05-23 19:08:51 +00:00
|
|
|
service_ident=service_ident,
|
2015-07-15 21:25:41 +00:00
|
|
|
metadata_json=json.dumps(metadata))
|
|
|
|
|
|
|
|
if set_password_notification:
|
|
|
|
notification.create_notification('password_required', new_user)
|
|
|
|
|
|
|
|
return new_user
|
|
|
|
|
|
|
|
|
2017-02-09 20:52:44 +00:00
|
|
|
def attach_federated_login(user, service_id, service_ident, metadata=None):
|
|
|
|
service = _get_login_service(service_id)
|
2016-05-23 19:08:51 +00:00
|
|
|
FederatedLogin.create(user=user, service=service, service_ident=service_ident,
|
2017-02-09 20:52:44 +00:00
|
|
|
metadata_json=json.dumps(metadata or {}))
|
2015-07-15 21:25:41 +00:00
|
|
|
return user
|
|
|
|
|
|
|
|
|
2017-01-23 22:53:34 +00:00
|
|
|
def verify_federated_login(service_id, service_ident):
|
2015-07-15 21:25:41 +00:00
|
|
|
try:
|
|
|
|
found = (FederatedLogin
|
|
|
|
.select(FederatedLogin, User)
|
|
|
|
.join(LoginService)
|
|
|
|
.switch(FederatedLogin).join(User)
|
2017-01-23 22:53:34 +00:00
|
|
|
.where(FederatedLogin.service_ident == service_ident, LoginService.name == service_id)
|
2015-07-15 21:25:41 +00:00
|
|
|
.get())
|
2018-03-13 00:30:19 +00:00
|
|
|
|
|
|
|
# Mark that the user was accessed.
|
|
|
|
_basequery.update_last_accessed(found.user)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
return found.user
|
|
|
|
except FederatedLogin.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def list_federated_logins(user):
|
|
|
|
selected = FederatedLogin.select(FederatedLogin.service_ident,
|
|
|
|
LoginService.name, FederatedLogin.metadata_json)
|
|
|
|
joined = selected.join(LoginService)
|
|
|
|
return joined.where(LoginService.name != 'quayrobot',
|
|
|
|
FederatedLogin.user == user)
|
|
|
|
|
|
|
|
|
|
|
|
def lookup_federated_login(user, service_name):
|
|
|
|
try:
|
|
|
|
return list_federated_logins(user).where(LoginService.name == service_name).get()
|
|
|
|
except FederatedLogin.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
2016-01-19 19:49:40 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def create_confirm_email_code(user, new_email=None):
|
|
|
|
if new_email:
|
|
|
|
if not validate_email(new_email):
|
|
|
|
raise InvalidEmailAddressException('Invalid email address: %s' %
|
|
|
|
new_email)
|
|
|
|
|
|
|
|
code = EmailConfirmation.create(user=user, email_confirm=True,
|
|
|
|
new_email=new_email)
|
|
|
|
return code
|
|
|
|
|
|
|
|
|
|
|
|
def confirm_user_email(code):
|
|
|
|
try:
|
|
|
|
code = EmailConfirmation.get(EmailConfirmation.code == code,
|
|
|
|
EmailConfirmation.email_confirm == True)
|
|
|
|
except EmailConfirmation.DoesNotExist:
|
2016-12-07 22:13:17 +00:00
|
|
|
raise DataModelException('Invalid email confirmation code')
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
user = code.user
|
|
|
|
user.verified = True
|
|
|
|
|
|
|
|
old_email = None
|
|
|
|
new_email = code.new_email
|
|
|
|
if new_email and new_email != old_email:
|
|
|
|
if find_user_by_email(new_email):
|
2016-12-07 22:13:17 +00:00
|
|
|
raise DataModelException('E-mail address already used')
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
old_email = user.email
|
|
|
|
user.email = new_email
|
|
|
|
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
code.delete_instance()
|
|
|
|
|
|
|
|
return user, new_email, old_email
|
|
|
|
|
|
|
|
|
|
|
|
def create_reset_password_email_code(email):
|
|
|
|
try:
|
|
|
|
user = User.get(User.email == email)
|
|
|
|
except User.DoesNotExist:
|
2016-12-07 22:13:17 +00:00
|
|
|
raise InvalidEmailAddressException('Email address was not found')
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
if user.organization:
|
2016-12-07 22:13:17 +00:00
|
|
|
raise InvalidEmailAddressException('Organizations can not have passwords')
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
code = EmailConfirmation.create(user=user, pw_reset=True)
|
|
|
|
return code
|
|
|
|
|
|
|
|
|
|
|
|
def validate_reset_code(code):
|
2017-12-06 22:06:03 +00:00
|
|
|
# Find the reset code.
|
2015-07-15 21:25:41 +00:00
|
|
|
try:
|
|
|
|
code = EmailConfirmation.get(EmailConfirmation.code == code,
|
|
|
|
EmailConfirmation.pw_reset == True)
|
|
|
|
except EmailConfirmation.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
2017-12-06 22:06:03 +00:00
|
|
|
# Make sure the code is not expired.
|
|
|
|
max_lifetime_duration = convert_to_timedelta(config.app_config['USER_RECOVERY_TOKEN_LIFETIME'])
|
|
|
|
if code.created + max_lifetime_duration < datetime.now():
|
|
|
|
code.delete_instance()
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Verify the user and return the code.
|
2015-07-15 21:25:41 +00:00
|
|
|
user = code.user
|
2016-04-08 17:38:16 +00:00
|
|
|
if not user.verified:
|
|
|
|
user.verified = True
|
|
|
|
user.save()
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
code.delete_instance()
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
def find_user_by_email(email):
|
|
|
|
try:
|
|
|
|
return User.get(User.email == email)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_nonrobot_user(username):
|
|
|
|
try:
|
|
|
|
return User.get(User.username == username, User.organization == False, User.robot == False)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_user(username):
|
|
|
|
try:
|
|
|
|
return User.get(User.username == username, User.organization == False)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_namespace_user(username):
|
|
|
|
try:
|
|
|
|
return User.get(User.username == username)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_or_org(username):
|
|
|
|
try:
|
|
|
|
return User.get(User.username == username, User.robot == False)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_by_id(user_db_id):
|
|
|
|
try:
|
|
|
|
return User.get(User.id == user_db_id, User.organization == False)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2015-06-28 10:29:22 +00:00
|
|
|
def get_namespace_user_by_user_id(namespace_user_db_id):
|
|
|
|
try:
|
|
|
|
return User.get(User.id == namespace_user_db_id, User.robot == False)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
raise InvalidUsernameException('User with id does not exist: %s' % namespace_user_db_id)
|
|
|
|
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def get_namespace_by_user_id(namespace_user_db_id):
|
|
|
|
try:
|
|
|
|
return User.get(User.id == namespace_user_db_id, User.robot == False).username
|
|
|
|
except User.DoesNotExist:
|
|
|
|
raise InvalidUsernameException('User with id does not exist: %s' % namespace_user_db_id)
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_by_uuid(user_uuid):
|
|
|
|
try:
|
|
|
|
return User.get(User.uuid == user_uuid, User.organization == False)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_or_org_by_customer_id(customer_id):
|
|
|
|
try:
|
|
|
|
return User.get(User.stripe_id == customer_id)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
2017-12-07 16:39:27 +00:00
|
|
|
def invalidate_all_sessions(user):
|
|
|
|
""" Invalidates all existing user sessions by rotating the user's UUID. """
|
|
|
|
if not user:
|
|
|
|
return
|
|
|
|
|
|
|
|
user.uuid = str(uuid4())
|
|
|
|
user.save()
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
def get_matching_user_namespaces(namespace_prefix, username, limit=10):
|
2018-06-19 14:51:30 +00:00
|
|
|
namespace_user = get_namespace_user(username)
|
|
|
|
namespace_user_id = namespace_user.id if namespace_user is not None else None
|
|
|
|
|
2017-01-11 19:53:14 +00:00
|
|
|
namespace_search = prefix_search(Namespace.username, namespace_prefix)
|
2015-07-15 21:25:41 +00:00
|
|
|
base_query = (Namespace
|
|
|
|
.select()
|
|
|
|
.distinct()
|
|
|
|
.join(Repository, on=(Repository.namespace_user == Namespace.id))
|
2018-04-06 17:48:01 +00:00
|
|
|
.join(RepositoryPermission, JOIN.LEFT_OUTER)
|
2015-10-14 16:18:04 +00:00
|
|
|
.where(namespace_search))
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2018-06-19 14:51:30 +00:00
|
|
|
return _basequery.filter_to_repos_for_user(base_query, namespace_user_id).limit(limit)
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2017-09-12 19:55:37 +00:00
|
|
|
def get_matching_users(username_prefix, robot_namespace=None, organization=None, limit=20,
|
|
|
|
exact_matches_only=False):
|
2017-01-11 19:53:14 +00:00
|
|
|
user_search = prefix_search(User.username, username_prefix)
|
2017-09-12 19:55:37 +00:00
|
|
|
if exact_matches_only:
|
|
|
|
user_search = (User.username == username_prefix)
|
|
|
|
|
2016-03-07 15:07:41 +00:00
|
|
|
direct_user_query = (user_search & (User.organization == False) & (User.robot == False))
|
|
|
|
|
|
|
|
if robot_namespace:
|
|
|
|
robot_prefix = format_robot_username(robot_namespace, username_prefix)
|
2017-01-11 19:53:14 +00:00
|
|
|
robot_search = prefix_search(User.username, robot_prefix)
|
2016-12-16 20:04:30 +00:00
|
|
|
direct_user_query = ((robot_search & (User.robot == True)) | direct_user_query)
|
2016-03-07 15:07:41 +00:00
|
|
|
|
|
|
|
query = (User
|
2016-11-02 20:22:35 +00:00
|
|
|
.select(User.id, User.username, User.email, User.robot)
|
2016-11-29 16:36:53 +00:00
|
|
|
.group_by(User.id, User.username, User.email, User.robot)
|
2016-03-07 15:07:41 +00:00
|
|
|
.where(direct_user_query))
|
|
|
|
|
|
|
|
if organization:
|
|
|
|
query = (query
|
2016-10-27 19:31:32 +00:00
|
|
|
.select(User.id, User.username, User.email, User.robot, fn.Sum(Team.id))
|
2018-04-06 17:48:01 +00:00
|
|
|
.join(TeamMember, JOIN.LEFT_OUTER)
|
|
|
|
.join(Team, JOIN.LEFT_OUTER, on=((Team.id == TeamMember.team) &
|
2016-12-16 20:04:30 +00:00
|
|
|
(Team.organization == organization)))
|
|
|
|
.order_by(User.robot.desc()))
|
2016-03-07 15:07:41 +00:00
|
|
|
|
|
|
|
class MatchingUserResult(object):
|
|
|
|
def __init__(self, *args):
|
2016-10-27 19:31:32 +00:00
|
|
|
self.id = args[0]
|
|
|
|
self.username = args[1]
|
|
|
|
self.email = args[2]
|
|
|
|
self.robot = args[3]
|
2016-03-07 15:07:41 +00:00
|
|
|
|
|
|
|
if organization:
|
|
|
|
self.is_org_member = (args[3] != None)
|
|
|
|
else:
|
|
|
|
self.is_org_member = None
|
|
|
|
|
2016-10-27 19:31:32 +00:00
|
|
|
return (MatchingUserResult(*args) for args in query.tuples().limit(limit))
|
2016-03-07 15:07:41 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
def verify_user(username_or_email, password):
|
2018-01-04 20:20:25 +00:00
|
|
|
""" Verifies that the given username/email + password pair is valid. If the username or e-mail
|
|
|
|
address is invalid, returns None. If the password specified does not match for the given user,
|
|
|
|
either returns None or raises TooManyLoginAttemptsException if there have been too many
|
|
|
|
invalid login attempts. Returns the user object if the login was valid.
|
|
|
|
"""
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
# Make sure we didn't get any unicode for the username.
|
|
|
|
try:
|
|
|
|
str(username_or_email)
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
2018-01-04 20:20:25 +00:00
|
|
|
# Fetch the user with the matching username or e-mail address.
|
2015-07-15 21:25:41 +00:00
|
|
|
try:
|
2018-01-04 20:20:25 +00:00
|
|
|
fetched = User.get((User.username == username_or_email) | (User.email == username_or_email))
|
2015-07-15 21:25:41 +00:00
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
2018-01-04 20:20:25 +00:00
|
|
|
# If the user has any invalid login attempts, check to see if we are within the exponential
|
|
|
|
# backoff window for the user. If so, we raise an exception indicating that the user cannot
|
|
|
|
# login.
|
2015-07-15 21:25:41 +00:00
|
|
|
now = datetime.utcnow()
|
|
|
|
if fetched.invalid_login_attempts > 0:
|
|
|
|
can_retry_at = exponential_backoff(fetched.invalid_login_attempts, EXPONENTIAL_BACKOFF_SCALE,
|
|
|
|
fetched.last_invalid_login)
|
|
|
|
|
|
|
|
if can_retry_at > now:
|
|
|
|
retry_after = can_retry_at - now
|
|
|
|
raise TooManyLoginAttemptsException('Too many login attempts.', retry_after.total_seconds())
|
|
|
|
|
2018-01-04 20:20:25 +00:00
|
|
|
# Hash the given password and compare it to the specified password.
|
2015-07-15 21:25:41 +00:00
|
|
|
if (fetched.password_hash and
|
|
|
|
hash_password(password, fetched.password_hash) == fetched.password_hash):
|
2018-01-04 20:20:25 +00:00
|
|
|
|
|
|
|
# If the user previously had any invalid login attempts, clear them out now.
|
2015-07-15 21:25:41 +00:00
|
|
|
if fetched.invalid_login_attempts > 0:
|
2018-01-04 20:20:25 +00:00
|
|
|
(User
|
|
|
|
.update(invalid_login_attempts=0)
|
|
|
|
.where(User.id == fetched.id)
|
|
|
|
.execute())
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2018-03-13 00:30:19 +00:00
|
|
|
# Mark that the user was accessed.
|
|
|
|
_basequery.update_last_accessed(fetched)
|
|
|
|
|
2018-01-04 20:20:25 +00:00
|
|
|
# Return the valid user.
|
2015-07-15 21:25:41 +00:00
|
|
|
return fetched
|
|
|
|
|
2018-01-04 20:20:25 +00:00
|
|
|
# Otherwise, update the user's invalid login attempts.
|
|
|
|
(User
|
|
|
|
.update(invalid_login_attempts=User.invalid_login_attempts+1, last_invalid_login=now)
|
|
|
|
.where(User.id == fetched.id)
|
|
|
|
.execute())
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
# We weren't able to authorize the user
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_repo_users(namespace_name, repository_name):
|
|
|
|
return (RepositoryPermission
|
2018-04-06 17:48:01 +00:00
|
|
|
.select(User, Role, RepositoryPermission)
|
2015-07-15 21:25:41 +00:00
|
|
|
.join(User)
|
|
|
|
.switch(RepositoryPermission)
|
|
|
|
.join(Role)
|
|
|
|
.switch(RepositoryPermission)
|
|
|
|
.join(Repository)
|
|
|
|
.join(Namespace, on=(Repository.namespace_user == Namespace.id))
|
|
|
|
.where(Namespace.username == namespace_name, Repository.name == repository_name))
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_repo_users_transitive_via_teams(namespace_name, repository_name):
|
|
|
|
return (User
|
|
|
|
.select()
|
|
|
|
.distinct()
|
|
|
|
.join(TeamMember)
|
|
|
|
.join(Team)
|
|
|
|
.join(RepositoryPermission)
|
|
|
|
.join(Repository)
|
|
|
|
.join(Namespace, on=(Repository.namespace_user == Namespace.id))
|
|
|
|
.where(Namespace.username == namespace_name, Repository.name == repository_name))
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_repo_users_transitive(namespace_name, repository_name):
|
|
|
|
# Load the users found via teams and directly via permissions.
|
|
|
|
via_teams = get_all_repo_users_transitive_via_teams(namespace_name, repository_name)
|
|
|
|
directly = [perm.user for perm in get_all_repo_users(namespace_name, repository_name)]
|
|
|
|
|
|
|
|
# Filter duplicates.
|
|
|
|
user_set = set()
|
|
|
|
|
|
|
|
def check_add(u):
|
|
|
|
if u.username in user_set:
|
|
|
|
return False
|
|
|
|
|
|
|
|
user_set.add(u.username)
|
|
|
|
return True
|
|
|
|
|
|
|
|
return [user for user in list(directly) + list(via_teams) if check_add(user)]
|
|
|
|
|
|
|
|
|
|
|
|
def get_private_repo_count(username):
|
|
|
|
return (Repository
|
|
|
|
.select()
|
|
|
|
.join(Visibility)
|
|
|
|
.switch(Repository)
|
|
|
|
.join(Namespace, on=(Repository.namespace_user == Namespace.id))
|
|
|
|
.where(Namespace.username == username, Visibility.name == 'private')
|
|
|
|
.count())
|
|
|
|
|
|
|
|
|
2018-05-15 13:01:20 +00:00
|
|
|
def get_active_users(disabled=True, deleted=False):
|
|
|
|
query = (User
|
|
|
|
.select()
|
|
|
|
.where(User.organization == False, User.robot == False))
|
|
|
|
|
2017-12-22 21:45:49 +00:00
|
|
|
if not disabled:
|
|
|
|
query = query.where(User.enabled == True)
|
2018-05-15 13:01:20 +00:00
|
|
|
|
|
|
|
if not deleted:
|
|
|
|
query = query.where(User.id.not_in(DeletedNamespace.select(DeletedNamespace.namespace)))
|
|
|
|
|
2017-12-22 21:45:49 +00:00
|
|
|
return query
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_active_user_count():
|
|
|
|
return get_active_users().count()
|
|
|
|
|
|
|
|
|
2016-09-12 20:19:19 +00:00
|
|
|
def get_robot_count():
|
|
|
|
return User.select().where(User.robot == True).count()
|
|
|
|
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def detach_external_login(user, service_name):
|
|
|
|
try:
|
|
|
|
service = LoginService.get(name=service_name)
|
|
|
|
except LoginService.DoesNotExist:
|
|
|
|
return
|
|
|
|
|
|
|
|
FederatedLogin.delete().where(FederatedLogin.user == user,
|
|
|
|
FederatedLogin.service == service).execute()
|
|
|
|
|
|
|
|
|
2016-08-09 21:58:33 +00:00
|
|
|
def get_solely_admined_organizations(user_obj):
|
|
|
|
""" Returns the organizations admined solely by the given user. """
|
|
|
|
orgs = (User.select()
|
|
|
|
.where(User.organization == True)
|
|
|
|
.join(Team)
|
|
|
|
.join(TeamRole)
|
|
|
|
.where(TeamRole.name == 'admin')
|
|
|
|
.switch(Team)
|
|
|
|
.join(TeamMember)
|
|
|
|
.where(TeamMember.user == user_obj)
|
|
|
|
.distinct())
|
|
|
|
|
|
|
|
# Filter to organizations where the user is the sole admin.
|
|
|
|
solely_admined = []
|
|
|
|
for org in orgs:
|
|
|
|
admin_user_count = (TeamMember.select()
|
|
|
|
.join(Team)
|
|
|
|
.join(TeamRole)
|
|
|
|
.where(Team.organization == org, TeamRole.name == 'admin')
|
|
|
|
.switch(TeamMember)
|
|
|
|
.join(User)
|
|
|
|
.where(User.robot == False)
|
|
|
|
.distinct()
|
|
|
|
.count())
|
|
|
|
|
|
|
|
if admin_user_count == 1:
|
|
|
|
solely_admined.append(org)
|
|
|
|
|
|
|
|
return solely_admined
|
|
|
|
|
|
|
|
|
2018-02-23 21:45:16 +00:00
|
|
|
def mark_namespace_for_deletion(user, queues, namespace_gc_queue, force=False):
|
|
|
|
""" Marks a namespace (as referenced by the given user) for deletion. A queue item will be added
|
|
|
|
to delete the namespace's repositories and storage, while the namespace itself will be
|
|
|
|
renamed, disabled, and delinked from other tables.
|
|
|
|
"""
|
|
|
|
if not user.enabled:
|
|
|
|
return None
|
|
|
|
|
2016-08-09 21:58:33 +00:00
|
|
|
if not force and not user.organization:
|
|
|
|
# Ensure that the user is not the sole admin for any organizations. If so, then the user
|
|
|
|
# cannot be deleted before those organizations are deleted or reassigned.
|
|
|
|
organizations = get_solely_admined_organizations(user)
|
|
|
|
if len(organizations) > 0:
|
|
|
|
message = 'Cannot delete %s as you are the only admin for organizations: ' % user.username
|
|
|
|
for index, org in enumerate(organizations):
|
|
|
|
if index > 0:
|
|
|
|
message = message + ', '
|
|
|
|
|
|
|
|
message = message + org.username
|
|
|
|
|
|
|
|
raise DataModelException(message)
|
|
|
|
|
|
|
|
# Delete all queue items for the user.
|
|
|
|
for queue in queues:
|
|
|
|
queue.delete_namespaced_items(user.username)
|
|
|
|
|
2018-02-23 21:45:16 +00:00
|
|
|
# Delete non-repository related items. This operation is very quick, so we can do so here.
|
|
|
|
_delete_user_linked_data(user)
|
|
|
|
|
|
|
|
with db_transaction():
|
|
|
|
original_username = user.username
|
|
|
|
user = db_for_update(User.select().where(User.id == user.id)).get()
|
|
|
|
|
|
|
|
# Mark the namespace as deleted and ready for GC.
|
|
|
|
try:
|
|
|
|
marker = DeletedNamespace.create(namespace=user,
|
|
|
|
original_username=original_username,
|
|
|
|
original_email=user.email)
|
|
|
|
except IntegrityError:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Disable the namespace itself, and replace its various unique fields with UUIDs.
|
|
|
|
user.enabled = False
|
|
|
|
user.username = str(uuid4())
|
|
|
|
user.email = str(uuid4())
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
# Add a queueitem to delete the namespace.
|
|
|
|
marker.queue_id = namespace_gc_queue.put([str(user.id)], json.dumps({
|
|
|
|
'marker_id': marker.id,
|
|
|
|
'original_username': original_username,
|
|
|
|
}))
|
|
|
|
marker.save()
|
|
|
|
return marker.id
|
|
|
|
|
|
|
|
|
|
|
|
def delete_namespace_via_marker(marker_id, queues):
|
|
|
|
""" Deletes a namespace referenced by the given DeletedNamespace marker ID. """
|
|
|
|
try:
|
|
|
|
marker = DeletedNamespace.get(id=marker_id)
|
|
|
|
except DeletedNamespace.DoesNotExist:
|
|
|
|
return
|
|
|
|
|
|
|
|
delete_user(marker.namespace, queues)
|
|
|
|
|
|
|
|
|
|
|
|
def delete_user(user, queues):
|
|
|
|
""" Deletes a user/organization/robot. Should *not* be called by any user-facing API. Instead,
|
|
|
|
mark_namespace_for_deletion should be used, and the queue should call this method.
|
|
|
|
"""
|
|
|
|
# Delete all queue items for the user.
|
|
|
|
for queue in queues:
|
|
|
|
queue.delete_namespaced_items(user.username)
|
|
|
|
|
2015-11-08 18:10:01 +00:00
|
|
|
# Delete any repositories under the user's namespace.
|
|
|
|
for repo in list(Repository.select().where(Repository.namespace_user == user)):
|
|
|
|
repository.purge_repository(user.username, repo.name)
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2018-02-23 21:45:16 +00:00
|
|
|
# Delete non-repository related items.
|
|
|
|
_delete_user_linked_data(user)
|
|
|
|
|
|
|
|
# Delete the user itself.
|
|
|
|
user.delete_instance(recursive=True, delete_nullable=True)
|
|
|
|
|
|
|
|
|
|
|
|
def _delete_user_linked_data(user):
|
2016-08-09 21:58:33 +00:00
|
|
|
if user.organization:
|
|
|
|
# Delete the organization's teams.
|
2018-05-09 11:52:28 +00:00
|
|
|
with db_transaction():
|
|
|
|
for team in Team.select().where(Team.organization == user):
|
|
|
|
team.delete_instance(recursive=True)
|
2016-08-09 21:58:33 +00:00
|
|
|
|
|
|
|
# Delete any OAuth approvals and tokens associated with the user.
|
2018-05-09 11:52:28 +00:00
|
|
|
with db_transaction():
|
|
|
|
for app in OAuthApplication.select().where(OAuthApplication.organization == user):
|
|
|
|
app.delete_instance(recursive=True)
|
2016-08-09 21:58:33 +00:00
|
|
|
else:
|
|
|
|
# Remove the user from any teams in which they are a member.
|
|
|
|
TeamMember.delete().where(TeamMember.user == user).execute()
|
|
|
|
|
|
|
|
# Delete any repository buildtriggers where the user is the connected user.
|
2018-05-09 11:52:28 +00:00
|
|
|
with db_transaction():
|
|
|
|
triggers = RepositoryBuildTrigger.select().where(RepositoryBuildTrigger.connected_user == user)
|
|
|
|
for trigger in triggers:
|
|
|
|
trigger.delete_instance(recursive=True, delete_nullable=False)
|
|
|
|
|
|
|
|
# Delete any robots owned by this user.
|
|
|
|
with db_transaction():
|
|
|
|
robots = list(list_namespace_robots(user.username))
|
|
|
|
for robot in robots:
|
|
|
|
robot.delete_instance(recursive=True, delete_nullable=True)
|
2016-08-09 21:58:33 +00:00
|
|
|
|
|
|
|
# Null out any service key approvals. We technically lose information here, but its better than
|
|
|
|
# falling and only occurs if a superuser is being deleted.
|
|
|
|
ServiceKeyApproval.update(approver=None).where(ServiceKeyApproval.approver == user).execute()
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
def get_pull_credentials(robotname):
|
|
|
|
try:
|
|
|
|
robot = lookup_robot(robotname)
|
|
|
|
except InvalidRobotException:
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
login_info = FederatedLogin.get(user=robot)
|
|
|
|
except FederatedLogin.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return {
|
2018-03-13 00:30:19 +00:00
|
|
|
'username': robot.username,
|
|
|
|
'password': login_info.service_ident,
|
|
|
|
'registry': '%s://%s/v1/' % (config.app_config['PREFERRED_URL_SCHEME'],
|
|
|
|
config.app_config['SERVER_HOSTNAME']),
|
2015-07-15 21:25:41 +00:00
|
|
|
}
|
2015-06-28 10:29:22 +00:00
|
|
|
|
|
|
|
def get_region_locations(user):
|
|
|
|
""" Returns the locations defined as preferred storage for the given user. """
|
|
|
|
query = UserRegion.select().join(ImageStorageLocation).where(UserRegion.user == user)
|
|
|
|
return set([region.location.name for region in query])
|
2016-10-27 19:31:32 +00:00
|
|
|
|
|
|
|
def get_federated_logins(user_ids, service_name):
|
|
|
|
""" Returns all federated logins for the given user ids under the given external service. """
|
|
|
|
if not user_ids:
|
|
|
|
return []
|
|
|
|
|
|
|
|
return (FederatedLogin
|
|
|
|
.select()
|
|
|
|
.join(User)
|
|
|
|
.switch(FederatedLogin)
|
|
|
|
.join(LoginService)
|
|
|
|
.where(FederatedLogin.user << user_ids,
|
|
|
|
LoginService.name == service_name))
|
2017-01-27 16:22:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LoginWrappedDBUser(UserMixin):
|
|
|
|
def __init__(self, user_uuid, db_user=None):
|
|
|
|
self._uuid = user_uuid
|
|
|
|
self._db_user = db_user
|
|
|
|
|
|
|
|
def db_user(self):
|
|
|
|
if not self._db_user:
|
|
|
|
self._db_user = get_user_by_uuid(self._uuid)
|
|
|
|
return self._db_user
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_authenticated(self):
|
|
|
|
return self.db_user() is not None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_active(self):
|
2017-07-20 19:40:14 +00:00
|
|
|
return self.db_user() and self.db_user().verified
|
2017-01-27 16:22:40 +00:00
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return unicode(self._uuid)
|