From c62f2eea706c2877e6bcc371671805514a74c9b4 Mon Sep 17 00:00:00 2001 From: Karl Broschke Date: Sun, 8 Jan 2023 20:33:13 +0100 Subject: [PATCH 01/24] feat: add ui for adding users to groups --- app/views/groups/_modal-add-member.html.erb | 28 +++++++++++++++++++++ app/views/groups/edit.html.erb | 5 +++- config/locales/views/groups/de.yml | 2 ++ config/locales/views/groups/en.yml | 2 ++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 app/views/groups/_modal-add-member.html.erb diff --git a/app/views/groups/_modal-add-member.html.erb b/app/views/groups/_modal-add-member.html.erb new file mode 100644 index 00000000..11fc185d --- /dev/null +++ b/app/views/groups/_modal-add-member.html.erb @@ -0,0 +1,28 @@ + \ No newline at end of file diff --git a/app/views/groups/edit.html.erb b/app/views/groups/edit.html.erb index 907ef898..b3c2d211 100644 --- a/app/views/groups/edit.html.erb +++ b/app/views/groups/edit.html.erb @@ -34,7 +34,10 @@
- <%= link_to t(:group_add_member), "", class: "btn btn-secondary mx-1" %> + + <%= render "modal-add-member", group: @group %>
- - <%# manual calls: %> - <%# user = User.where(:email => "form input").first %> - <%# Membership.create(user: user, group: group, role: :member) %> - - - <% end %> + <%= form_with(model: @group, url: group_add_user_path(@group), local: true) do |form| %> +
+ <%= form.label :user, t(:group_add_member_prompt), class: "form-label" %> + <%= form.text_field :user, placeholder: "example.user@hpi.de", class: "form-control" %> + + + <% end %>
\ No newline at end of file diff --git a/config/locales/views/groups/de.yml b/config/locales/views/groups/de.yml index c4bc2838..6564a8d6 100644 --- a/config/locales/views/groups/de.yml +++ b/config/locales/views/groups/de.yml @@ -30,3 +30,4 @@ de: group_new: "Die Gruppe wurde erfolgreich erstellt" group_update: "Die Gruppe wurde erfolgreich aktualisiert" group_destroy: "Die Gruppe wurde wurde erfolgreich gelöscht" + diff --git a/config/routes.rb b/config/routes.rb index 521d8e0b..8c64ed6b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html resources :groups, except: [:show, :new] do post :leave + post :add_user end resources :items do patch :reserve diff --git a/db/schema.rb b/db/schema.rb index 0b44fc6f..8e224158 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -15,6 +15,7 @@ t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "verified" t.integer "tag" end @@ -108,10 +109,10 @@ t.datetime "remember_created_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.string "provider", limit: 50, default: "", null: false - t.string "uid", limit: 50, default: "", null: false t.string "full_name", default: "", null: false t.string "description", default: "", null: false + t.string "provider", limit: 50, default: "", null: false + t.string "uid", limit: 50, default: "", null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end From 71081b707d305f5fed337fe9fe91430c3883f176 Mon Sep 17 00:00:00 2001 From: Kiru Spreu Date: Tue, 10 Jan 2023 16:44:51 +0100 Subject: [PATCH 03/24] fix: reverted one line in schema --- db/schema.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 8e224158..78a3276f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -15,7 +15,6 @@ t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.boolean "verified" t.integer "tag" end From 56e7636563f5763a9e5d4ea193058de9b9651515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20G=C3=B6rgens?= Date: Wed, 11 Jan 2023 10:53:11 +0100 Subject: [PATCH 04/24] fix: change route from PUT to PATCH and use field input --- app/controllers/groups_controller.rb | 2 +- config/routes.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index dd5c1eff..faad6fc2 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -67,7 +67,7 @@ def leave def add_user respond_to do |format| group = @group - user = User.where(email: "form input").first + user = User.where(email: params[:user]).first if Membership.create(user: user, group: group, role: :member) format.html { redirect_to groups_url, notice: t(:group_update) } format.json { head :no_content } diff --git a/config/routes.rb b/config/routes.rb index 8c64ed6b..510c943b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html resources :groups, except: [:show, :new] do post :leave - post :add_user + patch :add_user end resources :items do patch :reserve From 0054387122b23bcf228d5eda2a6ccb1f62222e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20G=C3=B6rgens?= Date: Wed, 11 Jan 2023 10:55:38 +0100 Subject: [PATCH 05/24] fix: fix comments --- app/controllers/groups_controller.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index faad6fc2..97d58cb5 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -61,9 +61,8 @@ def leave end end - ## this is never called ... --> - # POST /groups/1/add_user or /groups/1/add_user.json + # PATCH /groups/1/add_user or /groups/1/add_user.json def add_user respond_to do |format| group = @group @@ -77,7 +76,6 @@ def add_user end end - ## <-- private From 6429ed838fda7582d7a125ed57ab1242e4aac81c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20G=C3=B6rgens?= Date: Wed, 11 Jan 2023 10:57:52 +0100 Subject: [PATCH 06/24] fix: fix rubocop offenses --- app/controllers/groups_controller.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 97d58cb5..2e4bba95 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -61,7 +61,6 @@ def leave end end - # PATCH /groups/1/add_user or /groups/1/add_user.json def add_user respond_to do |format| @@ -76,7 +75,6 @@ def add_user end end - private def assure_signed_in From e8ce15ea08cc1fb5ad973d3297a0e3b7d7aee230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20G=C3=B6rgens?= Date: Wed, 11 Jan 2023 11:03:45 +0100 Subject: [PATCH 07/24] feat: hide remove user and chat button when user is admin --- app/views/groups/edit.html.erb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/views/groups/edit.html.erb b/app/views/groups/edit.html.erb index 14ab0b2c..10b6b6bc 100644 --- a/app/views/groups/edit.html.erb +++ b/app/views/groups/edit.html.erb @@ -24,10 +24,12 @@ <%= user.full_name %> <%= user.email %> - - - - + <% if not user.admin_in?(@group)%> + + + + + <% end %> <% end %> From 53fb5e97e162e931110ef40773c8d35df866432e Mon Sep 17 00:00:00 2001 From: Kiru Spreu Date: Fri, 13 Jan 2023 14:30:43 +0100 Subject: [PATCH 08/24] fix: add_user somewhat works *comments* --- app/controllers/groups_controller.rb | 17 +++++++++++++---- db/schema.rb | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 2e4bba95..ece6e6bc 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -1,7 +1,7 @@ class GroupsController < ApplicationController before_action :assure_signed_in before_action :set_user_group - before_action :set_group, only: %i[ edit update destroy ] + before_action :set_group, only: %i[ edit update destroy] before_action :assure_admin, only: %i[ edit update destroy ] # GET /groups or /groups.json @@ -63,10 +63,18 @@ def leave # PATCH /groups/1/add_user or /groups/1/add_user.json def add_user + + ## Todo: + ## * fix alert if user not found + ## * fix logic i.e. can't add self, duplicate, etc. + ## * clean up method + ## * parameters are messed up.. (thus can't use set_group) + respond_to do |format| - group = @group - user = User.where(email: params[:user]).first - if Membership.create(user: user, group: group, role: :member) + @group = Group.find(params[:group_id]) # this is required to access the group -- but why? + user = User.where(email: params[:group][:user]).first # this is also weird + + if !@group.blank? && !user.blank? && Membership.create(user: user, group: @group, role: :member) format.html { redirect_to groups_url, notice: t(:group_update) } format.json { head :no_content } else @@ -109,6 +117,7 @@ def unprocessable_response(format, redirect:, entity:) format.json { render json: entity.errors, status: :unprocessable_entity } end + # Only allow a list of trusted parameters through. def group_params params.require(:group).permit(:name) diff --git a/db/schema.rb b/db/schema.rb index 78a3276f..8e224158 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -15,6 +15,7 @@ t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "verified" t.integer "tag" end From 132b08725bf0db53dd0c9cded8cb9452b6468612 Mon Sep 17 00:00:00 2001 From: Kiru Spreu Date: Fri, 13 Jan 2023 14:36:21 +0100 Subject: [PATCH 09/24] fix: revert schema --- db/schema.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 8e224158..78a3276f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -15,7 +15,6 @@ t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.boolean "verified" t.integer "tag" end From 41372bc2b32af811f9c9ea3494b4a58bea973acf Mon Sep 17 00:00:00 2001 From: Malte Stellmacher Date: Mon, 16 Jan 2023 14:44:03 +0100 Subject: [PATCH 10/24] feat: add working remove button "form: {class: ..." is needed as button_to generates a form which wont be displayed inline otherwise Co-authored-by: Niklas Schilli --- app/controllers/groups_controller.rb | 22 ++++++++++++++++++++-- app/views/groups/edit.html.erb | 4 +++- config/routes.rb | 2 +- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index ece6e6bc..23837d78 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -1,8 +1,9 @@ class GroupsController < ApplicationController before_action :assure_signed_in before_action :set_user_group - before_action :set_group, only: %i[ edit update destroy] - before_action :assure_admin, only: %i[ edit update destroy ] + before_action :set_group, only: %i[ edit update destroy ] + before_action :set_group_from_id, only: %i[ remove_user ] + before_action :assure_admin, only: %i[ edit update destroy remove_user ] # GET /groups or /groups.json def index @@ -61,6 +62,19 @@ def leave end end + # 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) + format.html { redirect_to edit_group_path(@group), notice: t(:group_update) } + format.json { head :no_content } + 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 @@ -112,6 +126,10 @@ def set_group @group = Group.find(params[:id]) end + def set_group_from_id + @group = Group.find(params[:group_id]) + end + def unprocessable_response(format, redirect:, entity:) format.html { render redirect, status: :unprocessable_entity } format.json { render json: entity.errors, status: :unprocessable_entity } diff --git a/app/views/groups/edit.html.erb b/app/views/groups/edit.html.erb index 10b6b6bc..5e946291 100644 --- a/app/views/groups/edit.html.erb +++ b/app/views/groups/edit.html.erb @@ -27,7 +27,9 @@ <% if not user.admin_in?(@group)%> - + <%= 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 %> + + <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 510c943b..7702b5db 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ # 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 + patch :add_user, :remove_user end resources :items do patch :reserve From a3f587e3417db68b11a2b9c288e453d5ba9898db Mon Sep 17 00:00:00 2001 From: Malte Stellmacher Date: Mon, 16 Jan 2023 15:00:57 +0100 Subject: [PATCH 11/24] fix: add user through modal --- app/controllers/groups_controller.rb | 19 +++++-------------- app/views/groups/_modal-add-member.html.erb | 7 ++++--- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 23837d78..ad5c0583 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -2,8 +2,8 @@ class GroupsController < ApplicationController before_action :assure_signed_in before_action :set_user_group before_action :set_group, only: %i[ edit update destroy ] - before_action :set_group_from_id, only: %i[ remove_user ] - before_action :assure_admin, only: %i[ edit update destroy remove_user ] + before_action :set_group_from_id, only: %i[ remove_user add_user ] + before_action :assure_admin, only: %i[ edit update destroy remove_user add_user ] # GET /groups or /groups.json def index @@ -77,19 +77,10 @@ def remove_user # PATCH /groups/1/add_user or /groups/1/add_user.json def add_user - - ## Todo: - ## * fix alert if user not found - ## * fix logic i.e. can't add self, duplicate, etc. - ## * clean up method - ## * parameters are messed up.. (thus can't use set_group) - respond_to do |format| - @group = Group.find(params[:group_id]) # this is required to access the group -- but why? - user = User.where(email: params[:group][:user]).first # this is also weird - - if !@group.blank? && !user.blank? && Membership.create(user: user, group: @group, role: :member) - format.html { redirect_to groups_url, notice: t(:group_update) } + user = User.where(email: params[:user][:email]).first + if Membership.where(user: user, group: @group, role: :member).first_or_create + format.html { redirect_to edit_group_url(@group), notice: t(:group_update) } format.json { head :no_content } else unprocessable_response(format, redirect: :edit, entity: @group) diff --git a/app/views/groups/_modal-add-member.html.erb b/app/views/groups/_modal-add-member.html.erb index 256a7b92..244a94ca 100644 --- a/app/views/groups/_modal-add-member.html.erb +++ b/app/views/groups/_modal-add-member.html.erb @@ -6,16 +6,17 @@ \ No newline at end of file From c1d559bcfe435c62fa55fbb1a1a44fc0001c64b6 Mon Sep 17 00:00:00 2001 From: Malte Stellmacher Date: Mon, 16 Jan 2023 15:39:55 +0100 Subject: [PATCH 12/24] refactor: apply lint suggestion --- app/controllers/groups_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index ad5c0583..5c2a8bf3 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -126,7 +126,6 @@ def unprocessable_response(format, redirect:, entity:) format.json { render json: entity.errors, status: :unprocessable_entity } end - # Only allow a list of trusted parameters through. def group_params params.require(:group).permit(:name) From 2bb989dffcebaa112001bb5d30a7f00993e48341 Mon Sep 17 00:00:00 2001 From: Kiru Spreu Date: Fri, 20 Jan 2023 15:58:46 +0100 Subject: [PATCH 13/24] feat: worked on edit group page --- app/controllers/groups_controller.rb | 12 ++++++--- app/views/groups/_modal-add-member.html.erb | 2 +- app/views/groups/edit.html.erb | 30 ++++++++++++--------- config/locales/views/groups/de.yml | 3 +++ config/locales/views/groups/en.yml | 3 +++ 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 5c2a8bf3..fa68b869 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -79,9 +79,10 @@ def remove_user def add_user respond_to do |format| user = User.where(email: params[:user][:email]).first - if Membership.where(user: user, group: @group, role: :member).first_or_create - format.html { redirect_to edit_group_url(@group), notice: t(:group_update) } - format.json { head :no_content } + if user.blank? + respond_with_notice(format, redirect: edit_group_url(@group), notice: 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 @@ -126,6 +127,11 @@ def unprocessable_response(format, redirect:, entity:) format.json { render json: entity.errors, status: :unprocessable_entity } end + def respond_with_notice(format, redirect:, notice:) + format.html { redirect_to redirect, notice: notice } + format.json { head :no_content } + end + # Only allow a list of trusted parameters through. def group_params params.require(:group).permit(:name) diff --git a/app/views/groups/_modal-add-member.html.erb b/app/views/groups/_modal-add-member.html.erb index 244a94ca..5abbffb4 100644 --- a/app/views/groups/_modal-add-member.html.erb +++ b/app/views/groups/_modal-add-member.html.erb @@ -9,7 +9,7 @@ <%= form_with(model: User.new, url: group_add_user_path(@group), method: :patch, local: true) do |form| %>
<%= form.label :email, t(:group_add_member_prompt), class: "form-label" %> - <%= form.text_field :email, placeholder: "example.user@hpi.de", class: "form-control" %> + <%= form.email_field :email, placeholder: "example.user@hpi.de", class: "form-control" %> <% end %>
From ff4899e0a413eb81eeafdae15de63960e969cc03 Mon Sep 17 00:00:00 2001 From: Karl Broschke Date: Sun, 22 Jan 2023 14:46:08 +0100 Subject: [PATCH 17/24] feat: internationalize placeholder email --- app/views/groups/_modal-add-member.html.erb | 4 ++-- config/locales/views/groups/de.yml | 1 + config/locales/views/groups/en.yml | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/views/groups/_modal-add-member.html.erb b/app/views/groups/_modal-add-member.html.erb index 5ef55c6b..8983a519 100644 --- a/app/views/groups/_modal-add-member.html.erb +++ b/app/views/groups/_modal-add-member.html.erb @@ -9,7 +9,7 @@ <%= form_with(model: User.new, url: group_add_user_path(@group), method: :patch, local: true) do |form| %>
<%= form.label :email, t(:group_add_member_prompt), class: "form-label" %> - <%= form.email_field :email, placeholder: "example.user@hpi.de", class: "form-control" %> + <%= form.email_field :email, placeholder: t(:member_email_placeholder), class: "form-control" %>
- \ No newline at end of file + diff --git a/config/locales/views/groups/de.yml b/config/locales/views/groups/de.yml index 0c5d258b..26a71c10 100644 --- a/config/locales/views/groups/de.yml +++ b/config/locales/views/groups/de.yml @@ -23,6 +23,7 @@ de: member_name: "Name" member_email: "E-Mail" + member_email_placeholder: "beispiel.benutzer@hpi.de" login_first: "Du musst dich erst anmelden" only_admins: "Nur Administratoren dürfen diese Gruppe bearbeiten" diff --git a/config/locales/views/groups/en.yml b/config/locales/views/groups/en.yml index ada29616..a5f5e6be 100644 --- a/config/locales/views/groups/en.yml +++ b/config/locales/views/groups/en.yml @@ -23,6 +23,7 @@ en: member_name: "Name" member_email: "E-mail" + member_email_placeholder: "example.user@hpi.de" login_first: "You have to log in first" only_admins: "Only admins are allowed to edit this group" @@ -33,4 +34,5 @@ en: group_user_not_found: "This user could not be found" group_user_added: "User was successfully added" - group_user_removed: "User was successfully removed" \ No newline at end of file + group_user_removed: "User was successfully removed" + \ No newline at end of file From 42860a330bd5b8f79b9985aa9b6f3e8288095727 Mon Sep 17 00:00:00 2001 From: Karl Broschke Date: Sun, 22 Jan 2023 14:46:33 +0100 Subject: [PATCH 18/24] refactor: remove empty line --- app/views/groups/_modal-delete.html.erb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/views/groups/_modal-delete.html.erb b/app/views/groups/_modal-delete.html.erb index ec2f1b87..b3f9581c 100644 --- a/app/views/groups/_modal-delete.html.erb +++ b/app/views/groups/_modal-delete.html.erb @@ -12,7 +12,6 @@ <%= button_to t(:delete_group), group, method: :delete, class: "btn btn-primary" %> - - \ No newline at end of file + From f4f01f2900a49a42e37590c15ddd2cd90cfb90f5 Mon Sep 17 00:00:00 2001 From: Karl Broschke Date: Sun, 22 Jan 2023 15:25:58 +0100 Subject: [PATCH 19/24] refactor: remove another redundant admin check --- app/views/groups/edit.html.erb | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/app/views/groups/edit.html.erb b/app/views/groups/edit.html.erb index b2b2b4ab..a9763586 100644 --- a/app/views/groups/edit.html.erb +++ b/app/views/groups/edit.html.erb @@ -39,16 +39,14 @@ -<% if @user.admin_in?(@group) %> -
- - <%= render "modal-add-member", group: @group %> +
+ + <%= render "modal-add-member", group: @group %> - - <%= render "modal-delete", group: @group %> -
-<% end %> + + <%= render "modal-delete", group: @group %> +
From f700677424d0ee7ef5585bce33dfdeb7d8dd1b93 Mon Sep 17 00:00:00 2001 From: Karl Broschke Date: Sun, 22 Jan 2023 17:14:43 +0100 Subject: [PATCH 20/24] refactor: move user managedment to own controller --- app/controllers/groups_controller.rb | 33 +------------ app/controllers/memberships_controller.rb | 58 +++++++++++++++++++++++ config/routes.rb | 4 +- 3 files changed, 62 insertions(+), 33 deletions(-) create mode 100644 app/controllers/memberships_controller.rb diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 17d13d52..497a631a 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -2,8 +2,7 @@ class GroupsController < ApplicationController before_action :assure_signed_in before_action :set_user_group before_action :set_group, only: %i[ edit update destroy ] - before_action :set_group_from_id, only: %i[ remove_user add_user ] - before_action :assure_admin, only: %i[ edit update destroy remove_user add_user ] + before_action :assure_admin, only: %i[ edit update destroy ] # GET /groups or /groups.json def index @@ -70,32 +69,6 @@ def leave end end - # 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_notice(format, redirect: edit_group_url(@group), notice: 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 @@ -125,10 +98,6 @@ def set_group @group = Group.find(params[:id]) end - def set_group_from_id - @group = Group.find(params[:group_id]) - end - def unprocessable_response(format, redirect:, entity:) format.html { render redirect, status: :unprocessable_entity } format.json { render json: entity.errors, status: :unprocessable_entity } diff --git a/app/controllers/memberships_controller.rb b/app/controllers/memberships_controller.rb new file mode 100644 index 00000000..d6f463b5 --- /dev/null +++ b/app/controllers/memberships_controller.rb @@ -0,0 +1,58 @@ +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_notice(format, redirect: edit_group_url(@group), notice: 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 +end diff --git a/config/routes.rb b/config/routes.rb index e3e30742..cad61d45 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,8 +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, :remove_user + patch :add_user, to: "memberships#add_user" + patch :remove_user, to: "memberships#remove_user" end + resources :items do patch :reserve patch :borrow From d2b2e458cfa4fca69377b089d67507adeadb21ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20G=C3=B6rgens?= Date: Wed, 25 Jan 2023 12:40:31 +0100 Subject: [PATCH 21/24] fix: refactor German localization --- config/locales/views/groups/de.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/locales/views/groups/de.yml b/config/locales/views/groups/de.yml index 26a71c10..f38c1fac 100644 --- a/config/locales/views/groups/de.yml +++ b/config/locales/views/groups/de.yml @@ -11,7 +11,7 @@ de: edit_group: "Gruppe bearbeiten" group_add_member: "Nutzer hinzufügen" - group_add_member_prompt: "Bitte gib die E-Mail-Adresse des Nutzers ein, den du zur Gruppe hinzufügen möchtest." + group_add_member_prompt: "Bitte geben Sie die E-Mail-Adresse des Nutzers ein, den Sie zur Gruppe hinzufügen möchten." group_name : "Gruppenname" group_name_prompt: "Bitte gib deiner Gruppe einen Namen" From c822634fe831d920d29170d5e890b6ba84b7b67b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20G=C3=B6rgens?= Date: Wed, 25 Jan 2023 12:58:04 +0100 Subject: [PATCH 22/24] fix: show alert when user is not found --- app/controllers/memberships_controller.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/controllers/memberships_controller.rb b/app/controllers/memberships_controller.rb index d6f463b5..7ddba3bf 100644 --- a/app/controllers/memberships_controller.rb +++ b/app/controllers/memberships_controller.rb @@ -19,7 +19,7 @@ def add_user respond_to do |format| user = User.where(email: params[:user][:email]).first if user.blank? - respond_with_notice(format, redirect: edit_group_url(@group), notice: t(:group_user_not_found)) + 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 @@ -55,4 +55,9 @@ 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 From 43c5675aecc1dbfd5782cb26103bc20ef444795c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20G=C3=B6rgens?= Date: Wed, 25 Jan 2023 13:00:27 +0100 Subject: [PATCH 23/24] fix: remove unused function --- app/controllers/groups_controller.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 497a631a..b7e4b445 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -103,11 +103,6 @@ def unprocessable_response(format, redirect:, entity:) format.json { render json: entity.errors, status: :unprocessable_entity } end - def respond_with_notice(format, redirect:, notice:) - format.html { redirect_to redirect, notice: notice } - format.json { head :no_content } - end - # Only allow a list of trusted parameters through. def group_params params.require(:group).permit(:name) From bc5eaaa7296e0cfc9dcb725afaced3c51b5d1d25 Mon Sep 17 00:00:00 2001 From: Karl Broschke Date: Sun, 29 Jan 2023 15:35:35 +0100 Subject: [PATCH 24/24] Update button text in add user modal Co-authored-by: Malte S. <18194757+DieKautz@users.noreply.github.com> --- app/views/groups/_modal-add-member.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/groups/_modal-add-member.html.erb b/app/views/groups/_modal-add-member.html.erb index 8983a519..b8ca7535 100644 --- a/app/views/groups/_modal-add-member.html.erb +++ b/app/views/groups/_modal-add-member.html.erb @@ -13,7 +13,7 @@ <% end %>