2015-05-14 20:47:38 +00:00
|
|
|
""" Manage the current user. """
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
import logging
|
2014-03-19 19:39:44 +00:00
|
|
|
import json
|
2016-12-17 07:13:36 +00:00
|
|
|
import recaptcha2
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2015-04-02 20:34:41 +00:00
|
|
|
from flask import request, abort
|
2016-09-29 00:17:14 +00:00
|
|
|
from flask_login import logout_user
|
|
|
|
from flask_principal import identity_changed, AnonymousIdentity
|
2014-12-11 20:06:30 +00:00
|
|
|
from peewee import IntegrityError
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
import features
|
|
|
|
|
2017-01-24 00:06:19 +00:00
|
|
|
from app import (app, billing as stripe, authentication, avatar, user_analytics, all_queues,
|
|
|
|
oauth_login)
|
|
|
|
|
2016-09-29 00:17:14 +00:00
|
|
|
from auth import scopes
|
|
|
|
from auth.auth_context import get_authenticated_user
|
|
|
|
from auth.permissions import (AdministerOrganizationPermission, CreateRepositoryPermission,
|
|
|
|
UserAdminPermission, UserReadPermission, SuperUserPermission)
|
|
|
|
from data import model
|
|
|
|
from data.billing import get_plan
|
2016-11-04 21:57:55 +00:00
|
|
|
from data.database import Repository as RepositoryTable, UserPromptTypes
|
2014-03-13 19:19:49 +00:00
|
|
|
from endpoints.api import (ApiResource, nickname, resource, validate_json_request, request_error,
|
2016-04-11 20:20:11 +00:00
|
|
|
log_action, internal_only, require_user_admin, parse_args,
|
|
|
|
query_param, require_scope, format_date, show_if,
|
2016-10-20 18:58:35 +00:00
|
|
|
require_fresh_login, path_param, define_json_response,
|
2016-02-01 11:06:13 +00:00
|
|
|
RepositoryParamResource, page_support)
|
2017-01-24 00:06:19 +00:00
|
|
|
from endpoints.exception import NotFound, InvalidToken, InvalidRequest, DownstreamIssue
|
2014-03-13 19:19:49 +00:00
|
|
|
from endpoints.api.subscribe import subscribe
|
|
|
|
from endpoints.common import common_login
|
2016-12-08 21:11:57 +00:00
|
|
|
from endpoints.csrf import generate_csrf_token, OAUTH_CSRF_TOKEN_NAME
|
2015-05-19 21:52:44 +00:00
|
|
|
from endpoints.decorators import anon_allowed
|
2017-01-24 00:06:19 +00:00
|
|
|
from oauth.oidc import DiscoveryFailureException
|
2015-07-15 21:25:41 +00:00
|
|
|
from util.useremails import (send_confirmation_email, send_recovery_email, send_change_email,
|
2015-12-28 20:25:31 +00:00
|
|
|
send_password_changed, send_org_recovery_email)
|
2014-09-11 19:45:41 +00:00
|
|
|
from util.names import parse_single_urn
|
2017-04-05 15:25:29 +00:00
|
|
|
from util.saas.useranalytics import build_error_callback
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2016-09-29 00:17:14 +00:00
|
|
|
|
2016-02-01 11:06:13 +00:00
|
|
|
REPOS_PER_PAGE = 100
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2016-09-29 00:17:14 +00:00
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-09-29 00:17:14 +00:00
|
|
|
|
2015-07-16 12:00:51 +00:00
|
|
|
def handle_invite_code(invite_code, user):
|
|
|
|
""" Checks that the given invite code matches the specified user's e-mail address. If so, the
|
|
|
|
user is marked as having a verified e-mail address and this method returns True.
|
|
|
|
"""
|
|
|
|
parsed_invite = parse_single_urn(invite_code)
|
|
|
|
if parsed_invite is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if parsed_invite[0] != 'teaminvite':
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Check to see if the team invite is valid. If so, then we know the user has
|
|
|
|
# a possible matching email address.
|
|
|
|
try:
|
|
|
|
found = model.team.find_matching_team_invite(invite_code, user)
|
|
|
|
except model.DataModelException:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Since we sent the invite code via email, mark the user as having a verified
|
|
|
|
# email address.
|
|
|
|
if found.email != user.email:
|
|
|
|
return False
|
|
|
|
|
|
|
|
user.verified = True
|
|
|
|
user.save()
|
|
|
|
return True
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2016-09-08 22:43:50 +00:00
|
|
|
def user_view(user, previous_username=None):
|
2015-10-21 18:37:18 +00:00
|
|
|
def org_view(o, user_admin=True):
|
2014-03-13 19:19:49 +00:00
|
|
|
admin_org = AdministerOrganizationPermission(o.username)
|
2015-10-21 18:37:18 +00:00
|
|
|
org_response = {
|
2014-03-13 19:19:49 +00:00
|
|
|
'name': o.username,
|
2015-03-30 21:55:04 +00:00
|
|
|
'avatar': avatar.get_data_for_org(o),
|
2015-10-21 18:37:18 +00:00
|
|
|
'can_create_repo': CreateRepositoryPermission(o.username).can(),
|
2014-03-13 19:19:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 18:37:18 +00:00
|
|
|
if user_admin:
|
|
|
|
org_response.update({
|
|
|
|
'is_org_admin': admin_org.can(),
|
|
|
|
'preferred_namespace': not (o.stripe_id is None),
|
|
|
|
})
|
|
|
|
|
|
|
|
return org_response
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
organizations = model.organization.get_user_organizations(user.username)
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
def login_view(login):
|
2014-08-11 22:25:01 +00:00
|
|
|
try:
|
|
|
|
metadata = json.loads(login.metadata_json)
|
|
|
|
except:
|
2014-08-11 22:35:26 +00:00
|
|
|
metadata = {}
|
2014-08-11 22:25:01 +00:00
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
return {
|
|
|
|
'service': login.service.name,
|
|
|
|
'service_identifier': login.service_ident,
|
2014-08-11 22:25:01 +00:00
|
|
|
'metadata': metadata
|
2014-03-13 19:19:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
logins = model.user.list_federated_logins(user)
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2014-03-25 21:26:45 +00:00
|
|
|
user_response = {
|
2014-03-13 19:19:49 +00:00
|
|
|
'anonymous': False,
|
|
|
|
'username': user.username,
|
2015-10-21 18:37:18 +00:00
|
|
|
'avatar': avatar.get_data_for_user(user),
|
2014-03-13 19:19:49 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 22:43:50 +00:00
|
|
|
user_admin = UserAdminPermission(previous_username if previous_username else user.username)
|
2014-03-25 21:26:45 +00:00
|
|
|
if user_admin.can():
|
|
|
|
user_response.update({
|
2015-10-21 18:37:18 +00:00
|
|
|
'can_create_repo': True,
|
2015-04-02 20:34:41 +00:00
|
|
|
'is_me': True,
|
|
|
|
'verified': user.verified,
|
2014-08-28 00:57:46 +00:00
|
|
|
'email': user.email,
|
2014-03-25 21:26:45 +00:00
|
|
|
'logins': [login_view(login) for login in logins],
|
|
|
|
'invoice_email': user.invoice_email,
|
2015-12-28 18:59:50 +00:00
|
|
|
'invoice_email_address': user.invoice_email_address,
|
2014-03-25 21:26:45 +00:00
|
|
|
'preferred_namespace': not (user.stripe_id is None),
|
2017-04-05 18:01:55 +00:00
|
|
|
'tag_expiration_s': user.removed_tag_expiration_s,
|
2016-09-08 22:43:50 +00:00
|
|
|
'prompts': model.user.get_user_prompts(user),
|
2014-03-25 21:26:45 +00:00
|
|
|
})
|
|
|
|
|
2016-10-13 17:48:35 +00:00
|
|
|
analytics_metadata = user_analytics.get_user_analytics_metadata(user)
|
|
|
|
|
|
|
|
# This is a sync call, but goes through the async wrapper interface and
|
|
|
|
# returns a Future. By calling with timeout 0 immediately after the method
|
|
|
|
# call, we ensure that if it ever accidentally becomes async it will raise
|
|
|
|
# a TimeoutError.
|
|
|
|
user_response.update(analytics_metadata.result(timeout=0))
|
|
|
|
|
2015-10-21 18:37:18 +00:00
|
|
|
user_view_perm = UserReadPermission(user.username)
|
|
|
|
if user_view_perm.can():
|
|
|
|
user_response.update({
|
|
|
|
'organizations': [org_view(o, user_admin=user_admin.can()) for o in organizations],
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2015-04-02 20:34:41 +00:00
|
|
|
if features.SUPER_USERS and SuperUserPermission().can():
|
2014-04-10 04:26:55 +00:00
|
|
|
user_response.update({
|
|
|
|
'super_user': user and user == get_authenticated_user() and SuperUserPermission().can()
|
|
|
|
})
|
|
|
|
|
2014-03-25 21:26:45 +00:00
|
|
|
return user_response
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def notification_view(note):
|
2014-03-19 19:39:44 +00:00
|
|
|
return {
|
2015-07-15 21:25:41 +00:00
|
|
|
'id': note.uuid,
|
|
|
|
'organization': note.target.username if note.target.organization else None,
|
|
|
|
'kind': note.kind.name,
|
|
|
|
'created': format_date(note.created),
|
|
|
|
'metadata': json.loads(note.metadata_json),
|
|
|
|
'dismissed': note.dismissed
|
2014-03-19 19:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
@resource('/v1/user/')
|
|
|
|
class User(ApiResource):
|
|
|
|
""" Operations related to users. """
|
|
|
|
schemas = {
|
|
|
|
'NewUser': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Fields which must be specified for a new user.',
|
2014-03-17 16:25:41 +00:00
|
|
|
'required': [
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
],
|
2014-03-13 19:19:49 +00:00
|
|
|
'properties': {
|
|
|
|
'username': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s username',
|
|
|
|
},
|
|
|
|
'password': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s password',
|
|
|
|
},
|
|
|
|
'email': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s email address',
|
|
|
|
},
|
2014-10-03 19:05:34 +00:00
|
|
|
'invite_code': {
|
|
|
|
'type': 'string',
|
2016-11-04 21:57:55 +00:00
|
|
|
'description': 'The optional invite code',
|
|
|
|
},
|
2016-12-17 07:13:36 +00:00
|
|
|
'recaptcha_response': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The (may be disabled) recaptcha response code for verification',
|
|
|
|
},
|
2014-03-13 19:19:49 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'UpdateUser': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Fields which can be updated in a user.',
|
|
|
|
'properties': {
|
|
|
|
'password': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s password',
|
|
|
|
},
|
|
|
|
'invoice_email': {
|
|
|
|
'type': 'boolean',
|
|
|
|
'description': 'Whether the user desires to receive an invoice email.',
|
|
|
|
},
|
|
|
|
'email': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s email address',
|
|
|
|
},
|
2017-04-05 18:01:55 +00:00
|
|
|
'tag_expiration_s': {
|
2015-02-12 19:37:11 +00:00
|
|
|
'type': 'integer',
|
|
|
|
'minimum': 0,
|
2017-04-05 18:01:55 +00:00
|
|
|
'description': 'The number of seconds for tag expiration',
|
2015-02-12 19:37:11 +00:00
|
|
|
},
|
2014-10-01 18:23:15 +00:00
|
|
|
'username': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s username',
|
|
|
|
},
|
2015-12-28 18:59:50 +00:00
|
|
|
'invoice_email_address': {
|
2016-02-16 16:52:57 +00:00
|
|
|
'type': ['string', 'null'],
|
2015-12-28 18:59:50 +00:00
|
|
|
'description': 'Custom email address for receiving invoices',
|
2016-11-04 21:57:55 +00:00
|
|
|
},
|
2016-11-09 20:29:53 +00:00
|
|
|
'given_name': {
|
2016-11-04 21:57:55 +00:00
|
|
|
'type': 'string',
|
2016-11-09 20:29:53 +00:00
|
|
|
'description': 'The optional entered given name for the user',
|
|
|
|
},
|
|
|
|
'family_name': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The optional entered family name for the user',
|
2016-11-04 21:57:55 +00:00
|
|
|
},
|
|
|
|
'company': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The optional entered company for the user',
|
|
|
|
},
|
2014-03-13 19:19:49 +00:00
|
|
|
},
|
|
|
|
},
|
2014-08-28 00:57:46 +00:00
|
|
|
'UserView': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Describes a user',
|
2016-09-08 22:43:50 +00:00
|
|
|
'required': ['anonymous', 'avatar'],
|
2014-08-28 00:57:46 +00:00
|
|
|
'properties': {
|
|
|
|
'verified': {
|
|
|
|
'type': 'boolean',
|
|
|
|
'description': 'Whether the user\'s email address has been verified'
|
|
|
|
},
|
|
|
|
'anonymous': {
|
|
|
|
'type': 'boolean',
|
|
|
|
'description': 'true if this user data represents a guest user'
|
|
|
|
},
|
|
|
|
'email': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s email address',
|
|
|
|
},
|
2014-11-26 00:59:24 +00:00
|
|
|
'avatar': {
|
2015-03-31 22:50:43 +00:00
|
|
|
'type': 'object',
|
|
|
|
'description': 'Avatar data representing the user\'s icon'
|
2014-08-28 00:57:46 +00:00
|
|
|
},
|
|
|
|
'organizations': {
|
|
|
|
'type': 'array',
|
2015-05-14 20:47:38 +00:00
|
|
|
'description': 'Information about the organizations in which the user is a member',
|
|
|
|
'items': {
|
|
|
|
'type': 'object'
|
|
|
|
}
|
2014-08-28 00:57:46 +00:00
|
|
|
},
|
|
|
|
'logins': {
|
|
|
|
'type': 'array',
|
2015-05-14 20:47:38 +00:00
|
|
|
'description': 'The list of external login providers against which the user has authenticated',
|
|
|
|
'items': {
|
|
|
|
'type': 'object'
|
|
|
|
}
|
2014-08-28 00:57:46 +00:00
|
|
|
},
|
|
|
|
'can_create_repo': {
|
|
|
|
'type': 'boolean',
|
|
|
|
'description': 'Whether the user has permission to create repositories'
|
|
|
|
},
|
|
|
|
'preferred_namespace': {
|
|
|
|
'type': 'boolean',
|
|
|
|
'description': 'If true, the user\'s namespace is the preferred namespace to display'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2014-03-13 19:19:49 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 21:58:19 +00:00
|
|
|
@require_scope(scopes.READ_USER)
|
2014-03-13 19:19:49 +00:00
|
|
|
@nickname('getLoggedInUser')
|
2014-08-28 00:57:46 +00:00
|
|
|
@define_json_response('UserView')
|
2015-05-19 21:52:44 +00:00
|
|
|
@anon_allowed
|
2014-03-13 19:19:49 +00:00
|
|
|
def get(self):
|
|
|
|
""" Get user information for the authenticated user. """
|
|
|
|
user = get_authenticated_user()
|
2014-03-25 21:58:19 +00:00
|
|
|
if user is None or user.organization or not UserReadPermission(user.username).can():
|
2014-03-19 17:57:36 +00:00
|
|
|
raise InvalidToken("Requires authentication", payload={'session_required': False})
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
return user_view(user)
|
|
|
|
|
2014-03-18 23:21:27 +00:00
|
|
|
@require_user_admin
|
2014-09-04 18:24:20 +00:00
|
|
|
@require_fresh_login
|
2014-03-13 19:19:49 +00:00
|
|
|
@nickname('changeUserDetails')
|
2014-03-14 22:07:03 +00:00
|
|
|
@internal_only
|
2014-03-13 19:19:49 +00:00
|
|
|
@validate_json_request('UpdateUser')
|
2014-08-28 00:57:46 +00:00
|
|
|
@define_json_response('UserView')
|
2014-03-13 19:19:49 +00:00
|
|
|
def put(self):
|
|
|
|
""" Update a users details such as password or email. """
|
|
|
|
user = get_authenticated_user()
|
|
|
|
user_data = request.get_json()
|
2016-09-08 22:43:50 +00:00
|
|
|
previous_username = None
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2014-11-26 00:59:24 +00:00
|
|
|
try:
|
2014-03-13 19:19:49 +00:00
|
|
|
if 'password' in user_data:
|
|
|
|
logger.debug('Changing password for user: %s', user.username)
|
|
|
|
log_action('account_change_password', user.username)
|
2015-03-27 00:04:32 +00:00
|
|
|
|
|
|
|
# Change the user's password.
|
2015-07-15 21:25:41 +00:00
|
|
|
model.user.change_password(user, user_data['password'])
|
2014-09-22 23:11:48 +00:00
|
|
|
|
2015-03-27 00:04:32 +00:00
|
|
|
# Login again to reset their session cookie.
|
|
|
|
common_login(user)
|
|
|
|
|
2014-09-22 23:11:48 +00:00
|
|
|
if features.MAILING:
|
|
|
|
send_password_changed(user.username, user.email)
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
if 'invoice_email' in user_data:
|
|
|
|
logger.debug('Changing invoice_email for user: %s', user.username)
|
2015-12-28 18:59:50 +00:00
|
|
|
model.user.change_send_invoice_email(user, user_data['invoice_email'])
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2017-04-05 18:01:55 +00:00
|
|
|
if features.CHANGE_TAG_EXPIRATION and 'tag_expiration_s' in user_data:
|
|
|
|
logger.debug('Changing user tag expiration to: %ss', user_data['tag_expiration_s'])
|
|
|
|
model.user.change_user_tag_expiration(user, user_data['tag_expiration_s'])
|
2015-02-12 19:37:11 +00:00
|
|
|
|
2015-12-28 18:59:50 +00:00
|
|
|
if ('invoice_email_address' in user_data and
|
|
|
|
user_data['invoice_email_address'] != user.invoice_email_address):
|
|
|
|
model.user.change_invoice_email_address(user, user_data['invoice_email_address'])
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
if 'email' in user_data and user_data['email'] != user.email:
|
|
|
|
new_email = user_data['email']
|
2015-07-15 21:25:41 +00:00
|
|
|
if model.user.find_user_by_email(new_email):
|
2014-03-13 19:19:49 +00:00
|
|
|
# Email already used.
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(message='E-mail address already used')
|
2014-11-26 00:59:24 +00:00
|
|
|
|
|
|
|
if features.MAILING:
|
2014-09-22 23:11:48 +00:00
|
|
|
logger.debug('Sending email to change email address for user: %s',
|
|
|
|
user.username)
|
2015-07-15 21:25:41 +00:00
|
|
|
code = model.user.create_confirm_email_code(user, new_email=new_email)
|
2014-09-22 23:11:48 +00:00
|
|
|
send_change_email(user.username, user_data['email'], code.code)
|
|
|
|
else:
|
2017-04-05 15:25:29 +00:00
|
|
|
ua_future = user_analytics.change_email(user.email, new_email)
|
|
|
|
ua_future.add_done_callback(build_error_callback('Change email failed'))
|
2015-07-15 21:25:41 +00:00
|
|
|
model.user.update_email(user, new_email, auto_verify=not features.MAILING)
|
2014-10-01 18:23:15 +00:00
|
|
|
|
2016-11-09 20:29:53 +00:00
|
|
|
if 'given_name' in user_data or 'family_name' in user_data or 'company' in user_data:
|
|
|
|
model.user.update_user_metadata(user, user_data.get('given_name'),
|
|
|
|
user_data.get('family_name'), user_data.get('company'))
|
2017-04-05 15:25:29 +00:00
|
|
|
ua_mdata_future = user_analytics.change_metadata(
|
|
|
|
user.email,
|
|
|
|
user_data.get('given_name'),
|
|
|
|
user_data.get('family_name'),
|
|
|
|
user_data.get('company'),
|
|
|
|
)
|
|
|
|
ua_mdata_future.add_done_callback(build_error_callback('Change metadata failed'))
|
2016-11-04 21:57:55 +00:00
|
|
|
|
2016-09-08 22:43:50 +00:00
|
|
|
# Check for username rename. A username can be renamed if the feature is enabled OR the user
|
|
|
|
# currently has a confirm_username prompt.
|
|
|
|
if 'username' in user_data:
|
|
|
|
confirm_username = model.user.has_user_prompt(user, 'confirm_username')
|
|
|
|
new_username = user_data.get('username')
|
|
|
|
previous_username = user.username
|
2014-10-01 18:23:15 +00:00
|
|
|
|
2016-09-08 22:43:50 +00:00
|
|
|
rename_allowed = features.USER_RENAME or confirm_username
|
|
|
|
username_changing = new_username and new_username != previous_username
|
|
|
|
|
|
|
|
if rename_allowed and username_changing:
|
|
|
|
if model.user.get_user_or_org(new_username) is not None:
|
|
|
|
# Username already used.
|
|
|
|
raise request_error(message='Username is already in use')
|
|
|
|
|
|
|
|
user = model.user.change_username(user.id, new_username)
|
2017-04-05 15:25:29 +00:00
|
|
|
username_future = user_analytics.change_username(user.email, new_username)
|
|
|
|
username_future.add_done_callback(build_error_callback('Change username failed'))
|
|
|
|
|
2016-09-08 22:43:50 +00:00
|
|
|
elif confirm_username:
|
|
|
|
model.user.remove_user_prompt(user, 'confirm_username')
|
2014-11-26 00:59:24 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
except model.user.InvalidPasswordException, ex:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(exception=ex)
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2016-09-08 22:43:50 +00:00
|
|
|
return user_view(user, previous_username=previous_username)
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2014-10-02 18:49:18 +00:00
|
|
|
@show_if(features.USER_CREATION)
|
2015-09-04 20:14:46 +00:00
|
|
|
@show_if(features.DIRECT_LOGIN)
|
2014-03-13 19:19:49 +00:00
|
|
|
@nickname('createNewUser')
|
2014-03-14 22:07:03 +00:00
|
|
|
@internal_only
|
2014-03-13 19:19:49 +00:00
|
|
|
@validate_json_request('NewUser')
|
2014-10-03 19:05:34 +00:00
|
|
|
def post(self):
|
2014-03-13 20:31:37 +00:00
|
|
|
""" Create a new user. """
|
2016-08-09 21:58:33 +00:00
|
|
|
if app.config['AUTHENTICATION_TYPE'] != 'Database':
|
|
|
|
abort(404)
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
user_data = request.get_json()
|
2016-12-17 07:13:36 +00:00
|
|
|
|
|
|
|
# If recaptcha is enabled, then verify the user is a human.
|
|
|
|
if features.RECAPTCHA:
|
|
|
|
recaptcha_response = user_data.get('recaptcha_response', '')
|
|
|
|
result = recaptcha2.verify(app.config['RECAPTCHA_SECRET_KEY'],
|
|
|
|
recaptcha_response,
|
|
|
|
request.remote_addr)
|
|
|
|
|
|
|
|
if not result['success']:
|
|
|
|
return {
|
|
|
|
'message': 'Are you a bot? If not, please revalidate the captcha.'
|
|
|
|
}, 400
|
|
|
|
|
2014-10-03 19:05:34 +00:00
|
|
|
invite_code = user_data.get('invite_code', '')
|
2015-07-15 21:25:41 +00:00
|
|
|
existing_user = model.user.get_nonrobot_user(user_data['username'])
|
2014-03-13 19:19:49 +00:00
|
|
|
if existing_user:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(message='The username already exists')
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2016-09-08 16:24:47 +00:00
|
|
|
if features.MAILING and not user_data.get('email'):
|
|
|
|
raise request_error(message='Email address is required')
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
try:
|
2016-11-04 21:57:55 +00:00
|
|
|
prompts = model.user.get_default_user_prompts(features)
|
2015-07-15 21:25:41 +00:00
|
|
|
new_user = model.user.create_user(user_data['username'], user_data['password'],
|
2016-09-08 16:24:47 +00:00
|
|
|
user_data.get('email'),
|
|
|
|
auto_verify=not features.MAILING,
|
2016-11-04 21:57:55 +00:00
|
|
|
email_required=features.MAILING,
|
|
|
|
prompts=prompts)
|
2014-09-11 19:45:41 +00:00
|
|
|
|
2015-07-16 12:00:51 +00:00
|
|
|
email_address_confirmed = handle_invite_code(invite_code, new_user)
|
|
|
|
if features.MAILING and not email_address_confirmed:
|
2015-07-15 21:25:41 +00:00
|
|
|
code = model.user.create_confirm_email_code(new_user)
|
2014-09-22 23:11:48 +00:00
|
|
|
send_confirmation_email(new_user.username, new_user.email, code.code)
|
|
|
|
return {
|
|
|
|
'awaiting_verification': True
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
common_login(new_user)
|
|
|
|
return user_view(new_user)
|
2015-07-15 21:25:41 +00:00
|
|
|
except model.user.DataModelException as ex:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(exception=ex)
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2016-08-09 21:58:33 +00:00
|
|
|
@require_user_admin
|
|
|
|
@require_fresh_login
|
|
|
|
@nickname('deleteCurrentUser')
|
|
|
|
@internal_only
|
|
|
|
def delete(self):
|
|
|
|
""" Deletes the current user. """
|
|
|
|
if app.config['AUTHENTICATION_TYPE'] != 'Database':
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
model.user.delete_user(get_authenticated_user(), all_queues)
|
2016-12-06 21:26:28 +00:00
|
|
|
return '', 204
|
2016-08-09 21:58:33 +00:00
|
|
|
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
@resource('/v1/user/private')
|
2014-03-19 16:09:07 +00:00
|
|
|
@internal_only
|
2014-04-06 04:36:19 +00:00
|
|
|
@show_if(features.BILLING)
|
2014-03-13 19:19:49 +00:00
|
|
|
class PrivateRepositories(ApiResource):
|
|
|
|
""" Operations dealing with the available count of private repositories. """
|
2014-03-18 23:21:27 +00:00
|
|
|
@require_user_admin
|
2014-03-13 19:19:49 +00:00
|
|
|
@nickname('getUserPrivateAllowed')
|
|
|
|
def get(self):
|
|
|
|
""" Get the number of private repos this user has, and whether they are allowed to create more.
|
|
|
|
"""
|
|
|
|
user = get_authenticated_user()
|
2015-07-15 21:25:41 +00:00
|
|
|
private_repos = model.user.get_private_repo_count(user.username)
|
2014-03-13 19:19:49 +00:00
|
|
|
repos_allowed = 0
|
|
|
|
|
|
|
|
if user.stripe_id:
|
|
|
|
cus = stripe.Customer.retrieve(user.stripe_id)
|
|
|
|
if cus.subscription:
|
|
|
|
plan = get_plan(cus.subscription.plan.id)
|
|
|
|
if plan:
|
|
|
|
repos_allowed = plan['privateRepos']
|
2014-11-26 00:59:24 +00:00
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
return {
|
|
|
|
'privateCount': private_repos,
|
|
|
|
'privateAllowed': (private_repos < repos_allowed)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-25 22:43:12 +00:00
|
|
|
@resource('/v1/user/clientkey')
|
|
|
|
@internal_only
|
|
|
|
class ClientKey(ApiResource):
|
|
|
|
""" Operations for returning an encrypted key which can be used in place of a password
|
|
|
|
for the Docker client. """
|
|
|
|
schemas = {
|
|
|
|
'GenerateClientKey': {
|
|
|
|
'type': 'object',
|
|
|
|
'required': [
|
|
|
|
'password',
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'password': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s password',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('generateUserClientKey')
|
|
|
|
@validate_json_request('GenerateClientKey')
|
|
|
|
def post(self):
|
|
|
|
""" Return's the user's private client key. """
|
|
|
|
username = get_authenticated_user().username
|
|
|
|
password = request.get_json()['password']
|
2015-05-20 20:37:09 +00:00
|
|
|
(result, error_message) = authentication.confirm_existing_user(username, password)
|
2015-03-25 22:43:12 +00:00
|
|
|
if not result:
|
|
|
|
raise request_error(message=error_message)
|
|
|
|
|
|
|
|
return {
|
2015-03-26 19:10:58 +00:00
|
|
|
'key': authentication.encrypt_user_password(password)
|
2015-03-25 22:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-16 12:00:51 +00:00
|
|
|
def conduct_signin(username_or_email, password, invite_code=None):
|
2014-03-13 19:19:49 +00:00
|
|
|
needs_email_verification = False
|
|
|
|
invalid_credentials = False
|
2014-05-28 19:53:53 +00:00
|
|
|
|
2016-10-20 18:58:35 +00:00
|
|
|
(found_user, error_message) = authentication.verify_and_link_user(username_or_email, password)
|
2015-07-16 12:00:51 +00:00
|
|
|
if found_user:
|
2015-07-24 16:09:25 +00:00
|
|
|
# If there is an attached invitation code, handle it here. This will mark the
|
|
|
|
# user as verified if the code is valid.
|
|
|
|
if invite_code:
|
|
|
|
handle_invite_code(invite_code, found_user)
|
|
|
|
|
2015-07-16 12:00:51 +00:00
|
|
|
if common_login(found_user):
|
2014-03-13 19:19:49 +00:00
|
|
|
return {'success': True}
|
|
|
|
else:
|
|
|
|
needs_email_verification = True
|
|
|
|
|
|
|
|
else:
|
|
|
|
invalid_credentials = True
|
|
|
|
|
|
|
|
return {
|
|
|
|
'needsEmailVerification': needs_email_verification,
|
|
|
|
'invalidCredentials': invalid_credentials,
|
2015-05-11 21:13:42 +00:00
|
|
|
'message': error_message
|
2014-03-13 19:19:49 +00:00
|
|
|
}, 403
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/user/convert')
|
2014-03-14 22:07:03 +00:00
|
|
|
@internal_only
|
2014-05-28 19:53:53 +00:00
|
|
|
@show_if(app.config['AUTHENTICATION_TYPE'] == 'Database')
|
2014-03-13 19:19:49 +00:00
|
|
|
class ConvertToOrganization(ApiResource):
|
|
|
|
""" Operations for converting a user to an organization. """
|
|
|
|
schemas = {
|
|
|
|
'ConvertUser': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Information required to convert a user to an organization.',
|
2014-03-17 16:25:41 +00:00
|
|
|
'required': [
|
2014-03-17 18:52:52 +00:00
|
|
|
'adminUser',
|
2014-04-06 04:36:19 +00:00
|
|
|
'adminPassword'
|
2014-03-17 16:25:41 +00:00
|
|
|
],
|
2014-03-13 19:19:49 +00:00
|
|
|
'properties': {
|
2014-03-17 18:52:52 +00:00
|
|
|
'adminUser': {
|
2014-03-13 19:19:49 +00:00
|
|
|
'type': 'string',
|
2014-03-17 18:52:52 +00:00
|
|
|
'description': 'The user who will become an org admin\'s username',
|
2014-03-13 19:19:49 +00:00
|
|
|
},
|
2014-03-17 18:52:52 +00:00
|
|
|
'adminPassword': {
|
2014-03-13 19:19:49 +00:00
|
|
|
'type': 'string',
|
2014-03-17 18:52:52 +00:00
|
|
|
'description': 'The user who will become an org admin\'s password',
|
2014-03-13 19:19:49 +00:00
|
|
|
},
|
2014-03-17 18:52:52 +00:00
|
|
|
'plan': {
|
2014-03-13 19:19:49 +00:00
|
|
|
'type': 'string',
|
2014-04-06 04:36:19 +00:00
|
|
|
'description': 'The plan to which the organization should be subscribed',
|
2014-03-13 19:19:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-03-18 23:21:27 +00:00
|
|
|
@require_user_admin
|
2014-03-13 19:19:49 +00:00
|
|
|
@nickname('convertUserToOrganization')
|
|
|
|
@validate_json_request('ConvertUser')
|
|
|
|
def post(self):
|
|
|
|
""" Convert the user to an organization. """
|
|
|
|
user = get_authenticated_user()
|
|
|
|
convert_data = request.get_json()
|
|
|
|
|
|
|
|
# Ensure that the sign in credentials work.
|
2015-04-30 00:30:37 +00:00
|
|
|
admin_username = convert_data['adminUser']
|
2014-03-13 19:19:49 +00:00
|
|
|
admin_password = convert_data['adminPassword']
|
2015-07-20 15:39:59 +00:00
|
|
|
(admin_user, _) = authentication.verify_and_link_user(admin_username, admin_password)
|
2014-07-08 22:19:13 +00:00
|
|
|
if not admin_user:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(reason='invaliduser',
|
2015-07-15 21:25:41 +00:00
|
|
|
message='The admin user credentials are not valid')
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2015-04-30 00:30:37 +00:00
|
|
|
# Ensure that the new admin user is the not user being converted.
|
|
|
|
if admin_user.id == user.id:
|
2015-07-15 21:25:41 +00:00
|
|
|
raise request_error(reason='invaliduser', message='The admin user is not valid')
|
2015-04-30 00:30:37 +00:00
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
# Subscribe the organization to the new plan.
|
2014-04-06 04:36:19 +00:00
|
|
|
if features.BILLING:
|
|
|
|
plan = convert_data.get('plan', 'free')
|
|
|
|
subscribe(user, plan, None, True) # Require business plans
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
# Convert the user to an organization.
|
2015-07-15 21:25:41 +00:00
|
|
|
model.organization.convert_user_to_organization(user, admin_user)
|
2014-03-13 19:19:49 +00:00
|
|
|
log_action('account_convert', user.username)
|
|
|
|
|
|
|
|
# And finally login with the admin credentials.
|
|
|
|
return conduct_signin(admin_username, admin_password)
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/signin')
|
2015-09-04 20:14:46 +00:00
|
|
|
@show_if(features.DIRECT_LOGIN)
|
2014-03-14 22:07:03 +00:00
|
|
|
@internal_only
|
2014-03-13 19:19:49 +00:00
|
|
|
class Signin(ApiResource):
|
|
|
|
""" Operations for signing in the user. """
|
|
|
|
schemas = {
|
|
|
|
'SigninUser': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Information required to sign in a user.',
|
2014-03-17 16:25:41 +00:00
|
|
|
'required': [
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
],
|
2014-03-13 19:19:49 +00:00
|
|
|
'properties': {
|
|
|
|
'username': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s username',
|
|
|
|
},
|
|
|
|
'password': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s password',
|
|
|
|
},
|
2015-07-16 12:00:51 +00:00
|
|
|
'invite_code': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The optional invite code'
|
2016-10-10 17:00:59 +00:00
|
|
|
},
|
2014-03-13 19:19:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@nickname('signinUser')
|
|
|
|
@validate_json_request('SigninUser')
|
2015-05-19 21:52:44 +00:00
|
|
|
@anon_allowed
|
2014-03-13 19:19:49 +00:00
|
|
|
def post(self):
|
|
|
|
""" Sign in the user with the specified credentials. """
|
|
|
|
signin_data = request.get_json()
|
|
|
|
if not signin_data:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
username = signin_data['username']
|
|
|
|
password = signin_data['password']
|
2015-07-16 12:00:51 +00:00
|
|
|
invite_code = signin_data.get('invite_code', '')
|
|
|
|
return conduct_signin(username, password, invite_code=invite_code)
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
|
2014-09-04 18:24:20 +00:00
|
|
|
@resource('/v1/signin/verify')
|
|
|
|
@internal_only
|
|
|
|
class VerifyUser(ApiResource):
|
|
|
|
""" Operations for verifying the existing user. """
|
|
|
|
schemas = {
|
|
|
|
'VerifyUser': {
|
|
|
|
'id': 'VerifyUser',
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Information required to verify the signed in user.',
|
|
|
|
'required': [
|
|
|
|
'password',
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'password': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s password',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('verifyUser')
|
|
|
|
@validate_json_request('VerifyUser')
|
|
|
|
def post(self):
|
|
|
|
""" Verifies the signed in the user with the specified credentials. """
|
|
|
|
signin_data = request.get_json()
|
|
|
|
password = signin_data['password']
|
2015-05-22 20:26:26 +00:00
|
|
|
|
|
|
|
username = get_authenticated_user().username
|
|
|
|
(result, error_message) = authentication.confirm_existing_user(username, password)
|
|
|
|
if not result:
|
|
|
|
return {
|
|
|
|
'message': error_message,
|
|
|
|
'invalidCredentials': True,
|
|
|
|
}, 403
|
|
|
|
|
|
|
|
common_login(result)
|
|
|
|
return {'success': True}
|
2014-09-04 18:24:20 +00:00
|
|
|
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
@resource('/v1/signout')
|
2014-03-14 22:07:03 +00:00
|
|
|
@internal_only
|
2014-03-13 19:19:49 +00:00
|
|
|
class Signout(ApiResource):
|
|
|
|
""" Resource for signing out users. """
|
|
|
|
@nickname('logout')
|
|
|
|
def post(self):
|
|
|
|
""" Request that the current user be signed out. """
|
|
|
|
logout_user()
|
|
|
|
identity_changed.send(app, identity=AnonymousIdentity())
|
|
|
|
return {'success': True}
|
|
|
|
|
|
|
|
|
2017-01-24 00:06:19 +00:00
|
|
|
@resource('/v1/externallogin/<service_id>')
|
2016-12-08 21:11:57 +00:00
|
|
|
@internal_only
|
2017-01-24 00:06:19 +00:00
|
|
|
class ExternalLoginInformation(ApiResource):
|
|
|
|
""" Resource for both setting a token for external login and returning its authorization
|
|
|
|
url.
|
|
|
|
"""
|
|
|
|
schemas = {
|
|
|
|
'GetLogin': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Information required to an retrieve external login URL.',
|
|
|
|
'required': [
|
|
|
|
'kind',
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'kind': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The kind of URL',
|
|
|
|
'enum': ['login', 'attach'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@nickname('retrieveExternalLoginAuthorizationUrl')
|
|
|
|
@anon_allowed
|
|
|
|
@validate_json_request('GetLogin')
|
|
|
|
def post(self, service_id):
|
|
|
|
""" Generates the auth URL and CSRF token explicitly for OIDC/OAuth-associated login. """
|
|
|
|
login_service = oauth_login.get_service(service_id)
|
|
|
|
if login_service is None:
|
|
|
|
raise InvalidRequest()
|
|
|
|
|
|
|
|
csrf_token = generate_csrf_token(OAUTH_CSRF_TOKEN_NAME)
|
|
|
|
kind = request.get_json()['kind']
|
|
|
|
redirect_suffix = '/attach' if kind == 'attach' else ''
|
|
|
|
|
|
|
|
try:
|
|
|
|
login_scopes = login_service.get_login_scopes()
|
|
|
|
auth_url = login_service.get_auth_url(app.config, redirect_suffix, csrf_token, login_scopes)
|
|
|
|
return {'auth_url': auth_url}
|
|
|
|
except DiscoveryFailureException as dfe:
|
|
|
|
logger.exception('Could not discovery OAuth endpoint information')
|
|
|
|
raise DownstreamIssue(dfe.message)
|
2016-12-08 21:11:57 +00:00
|
|
|
|
2014-09-15 16:01:02 +00:00
|
|
|
|
2017-01-20 20:21:08 +00:00
|
|
|
@resource('/v1/detachexternal/<service_id>')
|
2015-09-04 20:14:46 +00:00
|
|
|
@show_if(features.DIRECT_LOGIN)
|
2014-09-15 16:01:02 +00:00
|
|
|
@internal_only
|
|
|
|
class DetachExternal(ApiResource):
|
|
|
|
""" Resource for detaching an external login. """
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('detachExternalLogin')
|
2017-01-20 20:21:08 +00:00
|
|
|
def post(self, service_id):
|
2014-09-15 16:01:02 +00:00
|
|
|
""" Request that the current user be detached from the external login service. """
|
2017-01-20 20:21:08 +00:00
|
|
|
model.user.detach_external_login(get_authenticated_user(), service_id)
|
2014-09-15 16:01:02 +00:00
|
|
|
return {'success': True}
|
|
|
|
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
@resource("/v1/recovery")
|
2014-09-22 23:11:48 +00:00
|
|
|
@show_if(features.MAILING)
|
2014-03-14 22:07:03 +00:00
|
|
|
@internal_only
|
2014-03-13 19:19:49 +00:00
|
|
|
class Recovery(ApiResource):
|
|
|
|
""" Resource for requesting a password recovery email. """
|
|
|
|
schemas = {
|
|
|
|
'RequestRecovery': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Information required to sign in a user.',
|
2014-03-17 16:25:41 +00:00
|
|
|
'required': [
|
|
|
|
'email',
|
|
|
|
],
|
2014-03-13 19:19:49 +00:00
|
|
|
'properties': {
|
|
|
|
'email': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s email address',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@nickname('requestRecoveryEmail')
|
2015-11-10 20:41:19 +00:00
|
|
|
@anon_allowed
|
2014-03-13 19:19:49 +00:00
|
|
|
@validate_json_request('RequestRecovery')
|
|
|
|
def post(self):
|
|
|
|
""" Request a password recovery email."""
|
2015-12-28 20:25:31 +00:00
|
|
|
def redact(value):
|
|
|
|
threshold = max((len(value) / 3) - 1, 1)
|
|
|
|
v = ''
|
|
|
|
for i in range(0, len(value)):
|
|
|
|
if i < threshold or i >= len(value) - threshold:
|
|
|
|
v = v + value[i]
|
|
|
|
else:
|
|
|
|
v = v + u'\u2022'
|
|
|
|
|
|
|
|
return v
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
email = request.get_json()['email']
|
2015-12-28 20:25:31 +00:00
|
|
|
user = model.user.find_user_by_email(email)
|
|
|
|
if not user:
|
|
|
|
raise model.InvalidEmailAddressException('Email address was not found.')
|
|
|
|
|
|
|
|
if user.organization:
|
|
|
|
send_org_recovery_email(user, model.organization.get_admin_users(user))
|
|
|
|
return {
|
|
|
|
'status': 'org',
|
|
|
|
'orgemail': email,
|
|
|
|
'orgname': redact(user.username),
|
|
|
|
}
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
code = model.user.create_reset_password_email_code(email)
|
2014-03-13 19:19:49 +00:00
|
|
|
send_recovery_email(email, code.code)
|
2015-12-28 20:25:31 +00:00
|
|
|
return {
|
|
|
|
'status': 'sent',
|
|
|
|
}
|
2014-03-19 19:39:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/user/notifications')
|
|
|
|
@internal_only
|
|
|
|
class UserNotificationList(ApiResource):
|
|
|
|
@require_user_admin
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2014-08-26 19:19:39 +00:00
|
|
|
@query_param('page', 'Offset page number. (int)', type=int, default=0)
|
|
|
|
@query_param('limit', 'Limit on the number of results (int)', type=int, default=5)
|
2014-03-19 19:39:44 +00:00
|
|
|
@nickname('listUserNotifications')
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, parsed_args):
|
|
|
|
page = parsed_args['page']
|
|
|
|
limit = parsed_args['limit']
|
2014-08-26 19:19:39 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
notifications = list(model.notification.list_notifications(get_authenticated_user(), page=page,
|
|
|
|
limit=limit + 1))
|
2014-08-26 19:19:39 +00:00
|
|
|
has_more = False
|
|
|
|
|
|
|
|
if len(notifications) > limit:
|
|
|
|
has_more = True
|
|
|
|
notifications = notifications[0:limit]
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
return {
|
2015-07-15 21:25:41 +00:00
|
|
|
'notifications': [notification_view(note) for note in notifications],
|
2014-08-26 19:19:39 +00:00
|
|
|
'additional': has_more
|
2014-03-25 00:57:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-28 22:23:46 +00:00
|
|
|
@resource('/v1/user/notifications/<uuid>')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('uuid', 'The uuid of the user notification')
|
2014-07-28 22:23:46 +00:00
|
|
|
@internal_only
|
|
|
|
class UserNotification(ApiResource):
|
|
|
|
schemas = {
|
|
|
|
'UpdateNotification': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Information for updating a notification',
|
|
|
|
'properties': {
|
|
|
|
'dismissed': {
|
|
|
|
'type': 'boolean',
|
|
|
|
'description': 'Whether the notification is dismissed by the user',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('getUserNotification')
|
|
|
|
def get(self, uuid):
|
2015-07-15 21:25:41 +00:00
|
|
|
note = model.notification.lookup_notification(get_authenticated_user(), uuid)
|
|
|
|
if not note:
|
2014-07-28 22:23:46 +00:00
|
|
|
raise NotFound()
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
return notification_view(note)
|
2014-07-28 22:23:46 +00:00
|
|
|
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('updateUserNotification')
|
|
|
|
@validate_json_request('UpdateNotification')
|
|
|
|
def put(self, uuid):
|
2015-07-15 21:25:41 +00:00
|
|
|
note = model.notification.lookup_notification(get_authenticated_user(), uuid)
|
|
|
|
if not note:
|
2014-07-28 22:23:46 +00:00
|
|
|
raise NotFound()
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
note.dismissed = request.get_json().get('dismissed', False)
|
|
|
|
note.save()
|
2014-07-28 22:23:46 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
return notification_view(note)
|
2014-07-28 22:23:46 +00:00
|
|
|
|
|
|
|
|
2014-03-25 00:57:02 +00:00
|
|
|
def authorization_view(access_token):
|
|
|
|
oauth_app = access_token.application
|
2015-03-30 21:55:04 +00:00
|
|
|
app_email = oauth_app.avatar_email or oauth_app.organization.email
|
2014-03-25 00:57:02 +00:00
|
|
|
return {
|
|
|
|
'application': {
|
|
|
|
'name': oauth_app.name,
|
|
|
|
'description': oauth_app.description,
|
|
|
|
'url': oauth_app.application_uri,
|
2015-03-30 21:55:04 +00:00
|
|
|
'avatar': avatar.get_data(oauth_app.name, app_email, 'app'),
|
2014-03-25 00:57:02 +00:00
|
|
|
'organization': {
|
|
|
|
'name': oauth_app.organization.username,
|
2015-03-30 21:55:04 +00:00
|
|
|
'avatar': avatar.get_data_for_org(oauth_app.organization)
|
2014-03-25 00:57:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'scopes': scopes.get_scope_information(access_token.scope),
|
|
|
|
'uuid': access_token.uuid
|
|
|
|
}
|
|
|
|
|
|
|
|
@resource('/v1/user/authorizations')
|
|
|
|
@internal_only
|
|
|
|
class UserAuthorizationList(ApiResource):
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('listUserAuthorizations')
|
|
|
|
def get(self):
|
|
|
|
access_tokens = model.oauth.list_access_tokens_for_user(get_authenticated_user())
|
|
|
|
|
|
|
|
return {
|
|
|
|
'authorizations': [authorization_view(token) for token in access_tokens]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/user/authorizations/<access_token_uuid>')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('access_token_uuid', 'The uuid of the access token')
|
2014-03-25 00:57:02 +00:00
|
|
|
@internal_only
|
|
|
|
class UserAuthorization(ApiResource):
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('getUserAuthorization')
|
|
|
|
def get(self, access_token_uuid):
|
2014-03-25 18:32:02 +00:00
|
|
|
access_token = model.oauth.lookup_access_token_for_user(get_authenticated_user(),
|
|
|
|
access_token_uuid)
|
2014-03-25 00:57:02 +00:00
|
|
|
if not access_token:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
return authorization_view(access_token)
|
|
|
|
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('deleteUserAuthorization')
|
|
|
|
def delete(self, access_token_uuid):
|
2015-07-15 21:25:41 +00:00
|
|
|
access_token = model.oauth.lookup_access_token_for_user(get_authenticated_user(), access_token_uuid)
|
2014-03-25 00:57:02 +00:00
|
|
|
if not access_token:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
access_token.delete_instance(recursive=True, delete_nullable=True)
|
2016-12-06 21:26:28 +00:00
|
|
|
return '', 204
|
2014-11-19 19:50:56 +00:00
|
|
|
|
|
|
|
@resource('/v1/user/starred')
|
|
|
|
class StarredRepositoryList(ApiResource):
|
|
|
|
""" Operations for creating and listing starred repositories. """
|
|
|
|
schemas = {
|
|
|
|
'NewStarredRepository': {
|
|
|
|
'type': 'object',
|
|
|
|
'required': [
|
|
|
|
'namespace',
|
|
|
|
'repository',
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'namespace': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'Namespace in which the repository belongs',
|
|
|
|
},
|
|
|
|
'repository': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'Repository name'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@nickname('listStarredRepos')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2015-02-19 22:03:36 +00:00
|
|
|
@require_user_admin
|
2016-02-01 11:06:13 +00:00
|
|
|
@page_support()
|
|
|
|
def get(self, page_token, parsed_args):
|
2014-11-19 19:50:56 +00:00
|
|
|
""" List all starred repositories. """
|
2016-02-01 11:06:13 +00:00
|
|
|
repo_query = model.repository.get_user_starred_repositories(get_authenticated_user())
|
|
|
|
|
|
|
|
repos, next_page_token = model.modelutil.paginate(repo_query, RepositoryTable,
|
|
|
|
page_token=page_token, limit=REPOS_PER_PAGE)
|
|
|
|
|
2014-12-30 20:07:14 +00:00
|
|
|
def repo_view(repo_obj):
|
|
|
|
return {
|
|
|
|
'namespace': repo_obj.namespace_user.username,
|
|
|
|
'name': repo_obj.name,
|
|
|
|
'description': repo_obj.description,
|
|
|
|
'is_public': repo_obj.visibility.name == 'public',
|
|
|
|
}
|
|
|
|
|
2016-02-01 11:06:13 +00:00
|
|
|
return {'repositories': [repo_view(repo) for repo in repos]}, next_page_token
|
2014-11-19 19:50:56 +00:00
|
|
|
|
|
|
|
@require_scope(scopes.READ_REPO)
|
|
|
|
@nickname('createStar')
|
|
|
|
@validate_json_request('NewStarredRepository')
|
2015-02-19 22:03:36 +00:00
|
|
|
@require_user_admin
|
2014-11-19 19:50:56 +00:00
|
|
|
def post(self):
|
|
|
|
""" Star a repository. """
|
|
|
|
user = get_authenticated_user()
|
|
|
|
req = request.get_json()
|
|
|
|
namespace = req['namespace']
|
|
|
|
repository = req['repository']
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
2014-12-11 20:06:30 +00:00
|
|
|
|
2014-11-19 19:50:56 +00:00
|
|
|
if repo:
|
2014-12-11 20:06:30 +00:00
|
|
|
try:
|
2015-07-15 21:25:41 +00:00
|
|
|
model.repository.star_repository(user, repo)
|
2014-12-11 20:06:30 +00:00
|
|
|
except IntegrityError:
|
|
|
|
pass
|
|
|
|
|
2014-11-19 19:50:56 +00:00
|
|
|
return {
|
|
|
|
'namespace': namespace,
|
|
|
|
'repository': repository,
|
|
|
|
}, 201
|
|
|
|
|
2015-04-02 20:34:41 +00:00
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/user/starred/<apirepopath:repository>')
|
2014-11-25 18:25:49 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
2014-11-19 19:50:56 +00:00
|
|
|
class StarredRepository(RepositoryParamResource):
|
|
|
|
""" Operations for managing a specific starred repository. """
|
|
|
|
@nickname('deleteStar')
|
2015-02-19 22:03:36 +00:00
|
|
|
@require_user_admin
|
2014-11-19 19:50:56 +00:00
|
|
|
def delete(self, namespace, repository):
|
2015-05-14 20:47:38 +00:00
|
|
|
""" Removes a star from a repository. """
|
2014-11-19 19:50:56 +00:00
|
|
|
user = get_authenticated_user()
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
2014-12-11 20:06:30 +00:00
|
|
|
|
2014-11-19 19:50:56 +00:00
|
|
|
if repo:
|
2015-07-15 21:25:41 +00:00
|
|
|
model.repository.unstar_repository(user, repo)
|
2016-12-06 21:26:28 +00:00
|
|
|
return '', 204
|
2015-04-02 20:34:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/users/<username>')
|
|
|
|
class Users(ApiResource):
|
|
|
|
""" Operations related to retrieving information about other users. """
|
|
|
|
@nickname('getUserInformation')
|
|
|
|
def get(self, username):
|
|
|
|
""" Get user information for the specified user. """
|
2015-07-15 21:25:41 +00:00
|
|
|
user = model.user.get_nonrobot_user(username)
|
2015-05-26 21:47:33 +00:00
|
|
|
if user is None:
|
2015-04-02 20:34:41 +00:00
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return user_view(user)
|
|
|
|
|