feat: Ability for super admin to impersonate a user (#6382)

This commit is contained in:
Sojan Jose
2023-02-07 00:30:08 +05:30
committed by GitHub
parent 0a2d3130e0
commit 6b839a0442
6 changed files with 46 additions and 4 deletions

View File

@@ -13,8 +13,7 @@ class Platform::Api::V1::UsersController < PlatformController
end end
def login def login
encoded_email = ERB::Util.url_encode(@resource.email) render json: { url: @resource.generate_sso_link }
render json: { url: "#{ENV.fetch('FRONTEND_URL', nil)}/app/login?email=#{encoded_email}&sso_auth_token=#{@resource.generate_sso_auth_token}" }
end end
def show; end def show; end

View File

@@ -15,6 +15,11 @@ module SsoAuthenticatable
::Redis::Alfred.get(sso_token_key(token)).present? ::Redis::Alfred.get(sso_token_key(token)).present?
end end
def generate_sso_link
encoded_email = ERB::Util.url_encode(email)
"#{ENV.fetch('FRONTEND_URL', nil)}/app/login?email=#{encoded_email}&sso_auth_token=#{generate_sso_auth_token}"
end
private private
def sso_token_key(token) def sso_token_key(token)

View File

@@ -0,0 +1,8 @@
<section class="main-content__body">
<hr/>
<div class="form-actions">
<p class="text-color-red">Caution: Any actions executed after impersonate will appear as if performed by the impersonated user - [<%= page.resource.name %> ]</p>
<a class='button' target='_blank' href='<%= page.resource.generate_sso_link %>'>Impersonate user </a>
</div>
</section>

View File

@@ -85,3 +85,5 @@ as well as a link to its edit page.
<% end %> <% end %>
</section> </section>
<%= render partial: "impersonate", locals: {page: page} %>

View File

@@ -3,7 +3,7 @@
### Usage ##### ### Usage #####
# #
# # Seed an account with all data types in this class # # Seed an account with all data types in this class
# Seeders::AccountSeeder.new(account: @Account.find(1)).perform! # Seeders::AccountSeeder.new(account: Account.find(1)).perform!
# #
# #
############################################################ ############################################################
@@ -89,16 +89,25 @@ class Seeders::AccountSeeder
conversation = contact_inbox.conversations.create!(account: contact_inbox.inbox.account, contact: contact_inbox.contact, conversation = contact_inbox.conversations.create!(account: contact_inbox.inbox.account, contact: contact_inbox.contact,
inbox: contact_inbox.inbox, assignee: assignee) inbox: contact_inbox.inbox, assignee: assignee)
create_messages(conversation: conversation, messages: conversation_data['messages']) create_messages(conversation: conversation, messages: conversation_data['messages'])
conversation.update_labels(conversation_data[:labels]) if conversation_data[:labels].present?
end end
def create_messages(conversation:, messages:) def create_messages(conversation:, messages:)
messages.each do |message_data| messages.each do |message_data|
sender = User.find_by(email: message_data['sender']) if message_data['sender'].present? sender = find_message_sender(conversation, message_data)
conversation.messages.create!(message_data.slice('content', 'message_type').merge(account: conversation.inbox.account, sender: sender, conversation.messages.create!(message_data.slice('content', 'message_type').merge(account: conversation.inbox.account, sender: sender,
inbox: conversation.inbox)) inbox: conversation.inbox))
end end
end end
def find_message_sender(conversation, message_data)
if message_data['message_type'] == 'incoming'
User.find_by(email: message_data['sender']) if message_data['sender'].present?
else
conversation.contact
end
end
def seed_inboxes def seed_inboxes
Seeders::InboxSeeder.new(account: @account, company_data: @account_data[:company]).perform! Seeders::InboxSeeder.new(account: @account, company_data: @account_data[:company]).perform!
end end

View File

@@ -222,6 +222,7 @@ contacts:
gender: 'male' gender: 'male'
conversations: conversations:
- channel: Channel::Whatsapp - channel: Channel::Whatsapp
source_id: "123456723"
messages: messages:
- message_type: incoming - message_type: incoming
content: hello world content: hello world
@@ -263,6 +264,7 @@ contacts:
gender: 'male' gender: 'male'
conversations: conversations:
- channel: Channel::Whatsapp - channel: Channel::Whatsapp
source_id: "12323432"
messages: messages:
- message_type: incoming - message_type: incoming
content: hello world content: hello world
@@ -271,6 +273,7 @@ contacts:
gender: 'male' gender: 'male'
conversations: conversations:
- channel: Channel::Whatsapp - channel: Channel::Whatsapp
source_id: "12342234324"
messages: messages:
- message_type: incoming - message_type: incoming
content: hello world content: hello world
@@ -330,6 +333,12 @@ contacts:
gender: 'female' gender: 'female'
conversations: conversations:
- channel: Channel::Whatsapp - channel: Channel::Whatsapp
source_id: "1223423567"
labels:
- billing
- delivery
- ops-handover
- premium-customer
messages: messages:
- message_type: incoming - message_type: incoming
content: "Hey \n I didn't get the product delivered, but it shows it is delivered to my address. Please check" content: "Hey \n I didn't get the product delivered, but it shows it is delivered to my address. Please check"
@@ -338,6 +347,9 @@ contacts:
gender: 'male' gender: 'male'
conversations: conversations:
- channel: Channel::WebWidget - channel: Channel::WebWidget
labels:
- software
- ops-handover
messages: messages:
- message_type: incoming - message_type: incoming
content: "Hey there, \n I need some help with the product, my button is not working on the website." content: "Hey there, \n I need some help with the product, my button is not working on the website."
@@ -347,6 +359,10 @@ contacts:
conversations: conversations:
- channel: Channel::WebWidget - channel: Channel::WebWidget
assignee: michael_scott@paperlayer.test assignee: michael_scott@paperlayer.test
labels:
- billing
- software
- lead
messages: messages:
- message_type: incoming - message_type: incoming
content: "Hey, \n My card is not working on your website. Please help" content: "Hey, \n My card is not working on your website. Please help"
@@ -357,6 +373,9 @@ contacts:
- channel: Channel::Email - channel: Channel::Email
source_id: "cmathersonj@va.test" source_id: "cmathersonj@va.test"
assignee: michael_scott@paperlayer.test assignee: michael_scott@paperlayer.test
labels:
- billing
- lead
messages: messages:
- message_type: incoming - message_type: incoming
content: "Hey, \n I'm looking for some help to figure out if it is the right product for me." content: "Hey, \n I'm looking for some help to figure out if it is the right product for me."