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.3KB

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