2013-09-20 15:55:44 +00:00
|
|
|
import bcrypt
|
2013-09-20 22:38:17 +00:00
|
|
|
import logging
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-25 20:46:28 +00:00
|
|
|
from database import *
|
2013-09-20 22:38:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_user(username, password, email):
|
|
|
|
pw_hash = bcrypt.hashpw(password, bcrypt.gensalt())
|
|
|
|
new_user = User.create(username=username, password_hash=pw_hash,
|
|
|
|
email=email)
|
|
|
|
return new_user
|
|
|
|
|
|
|
|
|
2013-09-23 16:37:40 +00:00
|
|
|
def get_user(username):
|
|
|
|
try:
|
|
|
|
return User.get(User.username == username)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
def verify_user(username, password):
|
|
|
|
try:
|
|
|
|
fetched = User.get(User.username == username)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if bcrypt.hashpw(password, fetched.password_hash) == fetched.password_hash:
|
|
|
|
return fetched
|
|
|
|
|
|
|
|
# We weren't able to authorize the user
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
def verify_token(code):
|
|
|
|
try:
|
|
|
|
return AccessToken.get(code=code)
|
|
|
|
except AccessToken.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
def change_password(user, new_password):
|
|
|
|
pw_hash = bcrypt.hashpw(new_password, bcrypt.gensalt())
|
|
|
|
user.password_hash = pw_hash
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
|
|
|
|
def update_email(user, new_email):
|
|
|
|
user.email = new_email
|
|
|
|
user.verified = False
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
def get_all_repo_permissions(user):
|
|
|
|
select = User.select(User, Repository, RepositoryPermission)
|
|
|
|
joined = select.join(RepositoryPermission).join(Repository)
|
|
|
|
return joined.where(User.username == user.username)
|
|
|
|
|
|
|
|
|
|
|
|
def get_repository(namespace, name):
|
2013-09-20 15:55:44 +00:00
|
|
|
try:
|
2013-09-20 22:38:17 +00:00
|
|
|
return Repository.get(Repository.name == name and
|
2013-09-20 15:55:44 +00:00
|
|
|
Repository.namespace == namespace)
|
|
|
|
except Repository.DoesNotExist:
|
2013-09-20 22:38:17 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2013-09-23 16:37:40 +00:00
|
|
|
def get_user_repositories(user):
|
|
|
|
select = RepositoryPermission.select(RepositoryPermission, Repository, Role)
|
|
|
|
with_user = select.join(User).where(User.username == user.username)
|
|
|
|
with_role = with_user.switch(RepositoryPermission).join(Role)
|
|
|
|
with_repo = with_role.switch(RepositoryPermission).join(Repository)
|
|
|
|
return with_repo
|
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
def create_repository(namespace, name, owner):
|
|
|
|
private = Visibility.get(name='private')
|
|
|
|
repo = Repository.create(namespace=namespace, name=name,
|
|
|
|
visibility=private)
|
|
|
|
admin = Role.get(name='admin')
|
|
|
|
permission = RepositoryPermission.create(user=owner, repository=repo,
|
|
|
|
role=admin)
|
2013-09-20 15:55:44 +00:00
|
|
|
return repo
|
|
|
|
|
|
|
|
|
|
|
|
def create_image(image_id):
|
|
|
|
new_image = Image.create(image_id=image_id)
|
|
|
|
return new_image
|
|
|
|
|
|
|
|
|
|
|
|
def set_image_checksum(image_id, checksum):
|
|
|
|
fetched = Image.get(Image.image_id == image_id)
|
|
|
|
fetched.checksum = checksum
|
|
|
|
fetched.save()
|
|
|
|
return fetched
|
|
|
|
|
|
|
|
|
|
|
|
def assign_image_repository(repository, image, tag):
|
|
|
|
repo_image = RepositoryImage.create(repository=repository, image=image,
|
|
|
|
tag=tag)
|
|
|
|
return repo_image
|
|
|
|
|
|
|
|
|
|
|
|
def get_repository_images(namespace_name, repository_name):
|
|
|
|
select = Image.select(Image, RepositoryImage)
|
|
|
|
joined = select.join(RepositoryImage).join(Repository)
|
2013-09-20 22:38:17 +00:00
|
|
|
return joined.where(Repository.name == repository_name and
|
2013-09-20 15:55:44 +00:00
|
|
|
Repository.namespace == namespace_name)
|
|
|
|
|
|
|
|
|
2013-09-25 20:46:28 +00:00
|
|
|
def list_repository_tags(namespace_name, repository_name):
|
|
|
|
select = RepositoryTag.select(RepositoryTag, Image)
|
|
|
|
with_repo = select.join(Repository)
|
|
|
|
with_image = with_repo.switch(RepositoryTag).join(Image)
|
2013-09-25 21:46:42 +00:00
|
|
|
return with_image.where(Repository.name == repository_name and
|
|
|
|
Repository.namespace == namespace_name)
|
2013-09-25 20:46:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_tag_image(namespace_name, repository_name, tag_name):
|
|
|
|
joined = Image.select().join(RepositoryTag).join(Repository)
|
|
|
|
return joined.where(Repository.name == repository_name and
|
|
|
|
Repository.namespace == namespace_name and
|
|
|
|
RepositoryTag.name == tag_name)
|
|
|
|
|
|
|
|
|
|
|
|
def create_or_update_tag(namespace_name, repository_name, tag_name,
|
|
|
|
tag_image_id):
|
2013-09-25 21:46:42 +00:00
|
|
|
repo = Repository.get(Repository.name == repository_name and
|
|
|
|
Repository.namespace == namespace_name)
|
2013-09-25 20:46:28 +00:00
|
|
|
image = Image.get(Image.image_id == tag_image_id)
|
|
|
|
|
|
|
|
try:
|
|
|
|
tag = RepositoryTag.get(RepositoryTag.repository == repo and
|
2013-09-25 21:46:42 +00:00
|
|
|
RepositoryTag.name == tag_name)
|
2013-09-25 20:46:28 +00:00
|
|
|
tag.image = image
|
|
|
|
tag.save()
|
|
|
|
except RepositoryTag.DoesNotExist:
|
|
|
|
tag = RepositoryTag.create(repository=repo, image=image, name=tag_name)
|
|
|
|
|
|
|
|
return tag
|
|
|
|
|
|
|
|
|
|
|
|
def delete_tag(namespace_name, repository_name, tag_name):
|
2013-09-25 21:46:42 +00:00
|
|
|
repo = Repository.get(Repository.name == repository_name and
|
|
|
|
Repository.namespace == namespace_name)
|
2013-09-25 20:46:28 +00:00
|
|
|
tag = RepositoryTag.get(RepositoryTag.repository == repo and
|
2013-09-25 21:46:42 +00:00
|
|
|
RepositoryTag.name == tag_name)
|
2013-09-25 20:46:28 +00:00
|
|
|
tag.delete_instance()
|
|
|
|
|
|
|
|
|
|
|
|
def delete_all_repository_tags(namespace_name, repository_name):
|
2013-09-25 21:46:42 +00:00
|
|
|
repo = Repository.get(Repository.name == repository_name and
|
|
|
|
Repository.namespace == namespace_name)
|
2013-09-25 20:46:28 +00:00
|
|
|
RepositoryTag.delete().where(RepositoryTag.repository == repo)
|
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
def create_access_token(repository, user):
|
|
|
|
new_token = AccessToken.create(user=user, repository=repository)
|
2013-09-20 15:55:44 +00:00
|
|
|
return new_token
|
|
|
|
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
def get_user_repo_permissions(user, repository):
|
|
|
|
select = RepositoryPermission.select()
|
|
|
|
return select.where(RepositoryPermission.user == user and
|
|
|
|
RepositoryPermission.repository == repository)
|