2016-05-26 07:16:43 -04:00
|
|
|
module Banzai
|
|
|
|
module ReferenceParser
|
|
|
|
class UserParser < BaseParser
|
|
|
|
self.reference_type = :user
|
|
|
|
|
|
|
|
def referenced_by(nodes)
|
|
|
|
group_ids = []
|
|
|
|
user_ids = []
|
|
|
|
project_ids = []
|
|
|
|
|
|
|
|
nodes.each do |node|
|
|
|
|
if node.has_attribute?('data-group')
|
|
|
|
group_ids << node.attr('data-group').to_i
|
|
|
|
elsif node.has_attribute?(self.class.data_attribute)
|
|
|
|
user_ids << node.attr(self.class.data_attribute).to_i
|
|
|
|
elsif node.has_attribute?('data-project')
|
|
|
|
project_ids << node.attr('data-project').to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
find_users_for_groups(group_ids) | find_users(user_ids) |
|
|
|
|
find_users_for_projects(project_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def nodes_visible_to_user(user, nodes)
|
|
|
|
group_attr = 'data-group'
|
|
|
|
groups = lazy { grouped_objects_for_nodes(nodes, Group, group_attr) }
|
|
|
|
visible = []
|
|
|
|
remaining = []
|
|
|
|
|
|
|
|
nodes.each do |node|
|
|
|
|
if node.has_attribute?(group_attr)
|
2016-11-01 16:18:51 -04:00
|
|
|
next unless can_read_group_reference?(node, user, groups)
|
|
|
|
visible << node
|
|
|
|
elsif can_read_project_reference?(node)
|
|
|
|
visible << node
|
2016-05-26 07:16:43 -04:00
|
|
|
else
|
|
|
|
remaining << node
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-01 16:18:51 -04:00
|
|
|
# If project does not belong to a group
|
|
|
|
# and does not have the same project id as the current project
|
|
|
|
# base class will check if user can read the project that contains
|
|
|
|
# the user reference.
|
2016-05-26 07:16:43 -04:00
|
|
|
visible + super(current_user, remaining)
|
|
|
|
end
|
|
|
|
|
2016-11-01 16:18:51 -04:00
|
|
|
# Check if project belongs to a group which
|
|
|
|
# user can read.
|
|
|
|
def can_read_group_reference?(node, user, groups)
|
2016-08-24 13:11:48 -04:00
|
|
|
node_group = groups[node]
|
2016-11-01 16:18:51 -04:00
|
|
|
|
|
|
|
node_group && can?(user, :read_group, node_group)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_read_project_reference?(node)
|
|
|
|
node_id = node.attr('data-project').to_i
|
|
|
|
|
|
|
|
project && project.id == node_id
|
|
|
|
end
|
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
def nodes_user_can_reference(current_user, nodes)
|
|
|
|
project_attr = 'data-project'
|
|
|
|
author_attr = 'data-author'
|
|
|
|
|
|
|
|
projects = lazy { projects_for_nodes(nodes) }
|
|
|
|
users = lazy { grouped_objects_for_nodes(nodes, User, author_attr) }
|
|
|
|
|
|
|
|
nodes.select do |node|
|
|
|
|
project_id = node.attr(project_attr)
|
|
|
|
user_id = node.attr(author_attr)
|
|
|
|
|
|
|
|
if project && project_id && project.id == project_id.to_i
|
|
|
|
true
|
|
|
|
elsif project_id && user_id
|
2016-08-24 13:11:48 -04:00
|
|
|
project = projects[node]
|
|
|
|
user = users[node]
|
2016-05-26 07:16:43 -04:00
|
|
|
|
|
|
|
project && user ? project.team.member?(user) : false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_users(ids)
|
|
|
|
return [] if ids.empty?
|
|
|
|
|
2016-06-30 05:51:07 -04:00
|
|
|
collection_objects_for_ids(User, ids)
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_users_for_groups(ids)
|
|
|
|
return [] if ids.empty?
|
|
|
|
|
|
|
|
User.joins(:group_members).where(members: { source_id: ids }).to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_users_for_projects(ids)
|
|
|
|
return [] if ids.empty?
|
|
|
|
|
2016-06-30 05:51:07 -04:00
|
|
|
collection_objects_for_ids(Project, ids).
|
|
|
|
flat_map { |p| p.team.members.to_a }
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
2016-11-01 16:18:51 -04:00
|
|
|
|
|
|
|
def can_read_reference?(user, ref_project)
|
|
|
|
can?(user, :read_project, ref_project)
|
|
|
|
end
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|