2011-10-08 23:36:38 +02:00
|
|
|
class UsersProject < ActiveRecord::Base
|
2012-08-28 23:04:06 +02:00
|
|
|
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
|
|
|
|
2012-09-26 20:17:17 +02:00
|
|
|
attr_accessible :user, :user_id, :project_access
|
|
|
|
|
2011-10-08 23:36:38 +02:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :project
|
|
|
|
|
2011-12-07 00:27:07 +01:00
|
|
|
after_save :update_repository
|
|
|
|
after_destroy :update_repository
|
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, presence: true
|
2011-10-20 15:48:09 +02:00
|
|
|
|
2012-08-11 00:07:50 +02:00
|
|
|
delegate :name, :email, to: :user, prefix: true
|
2011-10-08 23:36:38 +02:00
|
|
|
|
2012-10-09 02:10:04 +02:00
|
|
|
class << self
|
2012-10-24 13:20:53 +02:00
|
|
|
def import_team(source_project, target_project)
|
2012-10-24 13:52:17 +02:00
|
|
|
UsersProject.without_repository_callback do
|
|
|
|
UsersProject.transaction do
|
|
|
|
team = source_project.users_projects.all
|
|
|
|
|
|
|
|
team.each do |tm|
|
|
|
|
# Skip if user already present in team
|
|
|
|
next if target_project.users.include?(tm.user)
|
|
|
|
|
|
|
|
new_tm = tm.dup
|
|
|
|
new_tm.id = nil
|
|
|
|
new_tm.project_id = target_project.id
|
|
|
|
new_tm.save
|
|
|
|
end
|
2012-10-24 13:20:53 +02:00
|
|
|
end
|
|
|
|
end
|
2012-10-24 13:52:17 +02:00
|
|
|
|
|
|
|
target_project.update_repository
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def without_repository_callback
|
|
|
|
UsersProject.skip_callback(:destroy, :after, :update_repository)
|
|
|
|
yield
|
|
|
|
UsersProject.set_callback(:destroy, :after, :update_repository)
|
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.destroy
|
|
|
|
end
|
2012-09-06 22:14:42 +02:00
|
|
|
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|
|
|
|
|
users_project.project_access = project_access
|
|
|
|
users_project.save
|
|
|
|
end
|
2012-09-06 22:14:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-09 02:10:04 +02:00
|
|
|
def bulk_import(project, user_ids, project_access)
|
|
|
|
UsersProject.transaction do
|
|
|
|
user_ids.each do |user_id|
|
|
|
|
users_project = UsersProject.new(
|
|
|
|
project_access: project_access,
|
|
|
|
user_id: user_id
|
|
|
|
)
|
|
|
|
users_project.project = project
|
|
|
|
users_project.save
|
|
|
|
end
|
2012-01-21 17:06:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-09 02:10:04 +02:00
|
|
|
def user_bulk_import(user, project_ids, project_access)
|
|
|
|
UsersProject.transaction do
|
|
|
|
project_ids.each do |project_id|
|
|
|
|
users_project = UsersProject.new(
|
|
|
|
project_access: project_access,
|
|
|
|
)
|
|
|
|
users_project.project_id = project_id
|
|
|
|
users_project.user_id = user.id
|
|
|
|
users_project.save
|
|
|
|
end
|
2012-02-11 18:56:18 +01:00
|
|
|
end
|
|
|
|
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
|
2012-08-28 23:04:06 +02:00
|
|
|
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
|
2011-10-08 23:36:38 +02:00
|
|
|
end
|
2012-09-27 08:20:36 +02:00
|
|
|
|
2011-10-08 23:36:38 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: users_projects
|
|
|
|
#
|
2012-09-27 07:36:31 +02:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# project_id :integer not null
|
2012-06-26 20:23:09 +02:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2012-09-27 07:36:31 +02:00
|
|
|
# project_access :integer default(0), not null
|
2011-10-08 23:36:38 +02:00
|
|
|
#
|
2012-10-09 10:14:17 +02:00
|
|
|
|