gitlab-org--gitlab-foss/app/models/members/project_member.rb

147 lines
3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class ProjectMember < Member
2017-02-21 18:32:18 -05:00
SOURCE_TYPE = 'Project'.freeze
include Gitlab::ShellAdapter
belongs_to :project, foreign_key: 'source_id'
# Make sure project member points only to project as it source
default_value_for :source_type, SOURCE_TYPE
2017-02-21 19:40:04 -05:00
validates :source_type, format: { with: /\AProject\z/ }
validates :access_level, inclusion: { in: Gitlab::Access.values }
default_scope { where(source_type: SOURCE_TYPE) }
scope :in_project, ->(project) { where(source_id: project.id) }
class << self
# Add users to projects with passed access option
#
# access can be an integer representing a access code
# or symbol like :maintainer representing role
#
# Ex.
# add_users_to_projects(
# project_ids,
# user_ids,
# ProjectMember::MAINTAINER
# )
#
# add_users_to_projects(
# project_ids,
# user_ids,
# :maintainer
# )
#
def add_users_to_projects(project_ids, users, access_level, current_user: nil, expires_at: nil)
self.transaction do
project_ids.each do |project_id|
project = Project.find(project_id)
add_users(
project,
users,
access_level,
current_user: current_user,
expires_at: expires_at
)
end
end
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
def truncate_team(project)
truncate_teams [project.id]
end
def access_level_roles
Gitlab::Access.options
end
private
def can_update_member?(current_user, member)
super || (member.owner? && member.new_record?)
end
end
2015-04-10 09:09:37 -04:00
def project
source
end
def owner?
project.owner == user
end
def notifiable_options
{ project: project }
2017-08-04 14:53:36 -04:00
end
2015-04-10 09:09:37 -04:00
private
def send_invite
run_after_commit_or_now { notification_service.invite_project_member(self, @raw_invite_token) }
super
end
def post_create_hook
unless owner?
event_service.join_project(self.project, self.user)
run_after_commit_or_now { notification_service.new_project_member(self) }
end
2015-04-10 09:09:37 -04:00
super
end
def post_update_hook
2015-04-10 09:09:37 -04:00
if access_level_changed?
run_after_commit { notification_service.update_project_member(self) }
2015-04-10 09:09:37 -04:00
end
super
end
def post_destroy_hook
if expired?
event_service.expired_leave_project(self.project, self.user)
else
event_service.leave_project(self.project, self.user)
end
2015-04-10 09:09:37 -04:00
super
end
2015-04-10 09:23:38 -04:00
def after_accept_invite
notification_service.accept_project_invite(self)
super
end
2015-04-10 10:37:02 -04:00
def after_decline_invite
notification_service.decline_project_invite(self)
super
end
# rubocop: disable CodeReuse/ServiceClass
def event_service
EventCreateService.new
end
# rubocop: enable CodeReuse/ServiceClass
end