-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from hpi-swt2/ba/group-add-user
Add user membership management to group management
- Loading branch information
Showing
7 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters