Skip to content

Commit

Permalink
Disable Pause/Unpause switch if user doesn't have edit access for DAG. (
Browse files Browse the repository at this point in the history
  • Loading branch information
jhtimmins authored Dec 8, 2020
1 parent 3da939b commit 73843d0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
11 changes: 10 additions & 1 deletion airflow/www/static/css/switch.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
font-weight: normal;
}

.switch-label.disabled {
cursor: not-allowed;
}

.switch-input {
/* Visusally hidden input but still accessible */
/* Visually hidden input but still accessible */
position: absolute;
overflow: hidden;
clip: rect(1px, 1px, 1px, 1px);
Expand Down Expand Up @@ -60,6 +64,11 @@
transition-property: transform, background-color;
}

.switch-input:disabled + .switch {
opacity: 0.4;
cursor: not-allowed;
}

.switch-input:checked + .switch {
background-color: #017cee;
}
Expand Down
13 changes: 11 additions & 2 deletions airflow/www/templates/airflow/dag.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,22 @@
<span class="material-icons" aria-hidden="true">keyboard_arrow_up</span>
DAG: {{ dag.parent_dag.dag_id }}</a>
{% endif %}

<div>
<h3 class="pull-left">
{% if dag.parent_dag is defined and dag.parent_dag %}
<span class="text-muted">SUBDAG:</span> {{ dag.dag_id }}
{% else %}
<label class="switch-label js-tooltip" title="Pause/Unpause DAG">
<input class="switch-input" id="pause_resume" data-dag-id="{{ dag.dag_id }}" type="checkbox" {{ "checked" if not dag.is_paused else "" }}>
{% set can_edit = appbuilder.sm.can_edit_dag(dag.dag_id) %}
{% if appbuilder.sm.can_edit_dag(dag.dag_id) %}
{% set switch_tooltip = 'Pause/Unpause DAG' %}
{% else %}
{% set switch_tooltip = 'DAG is Paused' if dag.is_paused else 'DAG is Active' %}
{% endif %}
<label class="switch-label{{' disabled' if not can_edit else '' }} js-tooltip" title="{{ switch_tooltip }}">
<input class="switch-input" id="pause_resume" data-dag-id="{{ dag.dag_id }}"
type="checkbox"{{ " checked" if not dag.is_paused else "" }}
{{ " disabled" if not can_edit else "" }}>
<span class="switch" aria-hidden="true"></span>
</label>
<span class="text-muted">DAG:</span> {{ dag.dag_id }}
Expand Down
11 changes: 9 additions & 2 deletions airflow/www/templates/airflow/dags.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,15 @@ <h2>DAGs</h2>
<tr>
{# Column 1: Turn dag on/off #}
<td style="padding-right:0;">
<label class="switch-label js-tooltip" title="Pause/Unpause DAG">
<input class="switch-input" id="toggle-{{ dag.dag_id }}" data-dag-id="{{ dag.dag_id }}" type="checkbox" {{ "checked" if not dag.is_paused else "" }}>
{% if dag.can_edit %}
{% set switch_tooltip = 'Pause/Unpause DAG' %}
{% else %}
{% set switch_tooltip = 'DAG is Paused' if dag.is_paused else 'DAG is Active' %}
{% endif %}
<label class="switch-label{{' disabled' if not dag.can_edit else '' }} js-tooltip" title="{{ switch_tooltip }}">
<input class="switch-input" id="toggle-{{ dag.dag_id }}" data-dag-id="{{ dag.dag_id }}" type="checkbox"
{{ " checked" if not dag.is_paused else "" }}
{{ " disabled" if not dag.can_edit else "" }}>
<span class="switch" aria-hidden="true"></span>
</label>
</td>
Expand Down
10 changes: 10 additions & 0 deletions airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,16 @@ def get_int_arg(value, default=0):
.all()
)

user_permissions = current_app.appbuilder.sm.get_all_permissions_views()
all_dags_editable = (permissions.ACTION_CAN_EDIT, permissions.RESOURCE_DAG) in user_permissions

for dag in dags:
if all_dags_editable:
dag.can_edit = True
else:
dag_resource_name = permissions.RESOURCE_DAG_PREFIX + dag.dag_id
dag.can_edit = (permissions.ACTION_CAN_EDIT, dag_resource_name) in user_permissions

dagtags = session.query(DagTag.name).distinct(DagTag.name).all()
tags = [
{"name": name, "selected": bool(arg_tags_filter and name in arg_tags_filter)}
Expand Down

0 comments on commit 73843d0

Please sign in to comment.