From 6bb026621d26b061149f7df07d8cb42c8f6fc773 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Mon, 29 Mar 2021 23:26:20 +0530 Subject: [PATCH] fix: Current.user nil issue when using access tokens (#2012) --- .rubocop.yml | 1 + .../api/v1/accounts/contacts/conversations_controller.rb | 6 +++--- app/controllers/concerns/access_token_auth_helper.rb | 3 ++- app/models/contact.rb | 1 + spec/factories/contacts.rb | 2 +- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 01951faea..0fca6f54b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -105,6 +105,7 @@ Rails/UniqueValidationWithoutIndex: Exclude: - 'app/models/channel/twitter_profile.rb' - 'app/models/webhook.rb' + - 'app/models/contact.rb' Rails/RenderInline: Exclude: - 'app/controllers/swagger_controller.rb' diff --git a/app/controllers/api/v1/accounts/contacts/conversations_controller.rb b/app/controllers/api/v1/accounts/contacts/conversations_controller.rb index 8a9199b6b..576fbaa2f 100644 --- a/app/controllers/api/v1/accounts/contacts/conversations_controller.rb +++ b/app/controllers/api/v1/accounts/contacts/conversations_controller.rb @@ -8,10 +8,10 @@ class Api::V1::Accounts::Contacts::ConversationsController < Api::V1::Accounts:: private def inbox_ids - if current_user.administrator? + if Current.user.administrator? Current.account.inboxes.pluck(:id) - elsif current_user.agent? - current_user.assigned_inboxes.pluck(:id) + elsif Current.user.agent? + Current.user.assigned_inboxes.pluck(:id) else [] end diff --git a/app/controllers/concerns/access_token_auth_helper.rb b/app/controllers/concerns/access_token_auth_helper.rb index 3d6f55674..91bd461aa 100644 --- a/app/controllers/concerns/access_token_auth_helper.rb +++ b/app/controllers/concerns/access_token_auth_helper.rb @@ -14,6 +14,7 @@ module AccessTokenAuthHelper render_unauthorized('Invalid Access Token') && return if @access_token.blank? @resource = @access_token.owner + Current.user = @resource if current_user.is_a?(User) end def super_admin? @@ -21,7 +22,7 @@ module AccessTokenAuthHelper end def validate_bot_access_token! - return if current_user.is_a?(User) + return if Current.user.is_a?(User) return if super_admin? return if agent_bot_accessible? diff --git a/app/models/contact.rb b/app/models/contact.rb index f62ffc1f8..3d7eedf9a 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -31,6 +31,7 @@ class Contact < ApplicationRecord validates :account_id, presence: true validates :email, allow_blank: true, uniqueness: { scope: [:account_id], case_sensitive: false } validates :identifier, allow_blank: true, uniqueness: { scope: [:account_id] } + validates :phone_number, allow_blank: true, uniqueness: { scope: [:account_id] } belongs_to :account has_many :conversations, dependent: :destroy diff --git a/spec/factories/contacts.rb b/spec/factories/contacts.rb index eaecd67e9..68ca981e0 100644 --- a/spec/factories/contacts.rb +++ b/spec/factories/contacts.rb @@ -4,7 +4,7 @@ FactoryBot.define do factory :contact do sequence(:name) { |n| "Contact #{n}" } sequence(:email) { |n| "contact-#{n}@example.com" } - phone_number { '+123456789011' } + phone_number { Faker::PhoneNumber.cell_phone_in_e164 } avatar { fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png') } account end