BigW Consortium Gitlab

project_member.rb 3.5 KB
Newer Older
Valery Sizov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# == Schema Information
#
# Table name: members
#
#  id                 :integer          not null, primary key
#  access_level       :integer          not null
#  source_id          :integer          not null
#  source_type        :string(255)      not null
#  user_id            :integer          not null
#  notification_level :integer          not null
#  type               :string(255)
#  created_at         :datetime
#  updated_at         :datetime
#

16 17 18 19 20
class ProjectMember < Member
  SOURCE_TYPE = 'Project'

  include Gitlab::ShellAdapter

21 22
  belongs_to :project, class_name: 'Project', foreign_key: 'source_id'

23

24 25
  # Make sure project member points only to project as it source
  default_value_for :source_type, SOURCE_TYPE
26
  default_value_for :notification_level, Notification::N_GLOBAL
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  validates_format_of :source_type, with: /\AProject\z/
  default_scope { where(source_type: SOURCE_TYPE) }

  after_create :post_create_hook
  after_update :post_update_hook
  after_destroy :post_destroy_hook

  scope :in_project, ->(project) { where(source_id: project.id) }
  scope :in_projects, ->(projects) { where(source_id: projects.pluck(:id)) }
  scope :with_user, ->(user) { where(user_id: user.id) }

  class << self

    # Add users to project teams with passed access option
    #
    # access can be an integer representing a access code
    # or symbol like :master representing role
    #
    # Ex.
    #   add_users_into_projects(
    #     project_ids,
    #     user_ids,
    #     ProjectMember::MASTER
    #   )
    #
    #   add_users_into_projects(
    #     project_ids,
    #     user_ids,
    #     :master
    #   )
    #
    def add_users_into_projects(project_ids, user_ids, access)
      access_level = if roles_hash.has_key?(access)
                       roles_hash[access]
                     elsif roles_hash.values.include?(access.to_i)
                       access
                     else
                       raise "Non valid access"
                     end

      ProjectMember.transaction do
        project_ids.each do |project_id|
          user_ids.each do |user_id|
            member = ProjectMember.new(access_level: access_level, user_id: user_id)
            member.source_id = project_id
            member.save
          end
        end
      end

      true
    rescue
      false
    end

    def truncate_teams(project_ids)
      ProjectMember.transaction do
        members = ProjectMember.where(source_id: project_ids)
        members.each do |member|
          member.destroy
        end
      end

      true
    rescue
      false
    end

95
    def truncate_team(project)
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
      truncate_teams [project.id]
    end

    def roles_hash
      Gitlab::Access.sym_options
    end

    def access_roles
      Gitlab::Access.options
    end
  end

  def access_field
    access_level
  end

  def owner?
    project.owner == user
  end

  def post_create_hook
117 118
    unless owner?
      event_service.join_project(self.project, self.user)
119
      notification_service.new_project_member(self)
120 121
    end
    
122 123 124 125
    system_hook_service.execute_hooks_for(self, :create)
  end

  def post_update_hook
126
    notification_service.update_project_member(self) if self.access_level_changed?
127 128 129
  end

  def post_destroy_hook
130
    event_service.leave_project(self.project, self.user)
131 132 133
    system_hook_service.execute_hooks_for(self, :destroy)
  end

134 135 136 137
  def event_service
    EventCreateService.new
  end

138 139 140 141 142 143 144 145 146 147 148 149
  def notification_service
    NotificationService.new
  end

  def system_hook_service
    SystemHooksService.new
  end

  def project
    source
  end
end