BigW Consortium Gitlab

project_member.rb 2.94 KB
Newer Older
1
class ProjectMember < Member
2
  SOURCE_TYPE = 'Project'.freeze
3 4 5

  include Gitlab::ShellAdapter

6
  belongs_to :project, foreign_key: 'source_id'
7

8 9
  # Make sure project member points only to project as it source
  default_value_for :source_type, SOURCE_TYPE
Douwe Maan committed
10
  validates :source_type, format: { with: /\AProject\z/ }
11
  validates :access_level, inclusion: { in: Gitlab::Access.values }
12 13 14 15
  default_scope { where(source_type: SOURCE_TYPE) }

  scope :in_project, ->(project) { where(source_id: project.id) }

16
  before_destroy :delete_member_todos
17

18
  class << self
19
    # Add users to projects with passed access option
20 21 22 23 24
    #
    # access can be an integer representing a access code
    # or symbol like :master representing role
    #
    # Ex.
25
    #   add_users_to_projects(
26 27 28 29 30
    #     project_ids,
    #     user_ids,
    #     ProjectMember::MASTER
    #   )
    #
31
    #   add_users_to_projects(
32 33 34 35 36
    #     project_ids,
    #     user_ids,
    #     :master
    #   )
    #
37 38
    def add_users_to_projects(project_ids, users, access_level, current_user: nil, expires_at: nil)
      self.transaction do
39
        project_ids.each do |project_id|
40 41
          project = Project.find(project_id)

42
          add_users(
43 44 45 46 47 48
            project,
            users,
            access_level,
            current_user: current_user,
            expires_at: expires_at
          )
49 50 51 52 53 54 55
        end
      end
    end

    def truncate_teams(project_ids)
      ProjectMember.transaction do
        members = ProjectMember.where(source_id: project_ids)
56

57 58 59 60 61 62 63 64 65 66
        members.each do |member|
          member.destroy
        end
      end

      true
    rescue
      false
    end

67
    def truncate_team(project)
68 69 70
      truncate_teams [project.id]
    end

71
    def access_level_roles
72 73
      Gitlab::Access.options
    end
74 75 76 77 78 79

    private

    def can_update_member?(current_user, member)
      super || (member.owner? && member.new_record?)
    end
80 81 82 83 84 85
  end

  def access_field
    access_level
  end

86 87 88 89
  def project
    source
  end

90 91 92 93
  def owner?
    project.owner == user
  end

94 95
  private

96 97 98 99
  def delete_member_todos
    user.todos.where(project_id: source_id).destroy_all if user
  end

100 101
  def send_invite
    notification_service.invite_project_member(self, @raw_invite_token)
102 103 104 105

    super
  end

106
  def post_create_hook
107 108
    unless owner?
      event_service.join_project(self.project, self.user)
109
      notification_service.new_project_member(self)
110
    end
111

112
    super
113 114 115
  end

  def post_update_hook
116
    if access_level_changed?
117
      notification_service.update_project_member(self)
118 119 120
    end

    super
121 122 123
  end

  def post_destroy_hook
124 125 126 127 128
    if expired?
      event_service.expired_leave_project(self.project, self.user)
    else
      event_service.leave_project(self.project, self.user)
    end
129 130

    super
131 132
  end

133 134 135 136 137 138
  def after_accept_invite
    notification_service.accept_project_invite(self)

    super
  end

Douwe Maan committed
139 140 141 142 143 144
  def after_decline_invite
    notification_service.decline_project_invite(self)

    super
  end

145 146 147
  def event_service
    EventCreateService.new
  end
148
end