Restrict access to the "new" direct message action

This way only verified users will be able to access this page, which
shows the username of the receiver of the direct message. With this,
it's no longer possible for unverified users to browse direct message
URLs in order to collect usernames from every user.
This commit is contained in:
Javi Martín
2023-11-24 16:23:33 +01:00
parent 80f0d710fd
commit 2db807baa7
5 changed files with 12 additions and 24 deletions

View File

@@ -1,8 +1,12 @@
class DirectMessagesController < ApplicationController class DirectMessagesController < ApplicationController
before_action :authenticate_user!
load_and_authorize_resource :user, instance_name: :receiver load_and_authorize_resource :user, instance_name: :receiver
load_and_authorize_resource through: :receiver, through_association: :direct_messages_received load_resource through: :receiver, through_association: :direct_messages_received
authorize_resource except: :new
def new def new
authorize! :new, @direct_message, message: t("users.direct_messages.new.verified_only",
verify_account: helpers.link_to_verify_account)
end end
def create def create

View File

@@ -18,7 +18,6 @@ module Abilities
can :read_results, Budget, id: Budget.finished.results_enabled.ids can :read_results, Budget, id: Budget.finished.results_enabled.ids
can :read_stats, Budget, id: Budget.valuating_or_later.stats_enabled.ids can :read_stats, Budget, id: Budget.valuating_or_later.stats_enabled.ids
can :read_executions, Budget, phase: "finished" can :read_executions, Budget, phase: "finished"
can :new, DirectMessage
can [:read, :debate, :draft_publication, :allegations, :result_publication, can [:read, :debate, :draft_publication, :allegations, :result_publication,
:proposals, :milestones], Legislation::Process, published: true :proposals, :milestones], Legislation::Process, published: true
can :summary, Legislation::Process, can :summary, Legislation::Process,

View File

@@ -6,21 +6,7 @@
<%= t("users.direct_messages.new.title", receiver: @receiver.name) %> <%= t("users.direct_messages.new.title", receiver: @receiver.name) %>
</h1> </h1>
<% if not current_user %> <% if @receiver.email_on_direct_message? %>
<div class="callout primary">
<p>
<%= sanitize(t("users.login_to_continue",
signin: link_to_signin,
signup: link_to_signup)) %>
</p>
</div>
<% elsif not @receiver.email_on_direct_message? %>
<div class="callout primary">
<p>
<%= t("users.direct_messages.new.direct_messages_bloqued") %>
</p>
</div>
<% elsif can? :create, @direct_message %>
<%= form_for [@receiver, @direct_message] do |f| %> <%= form_for [@receiver, @direct_message] do |f| %>
<%= render "shared/errors", resource: @direct_message %> <%= render "shared/errors", resource: @direct_message %>
@@ -32,10 +18,9 @@
</div> </div>
<% end %> <% end %>
<% else %> <% else %>
<div class="callout warning"> <div class="callout primary">
<p> <p>
<%= sanitize(t("users.direct_messages.new.verified_only", <%= t("users.direct_messages.new.direct_messages_bloqued") %>
verify_account: link_to_verify_account)) %>
</p> </p>
</div> </div>
<% end %> <% end %>

View File

@@ -92,7 +92,7 @@ describe Abilities::Common do
it { should_not be_able_to(:comment_as_administrator, proposal) } it { should_not be_able_to(:comment_as_administrator, proposal) }
it { should_not be_able_to(:comment_as_moderator, proposal) } it { should_not be_able_to(:comment_as_moderator, proposal) }
it { should be_able_to(:new, DirectMessage) } it { should_not be_able_to(:new, DirectMessage) }
it { should_not be_able_to(:create, DirectMessage) } it { should_not be_able_to(:create, DirectMessage) }
it { should_not be_able_to(:show, DirectMessage) } it { should_not be_able_to(:show, DirectMessage) }

View File

@@ -54,7 +54,7 @@ describe "Direct messages" do
visit new_user_direct_message_path(receiver) visit new_user_direct_message_path(receiver)
expect(page).to have_content "To send a private message verify your account" expect(page).to have_content "To send a private message verify your account"
expect(page).not_to have_link "Send private message" expect(page).to have_current_path root_path
end end
scenario "User not logged in" do scenario "User not logged in" do
@@ -62,8 +62,8 @@ describe "Direct messages" do
visit new_user_direct_message_path(receiver) visit new_user_direct_message_path(receiver)
expect(page).to have_content "You must sign in or sign up to continue." expect(page).to have_content "You must sign in or register to continue."
expect(page).not_to have_link "Send private message" expect(page).to have_current_path new_user_session_path
end end
scenario "Accessing form directly" do scenario "Accessing form directly" do