2014-03-13 19:19:49 +00:00
|
|
|
import logging
|
2014-03-19 19:39:44 +00:00
|
|
|
import json
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2015-03-25 22:43:12 +00:00
|
|
|
from random import SystemRandom
|
2015-04-02 20:34:41 +00:00
|
|
|
from flask import request, abort
|
2014-03-13 19:19:49 +00:00
|
|
|
from flask.ext.login import logout_user
|
|
|
|
from flask.ext.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
|
|
|
|
2014-11-25 00:25:13 +00:00
|
|
|
from app import app, billing as stripe, authentication, avatar
|
2014-03-13 19:19:49 +00:00
|
|
|
from endpoints.api import (ApiResource, nickname, resource, validate_json_request, request_error,
|
2014-08-26 19:19:39 +00:00
|
|
|
log_action, internal_only, NotFound, require_user_admin, parse_args,
|
|
|
|
query_param, InvalidToken, require_scope, format_date, hide_if, show_if,
|
2014-11-19 19:50:56 +00:00
|
|
|
license_error, require_fresh_login, path_param, define_json_response,
|
|
|
|
RepositoryParamResource)
|
2014-03-13 19:19:49 +00:00
|
|
|
from endpoints.api.subscribe import subscribe
|
|
|
|
from endpoints.common import common_login
|
2014-09-11 19:45:41 +00:00
|
|
|
from endpoints.api.team import try_accept_invite
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
from data import model
|
2014-04-10 19:20:16 +00:00
|
|
|
from data.billing import get_plan
|
2014-03-25 21:26:45 +00:00
|
|
|
from auth.permissions import (AdministerOrganizationPermission, CreateRepositoryPermission,
|
2014-04-10 04:26:55 +00:00
|
|
|
UserAdminPermission, UserReadPermission, SuperUserPermission)
|
2014-03-13 19:19:49 +00:00
|
|
|
from auth.auth_context import get_authenticated_user
|
2014-03-19 17:57:36 +00:00
|
|
|
from auth import scopes
|
2014-09-05 23:57:33 +00:00
|
|
|
from util.useremails import (send_confirmation_email, send_recovery_email, send_change_email, send_password_changed)
|
2014-09-11 19:45:41 +00:00
|
|
|
from util.names import parse_single_urn
|
2014-03-13 19:19:49 +00:00
|
|
|
|
2014-04-03 23:32:09 +00:00
|
|
|
import features
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def user_view(user):
|
|
|
|
def org_view(o):
|
|
|
|
admin_org = AdministerOrganizationPermission(o.username)
|
|
|
|
return {
|
|
|
|
'name': o.username,
|
2015-03-30 21:55:04 +00:00
|
|
|
'avatar': avatar.get_data_for_org(o),
|
2014-03-13 19:19:49 +00:00
|
|
|
'is_org_admin': admin_org.can(),
|
|
|
|
'can_create_repo': admin_org.can() or CreateRepositoryPermission(o.username).can(),
|
|
|
|
'preferred_namespace': not (o.stripe_id is None)
|
|
|
|
}
|
|
|
|
|
|
|
|
organizations = model.get_user_organizations(user.username)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
logins = model.list_federated_logins(user)
|
|
|
|
|
2014-03-25 21:26:45 +00:00
|
|
|
user_response = {
|
2014-03-13 19:19:49 +00:00
|
|
|
'anonymous': False,
|
|
|
|
'username': user.username,
|
2015-03-30 21:55:04 +00:00
|
|
|
'avatar': avatar.get_data_for_user(user)
|
2014-03-13 19:19:49 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 21:26:45 +00:00
|
|
|
user_admin = UserAdminPermission(user.username)
|
|
|
|
if user_admin.can():
|
|
|
|
user_response.update({
|
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
|
|
|
'organizations': [org_view(o) for o in organizations],
|
|
|
|
'logins': [login_view(login) for login in logins],
|
|
|
|
'can_create_repo': True,
|
|
|
|
'invoice_email': user.invoice_email,
|
|
|
|
'preferred_namespace': not (user.stripe_id is None),
|
2015-02-12 19:37:11 +00:00
|
|
|
'tag_expiration': user.removed_tag_expiration_s,
|
2014-03-25 21:26:45 +00:00
|
|
|
})
|
|
|
|
|
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
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
def notification_view(notification):
|
|
|
|
return {
|
2014-07-28 22:23:46 +00:00
|
|
|
'id': notification.uuid,
|
2014-03-19 19:39:44 +00:00
|
|
|
'organization': notification.target.username if notification.target.organization else None,
|
|
|
|
'kind': notification.kind.name,
|
|
|
|
'created': format_date(notification.created),
|
|
|
|
'metadata': json.loads(notification.metadata_json),
|
2014-07-28 22:23:46 +00:00
|
|
|
'dismissed': notification.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': {
|
2014-08-11 22:25:01 +00:00
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
'id': 'NewUser',
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Fields which must be specified for a new user.',
|
2014-03-17 16:25:41 +00:00
|
|
|
'required': [
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
'email',
|
|
|
|
],
|
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',
|
|
|
|
'description': 'The optional invite code'
|
|
|
|
}
|
2014-03-13 19:19:49 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'UpdateUser': {
|
|
|
|
'id': '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',
|
|
|
|
},
|
2015-02-12 19:37:11 +00:00
|
|
|
'tag_expiration': {
|
|
|
|
'type': 'integer',
|
|
|
|
'maximum': 2592000,
|
|
|
|
'minimum': 0,
|
|
|
|
},
|
2014-10-01 18:23:15 +00:00
|
|
|
'username': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The user\'s username',
|
|
|
|
},
|
2014-03-13 19:19:49 +00:00
|
|
|
},
|
|
|
|
},
|
2014-08-28 00:57:46 +00:00
|
|
|
'UserView': {
|
|
|
|
'id': 'UserView',
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Describes a user',
|
2014-11-26 00:59:24 +00:00
|
|
|
'required': ['verified', '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',
|
|
|
|
'description': 'Information about the organizations in which the user is a member'
|
|
|
|
},
|
|
|
|
'logins': {
|
|
|
|
'type': 'array',
|
|
|
|
'description': 'The list of external login providers against which the user has authenticated'
|
|
|
|
},
|
|
|
|
'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')
|
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()
|
|
|
|
|
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.
|
2014-03-13 19:19:49 +00:00
|
|
|
model.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)
|
|
|
|
model.change_invoice_email(user, user_data['invoice_email'])
|
|
|
|
|
2015-02-12 19:37:11 +00:00
|
|
|
if 'tag_expiration' in user_data:
|
|
|
|
logger.debug('Changing user tag expiration to: %ss', user_data['tag_expiration'])
|
|
|
|
model.change_user_tag_expiration(user, user_data['tag_expiration'])
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
if 'email' in user_data and user_data['email'] != user.email:
|
|
|
|
new_email = user_data['email']
|
|
|
|
if model.find_user_by_email(new_email):
|
|
|
|
# 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)
|
|
|
|
code = model.create_confirm_email_code(user, new_email=new_email)
|
|
|
|
send_change_email(user.username, user_data['email'], code.code)
|
|
|
|
else:
|
|
|
|
model.update_email(user, new_email, auto_verify=not features.MAILING)
|
2014-10-01 18:23:15 +00:00
|
|
|
|
2014-11-20 20:36:39 +00:00
|
|
|
if ('username' in user_data and user_data['username'] != user.username and
|
|
|
|
features.USER_RENAME):
|
2014-10-01 18:23:15 +00:00
|
|
|
new_username = user_data['username']
|
|
|
|
if model.get_user_or_org(new_username) is not None:
|
|
|
|
# Username already used
|
|
|
|
raise request_error(message='Username is already in use')
|
|
|
|
|
2015-01-30 21:32:13 +00:00
|
|
|
model.change_username(user.id, new_username)
|
2014-11-26 00:59:24 +00:00
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
except model.InvalidPasswordException, ex:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(exception=ex)
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
return user_view(user)
|
|
|
|
|
2014-10-02 18:49:18 +00:00
|
|
|
@show_if(features.USER_CREATION)
|
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. """
|
2014-03-13 19:19:49 +00:00
|
|
|
user_data = request.get_json()
|
2014-10-03 19:05:34 +00:00
|
|
|
invite_code = user_data.get('invite_code', '')
|
2014-03-13 19:19:49 +00:00
|
|
|
|
|
|
|
existing_user = model.get_user(user_data['username'])
|
|
|
|
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
|
|
|
|
|
|
|
try:
|
|
|
|
new_user = model.create_user(user_data['username'], user_data['password'],
|
2014-09-22 23:11:48 +00:00
|
|
|
user_data['email'], auto_verify=not features.MAILING)
|
2014-09-11 19:45:41 +00:00
|
|
|
|
|
|
|
# Handle any invite codes.
|
|
|
|
parsed_invite = parse_single_urn(invite_code)
|
|
|
|
if parsed_invite is not None:
|
|
|
|
if parsed_invite[0] == 'teaminvite':
|
|
|
|
# Add the user to the team.
|
2014-09-12 18:29:01 +00:00
|
|
|
try:
|
|
|
|
try_accept_invite(invite_code, new_user)
|
|
|
|
except model.DataModelException:
|
|
|
|
pass
|
2014-09-11 19:45:41 +00:00
|
|
|
|
2014-09-22 23:11:48 +00:00
|
|
|
|
|
|
|
if features.MAILING:
|
|
|
|
code = model.create_confirm_email_code(new_user)
|
|
|
|
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)
|
|
|
|
|
2014-05-28 19:22:36 +00:00
|
|
|
except model.TooManyUsersException as ex:
|
|
|
|
raise license_error(exception=ex)
|
2014-03-13 19:19:49 +00:00
|
|
|
except model.DataModelException as ex:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise request_error(exception=ex)
|
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()
|
|
|
|
private_repos = model.get_private_repo_count(user.username)
|
|
|
|
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': {
|
|
|
|
'id': '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
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
def conduct_signin(username_or_email, password):
|
|
|
|
needs_email_verification = False
|
|
|
|
invalid_credentials = False
|
|
|
|
|
2014-05-28 19:53:53 +00:00
|
|
|
verified = None
|
|
|
|
try:
|
2015-03-25 22:43:12 +00:00
|
|
|
(verified, error_message) = authentication.verify_user(username_or_email, password)
|
2014-05-28 19:53:53 +00:00
|
|
|
except model.TooManyUsersException as ex:
|
|
|
|
raise license_error(exception=ex)
|
|
|
|
|
2014-03-13 19:19:49 +00:00
|
|
|
if verified:
|
|
|
|
if common_login(verified):
|
|
|
|
return {'success': True}
|
|
|
|
else:
|
|
|
|
needs_email_verification = True
|
|
|
|
|
|
|
|
else:
|
|
|
|
invalid_credentials = True
|
|
|
|
|
|
|
|
return {
|
|
|
|
'needsEmailVerification': needs_email_verification,
|
|
|
|
'invalidCredentials': invalid_credentials,
|
|
|
|
}, 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': {
|
|
|
|
'id': '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-03-25 22:43:12 +00:00
|
|
|
(admin_user, error_message) = authentication.verify_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',
|
2014-03-13 19:19:49 +00:00
|
|
|
message='The admin user credentials are not valid')
|
|
|
|
|
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:
|
|
|
|
raise request_error(reason='invaliduser',
|
|
|
|
message='The admin user is not valid')
|
|
|
|
|
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.
|
2014-07-08 22:19:13 +00:00
|
|
|
model.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')
|
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': {
|
|
|
|
'id': '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',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@nickname('signinUser')
|
|
|
|
@validate_json_request('SigninUser')
|
|
|
|
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']
|
|
|
|
|
|
|
|
return conduct_signin(username, password)
|
|
|
|
|
|
|
|
|
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']
|
|
|
|
return conduct_signin(get_authenticated_user().username, password)
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
|
|
|
|
2014-09-15 16:01:02 +00:00
|
|
|
|
|
|
|
@resource('/v1/detachexternal/<servicename>')
|
|
|
|
@internal_only
|
|
|
|
class DetachExternal(ApiResource):
|
|
|
|
""" Resource for detaching an external login. """
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('detachExternalLogin')
|
|
|
|
def post(self, servicename):
|
|
|
|
""" Request that the current user be detached from the external login service. """
|
|
|
|
model.detach_external_login(get_authenticated_user(), servicename)
|
|
|
|
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': {
|
|
|
|
'id': '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')
|
|
|
|
@validate_json_request('RequestRecovery')
|
|
|
|
def post(self):
|
|
|
|
""" Request a password recovery email."""
|
|
|
|
email = request.get_json()['email']
|
|
|
|
code = model.create_reset_password_email_code(email)
|
|
|
|
send_recovery_email(email, code.code)
|
|
|
|
return 'Created', 201
|
2014-03-19 19:39:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/user/notifications')
|
|
|
|
@internal_only
|
|
|
|
class UserNotificationList(ApiResource):
|
|
|
|
@require_user_admin
|
2014-08-26 19:19:39 +00:00
|
|
|
@parse_args
|
|
|
|
@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')
|
2014-08-26 19:19:39 +00:00
|
|
|
def get(self, args):
|
|
|
|
page = args['page']
|
|
|
|
limit = args['limit']
|
|
|
|
|
|
|
|
notifications = list(model.list_notifications(get_authenticated_user(), page=page, limit=limit + 1))
|
|
|
|
has_more = False
|
|
|
|
|
|
|
|
if len(notifications) > limit:
|
|
|
|
has_more = True
|
|
|
|
notifications = notifications[0:limit]
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
return {
|
2014-08-26 19:19:39 +00:00
|
|
|
'notifications': [notification_view(notification) for notification in notifications],
|
|
|
|
'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': {
|
|
|
|
'id': '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):
|
|
|
|
notification = model.lookup_notification(get_authenticated_user(), uuid)
|
|
|
|
if not notification:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
return notification_view(notification)
|
|
|
|
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('updateUserNotification')
|
|
|
|
@validate_json_request('UpdateNotification')
|
|
|
|
def put(self, uuid):
|
|
|
|
notification = model.lookup_notification(get_authenticated_user(), uuid)
|
|
|
|
if not notification:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
notification.dismissed = request.get_json().get('dismissed', False)
|
|
|
|
notification.save()
|
|
|
|
|
|
|
|
return notification_view(notification)
|
|
|
|
|
|
|
|
|
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):
|
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()
|
|
|
|
|
|
|
|
access_token.delete_instance(recursive=True, delete_nullable=True)
|
|
|
|
return 'Deleted', 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': {
|
|
|
|
'id': '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')
|
|
|
|
@parse_args
|
|
|
|
@query_param('page', 'Offset page number. (int)', type=int)
|
|
|
|
@query_param('limit', 'Limit on the number of results (int)', type=int)
|
2015-02-19 22:03:36 +00:00
|
|
|
@require_user_admin
|
2014-11-30 22:46:24 +00:00
|
|
|
def get(self, args):
|
2014-11-19 19:50:56 +00:00
|
|
|
""" List all starred repositories. """
|
|
|
|
page = args['page']
|
|
|
|
limit = args['limit']
|
2015-02-24 22:50:54 +00:00
|
|
|
starred_repos = model.get_user_starred_repositories(get_authenticated_user(),
|
|
|
|
page=page,
|
|
|
|
limit=limit)
|
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',
|
|
|
|
}
|
|
|
|
|
2014-11-19 19:50:56 +00:00
|
|
|
return {'repositories': [repo_view(repo) for repo in starred_repos]}
|
|
|
|
|
|
|
|
@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']
|
|
|
|
repo = model.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:
|
|
|
|
model.star_repository(user, repo)
|
|
|
|
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
|
|
|
|
2014-11-19 19:50:56 +00:00
|
|
|
@resource('/v1/user/starred/<repopath: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. """
|
2015-02-23 19:23:32 +00:00
|
|
|
|
2014-11-19 19:50:56 +00:00
|
|
|
@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):
|
|
|
|
user = get_authenticated_user()
|
|
|
|
repo = model.get_repository(namespace, repository)
|
2014-12-11 20:06:30 +00:00
|
|
|
|
2014-11-19 19:50:56 +00:00
|
|
|
if repo:
|
|
|
|
model.unstar_repository(user, repo)
|
|
|
|
return 'Deleted', 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. """
|
|
|
|
user = model.get_user(username)
|
|
|
|
if user is None or user.organization or user.robot:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return user_view(user)
|
|
|
|
|