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:]
|
2022-09-06 14:42:22 +02:00
|
|
|
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)
|
2022-07-01 15:57:08 +02:00
|
|
|
.filter(Project.name == project_name)
|
2021-08-08 18:03:52 +02:00
|
|
|
).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
|
2022-06-30 23:05:56 +02:00
|
|
|
if project.visibility in (Visibility.PUBLIC, Visibility.UNLISTED):
|
2020-03-24 15:26:15 +01:00
|
|
|
return project.owner, project
|
2022-06-30 23:05:56 +02:00
|
|
|
elif project.visibility == Visibility.PRIVATE:
|
2020-03-24 15:26:15 +01:00
|
|
|
abort(401)
|
|
|
|
assert False
|