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
|
2012-03-02 08:47:16 +00:00
|
|
|
@group = parent
|
2011-12-05 12:32:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2012-03-01 15:41:40 +00:00
|
|
|
params['user'].keys.each { |user_id|
|
|
|
|
role = params['user'][user_id]
|
2011-12-05 12:32:18 +00:00
|
|
|
|
2012-03-01 15:41:40 +00:00
|
|
|
if relation = parent.objects.find_by_object_id_and_object_type(user_id, 'User')
|
|
|
|
relation.update_attribute(:role, role)
|
|
|
|
else
|
|
|
|
relation = parent.objects.build(:object_id => user_id, :object_type => 'User', :role => role)
|
|
|
|
relation.save!
|
|
|
|
end
|
|
|
|
} if params['user']
|
2011-12-05 12:32:18 +00:00
|
|
|
|
|
|
|
if parent.save
|
|
|
|
flash[:notice] = t("flash.members.successfully_changed")
|
|
|
|
else
|
|
|
|
flash[:error] = t("flash.members.error_in_changing")
|
|
|
|
end
|
2012-03-01 15:41:40 +00:00
|
|
|
|
|
|
|
redirect_to edit_group_members_path(parent)
|
2011-12-05 12:32:18 +00:00
|
|
|
end
|
|
|
|
|
2012-03-01 14:23:26 +00:00
|
|
|
def remove
|
2012-03-01 15:41:40 +00:00
|
|
|
if params[:id]
|
|
|
|
u = User.find(params[:id])
|
|
|
|
Relation.by_object(u).by_target(parent)[0].destroy
|
|
|
|
|
|
|
|
redirect_to groups_path
|
|
|
|
else
|
|
|
|
all_user_ids = []
|
|
|
|
|
|
|
|
params['user_remove'].keys.each { |user_id|
|
|
|
|
all_user_ids << user_id if params['user_remove'][user_id] == ["1"]
|
|
|
|
} if params['user_remove']
|
|
|
|
|
|
|
|
all_user_ids.each do |user_id|
|
|
|
|
u = User.find(user_id)
|
|
|
|
Relation.by_object(u).by_target(parent).each {|r| r.destroy}
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to edit_group_members_path(parent)
|
|
|
|
end
|
2011-12-05 12:32:18 +00:00
|
|
|
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'
|
2012-03-01 15:41:40 +00:00
|
|
|
relation = parent.objects.build(:object_id => @user.id, :object_type => 'User', :role => params[:role])
|
2011-12-10 17:26:57 +00:00
|
|
|
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
|