fix(signatures): allow admins to manage inbox signatures without explicit membership (#260)
Administrators have access to all inboxes in an account but the validate_inbox_access filter only checked InboxMember records, returning 401 for admins not explicitly added as inbox members.
This commit is contained in:
parent
8cf6e8907f
commit
b0a8fa70d0
@ -48,8 +48,14 @@ class Api::V1::Profile::InboxSignaturesController < Api::BaseController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def validate_inbox_access
|
def validate_inbox_access
|
||||||
inbox_id = params[:inbox_id]
|
inbox = Inbox.find_by(id: params[:inbox_id])
|
||||||
return if InboxMember.exists?(user_id: @user.id, inbox_id: inbox_id)
|
return head :not_found unless inbox
|
||||||
|
|
||||||
|
account_user = @user.account_users.find_by(account_id: inbox.account_id)
|
||||||
|
return head :unauthorized unless account_user
|
||||||
|
|
||||||
|
return if account_user.administrator?
|
||||||
|
return if InboxMember.exists?(user_id: @user.id, inbox_id: inbox.id)
|
||||||
|
|
||||||
head :unauthorized
|
head :unauthorized
|
||||||
end
|
end
|
||||||
|
|||||||
@ -102,6 +102,23 @@ RSpec.describe 'Profile Inbox Signatures API', type: :request do
|
|||||||
expect(response).to have_http_status(:unauthorized)
|
expect(response).to have_http_status(:unauthorized)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when the user is an administrator without inbox membership' do
|
||||||
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||||
|
|
||||||
|
it 'returns the inbox signature' do
|
||||||
|
inbox_signature = create(:inbox_signature, user: admin, inbox: inbox)
|
||||||
|
|
||||||
|
get "/api/v1/profile/inbox_signatures/#{inbox.id}",
|
||||||
|
headers: admin.create_new_auth_token,
|
||||||
|
as: :json
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
json_response = response.parsed_body
|
||||||
|
expect(json_response['inbox_id']).to eq(inbox.id)
|
||||||
|
expect(json_response['message_signature']).to eq(inbox_signature.message_signature)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'PUT /api/v1/profile/inbox_signatures/:inbox_id' do
|
describe 'PUT /api/v1/profile/inbox_signatures/:inbox_id' do
|
||||||
@ -164,6 +181,23 @@ RSpec.describe 'Profile Inbox Signatures API', type: :request do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when the user is an administrator without inbox membership' do
|
||||||
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||||
|
|
||||||
|
it 'creates a new inbox signature' do
|
||||||
|
expect do
|
||||||
|
put "/api/v1/profile/inbox_signatures/#{inbox.id}",
|
||||||
|
params: signature_params,
|
||||||
|
headers: admin.create_new_auth_token,
|
||||||
|
as: :json
|
||||||
|
end.to change(InboxSignature, :count).by(1)
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
json_response = response.parsed_body
|
||||||
|
expect(json_response['message_signature']).to eq('<p>Custom Signature</p>')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when params are invalid' do
|
context 'when params are invalid' do
|
||||||
let(:invalid_params) do
|
let(:invalid_params) do
|
||||||
{
|
{
|
||||||
@ -221,5 +255,21 @@ RSpec.describe 'Profile Inbox Signatures API', type: :request do
|
|||||||
expect(response).to have_http_status(:unauthorized)
|
expect(response).to have_http_status(:unauthorized)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when the user is an administrator without inbox membership' do
|
||||||
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||||
|
|
||||||
|
it 'deletes the inbox signature' do
|
||||||
|
create(:inbox_signature, user: admin, inbox: inbox)
|
||||||
|
|
||||||
|
expect do
|
||||||
|
delete "/api/v1/profile/inbox_signatures/#{inbox.id}",
|
||||||
|
headers: admin.create_new_auth_token,
|
||||||
|
as: :json
|
||||||
|
end.to change(InboxSignature, :count).by(-1)
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:no_content)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user