Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# this is to speed up secure Project.find
def self.forbidden_project_ids
# Admins don't have forbidden projects
return [0] if User.current && User.current.is_admin?
# This will cache and return an array:
# [ Project1_id, Project2_id, Project3_id ]
forbidden_projects = Rails.cache.fetch('forbidden_projects') do
# We use project_user_cache to calculate this...
Rails.cache.delete('allowed_forbidden_projects_by_user')
puc = Hash.new
Relationship.find_by_sql("SELECT ur.project_id, ur.user_id from flags f,
relationships ur where f.flag = 'access' and f.status = 'disable' and ur.project_id = f.project_id").each do |r|
puc[r.project_id] ||= Hash.new
puc[r.project_id][r.user_id] = 1
end
puc
end
# This will cache and return a
# {forbidden_projecs: [p1,p2], whitelist: {u1: [p1], u2: [p1,p2], u3: [p2]} }
forbidden_projects_by_user = Rails.cache.fetch('allowed_project_ids_by_user') do
project_user_cache
end
# We don't need to check the relationships if we don't have a User
return project_user_cache.keys if User.current.nil? || User.current.is_nobody?
forbidden_project = []
project_user_cache.each do |project_id, users|
forbidden_projects << project_id unless users[userid]
end
# we always put a 0 in there to avoid having to check for NULL
forbidden_project << 0 if forbidden_project.blank?
forbidden_project
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment