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
|
|
|
|
|
2011-11-24 21:46:19 +00:00
|
|
|
load_and_authorize_resource :project
|
|
|
|
|
|
|
|
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
|
2011-11-19 12:11:44 +00:00
|
|
|
all_user_ids = []
|
2011-12-05 12:32:18 +00:00
|
|
|
all_groups_ids = []
|
2011-12-07 09:46:48 +00:00
|
|
|
puts params.inspect
|
2011-11-19 12:11:44 +00:00
|
|
|
Relation::ROLES.each { |r|
|
2011-12-07 09:46:48 +00:00
|
|
|
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
|
|
|
|
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
|
2011-12-05 12:32:18 +00:00
|
|
|
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
|
2011-11-19 12:11:44 +00:00
|
|
|
|
|
|
|
# Create relations
|
|
|
|
Relation::ROLES.each { |r|
|
|
|
|
#users_for_creating = users_for_creating params[:user].keys.map{|p| p.to_i} - @project.collaborators.map(&:id)
|
2011-12-05 12:32:18 +00:00
|
|
|
params['user'][r.to_sym].keys.each { |u|
|
2011-11-19 12:11:44 +00:00
|
|
|
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
|
2011-12-07 09:46:48 +00:00
|
|
|
} if params['user'] && params['user'][r.to_sym]
|
2011-12-05 12:32:18 +00:00
|
|
|
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
|
2011-12-07 09:46:48 +00:00
|
|
|
} 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
|
2011-11-18 18:26:22 +00:00
|
|
|
redirect_to project_path(@project)
|
2011-10-30 22:59:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def find_project
|
|
|
|
@project = Project.find params[:project_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_users
|
|
|
|
@users = User.all
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_groups
|
|
|
|
@groups = Group.all
|
|
|
|
end
|
|
|
|
end
|