Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

5.8 KB
Raw Blame
"""Per-resource access grants (#33 — per-site ACL).

A grant gives a user access to a specific resource (currently an application,
which transitively covers its WordPress site / databases / domains) without
owning it. This service is the single seam for creating, revoking, querying, and
checking grants; enforcement lives in WorkspaceService.scope_query (lists) and
the per-resource access helpers in the API layer.
"""

RESOURCE_APPLICATION = 'application'


class ResourceGrantService:

    @staticmethod
    def granted_ids(user_id, resource_type):
        """The resource_ids of `resource_type` this user has been granted."""
        from app.models.workspace import ResourceGrant
        rows = (ResourceGrant.query
                .with_entities(ResourceGrant.resource_id)
                .filter_by(user_id=user_id, resource_type=resource_type)
                .all())
        return [r[0] for r in rows]

    @staticmethod
    def user_has_grant(user_id, resource_type, resource_id):
        from app.models.workspace import ResourceGrant
        return ResourceGrant.query.filter_by(
            user_id=user_id, resource_type=resource_type, resource_id=resource_id
        ).first() is not None

    @staticmethod
    def grant_role(user_id, resource_type, resource_id):
        """The role of this user's grant on the resource ('viewer'/'editor'), or
        None if there is no grant."""
        from app.models.workspace import ResourceGrant
        row = ResourceGrant.query.filter_by(
            user_id=user_id, resource_type=resource_type, resource_id=resource_id
        ).first()
        return row.role if row else None

    # --- access helpers (the shared seam used by every app-related blueprint) ---

    @staticmethod
    def can_access_app(user, app):
        """Read access to an application: owner, admin, or ANY grant (#33 ACL).
        Uses user.id (int) — get_jwt_identity() is the stringified token id."""
        if user.is_admin or app.user_id == user.id:
            return True
        return ResourceGrantService.user_has_grant(user.id, 'application', app.id)

    @staticmethod
    def can_edit_app(user, app):
        """Write/operate access to an application: owner, admin, or an EDITOR
        grant (#33 ACL). A viewer grant confers read access only."""
        if user.is_admin or app.user_id == user.id:
            return True
        return ResourceGrantService.grant_role(user.id, 'application', app.id) == 'editor'

    @classmethod
    def get_app_for(cls, user, app_id, *, write=False, conceal=False):
        """The live application ``user`` may read (or edit, with ``write``).

        A missing app is a 404. A denied caller gets a 403, or the same 404 as
        a missing app when ``conceal`` is set, for surfaces that must not
        reveal that the app exists.
        """
        from app.exceptions import not_found, permission_denied
        from app.models.application import Application
        app = Application.query_active().filter_by(id=app_id).first()
        if app is None:
            raise not_found('service')
        allowed = cls.can_edit_app if write else cls.can_access_app
        if user is None or not allowed(user, app):
            raise not_found('service') if conceal else permission_denied()
        return app

    @staticmethod
    def can_operate_app(user, app):
        """Operate (member+) access to an application. Folds every path to the
        app into one capability tier (see rbac.app_access_tier) and admits
        anyone at 'member' or above: owner, panel admin, an editor grant, or a
        workspace member/admin/owner. The member-write seam."""
        from app.middleware.rbac import app_access_tier, _APP_ROLE_RANK
        tier = app_access_tier(user, app)
        return tier is not None and _APP_ROLE_RANK[tier] >= _APP_ROLE_RANK['member']

    @staticmethod
    def can_admin_app(user, app):
        """Administer (admin+) access to an application: panel admin, the app
        owner, or a workspace admin/owner. Grants never confer admin."""
        from app.middleware.rbac import app_access_tier, _APP_ROLE_RANK
        tier = app_access_tier(user, app)
        return tier is not None and _APP_ROLE_RANK[tier] >= _APP_ROLE_RANK['admin']

    @staticmethod
    def list_for_resource(resource_type, resource_id):
        from app.models.workspace import ResourceGrant
        return ResourceGrant.query.filter_by(
            resource_type=resource_type, resource_id=resource_id
        ).all()

    @staticmethod
    def grant(user_id, resource_type, resource_id, granted_by=None, role='editor'):
        """Create (or update) a grant. Idempotent on (user, resource)."""
        from app import db
        from app.models.workspace import ResourceGrant
        existing = ResourceGrant.query.filter_by(
            user_id=user_id, resource_type=resource_type, resource_id=resource_id
        ).first()
        if existing:
            existing.role = role
            db.session.commit()
            return existing
        row = ResourceGrant(user_id=user_id, resource_type=resource_type,
                            resource_id=resource_id, role=role, granted_by=granted_by)
        db.session.add(row)
        db.session.commit()
        return row

    @staticmethod
    def revoke(grant_id, resource_type=None, resource_id=None):
        """Delete a grant by id. When resource_type/resource_id are given, the
        grant must belong to that resource (so a caller can't revoke another
        resource's grant by id)."""
        from app import db
        from app.models.workspace import ResourceGrant
        row = ResourceGrant.query.get(grant_id)
        if row is None:
            return False
        if resource_type is not None and (row.resource_type != resource_type or row.resource_id != resource_id):
            return False
        db.session.delete(row)
        db.session.commit()
        return True