Skip to content

Commit a2a533b

Browse files
committed
pass in user id for just in time user impersonation override
1 parent ab34ca8 commit a2a533b

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.7
1+
3.4.7

lib/zendesk_api/client.rb

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,30 @@ def initialize
104104
add_warning_callback
105105
end
106106

107+
# token impersonation for the scope of the block
108+
# @param [String] username The username (email) of the user to impersonate
109+
# @yield The block to run while impersonating the user
110+
# @example
111+
# client.api_token_impersonate("[email protected]") do
112+
# client.tickets.create(:subject => "Help!")
113+
# end
114+
#
115+
# # creates a ticket on behalf of otheruser
116+
# @return
117+
# yielded value
118+
def api_token_impersonate(username)
119+
Thread.current[:local_connection] = build_connection(username)
120+
yield
121+
ensure
122+
Thread.current[:local_connection] = nil
123+
end
107124
# Creates a connection if there is none, otherwise returns the existing connection.
108125
#
109126
# @return [Faraday::Connection] Faraday connection for the client
110127
def connection
128+
if Thread.current[:local_connection]
129+
return Thread.current[:local_connection]
130+
end
111131
@connection ||= build_connection
112132
end
113133

@@ -142,7 +162,7 @@ def self.check_deprecated_namespace_usage(attributes, name)
142162
# Request logger if logger is not nil
143163
#
144164
# Retry middleware if retry is true
145-
def build_connection
165+
def build_connection(username = nil)
146166
Faraday.new(config.options) do |builder|
147167
# response
148168
builder.use ZendeskAPI::Middleware::Response::RaiseError
@@ -158,7 +178,7 @@ def build_connection
158178
builder.use ZendeskAPI::Middleware::Response::Deflate
159179
end
160180

161-
set_authentication(builder, config)
181+
set_authentication(builder, config, username)
162182

163183
if config.cache
164184
builder.use ZendeskAPI::Middleware::Request::EtagCache, :cache => config.cache
@@ -206,7 +226,6 @@ def set_raise_error_when_rated_limited
206226

207227
def set_token_auth
208228
return if config.password || config.token.nil?
209-
210229
if config.username.nil?
211230
raise ArgumentError, "you need to provide a username when using API token auth"
212231
else
@@ -234,13 +253,13 @@ def add_warning_callback
234253
end
235254

236255
# See https://lostisland.github.io/faraday/middleware/authentication
237-
def set_authentication(builder, config)
256+
def set_authentication(builder, config, username = nil)
238257
if config.access_token && !config.url_based_access_token
239258
builder.request :authorization, "Bearer", config.access_token
240259
elsif config.access_token
241260
builder.use ZendeskAPI::Middleware::Request::UrlBasedAccessToken, config.access_token
242261
else
243-
builder.request :authorization, :basic, config.username, config.password
262+
builder.request :authorization, :basic, username ? username + "/token" : config.username , config.password
244263
end
245264
end
246265
end

0 commit comments

Comments
 (0)