Commit 99f31f66 authored by Tobias Kallauke's avatar Tobias Kallauke
Browse files

Add group hierarchy display and subgroup create

parent 3e15ccfe
# frozen_string_literal: true
require 'rest-client'
require 'date'
......@@ -6,7 +8,8 @@ MIN_GROUP_ID = 0
class Group
include ActiveModel::Serializers::JSON
attr_accessor :id, :name, :description, :created_at, :valid_until, :type_of_group, :manager_id
attr_accessor :id, :name, :description, :type_of_group, :manager_id, :parent
attr_reader :created_at, :valid_until, :children
def attributes=(hash)
hash.each do |key, value|
......@@ -24,13 +27,31 @@ class Group
@valid_until = value
end
def attributes
{ 'id' => nil, 'name' => nil, 'description' => nil, 'created_at' => nil, 'valid_until' => nil, 'type_of_group' => nil, 'manager_id' => nil}
def children=(value)
@children = value.nil? ? [] : value
end
def parents
return [] if parent.nil?
class << self
list = [parent]
current = parent
loop do
break if current['parent'].nil?
list.push(current['parent'])
current = current['parent']
end
list.reverse
end
def attributes
{ 'id' => nil, 'name' => nil, 'description' => nil, 'created_at' => nil, 'valid_until' => nil,
'type_of_group' => nil, 'manager_id' => nil, 'parent' => nil, 'children' => nil }
end
class << self
def list
res = RestClient.get("#{ENV['API_URL']}groups")
......@@ -58,21 +79,26 @@ class Group
g
end
def create(name, description, uid)
# @param [String] name
# @param [String] description
# @param [Integer] user_id First member
# @param [Integer, nil] parent_id Group ID of parent group, or nil for no parent
def create(name, description, user_id, parent_id = nil)
data = {
'name': name,
'description': description,
'manager': uid,
'manager_id': user_id,
'parent_id': parent_id,
'users': [
{
'id': uid,
'id': user_id,
'capability': nil
}
]
}.to_json
res = RestClient.post(
"#{ENV['API_URL']}groups",
"#{ENV['API_URL']}groups/",
data,
{ content_type: :json, accept: :json }
)
......@@ -108,17 +134,12 @@ class Group
member.role = role
users.push(member)
if(m['id'] == my_id)
my_role = member.role
end
my_role = member.role if m['id'] == my_id
end
return users,my_role
[users, my_role]
end
def get_member_cababilities(id)
res = RestClient.get("#{ENV['API_URL']}groups/#{id}/members")
......@@ -129,7 +150,8 @@ class Group
members[e['id']] = GroupMember.new(e['id'], e['login']) unless members.key?(e['id'])
unless e['capability'].nil?
members[e['id']].capabilities.push(ConsumerCapability.new(e['capability']['id'], e['capability']['name'], e['capability']['description']))
members[e['id']].capabilities.push(ConsumerCapability.new(e['capability']['id'], e['capability']['name'],
e['capability']['description']))
end
end
......@@ -163,7 +185,6 @@ class Group
users = []
members.each do |m|
user = MemberCapabilities.new(m.id, m.login, consumer.capabilities.count)
m.capabilities.each do |c|
......@@ -174,7 +195,6 @@ class Group
end
users.push(user)
end
{
......@@ -184,11 +204,13 @@ class Group
end
def add_user(gid, uid)
res = RestClient.post("#{ENV['API_URL']}groups/#{gid}/members", { users: [{ id: Integer(uid), capability: nil }] }.to_json)
res = RestClient.post("#{ENV['API_URL']}groups/#{gid}/members",
{ users: [{ id: Integer(uid), capability: nil }] }.to_json)
end
def remove_user(gid, uid)
RestClient::Request.execute(method: 'delete', url: "#{ENV['API_URL']}groups/#{gid}/members", payload: { users: [uid] }.to_json)
RestClient::Request.execute(method: 'delete', url: "#{ENV['API_URL']}groups/#{gid}/members",
payload: { users: [uid] }.to_json)
end
def add_capability(group, pairs)
......@@ -210,7 +232,8 @@ class Group
end
mid = m['membership_id']
RestClient::Request.execute(method: 'delete', url: "#{ENV['API_URL']}groups/#{group}/members", payload: { memberships: [mid] }.to_json)
RestClient::Request.execute(method: 'delete', url: "#{ENV['API_URL']}groups/#{group}/members",
payload: { memberships: [mid] }.to_json)
end
end
......@@ -239,7 +262,6 @@ class GroupMember
@login = login
@capabilities = []
end
end
class MemberCapabilities
......@@ -273,7 +295,9 @@ class GroupConsumer
res = RestClient.get("#{ENV['API_URL']}consumers/capabilities/#{id}")
json = JSON.parse(res.body)
capabilities = json['result']['capabilities'].map { |i| ConsumerCapability.new(i['id'], i['name'], i['description']) }
capabilities = json['result']['capabilities'].map do |i|
ConsumerCapability.new(i['id'], i['name'], i['description'])
end
consumer = GroupConsumer.new(c.id, c.name)
consumer.capabilities = capabilities
......
......@@ -10,7 +10,7 @@ class GroupController < ApplicationController
@test = session[:user_id]
@group_id = Integer(params[:id])
@group = Group.get(params[:id])
@members,@my_role = Group.get_members(params[:id], session[:user_id])
@members, @my_role = Group.get_members(params[:id], session[:user_id])
@consumers = Group.get_consumers(params[:id])
@info = I18n.t 'help.infobox.groupList'
end
......@@ -37,7 +37,7 @@ class GroupController < ApplicationController
@group_id = Integer(params[:gid])
@group = Group.get(params[:gid])
@consumers = Group.get_consumers(params[:gid])
@members,@my_role = Group.get_members(params[:gid], session[:user_id])
@members, @my_role = Group.get_members(params[:gid], session[:user_id])
@info = I18n.t 'help.infobox.consumerList'
all = Consumer.list
......@@ -72,21 +72,45 @@ class GroupController < ApplicationController
def create
@info = I18n.t 'help.infobox.createGroup'
if request.post?
@result = create_group(params['groupname'], params['description'], session[:user_id], nil)
return redirect_to action: 'get', id: @result, status: :found if @result.is_a?(Integer)
end
end
def create_subgroup
if request.post?
begin
id = Group.create(params['groupname'], params['description'], session[:user_id])
rescue RestClient::Exception => e
@result = case e.http_code
when 409
I18n.t 'group.create.error.nameAlreadyTaken'
else
I18n.t 'group.create.error.apiError' + "#{e.http_code}"
end
else
Role.add_roles(id, { session[:user_id] => Roles::ADMIN })
redirect_to action: 'get', id: id, status: :found
end
@result = create_group(params['groupname'], params['description'], session[:user_id], Integer(params['id']))
return redirect_to action: 'get', id: @result, status: :found if @result.is_a?(Integer)
end
render 'group/create'
end
private
# @param [String] name
# @param [String] description
# @param [Integer] user_id User to add as admin and set as group manager
# @param [Integer, nil] parent_id
# @return [Integer, String] Group ID on success, Error message on failure
def create_group(name, description, user_id, parent_id)
begin
id = Group.create(name, description, user_id, parent_id)
rescue RestClient::Exception => e
return case e.http_code
when 409
I18n.t 'group.create.error.nameAlreadyTaken'
else
"#{t 'group.create.error.apiError'} #{e.http_code}"
end
else
Role.add_roles(id, { session[:user_id] => Roles::ADMIN })
end
id
end
end
<% content_for :breadcrumbs do %>
<% @group.parents.each do |g| %>
<li class="breadcrumb-item">
<a href="<%= url_for controller: 'group', action: 'get', id: g['id'] %>"><%= g['name'] %></a>
</li>
<% end %>
<li class="breadcrumb-item">
<span><%= @group.name %></span>
</li>
<li class="breadcrumb-item">
<div class="btn-group-sm dropright">
<button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Untergruppen
</button>
<div class="dropdown-menu">
<% if @group.children.empty? %>
<a class="dropdown-item disabled" href="#" tabindex="-1" aria-disabled="true">Keine Untergruppen</a>
<% else %>
<% @group.children.each do |g| %>
<a class="dropdown-item" href="<%= url_for controller: 'group', action: 'get', id: g['id'] %>">
<%= g['name'] %>
</a>
<% end %>
<% end %>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Untergruppe erstellen</a>
</div>
</div>
</li>
<% end %>
<%= render partial: "shared/group-header", locals: { group: @group } %>
<%= render partial: "shared/group-nav", locals: { group_id: @group_id, consumers: @consumers, members_selected: true } %>
<table class="table">
......
......@@ -346,11 +346,8 @@
<li class="breadcrumb-item">
<a class="house" href="/">Gruppenverwaltung</a>
</li>
<% if @group%>
<li class="breadcrumb-item active" aria-current="page">
<%= @group.name %>
</li>
<% end %>
<%= yield :breadcrumbs %>
</ol>
</nav>
</div>
......
......@@ -11,6 +11,8 @@ Rails.application.routes.draw do
get 'group/', to: 'group#list'
get 'group/:id', to: 'group#get', constraints: { id: /[0-9]+/ }
post 'group/:id/roles', to: 'group#update_roles', constraints: { id: /[0-9]+/ }
get 'group/:id/create', to: 'group#create_subgroup', constraints: { id: /[0-9]+/ }
post 'group/:id/create', to: 'group#create_subgroup', constraints: { id: /[0-9]+/ }
get 'group/:gid/consumer/:id', to: 'consumer#get', constraints: { gid: /[0-9]+/, id: /[0-9]+/ }
post 'group/:gid/consumer/:id', to: 'consumer#update', constraints: { gid: /[0-9]+/, id: /[0-9]+/ }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment