gitlabhq/app/models/users_project.rb

170 lines
4.4 KiB
Ruby
Raw Normal View History

2012-11-19 19:24:05 +01:00
# == Schema Information
#
# Table name: users_projects
#
# id :integer not null, primary key
# user_id :integer not null
# project_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# project_access :integer default(0), not null
#
2011-10-08 23:36:38 +02:00
class UsersProject < ActiveRecord::Base
include GitHost
2012-02-16 08:03:55 +01:00
GUEST = 10
REPORTER = 20
DEVELOPER = 30
MASTER = 40
2012-02-15 22:51:04 +01:00
attr_accessible :user, :user_id, :project_access
2011-10-08 23:36:38 +02:00
belongs_to :user
belongs_to :project
attr_accessor :skip_git
after_save :update_repository, unless: :skip_git?
after_destroy :update_repository, unless: :skip_git?
2011-10-08 23:36:38 +02:00
2012-10-09 02:10:04 +02:00
validates :user, presence: true
validates :user_id, uniqueness: { scope: [:project_id], message: "already exists in project" }
validates :project_access, inclusion: { in: [GUEST, REPORTER, DEVELOPER, MASTER] }, presence: true
2012-10-09 02:10:04 +02:00
validates :project, presence: true
delegate :name, :email, to: :user, prefix: true
2011-10-08 23:36:38 +02:00
2012-12-25 23:52:20 +01:00
scope :in_project, ->(project) { where(project_id: project.id) }
2012-10-09 02:10:04 +02:00
class << self
def add_users_into_projects(project_ids, user_ids, project_access)
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.skip_git = true
users_project.save
end
2012-10-24 13:20:53 +02:00
end
Gitlab::Gitolite.new.update_repositories(Project.where(id: project_ids))
2012-10-24 13:20:53 +02:00
end
true
rescue
false
end
2012-12-30 13:26:19 +01: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.skip_git = true
users_project.destroy
end
Gitlab::Gitolite.new.update_repositories(Project.where(id: project_ids))
end
true
rescue
false
end
def truncate_team project
truncate_teams [project.id]
end
def import_team(source_project, target_project)
source_team = source_project.users_projects.all
target_team = target_project.users_projects.all
target_user_ids = target_team.map(&:user_id)
source_team.reject! do |tm|
# Skip if user already present in team
target_user_ids.include?(tm.user_id)
end
source_team.map! do |tm|
new_tm = tm.dup
new_tm.id = nil
new_tm.project_id = target_project.id
new_tm.skip_git = true
new_tm
end
UsersProject.transaction do
source_team.each do |tm|
tm.save
end
target_project.update_repository
end
true
rescue
false
2012-10-24 13:20:53 +02:00
end
2012-10-09 02:10:04 +02:00
def bulk_delete(project, user_ids)
UsersProject.transaction do
UsersProject.where(user_id: user_ids, project_id: project.id).each do |users_project|
users_project.skip_git = true
2012-10-09 02:10:04 +02:00
users_project.destroy
end
project.update_repository
end
end
2012-10-09 02:10:04 +02:00
def bulk_update(project, user_ids, project_access)
UsersProject.transaction do
UsersProject.where(user_id: user_ids, project_id: project.id).each do |users_project|
2012-10-09 02:10:04 +02:00
users_project.project_access = project_access
users_project.skip_git = true
2012-10-09 02:10:04 +02:00
users_project.save
end
project.update_repository
end
end
# TODO: depreceate in future in favor of add_users_into_projects
2012-10-09 02:10:04 +02:00
def bulk_import(project, user_ids, project_access)
add_users_into_projects([project.id], user_ids, project_access)
end
# TODO: depreceate in future in favor of add_users_into_projects
2012-10-09 02:10:04 +02:00
def user_bulk_import(user, project_ids, project_access)
add_users_into_projects(project_ids, [user.id], project_access)
2012-02-11 18:56:18 +01:00
end
2012-10-09 02:10:04 +02:00
def access_roles
{
"Guest" => GUEST,
"Reporter" => REPORTER,
"Developer" => DEVELOPER,
"Master" => MASTER
}
end
2012-02-15 22:51:04 +01:00
end
def role_access
2012-02-16 08:03:55 +01:00
project_access
2012-02-15 22:51:04 +01:00
end
2011-12-05 08:43:53 +01:00
def update_repository
git_host.update_repository(project)
2011-10-08 23:36:38 +02:00
end
2012-02-05 20:26:04 +01:00
def project_access_human
Project.access_options.key(self.project_access)
end
def repo_access_human
2012-07-15 14:29:06 +02:00
self.class.access_roles.invert[self.project_access]
2012-02-05 20:26:04 +01:00
end
def skip_git?
!!@skip_git
end
2011-10-08 23:36:38 +02:00
end