##// END OF EJS Templates
Redirect to referer to preserve pagination and filter....
Jean-Philippe Lang -
r15376:f435d1844d9e
parent child
Show More
@@ -1,154 +1,154
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class GroupsController < ApplicationController
18 class GroupsController < ApplicationController
19 layout 'admin'
19 layout 'admin'
20
20
21 before_action :require_admin
21 before_action :require_admin
22 before_action :find_group, :except => [:index, :new, :create]
22 before_action :find_group, :except => [:index, :new, :create]
23 accept_api_auth :index, :show, :create, :update, :destroy, :add_users, :remove_user
23 accept_api_auth :index, :show, :create, :update, :destroy, :add_users, :remove_user
24
24
25 require_sudo_mode :add_users, :remove_user, :create, :update, :destroy, :edit_membership, :destroy_membership
25 require_sudo_mode :add_users, :remove_user, :create, :update, :destroy, :edit_membership, :destroy_membership
26
26
27 helper :custom_fields
27 helper :custom_fields
28 helper :principal_memberships
28 helper :principal_memberships
29
29
30 def index
30 def index
31 respond_to do |format|
31 respond_to do |format|
32 format.html {
32 format.html {
33 scope = Group.sorted
33 scope = Group.sorted
34 scope = scope.like(params[:name]) if params[:name].present?
34 scope = scope.like(params[:name]) if params[:name].present?
35
35
36 @group_count = scope.count
36 @group_count = scope.count
37 @group_pages = Paginator.new @group_count, per_page_option, params['page']
37 @group_pages = Paginator.new @group_count, per_page_option, params['page']
38 @groups = scope.limit(@group_pages.per_page).offset(@group_pages.offset).to_a
38 @groups = scope.limit(@group_pages.per_page).offset(@group_pages.offset).to_a
39 @user_count_by_group_id = user_count_by_group_id
39 @user_count_by_group_id = user_count_by_group_id
40 }
40 }
41 format.api {
41 format.api {
42 scope = Group.sorted
42 scope = Group.sorted
43 scope = scope.givable unless params[:builtin] == '1'
43 scope = scope.givable unless params[:builtin] == '1'
44 @groups = scope.to_a
44 @groups = scope.to_a
45 }
45 }
46 end
46 end
47 end
47 end
48
48
49 def show
49 def show
50 respond_to do |format|
50 respond_to do |format|
51 format.html
51 format.html
52 format.api
52 format.api
53 end
53 end
54 end
54 end
55
55
56 def new
56 def new
57 @group = Group.new
57 @group = Group.new
58 end
58 end
59
59
60 def create
60 def create
61 @group = Group.new
61 @group = Group.new
62 @group.safe_attributes = params[:group]
62 @group.safe_attributes = params[:group]
63
63
64 respond_to do |format|
64 respond_to do |format|
65 if @group.save
65 if @group.save
66 format.html {
66 format.html {
67 flash[:notice] = l(:notice_successful_create)
67 flash[:notice] = l(:notice_successful_create)
68 redirect_to(params[:continue] ? new_group_path : groups_path)
68 redirect_to(params[:continue] ? new_group_path : groups_path)
69 }
69 }
70 format.api { render :action => 'show', :status => :created, :location => group_url(@group) }
70 format.api { render :action => 'show', :status => :created, :location => group_url(@group) }
71 else
71 else
72 format.html { render :action => "new" }
72 format.html { render :action => "new" }
73 format.api { render_validation_errors(@group) }
73 format.api { render_validation_errors(@group) }
74 end
74 end
75 end
75 end
76 end
76 end
77
77
78 def edit
78 def edit
79 end
79 end
80
80
81 def update
81 def update
82 @group.safe_attributes = params[:group]
82 @group.safe_attributes = params[:group]
83
83
84 respond_to do |format|
84 respond_to do |format|
85 if @group.save
85 if @group.save
86 flash[:notice] = l(:notice_successful_update)
86 flash[:notice] = l(:notice_successful_update)
87 format.html { redirect_to(groups_path) }
87 format.html { redirect_to_referer_or(groups_path) }
88 format.api { render_api_ok }
88 format.api { render_api_ok }
89 else
89 else
90 format.html { render :action => "edit" }
90 format.html { render :action => "edit" }
91 format.api { render_validation_errors(@group) }
91 format.api { render_validation_errors(@group) }
92 end
92 end
93 end
93 end
94 end
94 end
95
95
96 def destroy
96 def destroy
97 @group.destroy
97 @group.destroy
98
98
99 respond_to do |format|
99 respond_to do |format|
100 format.html { redirect_to(groups_path) }
100 format.html { redirect_to_referer_or(groups_path) }
101 format.api { render_api_ok }
101 format.api { render_api_ok }
102 end
102 end
103 end
103 end
104
104
105 def new_users
105 def new_users
106 end
106 end
107
107
108 def add_users
108 def add_users
109 @users = User.not_in_group(@group).where(:id => (params[:user_id] || params[:user_ids])).to_a
109 @users = User.not_in_group(@group).where(:id => (params[:user_id] || params[:user_ids])).to_a
110 @group.users << @users
110 @group.users << @users
111 respond_to do |format|
111 respond_to do |format|
112 format.html { redirect_to edit_group_path(@group, :tab => 'users') }
112 format.html { redirect_to edit_group_path(@group, :tab => 'users') }
113 format.js
113 format.js
114 format.api {
114 format.api {
115 if @users.any?
115 if @users.any?
116 render_api_ok
116 render_api_ok
117 else
117 else
118 render_api_errors "#{l(:label_user)} #{l('activerecord.errors.messages.invalid')}"
118 render_api_errors "#{l(:label_user)} #{l('activerecord.errors.messages.invalid')}"
119 end
119 end
120 }
120 }
121 end
121 end
122 end
122 end
123
123
124 def remove_user
124 def remove_user
125 @group.users.delete(User.find(params[:user_id])) if request.delete?
125 @group.users.delete(User.find(params[:user_id])) if request.delete?
126 respond_to do |format|
126 respond_to do |format|
127 format.html { redirect_to edit_group_path(@group, :tab => 'users') }
127 format.html { redirect_to edit_group_path(@group, :tab => 'users') }
128 format.js
128 format.js
129 format.api { render_api_ok }
129 format.api { render_api_ok }
130 end
130 end
131 end
131 end
132
132
133 def autocomplete_for_user
133 def autocomplete_for_user
134 respond_to do |format|
134 respond_to do |format|
135 format.js
135 format.js
136 end
136 end
137 end
137 end
138
138
139 private
139 private
140
140
141 def find_group
141 def find_group
142 @group = Group.find(params[:id])
142 @group = Group.find(params[:id])
143 rescue ActiveRecord::RecordNotFound
143 rescue ActiveRecord::RecordNotFound
144 render_404
144 render_404
145 end
145 end
146
146
147 def user_count_by_group_id
147 def user_count_by_group_id
148 h = User.joins(:groups).group('group_id').count
148 h = User.joins(:groups).group('group_id').count
149 h.keys.each do |key|
149 h.keys.each do |key|
150 h[key.to_i] = h.delete(key)
150 h[key.to_i] = h.delete(key)
151 end
151 end
152 h
152 h
153 end
153 end
154 end
154 end
General Comments 0
You need to be logged in to leave comments. Login now