2012-11-19 18:24:05 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: users_projects
|
|
|
|
#
|
2013-04-04 19:11:51 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# project_id :integer not null
|
2014-04-09 12:05:03 +00:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2013-04-04 19:11:51 +00:00
|
|
|
# project_access :integer default(0), not null
|
|
|
|
# notification_level :integer default(3), not null
|
2012-11-19 18:24:05 +00:00
|
|
|
#
|
|
|
|
|
2011-10-08 21:36:38 +00:00
|
|
|
class UsersProject < ActiveRecord::Base
|
2013-03-21 19:01:14 +00:00
|
|
|
include Gitlab::ShellAdapter
|
2013-06-21 20:17:58 +00:00
|
|
|
include Notifiable
|
2013-08-20 12:59:45 +00:00
|
|
|
include Gitlab::Access
|
2012-02-15 21:51:04 +00:00
|
|
|
|
2011-10-08 21:36:38 +00:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :project
|
|
|
|
|
2012-10-09 00:10:04 +00:00
|
|
|
validates :user, presence: true
|
2012-12-30 11:37:33 +00:00
|
|
|
validates :user_id, uniqueness: { scope: [:project_id], message: "already exists in project" }
|
2013-08-20 12:59:45 +00:00
|
|
|
validates :project_access, inclusion: { in: Gitlab::Access.values }, presence: true
|
2012-10-09 00:10:04 +00:00
|
|
|
validates :project, presence: true
|
2011-10-20 13:48:09 +00:00
|
|
|
|
2013-01-30 15:07:44 +00:00
|
|
|
delegate :name, :username, :email, to: :user, prefix: true
|
2011-10-08 21:36:38 +00:00
|
|
|
|
2013-02-12 07:16:45 +00:00
|
|
|
scope :guests, -> { where(project_access: GUEST) }
|
|
|
|
scope :reporters, -> { where(project_access: REPORTER) }
|
|
|
|
scope :developers, -> { where(project_access: DEVELOPER) }
|
|
|
|
scope :masters, -> { where(project_access: MASTER) }
|
2013-01-19 17:06:50 +00:00
|
|
|
|
2012-12-25 22:52:20 +00:00
|
|
|
scope :in_project, ->(project) { where(project_id: project.id) }
|
2013-03-26 14:19:55 +00:00
|
|
|
scope :in_projects, ->(projects) { where(project_id: projects.map { |p| p.id }) }
|
2013-01-19 17:06:50 +00:00
|
|
|
scope :with_user, ->(user) { where(user_id: user.id) }
|
2012-12-25 22:52:20 +00:00
|
|
|
|
2014-06-17 19:51:43 +00:00
|
|
|
after_create :post_create_hook
|
|
|
|
after_update :post_update_hook
|
|
|
|
after_destroy :post_destroy_hook
|
|
|
|
|
2012-10-09 00:10:04 +00:00
|
|
|
class << self
|
2013-01-03 19:09:18 +00:00
|
|
|
|
|
|
|
# Add users to project teams with passed access option
|
|
|
|
#
|
|
|
|
# access can be an integer representing a access code
|
|
|
|
# or symbol like :master representing role
|
|
|
|
#
|
2013-01-04 06:43:25 +00:00
|
|
|
# Ex.
|
|
|
|
# add_users_into_projects(
|
|
|
|
# project_ids,
|
|
|
|
# user_ids,
|
|
|
|
# UsersProject::MASTER
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# add_users_into_projects(
|
|
|
|
# project_ids,
|
|
|
|
# user_ids,
|
|
|
|
# :master
|
|
|
|
# )
|
|
|
|
#
|
2013-01-03 19:09:18 +00:00
|
|
|
def add_users_into_projects(project_ids, user_ids, access)
|
2013-01-04 06:43:25 +00:00
|
|
|
project_access = if roles_hash.has_key?(access)
|
|
|
|
roles_hash[access]
|
|
|
|
elsif roles_hash.values.include?(access.to_i)
|
2013-01-03 19:09:18 +00:00
|
|
|
access
|
|
|
|
else
|
|
|
|
raise "Non valid access"
|
|
|
|
end
|
|
|
|
|
2012-12-30 11:37:33 +00:00
|
|
|
UsersProject.transaction do
|
|
|
|
project_ids.each do |project_id|
|
|
|
|
user_ids.each do |user_id|
|
|
|
|
users_project = UsersProject.new(project_access: project_access, user_id: user_id)
|
|
|
|
users_project.project_id = project_id
|
|
|
|
users_project.save
|
2012-10-24 11:52:17 +00:00
|
|
|
end
|
2012-10-24 11:20:53 +00:00
|
|
|
end
|
|
|
|
end
|
2012-10-24 11:52:17 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2012-12-30 12:26:19 +00:00
|
|
|
def truncate_teams(project_ids)
|
|
|
|
UsersProject.transaction do
|
|
|
|
users_projects = UsersProject.where(project_id: project_ids)
|
|
|
|
users_projects.each do |users_project|
|
|
|
|
users_project.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def truncate_team project
|
|
|
|
truncate_teams [project.id]
|
|
|
|
end
|
|
|
|
|
2013-01-03 19:09:18 +00:00
|
|
|
def roles_hash
|
2013-08-20 12:59:45 +00:00
|
|
|
Gitlab::Access.sym_options
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
2012-10-09 00:10:04 +00:00
|
|
|
def access_roles
|
2013-08-20 12:59:45 +00:00
|
|
|
Gitlab::Access.options
|
2012-10-09 00:10:04 +00:00
|
|
|
end
|
2012-02-15 21:51:04 +00:00
|
|
|
end
|
|
|
|
|
2013-08-20 12:59:45 +00:00
|
|
|
def access_field
|
|
|
|
project_access
|
2012-02-05 19:26:04 +00:00
|
|
|
end
|
2013-08-27 09:47:28 +00:00
|
|
|
|
|
|
|
def owner?
|
|
|
|
project.owner == user
|
|
|
|
end
|
2014-06-17 19:51:43 +00:00
|
|
|
|
|
|
|
def post_create_hook
|
|
|
|
Event.create(
|
|
|
|
project_id: self.project.id,
|
|
|
|
action: Event::JOINED,
|
|
|
|
author_id: self.user.id
|
|
|
|
)
|
|
|
|
|
2014-07-03 10:02:31 +00:00
|
|
|
notification_service.new_team_member(self) unless owner?
|
2014-06-17 19:51:43 +00:00
|
|
|
system_hook_service.execute_hooks_for(self, :create)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_update_hook
|
|
|
|
notification_service.update_team_member(self) if self.project_access_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_destroy_hook
|
|
|
|
Event.create(
|
|
|
|
project_id: self.project.id,
|
|
|
|
action: Event::LEFT,
|
|
|
|
author_id: self.user.id
|
|
|
|
)
|
|
|
|
|
|
|
|
system_hook_service.execute_hooks_for(self, :destroy)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notification_service
|
|
|
|
NotificationService.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def system_hook_service
|
|
|
|
SystemHooksService.new
|
|
|
|
end
|
2011-10-08 21:36:38 +00:00
|
|
|
end
|