Skip to content

Commit

Permalink
Create permissions on addition databases and datasources.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Kyryliuk committed Nov 29, 2016
1 parent b6d9c00 commit 030dbc8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
8 changes: 2 additions & 6 deletions superset/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,7 @@ def link(self):
@property
def schema_perm(self):
"""Returns schema permission if present, database one otherwise."""
if self.schema:
return "[{obj.database}].[{obj.schema}]".format(obj=self)
return self.database.perm
return utils.get_schema_perm(self.database, self.schema)

def get_perm(self):
return (
Expand Down Expand Up @@ -1644,9 +1642,7 @@ def schema(self):
@property
def schema_perm(self):
"""Returns schema permission if present, cluster one otherwise."""
if self.schema:
return "[{obj.database}].[{obj.schema}]".format(obj=self)
return self.cluster.perm
return utils.get_schema_perm(self.cluster, self.schema)

def get_perm(self):
return (
Expand Down
6 changes: 6 additions & 0 deletions superset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ def get_datasource_full_name(database_name, datasource_name, schema=None):
return "[{}].[{}].[{}]".format(database_name, schema, datasource_name)


def get_schema_perm(database, schema):
if schema:
return "[{}].[{}]".format(database, schema)
return database.perm


def validate_json(obj):
if obj:
try:
Expand Down
8 changes: 8 additions & 0 deletions superset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,9 @@ class DatabaseView(SupersetModelView, DeleteMixin): # noqa
def pre_add(self, db):
db.set_sqlalchemy_uri(db.sqlalchemy_uri)
security.merge_perm(sm, 'database_access', db.perm)
for schema in db.all_schema_names():
security.merge_perm(
sm, 'schema_access', utils.get_schema_perm(db, schema))

def pre_update(self, db):
self.pre_add(db)
Expand Down Expand Up @@ -691,6 +694,9 @@ def pre_add(self, table):
def post_add(self, table):
table.fetch_metadata()
security.merge_perm(sm, 'datasource_access', table.perm)
if table.schema:
security.merge_perm(sm, 'schema_access', table.schema_perm)

flash(_(
"The table was created. As part of this two phase configuration "
"process, you should now click the edit button by "
Expand Down Expand Up @@ -1055,6 +1061,8 @@ def pre_add(self, datasource):
def post_add(self, datasource):
datasource.generate_metrics()
security.merge_perm(sm, 'datasource_access', datasource.perm)
if datasource.schema:
security.merge_perm(sm, 'schema_access', datasource.schema_perm)

def post_update(self, datasource):
self.post_add(datasource)
Expand Down

0 comments on commit 030dbc8

Please sign in to comment.