107 lines
2.9 KiB
Python
107 lines
2.9 KiB
Python
import bcrypt
|
|
import logging
|
|
|
|
from database import (User, Repository, Image, RepositoryImage, AccessToken,
|
|
RepositoryPermission, Visibility, Role)
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
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
|
|
|
|
|
|
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
|
|
|
|
|
|
def verify_token(code):
|
|
try:
|
|
return AccessToken.get(code=code)
|
|
except AccessToken.DoesNotExist:
|
|
return None
|
|
|
|
|
|
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()
|
|
|
|
|
|
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):
|
|
try:
|
|
return Repository.get(Repository.name == name and
|
|
Repository.namespace == namespace)
|
|
except Repository.DoesNotExist:
|
|
return None
|
|
|
|
|
|
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)
|
|
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)
|
|
return joined.where(Repository.name == repository_name and
|
|
Repository.namespace == namespace_name)
|
|
|
|
|
|
def create_access_token(repository, user):
|
|
new_token = AccessToken.create(user=user, repository=repository)
|
|
return new_token
|
|
|
|
|
|
def get_user_repo_permissions(user, repository):
|
|
select = RepositoryPermission.select()
|
|
return select.where(RepositoryPermission.user == user and
|
|
RepositoryPermission.repository == repository)
|