Project

General

Profile

Feature #36696 » 36696-v2.patch

Go MAEDA, 2022-02-28 16:25

View differences:

app/models/group.rb
78 78
  end
79 79

  
80 80
  def user_added(user)
81
    members.each do |member|
82
      next if member.project.nil?
81
    members.preload(:member_roles).each do |member|
82
      next if member.project_id.nil?
83 83

  
84 84
      user_member =
85
        Member.find_by_project_id_and_user_id(member.project_id, user.id) ||
86
          Member.new(:project_id => member.project_id, :user_id => user.id)
87
      member.member_roles.each do |member_role|
88
        user_member.member_roles << MemberRole.new(:role => member_role.role,
89
                                                   :inherited_from => member_role.id)
90
      end
85
        Member.find_or_initialize_by(:project_id => member.project_id, :user_id => user.id)
86
      user_member.member_roles <<
87
        member.member_roles.pluck(:id, :role_id).map do |id, role_id|
88
          MemberRole.new(:role_id => role_id, :inherited_from => id)
89
        end
91 90
      user_member.save!
92 91
    end
93 92
  end
94 93

  
95 94
  def user_removed(user)
96
    members.each do |member|
97
      MemberRole.
98
        joins(:member).
99
        where("#{Member.table_name}.user_id = ? AND #{MemberRole.table_name}.inherited_from IN (?)", user.id, member.member_role_ids).
100
        each(&:destroy)
101
    end
95
    MemberRole.
96
      joins(:member).
97
      where("#{Member.table_name}.user_id = ? AND #{MemberRole.table_name}.inherited_from IN (?)", user.id, MemberRole.select(:id).where(:member => members)).
98
      destroy_all
102 99
  end
103 100

  
104 101
  def self.human_attribute_name(attribute_key_name, *args)
app/models/member_role.rb
30 30
  validate :validate_role_member
31 31

  
32 32
  def validate_role_member
33
    errors.add :role_id, :invalid if role && !role.member?
33
    errors.add :role_id, :invalid unless role&.member?
34 34
  end
35 35

  
36 36
  def inherited?
......
58 58
  end
59 59

  
60 60
  def add_role_to_group_users
61
    if member.principal.is_a?(Group) && !inherited?
62
      member.principal.users.each do |user|
63
        user_member = Member.find_or_new(member.project_id, user.id)
64
        user_member.member_roles << MemberRole.new(:role => role, :inherited_from => id)
61
    if !inherited? && member.principal.is_a?(Group)
62
      member.principal.users.ids.each do |user_id|
63
        user_member = Member.find_or_initialize_by(:project_id => member.project_id, :user_id => user_id)
64
        user_member.member_roles << MemberRole.new(:role_id => role_id, :inherited_from => id)
65 65
        user_member.save!
66 66
      end
67 67
    end
68 68
  end
69 69

  
70 70
  def add_role_to_subprojects
71
    member.project.children.each do |subproject|
72
      if subproject.inherit_members?
73
        child_member = Member.find_or_new(subproject.id, member.user_id)
74
        child_member.member_roles << MemberRole.new(:role => role, :inherited_from => id)
75
        child_member.save!
76
      end
71
    return if member.project.leaf?
72

  
73
    member.project.children.where(:inherit_members => true).ids.each do |subproject_id|
74
      child_member = Member.find_or_initialize_by(:project_id => subproject_id, :user_id => member.user_id)
75
      child_member.member_roles << MemberRole.new(:role => role, :inherited_from => id)
76
      child_member.save!
77 77
    end
78 78
  end
79 79

  
(2-2/2)