【发布时间】:2018-04-04 07:19:26
【问题描述】:
我有两个基本控制器类,一个用于 JSON 资源 API,另一个是应用程序控制器 API 类。我必须在操作之前添加适用于两个基类的权限检查。
我不想重复之前的操作代码,所以想在常见的地方添加。 如果我在那里检查祖先,那么我看不到它们之间有任何 Rails 默认公共类。
有什么建议吗?我还希望当前用户在上下文中。解决这个问题的好方法是什么?
class BaseResourceController < JSONAPI::ResourceController
before_action :check_permissions
def check_permissions
current_user.permissions
end
end
class ApplicationController < ActionController::API
before_action :check_permissions
def check_permissions
current_user.permissions
end
end
ActionController::API.ancestors =>
[ActionController::API, Devise::Controllers::UrlHelpers, Devise::Controllers::Helpers, Devise::Controllers::StoreLocation, Devise::Controllers::SignInOut, ActiveRecord::Railties::ControllerRuntime, ActionDispatch ::Routing::RouteSet::MountedHelpers, ActionController::RespondWith, ActionController::ParamsWrapper, ActionController::Instrumentation, ActionController::Rescue, ActionController::DataStreaming, ActionController::ForceSSL, AbstractController::Callbacks, ActiveSupport::Callbacks, ActionController::StrongParameters, ActiveSupport::Rescuable, ActionController::BasicImplicitRender, ActionController::ConditionalGet, ActionController::Head, ActionController::Renderers::All, ActionController::Renderers, ActionController::Rendering, ActionController::ApiRendering, ActionController: :重定向, ActiveSupport::Benchmarkable, AbstractController::Logger, ActionController::UrlFor, AbstractController::UrlFor, ActionDispatch::Routing::UrlFor, ActionDispatch::Routing::PolymorphicRoutes, Ab stractController::Rendering、ActionView::ViewPaths、ActionController::Metal、AbstractController::Base、ActiveSupport::Configurable、ActiveSupport::ToJsonWithActiveSupportEncoder、对象、PP::ObjectMixin、ActiveSupport::Dependencies::Loadable、JSON::Ext: :Generator::GeneratorMethods::Object, ActiveSupport::Tryable, Kernel, BasicObject]
JSONAPI::ResourceController.ancestors =>
[JSONAPI::ResourceController, JSONAPI::Callbacks, JSONAPI::ActsAsResourceController, ActionController::Base, Devise::Controllers::UrlHelpers, Devise::Controllers::Helpers, Devise::Controllers::StoreLocation, Devise: :Controllers::SignInOut, ActiveRecord::Railties::ControllerRuntime, ActionDispatch::Routing::RouteSet::MountedHelpers, ActionController::RespondWith, ActionController::ParamsWrapper, ActionController::Instrumentation, ActionController::Rescue, ActionController::HttpAuthentication: :Token::ControllerMethods, ActionController::HttpAuthentication::Digest::ControllerMethods, ActionController::HttpAuthentication::Basic::ControllerMethods, ActionController::DataStreaming, ActionController::Streaming, ActionController::ForceSSL, ActionController::RequestForgeryProtection, AbstractController: :Callbacks, ActiveSupport::Callbacks, ActionController::FormBuilder, ActionController::Flash, ActionController::Cookies, ActionController::ParameterEncoding, ActionController::StrongParameters , ActiveSupport::Rescuable, ActionController::ImplicitRender, ActionController::BasicImplicitRender, ActionController::MimeResponds, AbstractController::Caching, AbstractController::Caching::ConfigMethods, AbstractController::Caching::Fragments, ActionController::Caching, ActionController:: EtagWithFlash、ActionController::EtagWithTemplateDigest、ActionController::ConditionalGet、ActionController::Head、ActionController::Renderers::All、ActionController::Renderers、ActionController::Rendering、ActionView::Layouts、ActionView::Rendering、ActionController::Redirecting、 ActiveSupport::Benchmarkable, AbstractController::Logger, ActionController::UrlFor, AbstractController::UrlFor, ActionDispatch::Routing::UrlFor, ActionDispatch::Routing::PolymorphicRoutes, ActionController::Helpers, AbstractController::Helpers, AbstractController::AssetPaths , AbstractController::Translation, AbstractController::Rendering, ActionView::ViewPaths, ActionController::Metal, AbstractController::Base, ActiveSupport t::Configurable, ActiveSupport::ToJsonWithActiveSupportEncoder, Object, PP::ObjectMixin, ActiveSupport::Dependencies::Loadable, JSON::Ext::Generator::GeneratorMethods::Object, ActiveSupport::Tryable, Kernel, BasicObject]
【问题讨论】:
标签: ruby ruby-on-rails-3 inheritance