我們遇到以下問題:我們的應用程序通過交換Web服務(EWS)與交換接口連接,您可以將其視爲僅僅是一個奇特的數據庫。 作爲一個例子,我們有一個Appointment模型,它應該與Exchange上的日曆同步。現在EWS需要每個連接的用戶憑證。這產生了兩個問題:Rails - 基於用戶憑據的每個請求的數據庫連接
- 我們如何獲得每個請求的用戶憑證?
- 該模型應該如何與EWS進行交互?
我們不想要的是詢問用戶在每個需要與EWS交互的操作上的密碼。我們也不想犧牲安全。
我們目前的解決方案:
我們存儲從用戶在會話的憑據(我們在登錄過程中得到)。 這很醜陋,因爲這意味着用戶名和密碼存儲在客戶端計算機上的cookie中 - 我知道它是加密的,但它仍然很難看。
然後在控制器中,我們懶洋洋地創建一個EWS客戶端,並將它傳遞給每個需要與EWS交互的模型實例。 這很醜陋,因爲這意味着與其他數據庫操作不同(您可以將EWS視爲另一個數據庫),這意味着您必須爲每個操作執行額外的步驟。
例(簡體/剝離下來):
# app/models/appointments.rb
class Appointment
attr_writer :ews
def pull_from_exchange
exchange_event = ews.events[exchange_id, exchange_change_key]
# …update from exchange_event.to_hash…
self
end
def push_to_exchange
exchange_event = ews.events.new(to_hash_for_exchange)
exchange_event.save
update_attributes(exchange_id: exchange_event.id, exchange_change_key: exchange_event.change_key)
self
end
def ews
raise EwsClientNotProvided unless @ews
@ews
end
def save_and_push!
self.class.transaction do
save!
push_to_exchange
end
end
end
# app/controllers/appointments_controller.rb
class AppointmentsController < ApplicationController
def create
@appointment = Appointment.new(params[:appointment])
@appointment.ews = ews
@appointment.save_and_push!
end
private
def ews
@ews ||= Ews.client(session.slice(:username, :password, :email).merge(endpoint: ews_endpoint))
end
def ews_endpoint
Rails.application.configuration.ews_endpoint
end
end
我感興趣的替代性和/或更好的設計來處理這種情況。
任何意見/建議?
保留線程ID到客戶端的散列? – 2012-07-26 09:20:01
你能詳細說一下嗎? – apeiros 2012-07-27 06:44:50