hub.sr.ht/hubsrht/projects.py

32 lines
951 B
Python
Raw Permalink Normal View History

2020-03-24 15:26:15 +01:00
from flask import abort
2020-04-29 16:07:47 +02:00
from hubsrht.types import Project, User, Visibility
2020-03-24 15:26:15 +01:00
from srht.oauth import current_user
from enum import Enum
class ProjectAccess(Enum):
read = "read"
write = "write"
def get_project(owner, project_name, access, user=current_user):
if owner.startswith("~"):
owner = owner[1:]
else:
abort(404)
2020-03-24 15:26:15 +01:00
project = (Project.query
.join(User, Project.owner_id == User.id)
.filter(User.username == owner)
.filter(Project.name == project_name)
).one_or_none()
2020-03-24 15:26:15 +01:00
if not project:
abort(404)
if user != None and user.id == project.owner_id:
return project.owner, project
if access == ProjectAccess.write:
abort(401)
# TODO: ACLs
if project.visibility in (Visibility.PUBLIC, Visibility.UNLISTED):
2020-03-24 15:26:15 +01:00
return project.owner, project
elif project.visibility == Visibility.PRIVATE:
2020-03-24 15:26:15 +01:00
abort(401)
assert False