![](/img/trans.png)
[英]Will I regret using Devise for this? RoR, multiple user/admin layers, multi-tenency, fast user switching
[英]How to do RESTful deep linking when using multi-tenency with scopes and CanCan
背景
我的應用程序正在使用Multi-Tenency With Scopes ,這基本上意味着我的許多模型使用default_scope將current_tenant.id與tenant_id列進行比較,以確定current_user是否有權訪問該對象。
我也在使用CanCan進行授權。
問題:如何為尚未登錄應用程序的用戶深層鏈接到特定對象?
一般用例
我希望能夠通過電子郵件發送給用戶並說:“此任務已分配給您。點擊此處查看任務。” 然后,用戶可以單擊鏈接(例如, http : //myapp.com/task/15 ),如果他們已經登錄到相應的租戶(該任務所屬的),則直接進入該任務。 或者,如果他們沒有登錄,他們將被重定向到登錄頁面,並顯示“請登錄以訪問此頁面”的消息。
例子
首先,一個行為與我期望的一樣的例子:
如果用戶注銷並轉到http://myapp.com/tasks ,則CanCan會檢查“Guest”用戶是否有權轉到Tasks#index。 訪客未被授權這樣做,因此CanCan引發了一個異常,我通過記錄目標路徑並重定向到登錄頁面並輸入“請登錄以訪問此頁面”來處理該異常。
這是一個令我難過的例子:
用戶已注銷並單擊指向http://myapp.com/tasks/15的鏈接。 用戶將總是得到404(未找到)錯誤,因為Rails做的第一件事是嘗試找到該特定對象。 但是由於用戶已注銷(意味着未定義current_tenant),並且該任務是針對tenant_id == 5,因此Rails無法找到該對象並引發異常。
這是完整的調試/堆棧跟蹤:
INFO -- : Started GET "/tasks/15" for 127.0.0.1 at 2014-12-14 11:16:59 -0500
INFO -- : Processing by TasksController#show as HTML
INFO -- : Parameters: {"id"=>"15"}
DEBUG -- : Task Load (0.8ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."tenant_id" IS NULL AND "tasks"."id" = $1 LIMIT 1 [["id", 15]]
INFO -- : Completed 404 Not Found in 4ms
FATAL -- :
ActiveRecord::RecordNotFound (Couldn't find Task with 'id'=15 [WHERE "tasks"."tenant_id" IS NULL]):
app/controllers/application_controller.rb:46:in `scope_current_tenant'
這是application_controller的'scope_current_tenant'方法:
def scope_current_tenant
Tenant.current_id = current_tenant.id if current_tenant
yield
ensure
Tenant.current_id = nil
end
我喜歡發生的事情與用戶在注銷時嘗試訪問Tasks#index時發生的情況相同。
任何想法如何處理這個?
聽起來您需要在嘗試查找任務之前驗證用戶是否已登錄。 你有一個before_action :authenticate_user!
你的TasksController
類似的TasksController
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.