1

Я использую Authlogic для выполнения проверки подлинности для моего приложения Rails. Мои пользователи входят в поддомен, который хранится как часть модели клиента. Пользователи принадлежат клиентам и клиентам authenticate_many UserSessions. Вход в работу отлично; проблема, с которой я сталкиваюсь, заключается в том, что пользователи могут входить в любой субдомен независимо от того, принадлежат ли они клиенту этого субдомена. Вот мой код: КонтроллерКак я могу убедиться, что пользователи принадлежат к определенной модели с использованием Authlogic?

Применение:

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 контроллер:

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 модель:

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

    has_many :users, :uniq => true 
end 

пользователя и 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 

Я запускаю Rails 3 на Mac OS X с установленной версией Authlogic. Любая помощь приветствуется!

ответ

1

Вам нужно будет добавить специальную проверку.

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 

Не забудьте установить current_client атрибут аксессор в контроллере UserSessions, поскольку это не будет в противном случае будут доступны в модели.

+0

Спасибо! Я знал, что это должно быть что-то вроде этого. – mjaz