Top*_*gio 13 security database-design ruby-on-rails
我即将开始编写一个Rails应用程序,它允许客户端拥有一个单独的子域来访问我们的应用程序.从数据安全的角度思考,如果每个客户端的访问权限真正限制在他们的数据库中会很好,这样,如果生产代码中存在错误,他们只能访问自己的数据库,而不能访问任何其他数据库.客户端.
我知道如何做我想要的代码,但我想知道是否有一个更简单的解决方案,我可能会失踪.您将如何保护客户端数据,以便在出现错误或黑客威胁时,他们的数据不太可能被暴露?
Kri*_*ris 20
这是我用于解决这个问题的一些代码:
application_controller.rb
before_filter :set_database
helper_method :current_website
# I use the entire domain, just change to find_by_subdomain and pass only the subdomain
def current_website
@website ||= Website.find_by_domain(request.host)
end
def set_database
current_website.use_database
end
# Bonus - add view_path
def set_paths
self.prepend_view_path current_website.view_path unless current_website.view_path.blank?
end
Run Code Online (Sandbox Code Playgroud)
Website.rb
def use_database
ActiveRecord::Base.establish_connection(website_connection)
end
# Revert back to the shared database
def revert_database
ActiveRecord::Base.establish_connection(default_connection)
end
private
# Regular database.yml configuration hash
def default_connection
@default_config ||= ActiveRecord::Base.connection.instance_variable_get("@config").dup
end
# Return regular connection hash but with database name changed
# The database name is a attribute (column in the database)
def website_connection
default_connection.dup.update(:database => database_name)
end
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!