roles_controller.rb 2.68 KB
Newer Older
1
2
# Redmine - project management software
# Copyright (C) 2006-2011  Jean-Philippe Lang
3
4
5
6
7
#
# 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.
8
#
9
10
11
12
# 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.
13
#
14
15
16
17
18
# 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 RolesController < ApplicationController
19
  layout 'admin'
20

21
  before_filter :require_admin
jplang's avatar
jplang committed
22
  before_filter :find_role, :only => [:edit, :update, :destroy]
23

jplang's avatar
jplang committed
24

jplang's avatar
jplang committed
25
  def index
jplang's avatar
jplang committed
26
    @role_pages, @roles = paginate :roles, :per_page => 25, :order => 'builtin, position'
27
    render :action => "index", :layout => false if request.xhr?
jplang's avatar
jplang committed
28
29
30
  end

  def new
31
32
    # Prefills the form with 'Non member' role permissions
    @role = Role.new(params[:role] || {:permissions => Role.non_member.permissions})
33
    @roles = Role.sorted.all
jplang's avatar
jplang committed
34
35
36
37
  end

  def create
    @role = Role.new(params[:role])
jplang's avatar
jplang committed
38
    if request.post? && @role.save
jplang's avatar
jplang committed
39
40
41
42
      # workflow copy
      if !params[:copy_workflow_from].blank? && (copy_from = Role.find_by_id(params[:copy_workflow_from]))
        @role.workflows.copy(copy_from)
      end
jplang's avatar
jplang committed
43
      flash[:notice] = l(:notice_successful_create)
44
      redirect_to :action => 'index'
45
    else
46
      @roles = Role.sorted.all
jplang's avatar
jplang committed
47
      render :action => 'new'
jplang's avatar
jplang committed
48
49
50
51
    end
  end

  def edit
jplang's avatar
jplang committed
52
53
54
55
  end

  def update
    if request.put? and @role.update_attributes(params[:role])
jplang's avatar
jplang committed
56
      flash[:notice] = l(:notice_successful_update)
57
      redirect_to :action => 'index'
58
    else
jplang's avatar
jplang committed
59
      render :action => 'edit'
jplang's avatar
jplang committed
60
61
62
    end
  end

jplang's avatar
jplang committed
63
  verify :method => :delete, :only => :destroy, :redirect_to => { :action => :index }
jplang's avatar
jplang committed
64
  def destroy
65
    @role.destroy
66
    redirect_to :action => 'index'
67
  rescue
68
    flash[:error] =  l(:error_can_not_remove_role)
69
    redirect_to :action => 'index'
70
  end
71

jplang's avatar
jplang committed
72
  def permissions
73
    @roles = Role.sorted.all
jplang's avatar
jplang committed
74
    @permissions = Redmine::AccessControl.permissions.select { |p| !p.public? }
75
76
    if request.post?
      @roles.each do |role|
jplang's avatar
jplang committed
77
78
        role.permissions = params[:permissions][role.id.to_s]
        role.save
79
80
      end
      flash[:notice] = l(:notice_successful_update)
81
      redirect_to :action => 'index'
82
83
    end
  end
jplang's avatar
jplang committed
84
85
86
87
88
89
90
91

  private

  def find_role
    @role = Role.find(params[:id])
  rescue ActiveRecord::RecordNotFound
    render_404
  end
jplang's avatar
jplang committed
92
end