{#
# This file is part of Invenio.
# Copyright (C) 2012, 2014 CERN.
#
# Invenio is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#}
{% extends "page.html" %}
{% set title = role.name %}

{% block body %}
<script type="text/javascript">
$(document).ready(function() {
  $('#toggleids').change(function() {
    var toggle = $('#toggleids').attr("checked")
    $(".ids").attr("checked", function(i, val) {
      return ! $(this).attr("checked");
    });
  });
});
</script>

<ul class="nav nav-pills">
  <li>
    <a rel="tooltip" title="{{ _('go here to add a new role.') }}" href="{{ url_for('.addrole') }}">
      <i class="glyphicon glyphicon-pencil"></i> {{ _("Create new role") }}
    </a>
  </li>
  <li>
    <a rel="tooltip" title="{{ _('connect a user to role %(x_name)s.', x_name=role.name) }}" href="{{ url_for('.adduserrole', id_role=role.id) }}">
      <i class="glyphicon glyphicon-user"></i> {{ _("Connect user") }}
    </a>
  </li>
{#  <li><button class="btn" data-toggle="collapse" data-target="#filter" href="#filter">{{ _("Toggle Filter") }}</button></li>#}
</ul>

<div class="row">
<div class="col-md-4">
<h3>{{ _('Role details') }}</h3>
<dl>
  <dt>{{ _('Id') }}</dt>
  <dd>{{ role.id }}</dd>
  <dt>{{ _('Name') }}</dt>
  <dd>{{ role.name }}</dd>
  <dt>{{ _('Description') }}</dt>
  <dd>{{ role.description }}</dd>
  <dt>{{ _('Firewall like role definition') }}</dt>
  <dd><pre>{{ role.firerole_def_src }}</pre></dd>
</dl>
</div>
<div class="col-md-4">
<h3>{{ _('Users') }}</h3>
{%- if role.users -%}
<table class="table">
  {%- for u in role.users -%}
  <tr>
    <td>
    <a href="{{ url_for('.showuserdetails', id_user=u.user.id) }}">
      {{ u.user.email }}
    </a>
    </td><td>
    <a rel="tooltip" title="{{ _('disconnect from role') }}" class="pull-right" href="{{ url_for('.deleteuserrole', id_role=role.id, id_user=u.user.id) }}">
      <i class="glyphicon glyphicon-remove-sign"></i>
    </a>
    </td>
  </tr>
  {%- endfor -%}
</table>
{%- endif -%}
</div>

<div class="col-md-4">
<h3>{{ _('Actions') }}</h3>
{%- if role.authorizations -%}
<div class="accordion">
  {%- for action, authorizations in role.authorizations|groupby('action') -%}
  <div class="panel panel-default">
    <div class="panel-heading">
    <div data-toggle="collapse" data-target="#auths{{ action.id }}">
    <a href="{{ url_for('.showactiondetails', id_action=action.id) }}">
      {{ action.name }}
    </a>
    <a rel="tooltip" title="{{ _('disconnect action with all arguments from role') }}" class="pull-right" href="{{ url_for('.deleteauthorization', id_role=role.id, id_action=action.id) }}">
      <i class="glyphicon glyphicon-remove-sign"></i>
    </a>
    </div>
    </div>
    {%- if authorizations and authorizations[0].argument -%}
    <div id="auths{{ action.id }}" class="collapse in panel-collapse">
    <table class="table">
    {%- for i, auths in authorizations|groupby('argumentlistid') -%}
      {%- if loop.first -%}
        <tr><th>#</th><th>
        {{ auths|join('</th><th>', attribute='argument.keyword')|safe }}
        </th><th></th></tr>
      {%- endif -%}
      <tr><td>{{ i }}</td><td>
        {{ auths|join('</td><td>', attribute='argument.value')|safe }}
      </td>
      <td>
        <a rel="tooltip" title="{{ _('disconnect argument list from role') }}" class="pull-right" href="{{ url_for('.deleteauthorization', id_role=role.id, id_action=action.id, argumentlistid=i) }}">
          <i class="glyphicon glyphicon-remove-sign"></i>
        </a>
      </td>
      </tr>
    {%- endfor -%}
    </table>
    </div>
    {%- endif -%}
  </div>
  {%- endfor -%}
</div>
{%- endif -%}
</div>
</div>

{% endblock body %}
