2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2011-10-30 22:59:03 +00:00
|
|
|
class CollaboratorsController < ApplicationController
|
|
|
|
before_filter :authenticate_user!
|
|
|
|
|
|
|
|
before_filter :find_project
|
|
|
|
before_filter :find_users
|
|
|
|
before_filter :find_groups
|
|
|
|
|
2012-02-06 15:04:56 +00:00
|
|
|
load_resource :project
|
|
|
|
before_filter :authorize_collaborators
|
2011-11-24 21:46:19 +00:00
|
|
|
|
2012-02-06 15:04:56 +00:00
|
|
|
def index
|
2011-10-30 22:59:03 +00:00
|
|
|
redirect_to edit_project_collaborators_path(@project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2011-11-24 21:46:19 +00:00
|
|
|
if params[:id]
|
|
|
|
@user = User.find params[:id]
|
|
|
|
render :edit_rights and return
|
|
|
|
end
|
2011-10-30 22:59:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2012-02-28 21:01:25 +00:00
|
|
|
#all_user_ids = []
|
|
|
|
#all_groups_ids = []
|
|
|
|
#Relation::ROLES.each { |r|
|
|
|
|
# all_user_ids = all_user_ids | params['user'][r.to_sym].keys if params['user'] && params['user'][r.to_sym]
|
|
|
|
# all_groups_ids = all_groups_ids | params['group'][r.to_sym].keys if params['group'] && params['group'][r.to_sym]
|
|
|
|
#}
|
2011-11-19 12:11:44 +00:00
|
|
|
|
|
|
|
# Remove relations
|
2012-02-28 21:01:25 +00:00
|
|
|
#users_for_removing = @project.collaborators.select do |u|
|
|
|
|
# !all_user_ids.map{|k| k.to_i}.include? u.id and @project.owner != u
|
|
|
|
#end
|
|
|
|
#users_for_removing.each do |u|
|
|
|
|
# Relation.by_object(u).by_target(@project).each {|r| r.destroy}
|
|
|
|
#end
|
|
|
|
#groups_for_removing = @project.groups.select do |u|
|
|
|
|
# !all_groups_ids.map{|k| k.to_i}.include? u.id and @project.owner != u
|
|
|
|
#end
|
|
|
|
#groups_for_removing.each do |u|
|
|
|
|
# Relation.by_object(u).by_target(@project).each {|r| r.destroy}
|
|
|
|
#end
|
2012-02-06 15:04:56 +00:00
|
|
|
|
2011-11-19 12:11:44 +00:00
|
|
|
# Create relations
|
2012-02-28 21:01:25 +00:00
|
|
|
#Relation::ROLES.each { |r|
|
|
|
|
# #users_for_creating = users_for_creating params[:user].keys.map{|p| p.to_i} - @project.collaborators.map(&:id)
|
|
|
|
# params['user'][r.to_sym].keys.each { |u|
|
|
|
|
# if relation = @project.relations.find_by_object_id_and_object_type(u, 'User')
|
|
|
|
# relation.update_attribute(:role, r)
|
|
|
|
# else
|
|
|
|
# relation = @project.relations.build(:object_id => u, :object_type => 'User', :role => r)
|
|
|
|
# relation.save!
|
|
|
|
# end
|
|
|
|
# } if params['user'] && params['user'][r.to_sym]
|
|
|
|
# params['group'][r.to_sym].keys.each { |u|
|
|
|
|
# if relation = @project.relations.find_by_object_id_and_object_type(u, 'Group')
|
|
|
|
# relation.update_attribute(:role, r)
|
|
|
|
# else
|
|
|
|
# relation = @project.relations.build(:object_id => u, :object_type => 'Group', :role => r)
|
|
|
|
# relation.save!
|
|
|
|
# end
|
|
|
|
# } if params['group'] && params['group'][r.to_sym]
|
|
|
|
#}
|
|
|
|
|
|
|
|
params['user'].keys.each { |user_id|
|
|
|
|
role = params['user'][user_id]
|
|
|
|
|
|
|
|
if relation = @project.relations.find_by_object_id_and_object_type(user_id, 'User')
|
|
|
|
relation.update_attribute(:role, role)
|
|
|
|
else
|
|
|
|
relation = @project.relations.build(:object_id => user_id, :object_type => 'User', :role => role)
|
|
|
|
relation.save!
|
|
|
|
end
|
|
|
|
} if params['user']# && params['user'][r.to_sym]
|
|
|
|
|
|
|
|
params['group'].keys.each { |group_id|
|
|
|
|
role = params['group'][group_id]
|
|
|
|
if relation = @project.relations.find_by_object_id_and_object_type(group_id, 'Group')
|
|
|
|
relation.update_attribute(:role, role)
|
|
|
|
else
|
|
|
|
relation = @project.relations.build(:object_id => user_id, :object_type => 'Group', :role => role)
|
|
|
|
relation.save!
|
|
|
|
end
|
|
|
|
} if params['group']# && params['group'][r.to_sym]
|
2011-11-19 12:11:44 +00:00
|
|
|
|
2011-11-18 18:26:22 +00:00
|
|
|
if @project.save
|
|
|
|
flash[:notice] = t("flash.collaborators.successfully_changed")
|
2011-10-30 22:59:03 +00:00
|
|
|
else
|
2011-11-19 11:49:14 +00:00
|
|
|
flash[:error] = t("flash.collaborators.error_in_changing")
|
2011-10-30 22:59:03 +00:00
|
|
|
end
|
2012-02-28 21:01:25 +00:00
|
|
|
|
|
|
|
redirect_to edit_project_collaborators_path(@project)
|
2011-10-30 22:59:03 +00:00
|
|
|
end
|
|
|
|
|
2012-02-28 21:01:25 +00:00
|
|
|
def remove
|
|
|
|
all_user_ids = []
|
|
|
|
all_groups_ids = []
|
|
|
|
|
|
|
|
params['user_remove'].keys.each { |user_id|
|
|
|
|
all_user_ids << user_id if params['user_remove'][user_id] == ["1"]
|
|
|
|
} if params['user_remove']
|
|
|
|
params['group_remove'].keys.each { |group_id|
|
|
|
|
all_group_ids << group_id if params['group_remove'][group_id] == ["1"]
|
|
|
|
} if params['group_remove']
|
|
|
|
|
|
|
|
all_user_ids.each do |user_id|
|
|
|
|
u = User.find(user_id)
|
|
|
|
Relation.by_object(u).by_target(@project).each {|r| r.destroy}
|
|
|
|
end
|
|
|
|
all_groups_ids.each do |group_id|
|
|
|
|
g = Group.find(group_id)
|
|
|
|
Relation.by_object(g).by_target(@project).each {|r| r.destroy}
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to edit_project_collaborators_path(@project)
|
2011-10-30 22:59:03 +00:00
|
|
|
end
|
|
|
|
|
2011-12-11 23:42:13 +00:00
|
|
|
def add
|
|
|
|
# TODO: Here is used Chelyabinsk method to display Flash messages.
|
|
|
|
|
2011-12-14 15:28:31 +00:00
|
|
|
member = User.find(params['member_id']) if params['member_id'] && !params['member_id'].empty?
|
|
|
|
group = Group.find(params['group_id']) if params['group_id'] && !params['group_id'].empty?
|
2011-12-11 23:42:13 +00:00
|
|
|
|
|
|
|
flash[:notice], flash[:error], flash[:warning] = [], [], []
|
|
|
|
|
|
|
|
[member, group].compact.each do |mem|
|
|
|
|
if mem and @project.relations.exists?(:object_id => mem.id, :object_type => mem.class.to_s)
|
|
|
|
flash[:warning] << [t('flash.collaborators.member_already_added'), mem.uname]
|
|
|
|
end
|
|
|
|
unless @project.relations.exists?(:object_id => mem.id, :object_type => mem.class.to_s)
|
2012-02-28 21:01:25 +00:00
|
|
|
rel = @project.relations.build(:role => params[:role])
|
2011-12-11 23:42:13 +00:00
|
|
|
rel.object = mem
|
|
|
|
if rel.save
|
|
|
|
flash[:notice] << [t('flash.collaborators.successfully_added'), mem.uname]
|
|
|
|
else
|
|
|
|
flash[:notice] << [t('flash.collaborators.error_in_adding'), mem.uname]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[:notice, :warning, :error].each do |k|
|
|
|
|
if flash[k].size > 0
|
|
|
|
flash[k] = flash[k].map{|i| (i.is_a? Array) ? sprintf(i.first, i.last) : i}.join('; ')
|
|
|
|
else
|
2012-02-08 14:01:00 +00:00
|
|
|
flash.delete k
|
2011-12-11 23:42:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to(edit_project_collaborators_path(@project))
|
|
|
|
end
|
|
|
|
|
2011-10-30 22:59:03 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def find_project
|
|
|
|
@project = Project.find params[:project_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_users
|
2011-12-11 23:42:13 +00:00
|
|
|
@users = @project.collaborators#User.all
|
2011-10-30 22:59:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_groups
|
2011-12-11 23:42:13 +00:00
|
|
|
@groups = @project.groups#Group.all
|
2011-10-30 22:59:03 +00:00
|
|
|
end
|
2012-02-06 15:04:56 +00:00
|
|
|
|
|
|
|
def authorize_collaborators
|
|
|
|
authorize! :update, @project
|
|
|
|
end
|
2011-10-30 22:59:03 +00:00
|
|
|
end
|