Class: Banzai::ReferenceParser::UserParser

Inherits:
BaseParser
  • Object
show all
Defined in:
lib/banzai/reference_parser/user_parser.rb

Direct Known Subclasses

DirectlyAddressedUserParser

Instance Method Summary collapse

Methods inherited from BaseParser

#can?, #collection_cache_key, #collection_objects_for_ids, data_attribute, #find_projects_for_hash_keys, #gather_attributes_per_project, #gather_references, #grouped_objects_for_nodes, #initialize, #process, #project_for_node, #projects_for_nodes, #references_relation, #unique_attribute_values

Constructor Details

This class inherits a constructor from Banzai::ReferenceParser::BaseParser

Instance Method Details

#can_read_group_reference?(node, user, groups) ⇒ Boolean

Check if project belongs to a group which user can read.

Returns:

  • (Boolean)

54
55
56
57
58
# File 'lib/banzai/reference_parser/user_parser.rb', line 54

def can_read_group_reference?(node, user, groups)
  node_group = groups[node]

  node_group && can?(user, :read_group, node_group)
end

#can_read_project_reference?(node) ⇒ Boolean

Returns:

  • (Boolean)

60
61
62
63
64
# File 'lib/banzai/reference_parser/user_parser.rb', line 60

def can_read_project_reference?(node)
  node_id = node.attr('data-project').to_i

  project_for_node(node)&.id == node_id
end

#can_read_reference?(user, ref_project, node) ⇒ Boolean

Returns:

  • (Boolean)

112
113
114
# File 'lib/banzai/reference_parser/user_parser.rb', line 112

def can_read_reference?(user, ref_project, node)
  can?(user, :read_project, ref_project)
end

#find_users(ids) ⇒ Object


91
92
93
94
95
# File 'lib/banzai/reference_parser/user_parser.rb', line 91

def find_users(ids)
  return [] if ids.empty?

  collection_objects_for_ids(User, ids)
end

#find_users_for_groups(ids) ⇒ Object


97
98
99
100
101
102
103
# File 'lib/banzai/reference_parser/user_parser.rb', line 97

def find_users_for_groups(ids)
  return [] if ids.empty?

  User.joins(:group_members).where(members: {
    source_id: Namespace.where(id: ids).where('mentions_disabled IS NOT TRUE').select(:id)
  }).to_a
end

#find_users_for_projects(ids) ⇒ Object


105
106
107
108
109
110
# File 'lib/banzai/reference_parser/user_parser.rb', line 105

def find_users_for_projects(ids)
  return [] if ids.empty?

  collection_objects_for_ids(Project, ids)
    .flat_map { |p| p.team.members.to_a }
end

#nodes_user_can_reference(current_user, nodes) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/banzai/reference_parser/user_parser.rb', line 66

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)
    project = project_for_node(node)

    if project && project_id && project.id == project_id.to_i
      true
    elsif project_id && user_id
      project = projects[node]
      user = users[node]

      project && user ? project.team.member?(user) : false
    else
      true
    end
  end
end

#nodes_visible_to_user(user, nodes) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/banzai/reference_parser/user_parser.rb', line 27

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)
      next unless can_read_group_reference?(node, user, groups)

      visible << node
    elsif can_read_project_reference?(node)
      visible << node
    else
      remaining << node
    end
  end

  # 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.
  visible + super(current_user, remaining)
end

#referenced_by(nodes) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/banzai/reference_parser/user_parser.rb', line 8

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