Chris@909: # Redmine - project management software Chris@1115: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class RolesController < ApplicationController Chris@0: layout 'admin' Chris@909: Chris@1115: before_filter :require_admin, :except => [:index, :show] Chris@1115: before_filter :require_admin_or_api_request, :only => [:index, :show] Chris@1115: before_filter :find_role, :only => [:show, :edit, :update, :destroy] Chris@1115: accept_api_auth :index, :show Chris@0: Chris@0: def index Chris@1115: respond_to do |format| Chris@1115: format.html { Chris@1115: @role_pages, @roles = paginate :roles, :per_page => 25, :order => 'builtin, position' Chris@1115: render :action => "index", :layout => false if request.xhr? Chris@1115: } Chris@1115: format.api { Chris@1115: @roles = Role.givable.all Chris@1115: } Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: def show Chris@1115: respond_to do |format| Chris@1115: format.api Chris@1115: end Chris@0: end Chris@0: Chris@0: def new Chris@1115: # Prefills the form with 'Non member' role permissions by default Chris@0: @role = Role.new(params[:role] || {:permissions => Role.non_member.permissions}) Chris@1115: if params[:copy].present? && @copy_from = Role.find_by_id(params[:copy]) Chris@1115: @role.copy_from(@copy_from) Chris@1115: end Chris@1115: @roles = Role.sorted.all Chris@1115: end Chris@1115: Chris@1115: def create Chris@1115: @role = Role.new(params[:role]) Chris@0: if request.post? && @role.save Chris@0: # workflow copy Chris@0: if !params[:copy_workflow_from].blank? && (copy_from = Role.find_by_id(params[:copy_workflow_from])) Chris@1115: @role.workflow_rules.copy(copy_from) Chris@0: end Chris@0: flash[:notice] = l(:notice_successful_create) Chris@0: redirect_to :action => 'index' Chris@441: else Chris@1115: @roles = Role.sorted.all Chris@1115: render :action => 'new' Chris@0: end Chris@0: end Chris@0: Chris@0: def edit Chris@1115: end Chris@1115: Chris@1115: def update Chris@1115: if request.put? and @role.update_attributes(params[:role]) Chris@0: flash[:notice] = l(:notice_successful_update) Chris@0: redirect_to :action => 'index' Chris@441: else Chris@1115: render :action => 'edit' Chris@0: end Chris@0: end Chris@0: Chris@0: def destroy Chris@0: @role.destroy Chris@0: redirect_to :action => 'index' Chris@0: rescue Chris@0: flash[:error] = l(:error_can_not_remove_role) Chris@0: redirect_to :action => 'index' Chris@0: end Chris@909: Chris@1115: def permissions Chris@1115: @roles = Role.sorted.all Chris@0: @permissions = Redmine::AccessControl.permissions.select { |p| !p.public? } Chris@0: if request.post? Chris@0: @roles.each do |role| Chris@0: role.permissions = params[:permissions][role.id.to_s] Chris@0: role.save Chris@0: end Chris@0: flash[:notice] = l(:notice_successful_update) Chris@0: redirect_to :action => 'index' Chris@0: end Chris@0: end Chris@1115: Chris@1115: private Chris@1115: Chris@1115: def find_role Chris@1115: @role = Role.find(params[:id]) Chris@1115: rescue ActiveRecord::RecordNotFound Chris@1115: render_404 Chris@1115: end Chris@0: end