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
|
2013-01-03 08:06:07 +01:00
|
|
|
include Gitolited
|
2012-08-28 23:04:06 +02:00
|
|
|
|
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
|
|
|
|
|
2012-12-30 12:37:33 +01:00
|
|
|
attr_accessor :skip_git
|
|
|
|
|
2012-10-09 02:10:04 +02:00
|
|
|
validates :user, presence: true
|
2012-12-30 12:37:33 +01:00
|
|
|
validates :user_id, uniqueness: { scope: [:project_id], message: "already exists in project" }
|
2012-12-22 18:52:28 +01:00
|
|
|
validates :project_access, inclusion: { in: [GUEST, REPORTER, DEVELOPER, MASTER] }, presence: true
|
2012-10-09 02:10:04 +02:00
|
|
|
validates :project, presence: true
|
2011-10-20 15:48:09 +02:00
|
|
|
|
2013-01-30 16:07:44 +01:00
|
|
|
delegate :name, :username, :email, to: :user, prefix: true
|
2011-10-08 23:36:38 +02:00
|
|
|
|
2013-01-03 18:11:14 +01: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 18:06:50 +01:00
|
|
|
|
2012-12-25 23:52:20 +01:00
|
|
|
scope :in_project, ->(project) { where(project_id: project.id) }
|
2013-01-20 22:05:51 +01:00
|
|
|
scope :in_projects, ->(projects) { where(project_id: project_ids) }
|
2013-01-19 18:06:50 +01:00
|
|
|
scope :with_user, ->(user) { where(user_id: user.id) }
|
2012-12-25 23:52:20 +01:00
|
|
|
|
2012-10-09 02:10:04 +02:00
|
|
|
class << self
|
2013-01-03 20:09:18 +01: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 07:43:25 +01:00
|
|
|
# Ex.
|
|
|
|
# add_users_into_projects(
|
|
|
|
# project_ids,
|
|
|
|
# user_ids,
|
|
|
|
# UsersProject::MASTER
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# add_users_into_projects(
|
|
|
|
# project_ids,
|
|
|
|
# user_ids,
|
|
|
|
# :master
|
|
|
|
# )
|
|
|
|
#
|
2013-01-03 20:09:18 +01:00
|
|
|
def add_users_into_projects(project_ids, user_ids, access)
|
2013-01-04 07:43:25 +01:00
|
|
|
project_access = if roles_hash.has_key?(access)
|
|
|
|
roles_hash[access]
|
|
|
|
elsif roles_hash.values.include?(access.to_i)
|
2013-01-03 20:09:18 +01:00
|
|
|
access
|
|
|
|
else
|
|
|
|
raise "Non valid access"
|
|
|
|
end
|
|
|
|
|
2012-12-30 12:37:33 +01: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.skip_git = true
|
|
|
|
users_project.save
|
2012-10-24 13:52:17 +02:00
|
|
|
end
|
2012-10-24 13:20:53 +02:00
|
|
|
end
|
|
|
|
end
|
2012-10-24 13:52:17 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def truncate_team project
|
|
|
|
truncate_teams [project.id]
|
|
|
|
end
|
|
|
|
|
2013-01-03 20:09:18 +01:00
|
|
|
def roles_hash
|
|
|
|
{
|
|
|
|
guest: GUEST,
|
|
|
|
reporter: REPORTER,
|
|
|
|
developer: DEVELOPER,
|
|
|
|
master: MASTER
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
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
|
2012-12-30 12:37:33 +01:00
|
|
|
|
|
|
|
def skip_git?
|
|
|
|
!!@skip_git
|
|
|
|
end
|
2011-10-08 23:36:38 +02:00
|
|
|
end
|