groups_controller.rb
140 lines
| 3.8 KiB
| text/x-ruby
|
RubyLexer
|
r2755 | # Redmine - project management software | ||
|
r9453 | # Copyright (C) 2006-2012 Jean-Philippe Lang | ||
|
r2755 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r6784 | # | ||
|
r2755 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r6784 | # | ||
|
r2755 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
class GroupsController < ApplicationController | ||||
|
r3062 | layout 'admin' | ||
|
r6784 | |||
|
r2755 | before_filter :require_admin | ||
|
r9575 | before_filter :find_group, :except => [:index, :new, :create] | ||
accept_api_auth :index, :show, :create, :update, :destroy, :add_users, :remove_user | ||||
|
r6784 | |||
|
r2755 | helper :custom_fields | ||
|
r6784 | |||
|
r2755 | def index | ||
|
r9764 | @groups = Group.sorted.all | ||
|
r2755 | |||
respond_to do |format| | ||||
|
r9564 | format.html | ||
|
r9575 | format.api | ||
|
r2755 | end | ||
end | ||||
def show | ||||
respond_to do |format| | ||||
|
r9564 | format.html | ||
|
r9575 | format.api | ||
|
r2755 | end | ||
end | ||||
def new | ||||
@group = Group.new | ||||
end | ||||
def create | ||||
|
r9563 | @group = Group.new | ||
@group.safe_attributes = params[:group] | ||||
|
r2755 | |||
respond_to do |format| | ||||
if @group.save | ||||
|
r6184 | format.html { | ||
flash[:notice] = l(:notice_successful_create) | ||||
redirect_to(params[:continue] ? new_group_path : groups_path) | ||||
} | ||||
|
r9575 | format.api { render :action => 'show', :status => :created, :location => group_url(@group) } | ||
|
r2755 | else | ||
format.html { render :action => "new" } | ||||
|
r9575 | format.api { render_validation_errors(@group) } | ||
|
r2755 | end | ||
end | ||||
end | ||||
|
r9564 | def edit | ||
end | ||||
|
r2755 | def update | ||
|
r9563 | @group.safe_attributes = params[:group] | ||
|
r2755 | |||
respond_to do |format| | ||||
|
r9563 | if @group.save | ||
|
r2755 | flash[:notice] = l(:notice_successful_update) | ||
format.html { redirect_to(groups_path) } | ||||
|
r9792 | format.api { render_api_ok } | ||
|
r2755 | else | ||
format.html { render :action => "edit" } | ||||
|
r9575 | format.api { render_validation_errors(@group) } | ||
|
r2755 | end | ||
end | ||||
end | ||||
def destroy | ||||
@group.destroy | ||||
respond_to do |format| | ||||
format.html { redirect_to(groups_url) } | ||||
|
r9792 | format.api { render_api_ok } | ||
|
r2755 | end | ||
end | ||||
|
r6784 | |||
|
r2755 | def add_users | ||
|
r9861 | @users = User.find_all_by_id(params[:user_id] || params[:user_ids]) | ||
@group.users << @users if request.post? | ||||
|
r2755 | respond_to do |format| | ||
format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'users' } | ||||
|
r9861 | format.js | ||
|
r9792 | format.api { render_api_ok } | ||
|
r2755 | end | ||
end | ||||
|
r6784 | |||
|
r2755 | def remove_user | ||
|
r7826 | @group.users.delete(User.find(params[:user_id])) if request.delete? | ||
|
r2755 | respond_to do |format| | ||
format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'users' } | ||||
|
r9861 | format.js | ||
|
r9792 | format.api { render_api_ok } | ||
|
r2755 | end | ||
end | ||||
|
r6784 | |||
|
r2755 | def autocomplete_for_user | ||
|
r5164 | @users = User.active.not_in_group(@group).like(params[:q]).all(:limit => 100) | ||
|
r2755 | render :layout => false | ||
end | ||||
|
r6784 | |||
|
r2755 | def edit_membership | ||
|
r3487 | @membership = Member.edit_membership(params[:membership_id], params[:membership], @group) | ||
|
r2755 | @membership.save if request.post? | ||
respond_to do |format| | ||||
|
r9861 | format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'memberships' } | ||
format.js | ||||
|
r3820 | end | ||
|
r2755 | end | ||
|
r6784 | |||
|
r2755 | def destroy_membership | ||
Member.find(params[:membership_id]).destroy if request.post? | ||||
respond_to do |format| | ||||
format.html { redirect_to :controller => 'groups', :action => 'edit', :id => @group, :tab => 'memberships' } | ||||
|
r9861 | format.js | ||
|
r2755 | end | ||
end | ||||
|
r9565 | |||
private | ||||
def find_group | ||||
@group = Group.find(params[:id]) | ||||
rescue ActiveRecord::RecordNotFound | ||||
render_404 | ||||
end | ||||
|
r2755 | end | ||