Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

14.5 KB
Raw Blame
<div>
<%= render 'organisations/shared/settings_subnav', active: :roles %>
<div class="flex justify-end items-center gap-2 mb-3.5">
  <%= render 'common/contextual_help_menu', context: :settings_roles %>
  <%= render Neo::Buttons::IconButtonComponent.new(
    variant: :primary,
    icon: "plus-lg",
    size: :medium,
    title: "Neue Rolle erstellen",
    url: new_organisation_role_path(current_organisation)
  ) %>
</div>

<% if notice %>
  <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded my-4" role="alert">
    <%= notice %>
  </div>
<% end %>

<% if alert %>
  <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded my-4" role="alert">
    <%= alert %>
  </div>
<% end %>

<!-- View switcher -->
<div class="flex items-center gap-3 my-4">
  <span class="text-sm text-cream-600"><%= t('.view_label') %>:</span>
  <div class="inline-flex -space-x-px items-center rounded justify-start">
    <button type="button" id="list-view-btn"
            class="active [&.active]:bg-cream-200 [&.active]:border-ink segmented-item h-8 py-2 px-4 segmented-item relative inline-flex flex-row items-center justify-center gap-x-2 text-sm tracking-[.00714em] font-medium border border-cream-300 bg-cream-50 first:rounded-s last:rounded-e"
            onclick="switchView('list-view')"
            title="<%= t('.list_view') %>">
      <i class="bi bi-list-ul"></i>
    </button>
    <button type="button" id="matrix-view-btn"
            class="[&.active]:bg-cream-200 [&.active]:border-ink segmented-item h-8 py-2 px-4 segmented-item relative inline-flex flex-row items-center justify-center gap-x-2 text-sm tracking-[.00714em] font-medium border border-cream-300 bg-cream-50 first:rounded-s last:rounded-e"
            onclick="switchView('matrix-view')"
            title="Rechte-Matrix: Übersicht aller Rollen und Berechtigungen">
      <i class="bi bi-grid-3x3"></i>
    </button>
  </div>
</div>

<!-- List view -->
<div id="list-view" class="mb-2">
  <div>
  <table class="min-w-full border-collapse">
    <thead>
      <tr>
        <th scope="col" class="px-4 py-4 first:ps-0 last:pe-0 text-start text-[11px] font-mono font-medium uppercase tracking-wider text-cream-600 border-b border-ink">
          <%= t('.table_headers.title') %>
        </th>
        <th scope="col" class="px-4 py-4 first:ps-0 last:pe-0 text-start text-[11px] font-mono font-medium uppercase tracking-wider text-cream-600 border-b border-ink">
          <%= t('.table_headers.description') %>
        </th>
        <th scope="col" class="px-4 py-4 first:ps-0 last:pe-0 text-start text-[11px] font-mono font-medium uppercase tracking-wider text-cream-600 border-b border-ink">
          <%= t('.table_headers.default') %>
        </th>
        <th scope="col" class="w-24 px-4 py-4 first:ps-0 last:pe-0 text-start text-[11px] font-mono font-medium uppercase tracking-wider text-cream-600 border-b border-ink">
          <%= t('.table_headers.actions') %>
        </th>
      </tr>
    </thead>
    <tbody class="divide-y divide-cream-200">
      <% @roles.each do |role| %>
        <tr class="hover:bg-cream-100 transition-colors">
          <td class="px-4 py-4 first:ps-0 last:pe-0 whitespace-nowrap">
            <div class="text-sm font-medium"><%= t(".role_labels.#{role.title}", default: role.title) %></div>
          </td>
          <td class="px-4 py-4 first:ps-0 last:pe-0">
            <div class="text-sm"><%= role.description %></div>
          </td>
          <td class="px-4 py-4 first:ps-0 last:pe-0 whitespace-nowrap">
            <% if role.is_default %>
              <span class="inline-flex items-center rounded-md bg-green-100 px-2 py-1 text-xs font-medium text-green-700 border border-green-300"><%= t('.default_role') %></span>
            <% else %>
              <% unless role.account_admin? %>
                <%= render Neo::Buttons::ButtonComponent.new(variant: :secondary, size: :small, icon: "star-fill",
                      url: set_default_organisation_role_path(@current_organisation, role),
                      data: { turbo_method: :post }) do %>
                  <%= t('.set_as_default') %>
                <% end %>
              <% end %>
            <% end %>
          </td>
          <td class="px-4 py-4 first:ps-0 last:pe-0 whitespace-nowrap text-sm font-medium">
            <% unless role.account_admin? %>
              <div class="flex gap-2">
                <%= render Neo::Buttons::IconButtonComponent.new(
                  variant: :edit,
                  size: :medium,
                  tag: :a,
                  href: edit_organisation_role_path(@current_organisation, role),
                  icon: "pencil",
                  title: t('.edit')
                ) %>

                <%= render Neo::Buttons::IconButtonComponent.new(
                  variant: :danger,
                  size: :medium,
                  icon: "trash",
                  title: t('.delete'),
                  interactive: true,
                  confirm: t('.confirm_delete'),
                  tag: :a,
                  href: organisation_role_path(@current_organisation, role),
                  data: {
                    turbo_method: :delete
                  }
                ) %>
              </div>
            <% else %>
              <span class="inline-flex items-center rounded-md bg-cream-100 px-2 py-1 text-xs font-medium text-cream-700 border border-cream-300"><%= t('.system_role') %></span>
            <% end %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>
  </div>
</div>

<!-- Matrix view -->
<div id="matrix-view" class="mb-2 hidden">
  <%
    # Collect permissions for the matrix
    all_permissions = {}
    Permission::CATEGORIES.each do |category, actions|
      actions.each do |action|
        action_key = "#{category}__#{action}"
        all_permissions[action_key] = {
          category: category.to_s,
          action: action,
          display_name: "#{category_name(category.to_s)}: #{action.humanize.gsub('_', ' ')}",
          description: get_permission_description(category.to_s, action)
        }
      end
    end

    # Group roles
    admin_roles = @roles.select(&:account_admin?)
    standard_roles = @roles.select { |r| r.is_default && !r.account_admin? }
    other_roles = @roles.reject { |r| r.account_admin? || r.is_default }

    # Sorted role list
    sorted_roles = admin_roles + standard_roles + other_roles
  %>

  <div class="overflow-x-auto">
    <div class="min-w-max">
      <!-- Matrix with roles as columns and permissions as rows -->
      <table class="divide-y divide-cream-200 border border-cream-300">
        <thead class="bg-cream-100">
          <tr>
            <!-- First cell is empty (corner between permissions and roles) -->
            <th class="px-4 py-3 text-left bg-cream-100 border-b border-r border-ink sticky left-0 z-sticky" style="min-width: 320px;">
              <div class="text-[11px] font-mono font-medium uppercase tracking-wider text-cream-600"><%= t('.matrix.permissions_roles_header') %></div>
            </th>

            <!-- Column headers for each role -->
            <% sorted_roles.each do |role| %>
              <th class="px-4 py-3 text-center border-b border-ink border-r border-cream-200 min-w-[120px]">
                <div class="font-semibold
                          <%= role.account_admin? ? 'text-purple-800' : (role.is_default ? 'text-green-800' : 'text-cream-800') %>">
                  <%= role.title %>
                </div>

                <div class="mt-1">
                  <% if role.account_admin? %>
                    <span class="inline-flex items-center rounded-full bg-purple-100 px-2.5 py-0.5 text-xs font-medium text-purple-800 border border-purple-300">
                      <i class="bi bi-shield-fill-check mr-1"></i> <%= t('.matrix.admin_badge') %>
                    </span>
                  <% elsif role.is_default %>
                    <span class="inline-flex items-center rounded-full bg-green-100 px-2.5 py-0.5 text-xs font-medium text-green-800 border border-green-300">
                      <i class="bi bi-star-fill mr-1"></i> <%= t('.matrix.default_badge') %>
                    </span>
                  <% end %>
                </div>

                <% unless role.account_admin? %>
                  <div class="mt-2">
                    <%= link_to edit_organisation_role_path(@current_organisation, role), class: 'text-cobalt-600 hover:text-cobalt-800 text-xs' do %>
                      <i class="bi bi-pencil-fill"></i> <%= t('.matrix.edit_role') %>
                    <% end %>
                  </div>
                <% end %>
              </th>
            <% end %>
          </tr>
        </thead>

        <tbody class="bg-cream-50 divide-y divide-cream-200">
          <%
            # Group sorted permissions by category
            grouped_permissions = {}
            all_permissions.each do |key, perm|
              cat = perm[:category]
              grouped_permissions[cat] ||= []
              grouped_permissions[cat] << perm
            end
          %>

          <% grouped_permissions.each do |category, permissions| %>
            <!-- Category header -->
            <tr class="bg-cream-100">
              <td colspan="<%= sorted_roles.count + 1 %>" class="px-4 py-2 font-medium text-cream-800 sticky left-0">
                <% category_icon = case category
                                when 'accounts' then 'bi-building'
                                when 'projects' then 'bi-folder'
                                when 'tickets' then 'bi-ticket'
                                when 'users' then 'bi-people'
                                else 'bi-list-check'
                                end %>
                <i class="bi <%= category_icon %> mr-1"></i>
                <%= category_name(category) %>
              </td>
            </tr>

            <!-- Rows for each permission in this category -->
            <% permissions.each do |permission| %>
              <tr class="hover:bg-cream-100">
                <!-- Permission name (first column) -->
                <td class="px-4 py-2 text-left font-medium text-cream-700 border-r border-ink sticky left-0 bg-cream-50 z-sticky" style="min-width: 320px;">
                  <div>
                    <%= permission[:action].humanize.gsub('_', ' ') %>
                    <i class="bi bi-info-circle text-cream-500 ml-1 cursor-help"
                       title="<%= permission[:description] %>"></i>
                  </div>
                  <div class="text-xs text-cream-600"><%= permission[:description].truncate(70) %></div>
                </td>

                <!-- Permission checkmark for each role -->
                <% sorted_roles.each do |role| %>
                  <td class="px-4 py-2 text-center border-r border-cream-200">
                    <%
                      # Determine permission status
                      if role.account_admin?
                        has_permission = true
                        automatic = true
                      else
                        perm = role.permissions.find_by(category: permission[:category], action: permission[:action])
                        has_permission = perm&.allowed || false
                        automatic = false
                      end
                    %>

                    <% if has_permission %>
                      <% if automatic %>
                        <div class="text-center" title="<%= t('.matrix.automatic_permission_tooltip') %>">
                          <i class="bi bi-shield-check text-purple-600 text-xl"></i>
                        </div>
                      <% else %>
                        <div class="text-center" title="<%= t('.matrix.permission_granted_tooltip') %>">
                          <i class="bi bi-check-circle-fill text-green-600 text-xl"></i>
                        </div>
                      <% end %>
                    <% else %>
                      <div class="text-center" title="<%= t('.matrix.no_permission_tooltip') %>">
                        <i class="bi bi-x-circle text-cream-500 text-xl"></i>
                      </div>
                    <% end %>
                  </td>
                <% end %>
              </tr>
            <% end %>

            <!-- Separator row after each category -->
            <% unless category == grouped_permissions.keys.last %>
              <tr class="h-2 bg-cream-100">
                <td colspan="<%= sorted_roles.count + 1 %>" class="border-b-2 border-cream-200"></td>
              </tr>
            <% end %>
          <% end %>
        </tbody>
      </table>
    </div>
  </div>

  <div class="mt-6 bg-cream-100 p-4 rounded-lg border border-cream-200">
    <h3 class="text-lg font-semibold mb-2"><%= t('.matrix.legend.title') %></h3>
    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
      <div class="flex items-center">
        <i class="bi bi-check-circle-fill text-green-600 text-xl mr-2"></i>
        <span><%= t('.matrix.legend.permission_granted') %></span>
      </div>
      <div class="flex items-center">
        <i class="bi bi-shield-check text-purple-600 text-xl mr-2"></i>
        <span><%= t('.matrix.legend.automatic_permission') %></span>
      </div>
      <div class="flex items-center">
        <i class="bi bi-x-circle text-cream-500 text-xl mr-2"></i>
        <span><%= t('.matrix.legend.no_permission') %></span>
      </div>
    </div>
  </div>
</div>

<!-- JavaScript for the view switch -->
<script>
  function switchView(viewName) {
    // Hide all views
    document.getElementById('list-view').classList.add('hidden');
    document.getElementById('matrix-view').classList.add('hidden');

    // Show the selected view
    document.getElementById(viewName).classList.remove('hidden');

    // Adjust button styles (remove 'active' from all buttons)
    document.getElementById('list-view-btn').classList.remove('active');
    document.getElementById('matrix-view-btn').classList.remove('active');

    // Highlight the active button
    if (viewName === 'list-view') {
      document.getElementById('list-view-btn').classList.add('active');
    } else {
      document.getElementById('matrix-view-btn').classList.add('active');
    }

    // Store the user's selection in localStorage
    localStorage.setItem('rolesViewPreference', viewName);
  }

  // Restore the saved view on page load
  document.addEventListener('DOMContentLoaded', function() {
    const savedView = localStorage.getItem('rolesViewPreference');
    if (savedView && (savedView === 'matrix-view' || savedView === 'list-view')) {
      switchView(savedView);
    }

    // Initialize tooltips for the permission descriptions
    // If you use a tooltip library, you can initialize it here
  });
</script>
</div>