Skip to content

Commit

Permalink
add teams to dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
pa1ch committed Dec 9, 2024
1 parent 0886de0 commit 85e5e6f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
28 changes: 28 additions & 0 deletions superset/dashboards/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import enum
import json
import re
from typing import Any, Union
Expand Down Expand Up @@ -45,6 +46,12 @@
"level access. "
"If no roles defined then the dashboard is available to all roles."
)
teams_description = (
"Teams is a list which defines access to the dashboard. "
"These teams are always applied in addition to restrictions on dataset "
"level access. "
"If no teams defined then the dashboard is available to all teams."
)
position_json_description = (
"This json object describes the positioning of the widgets "
"in the dashboard. It is dynamically generated when "
Expand Down Expand Up @@ -158,6 +165,18 @@ def remove_show_native_filters( # pylint: disable=unused-argument, no-self-use
return data


class AccessLevel(enum.Enum):

"""
Access level for dashboard.
Can be setted for user, role and team
"""

read = 1
edit = 2


class UserSchema(Schema):
id = fields.Int()
username = fields.String()
Expand All @@ -168,7 +187,13 @@ class UserSchema(Schema):
class RolesSchema(Schema):
id = fields.Int()
name = fields.String()
access_level = fields.Enum(AccessLevel, by_value=True)


class TeamSchema(Schema):
id = fields.Int()
name = fields.String()
access_level = fields.Enum(AccessLevel, by_value=True)

class TagSchema(Schema):
id = fields.Int()
Expand Down Expand Up @@ -198,6 +223,7 @@ class DashboardGetResponseSchema(Schema):
charts = fields.List(fields.String(metadata={"description": charts_description}))
owners = fields.List(fields.Nested(UserSchema(exclude=(["username"]))))
roles = fields.List(fields.Nested(RolesSchema))
teams = fields.List(fields.Nested(TeamSchema))
tags = fields.Nested(TagSchema, many=True)
changed_on_humanized = fields.String(data_key="changed_on_delta_humanized")
is_managed_externally = fields.Boolean(allow_none=True, dump_default=False)
Expand Down Expand Up @@ -276,6 +302,7 @@ class DashboardPostSchema(BaseDashboardSchema):
)
owners = fields.List(fields.Integer(metadata={"description": owners_description}))
roles = fields.List(fields.Integer(metadata={"description": roles_description}))
teams = fields.List(fields.Integer(metadata={"description": teams_description}))
position_json = fields.String(
metadata={"description": position_json_description}, validate=validate_json
)
Expand Down Expand Up @@ -332,6 +359,7 @@ class DashboardPutSchema(BaseDashboardSchema):
roles = fields.List(
fields.Integer(metadata={"description": roles_description}, allow_none=True)
)
teams = fields.List(fields.Integer(metadata={"description": teams_description}, allow_none=True))
position_json = fields.String(
metadata={"description": position_json_description},
allow_none=True,
Expand Down
28 changes: 28 additions & 0 deletions superset/models/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from superset.models.filter_set import FilterSet
from superset.models.helpers import AuditMixinNullable, ImportExportMixin
from superset.models.slice import Slice
from superset.models.team import Team
from superset.models.user_attributes import UserAttribute
from superset.tasks.thumbnails import cache_dashboard_thumbnail
from superset.tasks.utils import get_current_user
Expand Down Expand Up @@ -139,6 +140,32 @@ def copy_dashboard(_mapper: Mapper, connection: Connection, target: Dashboard) -
ForeignKey("ab_role.id", ondelete="CASCADE"),
nullable=False,
),
Column(
"access_level",
Integer,
nullable=False,
default=1
)
)


DashboardTeams = Table(
"dashboard_teams",
metadata,
Column("id", Integer, primary_key=True),
Column(
"dashboard_id",
Integer,
ForeignKey("dashboards.id", ondelete="CASCADE"),
nullable=False,
),
Column(
"team_id",
Integer,
ForeignKey("teams.id", ondelete="CASCADE"),
nullable=False,
),
Column("access_level", Integer, nullable=False, default=1),
)


Expand Down Expand Up @@ -177,6 +204,7 @@ class Dashboard(Model, AuditMixinNullable, ImportExportMixin):
is_managed_externally = Column(Boolean, nullable=False, default=False)
external_url = Column(Text, nullable=True)
roles = relationship(security_manager.role_model, secondary=DashboardRoles)
teams = relationship(Team, secondary=DashboardTeams)
embedded = relationship(
"EmbeddedDashboard",
back_populates="dashboard",
Expand Down
2 changes: 0 additions & 2 deletions superset/models/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
Boolean,
String,
Table,
UniqueConstraint,
)
from sqlalchemy.orm import relationship
from flask_appbuilder import Model
from flask_appbuilder.security.sqla.models import User

from superset import security_manager

Expand Down
5 changes: 2 additions & 3 deletions superset/teams/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# DODO added #32839641

import logging
from flask import request, Response, g
from flask import request, Response
from flask_appbuilder.models.sqla.interface import SQLAInterface
from flask_appbuilder.api import expose, protect, safe
from marshmallow import ValidationError
Expand Down Expand Up @@ -34,8 +34,7 @@
BaseSupersetModelRestApi,
statsd_metrics,
)
from superset.views.utils import (finish_onboarding, get_dodo_role, find_team_by_slug,
update_user_roles)
from superset.views.utils import update_user_roles

logger = logging.getLogger(__name__)

Expand Down

0 comments on commit 85e5e6f

Please sign in to comment.