diff --git a/app/controllers/contact_topics_controller.rb b/app/controllers/contact_topics_controller.rb index 9545e76d7d..a781126f73 100644 --- a/app/controllers/contact_topics_controller.rb +++ b/app/controllers/contact_topics_controller.rb @@ -17,7 +17,7 @@ def edit # POST /contact_topics or /contact_topics.json def create - @contact_topic = ContactTopic.new(contact_topic_params) + @contact_topic = ContactTopic.new(contact_topic_params.merge(casa_org_id: current_user.casa_org_id)) authorize @contact_topic if @contact_topic.save @@ -56,8 +56,9 @@ def set_contact_topic @contact_topic = ContactTopic.find(params[:id]) end - # Only allow a list of trusted parameters through. + # Only allow a list of trusted parameters through. The org is never taken from params: new topics + # belong to the creator's org and existing topics cannot be moved to another one. def contact_topic_params - params.require(:contact_topic).permit(:casa_org_id, :question, :details, :active, :exclude_from_court_report) + params.require(:contact_topic).permit(:question, :details, :active, :exclude_from_court_report) end end diff --git a/app/controllers/contact_types_controller.rb b/app/controllers/contact_types_controller.rb index b93861d8bc..e9452bcf9d 100644 --- a/app/controllers/contact_types_controller.rb +++ b/app/controllers/contact_types_controller.rb @@ -39,7 +39,12 @@ def set_contact_type @contact_type = ContactType.find(params[:id]) end + # The group decides which org a contact type belongs to, so it must come from the current org. def contact_type_params - params.require(:contact_type).permit(:name, :contact_type_group_id, :active) + permitted = params.require(:contact_type).permit(:name, :contact_type_group_id, :active) + if permitted[:contact_type_group_id].present? + permitted[:contact_type_group_id] = current_organization.contact_type_groups.find(permitted[:contact_type_group_id]).id + end + permitted end end diff --git a/app/views/contact_topics/_form.html.erb b/app/views/contact_topics/_form.html.erb index b3e0b650d3..0d26b3ea77 100644 --- a/app/views/contact_topics/_form.html.erb +++ b/app/views/contact_topics/_form.html.erb @@ -12,7 +12,6 @@

<%= title %>

<%= form_with model: contact_topic, local: true, class: "space-y-5" do |form| %> - <%= form.hidden_field :casa_org_id %> <%= render "shared/form_errors", resource: contact_topic %>
<%= form.label :question, class: label_class do %>Question <%= req %><% end %> diff --git a/spec/requests/contact_topics_spec.rb b/spec/requests/contact_topics_spec.rb index d1ce67dd56..9589214d47 100644 --- a/spec/requests/contact_topics_spec.rb +++ b/spec/requests/contact_topics_spec.rb @@ -66,6 +66,16 @@ end end + context "when casa_org_id of another org is submitted" do + let(:other_org) { create(:casa_org) } + + it "creates the topic in the creator's own org" do + post contact_topics_url, params: {contact_topic: {casa_org_id: other_org.id, question: "q", details: "d"}} + + expect(ContactTopic.last.casa_org).to eq(casa_org) + end + end + context "with invalid parameters" do let(:attributes) do { @@ -129,7 +139,7 @@ end context "with invalid parameters" do - let(:attributes) { {casa_org_id: 0} } + let(:attributes) { {question: ""} } it "renders a response with 422 status (i.e. to display the 'edit' template)" do patch contact_topic_url(contact_topic), params: {contact_topic: attributes} @@ -138,6 +148,26 @@ end end + describe "PATCH /update with another org's id" do + let(:other_org) { create(:casa_org) } + let!(:contact_topic) { create(:contact_topic, casa_org:) } + + it "does not move the topic into another org" do + patch contact_topic_url(contact_topic), params: {contact_topic: {casa_org_id: other_org.id, question: "renamed"}} + + expect(contact_topic.reload.casa_org).to eq(casa_org) + expect(contact_topic.question).to eq("renamed") + end + + it "does not let an admin edit another org's topic" do + foreign = create(:contact_topic, casa_org: other_org) + + patch contact_topic_url(foreign), params: {contact_topic: {question: "hijacked"}} + + expect(foreign.reload.question).not_to eq("hijacked") + end + end + describe "DELETE /soft_delete" do let!(:contact_topic) { create(:contact_topic, casa_org: casa_org) } diff --git a/spec/requests/contact_types_spec.rb b/spec/requests/contact_types_spec.rb index 4bbdbe0850..94cd82a035 100644 --- a/spec/requests/contact_types_spec.rb +++ b/spec/requests/contact_types_spec.rb @@ -3,6 +3,38 @@ RSpec.describe "/contact_types", type: :request do let(:group) { create(:contact_type_group) } + describe "writes that reach into another org" do + let(:org) { create(:casa_org) } + let(:other_group) { create(:contact_type_group, casa_org: create(:casa_org)) } + let(:own_group) { create(:contact_type_group, casa_org: org) } + + before { sign_in create(:casa_admin, casa_org: org) } + + it "cannot create a contact type inside another org's group", :aggregate_failures do + expect { + post contact_types_path, params: {contact_type: {name: "Sneaky", contact_type_group_id: other_group.id}} + }.not_to change(ContactType, :count) + expect(response).to have_http_status(:not_found) + end + + it "cannot move a contact type into another org's group", :aggregate_failures do + contact_type = create(:contact_type, contact_type_group: own_group) + + put contact_type_path(contact_type), params: {contact_type: {contact_type_group_id: other_group.id}} + + expect(contact_type.reload.contact_type_group).to eq(own_group) + expect(response).to have_http_status(:not_found) + end + + it "cannot edit another org's contact type" do + foreign = create(:contact_type, contact_type_group: other_group) + + put contact_type_path(foreign), params: {contact_type: {name: "hijacked"}} + + expect(foreign.reload.name).not_to eq("hijacked") + end + end + describe "GET /contact_types/new" do context "logged in as admin user" do it "can successfully access a contact type create page" do