Skip to content

Commit

Permalink
Merge pull request #219 from hpi-swt2/ba/group-add-user
Browse files Browse the repository at this point in the history
Add user membership management to group management
  • Loading branch information
kbroschke authored Jan 29, 2023
2 parents 77ab57d + 9b640c1 commit d2a96f6
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 4 deletions.
63 changes: 63 additions & 0 deletions app/controllers/memberships_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class MembershipsController < ApplicationController
before_action :set_group
before_action :assure_admin

# PATCH /groups/1/remove_user or /groups/1/remove_user.json
def remove_user
user = User.find(params[:user])
respond_to do |format|
if user.memberships.destroy_by(group: @group)
respond_with_notice(format, redirect: edit_group_path(@group), notice: t(:group_user_removed))
else
unprocessable_response(format, redirect: :edit, entity: @group)
end
end
end

# PATCH /groups/1/add_user or /groups/1/add_user.json
def add_user
respond_to do |format|
user = User.where(email: params[:user][:email]).first
if user.blank?
respond_with_alert(format, redirect: edit_group_url(@group), alert: t(:group_user_not_found))
elsif Membership.where(user: user, group: @group, role: :member).first_or_create
respond_with_notice(format, redirect: edit_group_url(@group), notice: t(:group_user_added))
else
unprocessable_response(format, redirect: :edit, entity: @group)
end
end
end

private

def assure_signed_in
unless user_signed_in?
redirect_to new_user_session_path, notice: t(:login_first)
return false
end
true
end

def assure_admin
assure_signed_in
unless current_user.admin_in? @group
redirect_to groups_url, notice: t(:only_admins)
return false
end
true
end

def set_group
@group = Group.find(params[:group_id])
end

def respond_with_notice(format, redirect:, notice:)
format.html { redirect_to redirect, notice: notice }
format.json { head :no_content }
end

def respond_with_alert(format, redirect:, alert:)
format.html { redirect_to redirect, alert: alert }
format.json { head :no_content }
end
end
23 changes: 23 additions & 0 deletions app/views/groups/_modal-add-member.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div class="modal fade" id="addMemberGroupModal" tabindex="-1" aria-labelledby="addMemberGroupModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header border-0">
<h5 class="modal-title" id="addMemberGroupModalLabel"><%= t(:group_add_member) %></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<%= t(:cancel) %>"></button>
</div>
<div class="modal-body">
<%= form_with(model: User.new, url: group_add_user_path(@group), method: :patch, local: true) do |form| %>
<div>
<%= form.label :email, t(:group_add_member_prompt), class: "form-label" %>
<%= form.email_field :email, placeholder: t(:member_email_placeholder), class: "form-control" %>

<div class="modal-footer border-0 justify-content-evenly">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><%= t(:cancel) %></button>
<%= form.submit t(:group_add_member), :class => "btn btn-primary" %>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
3 changes: 1 addition & 2 deletions app/views/groups/_modal-delete.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><%= t(:cancel) %></button>
<%= button_to t(:delete_group), group, method: :delete, class: "btn btn-primary" %>
</div>

</div>
</div>
</div>
</div>
11 changes: 9 additions & 2 deletions app/views/groups/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,26 @@
<tr>
<td><%= user.full_name %></td>
<td><%= user.email %></td>
<% if not user.admin_in?(@group) %>
<td>
<%= mail_to user.email, class: "btn btn-sm btn-secondary" do %>
<i class="bi bi-chat-fill"></i>
<% end %>
<a href="#" class="btn btn-sm btn-primary"><i class="bi bi-person-dash-fill"></i></a>
<%= button_to group_remove_user_path(@group), method: :patch, params: {group: @group, user: user}, class: "btn btn-sm btn-primary", form: {class: "d-inline"} do %>
<i class="bi bi-person-dash-fill"></i>
<% end %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>

<div class="d-flex justify-content-center">
<%= link_to t(:group_add_member), "", class: "btn btn-secondary mx-1" %>
<button type="button" class="btn btn-secondary mx-1" data-bs-toggle="modal" data-bs-target="#addMemberGroupModal">
<%= t(:group_add_member) %>
</button>
<%= render "modal-add-member", group: @group %>

<button type="button" class="btn btn-primary mx-1" data-bs-toggle="modal" data-bs-target="#deleteGroupModal">
<%= t(:delete_group) %>
Expand Down
7 changes: 7 additions & 0 deletions config/locales/views/groups/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ de:
delete_group: "Gruppe löschen"
leave_group: "Gruppe verlassen"
edit_group: "Gruppe bearbeiten"

group_add_member: "Nutzer hinzufügen"
group_add_member_prompt: "Bitte geben Sie die E-Mail-Adresse des Nutzers ein, den Sie zur Gruppe hinzufügen möchten."
group_no_items: "Keine Items vorhanden"

group_name : "Gruppenname"
Expand All @@ -22,10 +24,15 @@ de:

member_name: "Name"
member_email: "E-Mail"
member_email_placeholder: "[email protected]"

login_first: "Sie müssen sich erst anmelden"
only_admins: "Nur Administratoren dürfen diese Gruppe bearbeiten"

group_new: "Die Gruppe wurde erfolgreich erstellt"
group_update: "Die Gruppe wurde erfolgreich aktualisiert"
group_destroy: "Die Gruppe wurde wurde erfolgreich gelöscht"

group_user_not_found: "Dieser Nutzer konnte nicht gefunden werden"
group_user_added: "Nutzer wurde hinzugefügt"
group_user_removed: "Nutzer wurde entfernt"
8 changes: 8 additions & 0 deletions config/locales/views/groups/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ en:
delete_group: "Delete group"
leave_group: "Leave group"
edit_group: "Edit group"

group_add_member: "Add user"
group_add_member_prompt: "Please provide the email address of the user you'd like to add to this group."
group_no_items: "No items present"

group_name : "Groupname"
Expand All @@ -22,10 +24,16 @@ en:

member_name: "Name"
member_email: "E-mail"
member_email_placeholder: "[email protected]"

login_first: "You have to log in first"
only_admins: "Only admins are allowed to edit this group"

group_new: "Group has been created successfully"
group_update: "Group has been updated successfully"
group_destroy: "Group has been deleted successfully"

group_user_not_found: "This user could not be found"
group_user_added: "User was successfully added"
group_user_removed: "User was successfully removed"

3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
resources :groups, except: [:show, :new] do
post :leave
patch :add_user, to: "memberships#add_user"
patch :remove_user, to: "memberships#remove_user"
end

resources :items do
patch :reserve
patch :borrow
Expand Down

0 comments on commit d2a96f6

Please sign in to comment.