1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 class RolesController < ApplicationController
19 layout 'admin'
20
21 before_filter :require_admin, :except => :index
22 before_filter :require_admin_or_api_request, :only => :index
23 before_filter :find_role, :only => [:edit, :update, :destroy]
24 accept_api_auth :index
25
26 def index
27 respond_to do |format|
28 format.html {
29 @role_pages, @roles = paginate :roles, :per_page => 25, :order => 'builtin, position'
30 render :action => "index", :layout => false if request.xhr?
31 }
32 format.api {
33 @roles = Role.givable.all
34 }
35 end
36 end
37
38 def new
39 # Prefills the form with 'Non member' role permissions
40 @role = Role.new(params[:role] || {:permissions => Role.non_member.permissions})
41 @roles = Role.sorted.all
42 end
43
44 def create
45 @role = Role.new(params[:role])
46 if request.post? && @role.save
47 # workflow copy
48 if !params[:copy_workflow_from].blank? && (copy_from = Role.find_by_id(params[:copy_workflow_from]))
49 @role.workflows.copy(copy_from)
50 end
51 flash[:notice] = l(:notice_successful_create)
52 redirect_to :action => 'index'
53 else
54 @roles = Role.sorted.all
55 render :action => 'new'
56 end
57 end
58
59 def edit
60 end
61
62 def update
63 if request.put? and @role.update_attributes(params[:role])
64 flash[:notice] = l(:notice_successful_update)
65 redirect_to :action => 'index'
66 else
67 render :action => 'edit'
68 end
69 end
70
71 def destroy
72 @role.destroy
73 redirect_to :action => 'index'
74 rescue
75 flash[:error] = l(:error_can_not_remove_role)
76 redirect_to :action => 'index'
77 end
78
79 def permissions
80 @roles = Role.sorted.all
81 @permissions = Redmine::AccessControl.permissions.select { |p| !p.public? }
82 if request.post?
83 @roles.each do |role|
84 role.permissions = params[:permissions][role.id.to_s]
85 role.save
86 end
87 flash[:notice] = l(:notice_successful_update)
88 redirect_to :action => 'index'
89 end
90 end
91
92 private
93
94 def find_role
95 @role = Role.find(params[:id])
96 rescue ActiveRecord::RecordNotFound
97 render_404
98 end
99 end