2017-03-18 76 views
0

我想以其他用户的身份登录,但我希望该会话会在5分钟后自动过期。有没有办法在设计的登录功能中通过超时参数?

但我的默认到期时间为1小时,我在devise.rb设置为

config.timeout_in = 1.hour 

Forexample我有一个用户列表。 one is [email protected] another is [email protected]

我以[email protected]身份登录。 现在我想登录为[email protected], 所以我会注销,然后登录为作为

sign_out 

sign_in (:user, test_user_2)

但因为我原来的帐户是[email protected],并我只是以[email protected]为开发目的登录,我希望会话在一段时间后(如5分钟)自动超时,而不是默认1小时。

有没有办法在devise的登录函数中传递timeout参数?

回答

2

这有点难做。您可以通过重写def timeout_in Devise方法来动态设置用户模型中的timeout_。

# You could add a boolean onto the user if testing and set it to true once signed in with a testing user. 
# You can then somehow set it to false once the user signs out after testing a user. 

def timeout_in 
    if self.testing? 
    5.minutes 
    else 
    30.minutes 
    end 
end 

另一个解决这个问题,虽然它并不直接回答你的问题是让以前的管理员辞职到他以前的帐户时,他/她与测试完成工作的能力。成为另一个用户时,您可以使用之前用户的ID设置会话[:admin_logged_in]。如果会话[:admin_logged_in]存在,则可以创建一个路由,让用户再次成为管理员并签出测试用户。在用户再次成为管理员用户后,您可以清除会话[:admin_logged_in]。

def become_other_user 
    session[:admin_logged_in] = current_user.id 
    sign_out current_user 
    user = User.find(params[:id]) 
    sign_in(:user, user, { :bypass => true }) 
    redirect_to something_path 
end 


def become_admin_user 
    if session[:admin_logged_in].present? 
    if current_user 
     admin_user = User.find_by_id(session[:admin_logged_in]) 
     sign_out current_user 
     sign_in(:user, admin_user, { :bypass => true }) 
     session[:admin_logged_in].clear 
     flash.clear if flash.present? 
    end 
    end 
end 

# erb nav file 

<% if session[:admin_logged_in].present? %> 
    <%= link_to 'Switch To Admin', become_admin_user_path, class: '#' %> 
<% end %> 
+0

感谢这个信息大卫,我很好奇,顺便说一句是不是有可以覆盖一些设备的方法?设计会检查会话是否存在? – user1735921

相关问题