2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2011-12-05 12:32:18 +00:00
|
|
|
class MembersController < ApplicationController
|
|
|
|
before_filter :authenticate_user!
|
|
|
|
is_related_controller!
|
|
|
|
|
|
|
|
belongs_to :group, :optional => true
|
|
|
|
|
|
|
|
# before_filter :find_target
|
|
|
|
before_filter :find_users
|
|
|
|
|
|
|
|
def index
|
|
|
|
redirect_to edit_group_members_path(parent)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
if params[:id]
|
|
|
|
@user = User.find params[:id]
|
|
|
|
render :edit_rights and return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
all_user_ids = []
|
|
|
|
Relation::ROLES.each { |r|
|
|
|
|
all_user_ids = all_user_ids | params[r.to_sym].keys if params[r.to_sym]
|
|
|
|
}
|
|
|
|
|
|
|
|
# Remove relations
|
|
|
|
users_for_removing = parent.members.select do |u|
|
|
|
|
!all_user_ids.map{|k| k.to_i}.include? u.id and parent.owner != u
|
|
|
|
end
|
|
|
|
users_for_removing.each do |u|
|
|
|
|
Relation.by_object(u).by_target(parent).each {|r| r.destroy}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create relations
|
|
|
|
Relation::ROLES.each { |r|
|
|
|
|
#users_for_creating = users_for_creating params[:user].keys.map{|p| p.to_i} - @project.collaborators.map(&:id)
|
|
|
|
params[r.to_sym].keys.each { |u|
|
|
|
|
if relation = parent.objects.find_by_object_id_and_object_type(u, 'User')
|
|
|
|
relation.update_attribute(:role, r)
|
|
|
|
else
|
|
|
|
relation = parent.objects.build(:object_id => u, :object_type => 'User', :role => r)
|
|
|
|
puts relation.inspect
|
|
|
|
puts r
|
|
|
|
relation.save!
|
|
|
|
end
|
|
|
|
} if params[r.to_sym]
|
|
|
|
}
|
|
|
|
|
|
|
|
if parent.save
|
|
|
|
flash[:notice] = t("flash.members.successfully_changed")
|
|
|
|
else
|
|
|
|
flash[:error] = t("flash.members.error_in_changing")
|
|
|
|
end
|
|
|
|
redirect_to parent_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
end
|
|
|
|
|
2011-12-09 23:40:13 +00:00
|
|
|
def add
|
|
|
|
if params['user_id'] and !params['user_id'].empty?
|
|
|
|
@user = User.find_by_uname(params['user_id'])
|
2011-12-10 17:26:57 +00:00
|
|
|
unless parent.objects.exists? :object_id => @user.id, :object_type => 'User'
|
|
|
|
relation = parent.objects.build(:object_id => @user.id, :object_type => 'User', :role => 'reader')
|
|
|
|
if relation.save
|
|
|
|
flash[:notice] = t("flash.members.successfully_added")
|
|
|
|
else
|
|
|
|
flash[:error] = t("flash.members.error_in_adding")
|
|
|
|
end
|
2011-12-09 23:40:13 +00:00
|
|
|
else
|
2011-12-10 17:26:57 +00:00
|
|
|
flash[:error] = t("flash.members.already_added")
|
2011-12-09 23:40:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
redirect_to edit_group_members_path(parent)
|
|
|
|
end
|
|
|
|
|
2011-12-05 12:32:18 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def find_users
|
2011-12-09 23:40:13 +00:00
|
|
|
@users = parent.members #User.all
|
2011-12-05 12:32:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|