From 6644a5bbe53461f763050bfb99e29e053ca5d248 Mon Sep 17 00:00:00 2001
From: Thomas Schneider <qsx@chaotikum.eu>
Date: Mon, 10 Feb 2020 11:25:15 +0100
Subject: [PATCH] Add UsersController#show and GroupsController

---
 app/controllers/groups_controller.rb       |  2 ++
 app/controllers/users_controller.rb        |  4 +++
 app/views/layouts/application.html.haml    |  2 +-
 app/views/users/index.html.haml            | 17 +++++++----
 app/views/users/show.html.haml             | 35 ++++++++++++++++++++++
 config/routes.rb                           |  1 +
 test/controllers/groups_controller_test.rb |  7 +++++
 7 files changed, 61 insertions(+), 7 deletions(-)
 create mode 100644 app/controllers/groups_controller.rb
 create mode 100644 app/views/users/show.html.haml
 create mode 100644 test/controllers/groups_controller_test.rb

diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb
new file mode 100644
index 0000000..2f973a6
--- /dev/null
+++ b/app/controllers/groups_controller.rb
@@ -0,0 +1,2 @@
+class GroupsController < ApplicationController
+end
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 330e9ab..8b12919 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -4,4 +4,8 @@ class UsersController < ApplicationController
   def index
     @users = User.find :all, '*'
   end
+
+  def show
+    @u = User.find :first, params[:id]
+  end
 end
diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml
index 3c8a863..a56283d 100644
--- a/app/views/layouts/application.html.haml
+++ b/app/views/layouts/application.html.haml
@@ -2,7 +2,7 @@
 %html
   %head
     %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
-    %title Usermanager
+    %title UserManager
     = csrf_meta_tags
     = csp_meta_tag
     = stylesheet_link_tag    'application', media: 'all'
diff --git a/app/views/users/index.html.haml b/app/views/users/index.html.haml
index 86db768..217d0bd 100644
--- a/app/views/users/index.html.haml
+++ b/app/views/users/index.html.haml
@@ -1,21 +1,26 @@
+%h1 User list
+.btn-group
+  %a.btn.btn-primary{role: 'button', href: new_user_path} Create user
 %table.table.table-striped#dttb
   %caption List of users
   %thead
     %tr
       %th User ID
-      %th First Name
-      %th Last Name
-      %th Primary Group
+      %th Given name
+      %th Surame
+      %th Primary group
       %th Groups
       %th Status
   %tbody
     - @users.each do |u|
       %tr
-        %td= u.cn
+        %td= link_to u.cn, user_path(u)
         %td= u.givenname
         %td= u.sn
-        %td= u.primary_group.cn if u.primary_group.exists?
-        %td= u.groups.map {|g| g.cn}.join(', ')
         %td
+          - if u.primary_group.exists?
+            = link_to u.primary_group.cn, group_path(u.primary_group)
+        %td!= u.groups.map {|g| link_to g.cn, group_path(g)}.join(', ')
+        %td{class: u.active? ? 'table-success' : 'table-warning'}
           %span{class: [:fas, u.active? ? 'fa-check' : 'fa-lock'], aria: {hidden: :true}, title: u.active? ? 'Enabled' : 'Disabled'}
           %span.sr-only= u.active? ? 'Enabled' : 'Disabled'
diff --git a/app/views/users/show.html.haml b/app/views/users/show.html.haml
new file mode 100644
index 0000000..55af8de
--- /dev/null
+++ b/app/views/users/show.html.haml
@@ -0,0 +1,35 @@
+%h1 User details
+.btn-group
+  %a.btn.btn-danger{role: 'button'} Disable user
+  %a.btn.btn-primary{role: 'button'} Change group(s)
+%table.table.table-hover
+  %tbody
+    %tr
+      %td User ID
+      %td= @u.cn
+    %tr
+      %td Given name
+      %td= @u.givenname
+    %tr
+      %td Surname
+      %td= @u.sn
+    %tr
+      %td Status
+      %td{class: @u.active? ? 'table-success' : 'table-warning'}
+        %span{class: [:fas, @u.active? ? 'fa-check' : 'fa-lock'], aria: {hidden: :true}, title: @u.active? ? 'Enabled' : 'Disabled'}
+        = @u.active? ? 'Enabled' : 'Disabled'
+    %tr
+      %td Primary group
+      - if @u.primary_group.exists?
+        %td= link_to @u.primary_group.cn, group_path(@u.primary_group)
+      - else
+        %td.table-warning No primary group
+    %tr
+      %td Groups
+      %td{class: @u.groups.empty? && 'table-warning'}
+        - if @u.groups.empty?
+          No groups
+        - else
+          %ul.list-group
+            - @u.groups.each do |g|
+              %li.list-group-item= link_to g.cn, group_path(g)
diff --git a/config/routes.rb b/config/routes.rb
index 5091501..f39bc03 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -3,4 +3,5 @@
 Rails.application.routes.draw do
   # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
   resources :users
+  resources :groups
 end
diff --git a/test/controllers/groups_controller_test.rb b/test/controllers/groups_controller_test.rb
new file mode 100644
index 0000000..23aba76
--- /dev/null
+++ b/test/controllers/groups_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class GroupsControllerTest < ActionDispatch::IntegrationTest
+  # test "the truth" do
+  #   assert true
+  # end
+end
-- 
GitLab