javaeye3/lib/support_permission_access.rb

72 lines
2.6 KiB
Ruby
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module SupportPermissionAccess
ADMINISTRATOR_ROLES = YAML.load_file("#{RAILS_ROOT}/data/administrator_roles.yml")
ADMINISTRATOR_POPEDOMS_ACTIONS_MAP = Hash[*ADMINISTRATOR_ROLES.collect{|role| [role["popedom"], role["actions"]]}.flatten(1)]
ADMINISTRATOR_POPEDOMS_ROLES_MAP = Hash[*ADMINISTRATOR_ROLES.collect{|role| [role["popedom"], role["name"]]}.flatten(1)]
# 当用户没有使用普通帐号登录时,将使用该默认帐号的普通用户进行登录
# 以解决网页编辑器中的文件上传部分因为逻辑与当前登录用户有关而导致的程序出错问题
# 以及一些其他可能存在的潜在逻辑错误
# User:id 34787 => ITeye管理员, 740768 => je_je-back-management(无前台权限)
if Rails.env == 'production'
ADMINISTRATOR_NORMAL_USER_ID = 740768
ADMINISTRATOR_ADMIN_USER_ID = 34787
else
ADMINISTRATOR_NORMAL_USER_ID = ADMINISTRATOR_ADMIN_USER_ID = 34787
end
def self.included(controller)
controller.helper_method(:can_access?, :current_admin, :admin_of?, :super_admin?) if controller.respond_to?(:helper_method)
end
def current_admin
@current_admin ||= ((session[:admin_id] && Administrator.find(session[:admin_id])) || -1)
end
def can_access?(url=nil)
return false unless admin_logged_in?
# 将url转化为route信息根据controller和action信息来判断用户是否有权限访问某个url
route = url ? ActionController::Routing::Routes.recognize_path(url) : { :controller => controller_path, :action => action_name }
if route[:id] == 'index'
route[:controller] = "#{route[:controller]}/#{route[:action]}"
route[:action] = route[:id]
end
return true if current_admin.super?
return true if current_admin.accessible_actions.include?("/#{route[:controller]}/*") || current_admin.accessible_actions.include?("/#{route[:controller]}/#{route[:action]}")
return false
end
def support_action_access_permission_required
if !can_access?
if request.xhr?
render :nothing => true, :status => 403
else
redirect_to support_login_path
end
end
end
def require_admin_of(board)
if admin_logged_in?
unless admin_of?(board)
flash[:error] = "您没有权限执行这项操作,请检查登录的账号是否正确"
redirect_to support_login_path
end
else
render_404
end
end
def super_admin?
admin_logged_in? && current_admin.super?
end
def admin_of?(board)
admin_logged_in? && (current_admin.super? || current_admin.popedoms.include?(board))
end
def admin_logged_in?
current_admin != -1
end
end