Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing overwrite and save slice permissions for a give role #298

Merged
merged 15 commits into from
Apr 18, 2016
Merged
4 changes: 4 additions & 0 deletions caravel/assets/stylesheets/caravel.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ input.form-control {
color: red;
}

.disabledButton {
pointer-events: none;
}

.col-left-fixed {
width:350px;
position: absolute;
Expand Down
6 changes: 3 additions & 3 deletions caravel/templates/caravel/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ <h2>
<button type="button" id="filters" class="btn btn-default" data-toggle="tooltip" title="View the list of active filters">
<i class="fa fa-filter"></i>
</button>
<button type="button" id="css" class="btn btn-default" data-toggle="modal" data-target="#css_modal">
<button type="button" id="css" class="btn btn-default {{ "disabled disabledButton" if not dash_edit_perm }} " data-toggle="modal" data-target="#css_modal">
<i class="fa fa-css3" data-toggle="tooltip" title="Edit the dashboard's CSS"></i>
</button>
<a id="editdash" class="btn btn-default" href="/dashboardmodelview/edit/{{ dashboard.id }}" title="Edit this dashboard's property" data-toggle="tooltip" >
<a id="editdash" class="btn btn-default {{ "disabled disabledButton" if not dash_edit_perm }} " href="/dashboardmodelview/edit/{{ dashboard.id }}" title="Edit this dashboard's property" data-toggle="tooltip" >
<i class="fa fa-edit"></i>
</a>
<button type="button" id="savedash" class="btn btn-default" data-toggle="tooltip" title="Save the current positioning and CSS">
<button type="button" id="savedash" class="btn btn-default {{ "disabled disabledButton" if not dash_save_perm }}" data-toggle="tooltip" title="Save the current positioning and CSS">
<i class="fa fa-save"></i>
</button>
</div>
Expand Down
12 changes: 6 additions & 6 deletions caravel/templates/caravel/explore.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<i class="fa fa-file-code-o"></i>
.json
</span>
<span class="btn btn-default " id="csv" title="Export to .csv format" data-toggle="tooltip">
<span class="btn btn-default {{ "disabled disabledButton" if not can_download }}" id="csv" title="Export to .csv format" data-toggle="tooltip">
<i class="fa fa-file-text-o"></i>.csv
</span>
<span class="btn btn-warning notbtn" id="timer">0 sec</span>
Expand All @@ -86,13 +86,13 @@
<i class="fa fa-bolt"></i>Query
</button>
{% if viz.form_data.slice_id %}
<button type="button" class="btn btn-default" id="btn_overwrite">
<button type="button" class="btn btn-default {{ "disabled disabledButton" if not can_edit }}" id="btn_overwrite">
<i class="fa fa-save"></i>Overwrite
</button>
</button>
{% endif %}
<button type="button" class="btn btn-default" id="btn_save">
<i class="fa fa-plus-circle"></i>Save as
</button>
<button type="button" class="btn btn-default {{ "disabled disabledButton" if not can_add }}" id="btn_save">
<i class="fa fa-plus-circle"></i>Save as
</button>
</div>
</div>
<br/>
Expand Down
50 changes: 35 additions & 15 deletions caravel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ class DatabaseView(CaravelModelView, DeleteMixin): # noqa
order_columns = utils.list_minus(list_columns, ['created_by_'])
add_columns = [
'database_name', 'sqlalchemy_uri', 'cache_timeout', 'extra']
show_columns = add_columns
search_exclude_columns = ('password',)
edit_columns = add_columns
add_template = "caravel/models/database/add.html"
Expand Down Expand Up @@ -463,6 +462,13 @@ def explore(self, datasource_type, datasource_id):
datasource = datasource[0] if datasource else None
slice_id = request.args.get("slice_id")
slc = None
slice_add_perm = self.appbuilder.sm.has_access(
'can_add', 'SliceModelView')
slice_edit_perm = self.appbuilder.sm.has_access(
'can_edit', 'SliceModelView')
slice_download_perm = self.appbuilder.sm.has_access(
'can_download', 'SliceModelView')

if slice_id:
slc = (
db.session.query(models.Slice)
Expand All @@ -483,7 +489,8 @@ def explore(self, datasource_type, datasource_id):

action = request.args.get('action')
if action in ('save', 'overwrite'):
return self.save(request.args, slc)
return self.save_or_overwrite_slice(
request.args, slc, slice_add_perm, slice_edit_perm)

viz_type = request.args.get("viz_type")
if not viz_type and datasource.default_endpoint:
Expand Down Expand Up @@ -529,7 +536,9 @@ def explore(self, datasource_type, datasource_id):
template = "caravel/explore.html"

resp = self.render_template(
template, viz=obj, slice=slc, datasources=datasources)
template, viz=obj, slice=slc, datasources=datasources,
can_add=slice_add_perm, can_edit=slice_edit_perm,
can_download=slice_download_perm)
try:
pass
except Exception as e:
Expand All @@ -541,9 +550,8 @@ def explore(self, datasource_type, datasource_id):
mimetype="application/json")
return resp

def save(self, args, slc):
"""Saves (inserts or overwrite a slice) """
session = db.session()
def save_or_overwrite_slice(self, args, slc, slice_add_perm, slice_edit_perm):
"""save or overwrite a slice"""
slice_name = args.get('slice_name')
action = args.get('action')

Expand All @@ -568,9 +576,6 @@ def save(self, args, slc):

if action == "save":
slc = models.Slice()
msg = "Slice [{}] has been saved".format(slice_name)
elif action == "overwrite":
msg = "Slice [{}] has been overwritten".format(slice_name)

slc.params = json.dumps(d, indent=4, sort_keys=True)
slc.datasource_name = args.get('datasource_name')
Expand All @@ -580,13 +585,26 @@ def save(self, args, slc):
slc.datasource_type = datasource_type
slc.slice_name = slice_name

if action == "save":
session.add(slc)
elif action == "overwrite":
session.merge(slc)
if action == 'save' and slice_add_perm:
self.save_slice(slc)
elif action == 'overwrite' and slice_edit_perm:
self.overwrite_slice(slc)

return redirect(slc.slice_url)

def save_slice(self, slc):
session = db.session()
msg = "Slice [{}] has been saved".format(slc.slice_name)
session.add(slc)
session.commit()
flash(msg, "info")

def overwrite_slice(self, slc):
session = db.session()
msg = "Slice [{}] has been overwritten".format(slc.slice_name)
session.merge(slc)
session.commit()
flash(msg, "info")
return redirect(slc.slice_url)

@has_access
@expose("/checkbox/<model_view>/<id_>/<attr>/<value>", methods=['GET'])
Expand Down Expand Up @@ -711,7 +729,9 @@ def dashboard(**kwargs): # noqa
return self.render_template(
"caravel/dashboard.html", dashboard=dash,
templates=templates,
pos_dict=pos_dict)
pos_dict=pos_dict,
dash_save_perm=appbuilder.sm.has_access('can_save_dash', 'Caravel'),
dash_edit_perm=appbuilder.sm.has_access('can_edit', 'DashboardModelView'))

@has_access
@expose("/sql/<database_id>/")
Expand Down