Created ScopedResource concept, create workspaces module
This commit is contained in:
parent
996596f2b3
commit
89f6c903d1
1
atst/domain/workspaces/__init__.py
Normal file
1
atst/domain/workspaces/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
from .workspaces import Workspaces
|
59
atst/domain/workspaces/scopes.py
Normal file
59
atst/domain/workspaces/scopes.py
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
from atst.domain.authz import Authorization
|
||||||
|
from atst.models.permissions import Permissions
|
||||||
|
from atst.domain.projects import Projects
|
||||||
|
from atst.domain.environments import Environments
|
||||||
|
|
||||||
|
|
||||||
|
class ScopedResource(object):
|
||||||
|
"""
|
||||||
|
An abstract class that represents a resource that is restricted
|
||||||
|
in some way by the priveleges of the user viewing that resource.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, user, resource):
|
||||||
|
self.user = user
|
||||||
|
self.resource = resource
|
||||||
|
|
||||||
|
def __getattr__(self, name):
|
||||||
|
return getattr(self.resource, name)
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.resource == other
|
||||||
|
|
||||||
|
|
||||||
|
class ScopedWorkspace(ScopedResource):
|
||||||
|
"""
|
||||||
|
An object that obeys the same API as a Workspace, but with the added
|
||||||
|
functionality that it only returns sub-resources (projects and environments)
|
||||||
|
that the given user is allowed to see.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def projects(self):
|
||||||
|
if Authorization.has_workspace_permission(
|
||||||
|
self.user, self.resource, Permissions.VIEW_APPLICATION_IN_WORKSPACE
|
||||||
|
):
|
||||||
|
projects = self.resource.projects
|
||||||
|
else:
|
||||||
|
projects = Projects.for_user(self.user, self.resource)
|
||||||
|
|
||||||
|
return [ScopedProject(self.user, project) for project in projects]
|
||||||
|
|
||||||
|
|
||||||
|
class ScopedProject(ScopedResource):
|
||||||
|
"""
|
||||||
|
An object that obeys the same API as a Workspace, but with the added
|
||||||
|
functionality that it only returns sub-resources (environments)
|
||||||
|
that the given user is allowed to see.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def environments(self):
|
||||||
|
if Authorization.has_workspace_permission(
|
||||||
|
self.user, self.resource, Permissions.VIEW_ENVIRONMENT_IN_APPLICATION
|
||||||
|
):
|
||||||
|
environments = self.resource.environments
|
||||||
|
else:
|
||||||
|
environments = Environments.for_user(self.user, self.resource)
|
||||||
|
|
||||||
|
return environments
|
@ -9,6 +9,7 @@ from atst.domain.authz import Authorization
|
|||||||
from atst.models.permissions import Permissions
|
from atst.models.permissions import Permissions
|
||||||
from atst.domain.users import Users
|
from atst.domain.users import Users
|
||||||
from atst.domain.workspace_users import WorkspaceUsers
|
from atst.domain.workspace_users import WorkspaceUsers
|
||||||
|
from .scopes import ScopedWorkspace
|
||||||
|
|
||||||
|
|
||||||
class Workspaces(object):
|
class Workspaces(object):
|
||||||
@ -30,7 +31,7 @@ class Workspaces(object):
|
|||||||
user, workspace, Permissions.VIEW_WORKSPACE, "get workspace"
|
user, workspace, Permissions.VIEW_WORKSPACE, "get workspace"
|
||||||
)
|
)
|
||||||
|
|
||||||
return workspace
|
return ScopedWorkspace(user, workspace)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_for_update(cls, user, workspace_id):
|
def get_for_update(cls, user, workspace_id):
|
Loading…
x
Reference in New Issue
Block a user