Chris@1517: # Redmine - project management software Chris@1517: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1517: # Chris@1517: # This program is free software; you can redistribute it and/or Chris@1517: # modify it under the terms of the GNU General Public License Chris@1517: # as published by the Free Software Foundation; either version 2 Chris@1517: # of the License, or (at your option) any later version. Chris@1517: # Chris@1517: # This program is distributed in the hope that it will be useful, Chris@1517: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1517: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1517: # GNU General Public License for more details. Chris@1517: # Chris@1517: # You should have received a copy of the GNU General Public License Chris@1517: # along with this program; if not, write to the Free Software Chris@1517: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1517: Chris@1517: class MembersController < ApplicationController Chris@1517: model_object Member Chris@1517: before_filter :find_model_object, :except => [:index, :create, :autocomplete] Chris@1517: before_filter :find_project_from_association, :except => [:index, :create, :autocomplete] Chris@1517: before_filter :find_project_by_project_id, :only => [:index, :create, :autocomplete] Chris@1517: before_filter :authorize Chris@1517: accept_api_auth :index, :show, :create, :update, :destroy Chris@1517: Chris@1517: def index Chris@1517: @offset, @limit = api_offset_and_limit Chris@1517: @member_count = @project.member_principals.count Chris@1517: @member_pages = Paginator.new @member_count, @limit, params['page'] Chris@1517: @offset ||= @member_pages.offset Chris@1517: @members = @project.member_principals. Chris@1517: order("#{Member.table_name}.id"). Chris@1517: limit(@limit). Chris@1517: offset(@offset). Chris@1517: all Chris@1517: respond_to do |format| Chris@1517: format.html { head 406 } Chris@1517: format.api Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def show Chris@1517: respond_to do |format| Chris@1517: format.html { head 406 } Chris@1517: format.api Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def create Chris@1517: members = [] Chris@1517: if params[:membership] Chris@1517: if params[:membership][:user_ids] Chris@1517: attrs = params[:membership].dup Chris@1517: user_ids = attrs.delete(:user_ids) Chris@1517: user_ids.each do |user_id| Chris@1517: members << Member.new(:role_ids => params[:membership][:role_ids], :user_id => user_id) Chris@1517: end Chris@1517: else Chris@1517: members << Member.new(:role_ids => params[:membership][:role_ids], :user_id => params[:membership][:user_id]) Chris@1517: end Chris@1517: @project.members << members Chris@1517: end Chris@1517: Chris@1517: respond_to do |format| Chris@1517: format.html { redirect_to_settings_in_projects } Chris@1517: format.js { @members = members } Chris@1517: format.api { Chris@1517: @member = members.first Chris@1517: if @member.valid? Chris@1517: render :action => 'show', :status => :created, :location => membership_url(@member) Chris@1517: else Chris@1517: render_validation_errors(@member) Chris@1517: end Chris@1517: } Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def update Chris@1517: if params[:membership] Chris@1517: @member.role_ids = params[:membership][:role_ids] Chris@1517: end Chris@1517: saved = @member.save Chris@1517: respond_to do |format| Chris@1517: format.html { redirect_to_settings_in_projects } Chris@1517: format.js Chris@1517: format.api { Chris@1517: if saved Chris@1517: render_api_ok Chris@1517: else Chris@1517: render_validation_errors(@member) Chris@1517: end Chris@1517: } Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def destroy Chris@1517: if request.delete? && @member.deletable? Chris@1517: @member.destroy Chris@1517: end Chris@1517: respond_to do |format| Chris@1517: format.html { redirect_to_settings_in_projects } Chris@1517: format.js Chris@1517: format.api { Chris@1517: if @member.destroyed? Chris@1517: render_api_ok Chris@1517: else Chris@1517: head :unprocessable_entity Chris@1517: end Chris@1517: } Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def autocomplete Chris@1517: respond_to do |format| Chris@1517: format.js Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: private Chris@1517: Chris@1517: def redirect_to_settings_in_projects Chris@1517: redirect_to settings_project_path(@project, :tab => 'members') Chris@1517: end Chris@1517: end