You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

groups_controller.rb 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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. class GroupsController < ApplicationController
  18. layout 'admin'
  19. self.main_menu = false
  20. before_action :require_admin
  21. before_action :find_group, :except => [:index, :new, :create]
  22. accept_api_auth :index, :show, :create, :update, :destroy, :add_users, :remove_user
  23. require_sudo_mode :add_users, :remove_user, :create, :update, :destroy, :edit_membership, :destroy_membership
  24. helper :custom_fields
  25. helper :principal_memberships
  26. def index
  27. respond_to do |format|
  28. format.html {
  29. scope = Group.sorted
  30. scope = scope.like(params[:name]) if params[:name].present?
  31. @group_count = scope.count
  32. @group_pages = Paginator.new @group_count, per_page_option, params['page']
  33. @groups = scope.limit(@group_pages.per_page).offset(@group_pages.offset).to_a
  34. @user_count_by_group_id = user_count_by_group_id
  35. }
  36. format.api {
  37. scope = Group.sorted
  38. scope = scope.givable unless params[:builtin] == '1'
  39. @groups = scope.to_a
  40. }
  41. end
  42. end
  43. def show
  44. respond_to do |format|
  45. format.html
  46. format.api
  47. end
  48. end
  49. def new
  50. @group = Group.new
  51. end
  52. def create
  53. @group = Group.new
  54. @group.safe_attributes = params[:group]
  55. respond_to do |format|
  56. if @group.save
  57. format.html {
  58. flash[:notice] = l(:notice_successful_create)
  59. redirect_to(params[:continue] ? new_group_path : groups_path)
  60. }
  61. format.api { render :action => 'show', :status => :created, :location => group_url(@group) }
  62. else
  63. format.html { render :action => "new" }
  64. format.api { render_validation_errors(@group) }
  65. end
  66. end
  67. end
  68. def edit
  69. end
  70. def update
  71. @group.safe_attributes = params[:group]
  72. respond_to do |format|
  73. if @group.save
  74. flash[:notice] = l(:notice_successful_update)
  75. format.html { redirect_to_referer_or(groups_path) }
  76. format.api { render_api_ok }
  77. else
  78. format.html { render :action => "edit" }
  79. format.api { render_validation_errors(@group) }
  80. end
  81. end
  82. end
  83. def destroy
  84. @group.destroy
  85. respond_to do |format|
  86. format.html { redirect_to_referer_or(groups_path) }
  87. format.api { render_api_ok }
  88. end
  89. end
  90. def new_users
  91. end
  92. def add_users
  93. @users = User.not_in_group(@group).where(:id => (params[:user_id] || params[:user_ids])).to_a
  94. @group.users << @users
  95. respond_to do |format|
  96. format.html { redirect_to edit_group_path(@group, :tab => 'users') }
  97. format.js
  98. format.api {
  99. if @users.any?
  100. render_api_ok
  101. else
  102. render_api_errors "#{l(:label_user)} #{l('activerecord.errors.messages.invalid')}"
  103. end
  104. }
  105. end
  106. end
  107. def remove_user
  108. @group.users.delete(User.find(params[:user_id])) if request.delete?
  109. respond_to do |format|
  110. format.html { redirect_to edit_group_path(@group, :tab => 'users') }
  111. format.js
  112. format.api { render_api_ok }
  113. end
  114. end
  115. def autocomplete_for_user
  116. respond_to do |format|
  117. format.js
  118. end
  119. end
  120. private
  121. def find_group
  122. @group = Group.find(params[:id])
  123. rescue ActiveRecord::RecordNotFound
  124. render_404
  125. end
  126. def user_count_by_group_id
  127. h = User.joins(:groups).group('group_id').count
  128. h.keys.each do |key|
  129. h[key.to_i] = h.delete(key)
  130. end
  131. h
  132. end
  133. end