2014-09-14 12:32:51 -04:00
|
|
|
class ProjectMember < Member
|
2017-02-21 18:32:18 -05:00
|
|
|
SOURCE_TYPE = 'Project'.freeze
|
2014-09-14 12:32:51 -04:00
|
|
|
|
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
2016-10-18 17:20:36 -04:00
|
|
|
belongs_to :project, foreign_key: 'source_id'
|
2014-09-14 13:38:57 -04:00
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
# 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/ }
|
2016-06-23 11:14:31 -04:00
|
|
|
validates :access_level, inclusion: { in: Gitlab::Access.values }
|
2014-09-14 12:32:51 -04:00
|
|
|
default_scope { where(source_type: SOURCE_TYPE) }
|
|
|
|
|
|
|
|
scope :in_project, ->(project) { where(source_id: project.id) }
|
|
|
|
|
2016-05-30 11:32:07 -04:00
|
|
|
before_destroy :delete_member_todos
|
2016-05-20 13:37:48 -04:00
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
class << self
|
2017-04-21 10:07:42 -04:00
|
|
|
# Add users to projects with passed access option
|
2014-09-14 12:32:51 -04:00
|
|
|
#
|
|
|
|
# access can be an integer representing a access code
|
|
|
|
# or symbol like :master representing role
|
|
|
|
#
|
|
|
|
# Ex.
|
2016-08-04 00:35:17 -04:00
|
|
|
# add_users_to_projects(
|
2014-09-14 12:32:51 -04:00
|
|
|
# project_ids,
|
|
|
|
# user_ids,
|
|
|
|
# ProjectMember::MASTER
|
|
|
|
# )
|
|
|
|
#
|
2016-08-04 00:35:17 -04:00
|
|
|
# add_users_to_projects(
|
2014-09-14 12:32:51 -04:00
|
|
|
# project_ids,
|
|
|
|
# user_ids,
|
|
|
|
# :master
|
|
|
|
# )
|
|
|
|
#
|
2016-09-16 11:54:21 -04:00
|
|
|
def add_users_to_projects(project_ids, users, access_level, current_user: nil, expires_at: nil)
|
|
|
|
self.transaction do
|
2014-09-14 12:32:51 -04:00
|
|
|
project_ids.each do |project_id|
|
2015-04-10 09:25:32 -04:00
|
|
|
project = Project.find(project_id)
|
|
|
|
|
2017-04-21 10:07:42 -04:00
|
|
|
add_users(
|
2016-09-16 11:54:21 -04:00
|
|
|
project,
|
|
|
|
users,
|
|
|
|
access_level,
|
|
|
|
current_user: current_user,
|
|
|
|
expires_at: expires_at
|
|
|
|
)
|
2014-09-14 12:32:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def truncate_teams(project_ids)
|
|
|
|
ProjectMember.transaction do
|
|
|
|
members = ProjectMember.where(source_id: project_ids)
|
2016-01-21 08:23:53 -05:00
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
members.each do |member|
|
|
|
|
member.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2014-09-25 18:07:40 -04:00
|
|
|
def truncate_team(project)
|
2014-09-14 12:32:51 -04:00
|
|
|
truncate_teams [project.id]
|
|
|
|
end
|
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
def access_level_roles
|
2014-09-14 12:32:51 -04:00
|
|
|
Gitlab::Access.options
|
|
|
|
end
|
2016-09-16 11:54:21 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def can_update_member?(current_user, member)
|
|
|
|
super || (member.owner? && member.new_record?)
|
|
|
|
end
|
2014-09-14 12:32:51 -04:00
|
|
|
end
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
def project
|
|
|
|
source
|
|
|
|
end
|
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
def owner?
|
|
|
|
project.owner == user
|
|
|
|
end
|
|
|
|
|
2017-08-04 16:56:33 -04:00
|
|
|
def notifiable_options
|
|
|
|
{ project: project }
|
2017-08-04 14:53:36 -04:00
|
|
|
end
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
private
|
|
|
|
|
2016-05-30 11:32:07 -04:00
|
|
|
def delete_member_todos
|
|
|
|
user.todos.where(project_id: source_id).destroy_all if user
|
|
|
|
end
|
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
def send_invite
|
|
|
|
notification_service.invite_project_member(self, @raw_invite_token)
|
2016-03-14 09:13:35 -04:00
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
def post_create_hook
|
2015-02-13 06:01:28 -05:00
|
|
|
unless owner?
|
|
|
|
event_service.join_project(self.project, self.user)
|
2015-03-13 11:22:03 -04:00
|
|
|
notification_service.new_project_member(self)
|
2015-02-13 06:01:28 -05:00
|
|
|
end
|
2016-01-21 08:23:53 -05:00
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
super
|
2014-09-14 12:32:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_update_hook
|
2015-04-10 09:09:37 -04:00
|
|
|
if access_level_changed?
|
2016-01-21 08:23:53 -05:00
|
|
|
notification_service.update_project_member(self)
|
2015-04-10 09:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
super
|
2014-09-14 12:32:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def post_destroy_hook
|
2016-10-06 11:19:27 -04:00
|
|
|
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
|
2014-09-14 12:32:51 -04:00
|
|
|
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
|
|
|
|
|
2015-02-13 05:57:56 -05:00
|
|
|
def event_service
|
|
|
|
EventCreateService.new
|
|
|
|
end
|
2014-09-14 12:32:51 -04:00
|
|
|
end
|