debian-mirror-gitlab/app/models/members/project_member.rb

149 lines
3.2 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
class ProjectMember < Member
2017-08-17 22:00:37 +05:30
SOURCE_TYPE = 'Project'.freeze
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
belongs_to :project, foreign_key: 'source_id'
2015-04-26 12:48:37 +05:30
# Make sure project member points only to project as it source
default_value_for :source_type, SOURCE_TYPE
2017-08-17 22:00:37 +05:30
validates :source_type, format: { with: /\AProject\z/ }
2016-09-13 17:45:13 +05:30
validates :access_level, inclusion: { in: Gitlab::Access.values }
2015-04-26 12:48:37 +05:30
default_scope { where(source_type: SOURCE_TYPE) }
scope :in_project, ->(project) { where(source_id: project.id) }
2019-01-03 12:48:30 +05:30
scope :in_namespaces, ->(groups) do
joins('INNER JOIN projects ON projects.id = members.source_id')
.where('projects.namespace_id in (?)', groups.select(:id))
end
2015-04-26 12:48:37 +05:30
class << self
2017-08-17 22:00:37 +05:30
# Add users to projects with passed access option
2015-04-26 12:48:37 +05:30
#
# access can be an integer representing a access code
2018-11-18 11:00:15 +05:30
# or symbol like :maintainer representing role
2015-04-26 12:48:37 +05:30
#
# Ex.
2016-09-13 17:45:13 +05:30
# add_users_to_projects(
2015-04-26 12:48:37 +05:30
# project_ids,
# user_ids,
2018-11-18 11:00:15 +05:30
# ProjectMember::MAINTAINER
2015-04-26 12:48:37 +05:30
# )
#
2016-09-13 17:45:13 +05:30
# add_users_to_projects(
2015-04-26 12:48:37 +05:30
# project_ids,
# user_ids,
2018-11-18 11:00:15 +05:30
# :maintainer
2015-04-26 12:48:37 +05:30
# )
#
2016-11-03 12:29:30 +05:30
def add_users_to_projects(project_ids, users, access_level, current_user: nil, expires_at: nil)
self.transaction do
2015-04-26 12:48:37 +05:30
project_ids.each do |project_id|
project = Project.find(project_id)
2017-08-17 22:00:37 +05:30
add_users(
2016-11-03 12:29:30 +05:30
project,
users,
access_level,
current_user: current_user,
expires_at: expires_at
)
2015-04-26 12:48:37 +05:30
end
end
end
def truncate_teams(project_ids)
ProjectMember.transaction do
members = ProjectMember.where(source_id: project_ids)
2016-02-05 20:25:01 +05:30
2015-04-26 12:48:37 +05:30
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
2015-04-26 12:48:37 +05:30
Gitlab::Access.options
end
2016-11-03 12:29:30 +05:30
private
def can_update_member?(current_user, member)
super || (member.owner? && member.new_record?)
end
2015-04-26 12:48:37 +05:30
end
def project
source
end
def owner?
project.owner == user
end
2018-03-17 18:26:18 +05:30
def notifiable_options
{ project: project }
end
2015-04-26 12:48:37 +05:30
private
def send_invite
2018-10-15 14:42:47 +05:30
run_after_commit_or_now { notification_service.invite_project_member(self, @raw_invite_token) }
2015-04-26 12:48:37 +05:30
super
end
def post_create_hook
unless owner?
event_service.join_project(self.project, self.user)
2018-10-15 14:42:47 +05:30
run_after_commit_or_now { notification_service.new_project_member(self) }
2015-04-26 12:48:37 +05:30
end
2016-02-05 20:25:01 +05:30
2015-04-26 12:48:37 +05:30
super
end
def post_update_hook
if access_level_changed?
2018-10-15 14:42:47 +05:30
run_after_commit { notification_service.update_project_member(self) }
2015-04-26 12:48:37 +05:30
end
super
end
def post_destroy_hook
2017-08-17 22:00:37 +05:30
if expired?
event_service.expired_leave_project(self.project, self.user)
else
event_service.leave_project(self.project, self.user)
end
2015-04-26 12:48:37 +05:30
super
end
def after_accept_invite
notification_service.accept_project_invite(self)
super
end
def after_decline_invite
notification_service.decline_project_invite(self)
super
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ServiceClass
2015-04-26 12:48:37 +05:30
def event_service
EventCreateService.new
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ServiceClass
2015-04-26 12:48:37 +05:30
end