Postal邮件服务器代码中ApplicationController未定义logged_in?方法问题
关于Postal邮件服务器中
logged_in?方法未定义的问题排查 我最近了解到Postal这款邮件服务器,对它免费开箱即用的功能很感兴趣。作为初级Rails开发者,我正在尝试对它做定制开发,已经完成部署且能正常运行,但遇到了一个问题:ApplicationController里频繁使用的logged_in?方法在代码库中找不到定义,也没找到提供该方法的Gem,不知道它是怎么加载的。相关代码如下:
require "authie/session" class ApplicationController < ActionController::Base protect_from_forgery with: :exception before_action :login_required before_action :set_timezone rescue_from Authie::Session::InactiveSession, with: :auth_session_error rescue_from Authie::Session::ExpiredSession, with: :auth_session_error rescue_from Authie::Session::BrowserMismatch, with: :auth_session_error private def login_required return if logged_in? redirect_to login_path(return_to: request.fullpath) end def admin_required if logged_in? unless current_user.admin? render plain: "Not permitted" end else redirect_to login_path(return_to: request.fullpath) end end def require_organization_owner return if organization.owner == current_user redirect_to organization_root_path(organization), alert: "This page can only be accessed by the organization's owner (#{organization.owner.name})" end def auth_session_error(exception) Rails.logger.info "AuthSessionError: #{exception.class}: #{exception.message}" redirect_to login_path(return_to: request.fullpath) end def page_title @page_title ||= ["Postal"] end helper_method :page_title def redirect_to_with_return_to(url, *args) if params[:return_to].blank? || !params[:return_to].starts_with?("/") redirect_to url_with_return_to(url), *args else redirect_to url_with_return_to(url), *args end end def set_timezone Time.zone = logged_in? ? current_user.time_zone : "UTC" end def append_info_to_payload(payload) super payload[:ip] = request.ip payload[:user] = logged_in? ? current_user.id : nil end def url_with_return_to(url) if params[:return_to].blank? || !params[:return_to].starts_with?("/") url_for(url) else params[:return_to] end end def redirect_to_with_json(url, flash_messages = {}) if url.is_a?(Array) && url[0] == :return_to url = url_with_return_to(url[1]) else url = url_for(url) end flash_messages.each do |key, value| flash[key] = value end respond_to do |wants| wants.html { redirect_to url } wants.json { render json: { redirect_to: url } } end end def render_form_errors(action_name, object) respond_to do |wants| wants.html { render action_name } wants.json { render json: { form_errors: object.errors.full_messages }, status: :unprocessable_entity } end end def flash_now(type, message, options = {}) respond_to do |wants| wants.html do flash.now[type] = message if options[:render_action] render options[:render_action] end end wants.json { render json: { flash: { type => message } } } end end def login(user) if logged_in? auth_session.invalidate! reset_session end Authie::Session.start(self, user: user) @current_user = user end end
排查方向
- Authie Gem的内置辅助方法:代码中引入了
authie/session,Authie默认会提供logged_in?这类辅助方法,通常是通过Authie::ControllerHelpers模块注入的。检查项目的初始化文件(如config/initializers/authie.rb)或ApplicationController的其他混入模块,看是否有include Authie::ControllerHelpers的代码。 - 项目自定义的Concern:Rails项目常用Concern复用认证逻辑,搜索项目中是否存在
app/controllers/concerns/authentication.rb这类文件,里面可能定义了logged_in?和current_user,并被ApplicationController引入。 - 动态注入或全局扩展:搜索项目代码中的
logged_in?字符串,排查是否通过define_method、include或extend在运行时动态添加了该方法,比如在lib目录的自定义扩展文件中。 - Postal源码结构参考:Postal作为开源项目,这类认证方法通常会放在
app/controllers/concerns或lib/postal目录下,直接查看对应目录的文件即可找到定义。
临时适配方案
如果暂时找不到方法来源,可以在ApplicationController的private块中手动添加适配Authie逻辑的方法:
private def logged_in? auth_session.present? && auth_session.active? end def current_user @current_user ||= auth_session&.user end def auth_session @auth_session ||= Authie::Session.get(self) end
内容的提问来源于stack exchange,提问作者Mighty
相关产品推荐
相关产品推荐

