简体   繁体   English

如何使用Authlogic确保用户属于特定模型?

[英]How can I make sure Users belong to a particular model using Authlogic?

I'm using Authlogic to perform authentication for my Rails app. 我正在使用Authlogic对我的Rails应用执行身份验证。 My users log in to a subdomain which is stored as part of a Client model. 我的用户登录到子域,该子域作为客户端模型的一部分存储。 Users belong_to clients and Clients authenticate_many UserSessions. 用户属于客户端,客户端属于authenticate_many UserSessions。 Logging in works fine; 登录工作正常; the problem I'm having is that users are able to log in to any subdomain whether or not they belong to that subdomain's client. 我遇到的问题是,无论用户是否属于该子域的客户端,他们都可以登录到任何子域。 Here's my code: 这是我的代码:

Application Controller: 应用控制器:

class ApplicationController < ActionController::Base
  helper_method :current_user_session, :current_user, :current_client

  private

  def current_user_session
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end

  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.user
  end

  def current_client
    subdomain = request.subdomain

    if subdomain.present? && subdomain != 'www'
      @current_client = Client.find_by_subdomain(subdomain)
    else
      @current_client = nil
    end
  end
end

UserSessions controller: UserSessions控制器:

class UserSessionsController < ApplicationController
  def new
    @user_session = current_client.user_sessions.new
  end

  def create
    params[:user_session][:client] = current_client
    @user_session = current_client.user_sessions.new(params[:user_session])
    if @user_session.save
      redirect_to dashboard_path
    else
      render :action => 'new'
    end
  end
end

Client model: 客户模型:

class Client < ActiveRecord::Base
  authenticates_many :user_sessions, :find_options => { :limit => 1 } 

  has_many :users, :uniq => true
end

User and UserSession models: 用户和UserSession模型:

class User < ActiveRecord::Base
  belongs_to :client

  acts_as_authentic do |c|
    c.validations_scope = :client_id
  end
end

class UserSession < Authlogic::Session::Base
end

I'm running Rails 3 on Mac os X with the latest version of Authlogic installed. 我在安装了最新版本Authlogic的Mac OS X上运行Rails 3。 Any help would be much appreciated! 任何帮助将非常感激!

You will need to add a custom validation. 您将需要添加自定义验证。

 class UserSession

   attr_accessor :current_client
   before_validation :check_if_user_of_client

   private
     def check_if_user_of_client
       errors.add(:base, "Not your client/subdomain etc.") unless client.eql?(current_client) # client.eql? == self.client.eql? the associated client of current_user
     end
 end

Remember to set the current_client attribute accessor in the UserSessions controller as this will not otherwise be available in the model. 记住要在UserSessions控制器中设置current_client属性访问器,否则在模型中将不可用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能使用户具有has_many个对象,但每个对象都可以属于许多用户? - How can I make a user has_many objects but each object can belong_to MANY users? 我可以使用authlogic轻松地将用户密码从一个应用程序导出到另一个应用程序吗? - Can I easily export users passwords from one app to another using authlogic? 如何使模型的实例属于用户模型中的所有用户? - How should I get my model's instances to belong to all users in my user model? 如何将AuthLogic配置为仅对某些用户(管理员)超时会话? - How can AuthLogic be configured to only timeout sessions for certain users (admins)? 如何在Rails中建模“可以属于A或B”关系? - How to model a “can belong to A or B” relationship in Rails? 如何使用存根指定authlogic会话控制器? - How can I spec out an authlogic sessions controller using using a stub? 使用Carrierwave,如何在尝试将其保存到记录之前确保远程URL是有效的图片? - Using Carrierwave, how can I make sure the remote url is valid picture before trying to save it to the record? Rails authlogic:如何制作关卡? - Rails authlogic : How to make Levels? 如何使用Rails RSpec和Capybara和Selenium进行测试以确保显示模态 - Using Rails RSpec and Capybara and Selenium how can I test to make sure a modal is being displayed 如果模型的父母只能属于其中一个,如何通过该父母映射关系? - How do I map an association through a model's parents if it can only belong to one of them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM