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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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
  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. respond_to do |format|
  46. format.html
  47. format.api
  48. end
  49. end
  50. def new
  51. @group = Group.new
  52. end
  53. def create
  54. @group = Group.new
  55. @group.safe_attributes = params[:group]
  56. respond_to do |format|
  57. if @group.save
  58. format.html do
  59. flash[:notice] = l(:notice_successful_create)
  60. redirect_to(params[:continue] ? new_group_path : groups_path)
  61. end
  62. format.api do
  63. render(:action => 'show', :status => :created,
  64. :location => group_url(@group))
  65. end
  66. else
  67. format.html {render :action => "new"}
  68. format.api {render_validation_errors(@group)}
  69. end
  70. end
  71. end
  72. def edit
  73. end
  74. def update
  75. @group.safe_attributes = params[:group]
  76. respond_to do |format|
  77. if @group.save
  78. flash[:notice] = l(:notice_successful_update)
  79. format.html {redirect_to_referer_or(groups_path)}
  80. format.api {render_api_ok}
  81. else
  82. format.html {render :action => "edit"}
  83. format.api {render_validation_errors(@group)}
  84. end
  85. end
  86. end
  87. def destroy
  88. @group.destroy
  89. respond_to do |format|
  90. format.html {redirect_to_referer_or(groups_path)}
  91. format.api {render_api_ok}
  92. end
  93. end
  94. def new_users
  95. end
  96. def add_users
  97. @users = User.not_in_group(@group).where(:id => (params[:user_id] || params[:user_ids])).to_a
  98. @group.users << @users
  99. respond_to do |format|
  100. format.html {redirect_to edit_group_path(@group, :tab => 'users')}
  101. format.js
  102. format.api do
  103. if @users.any?
  104. render_api_ok
  105. else
  106. render_api_errors "#{l(:label_user)} #{l('activerecord.errors.messages.invalid')}"
  107. end
  108. end
  109. end
  110. end
  111. def remove_user
  112. @group.users.delete(User.find(params[:user_id])) if request.delete?
  113. respond_to do |format|
  114. format.html {redirect_to edit_group_path(@group, :tab => 'users')}
  115. format.js
  116. format.api {render_api_ok}
  117. end
  118. end
  119. def autocomplete_for_user
  120. respond_to do |format|
  121. format.js
  122. end
  123. end
  124. private
  125. def find_group
  126. @group = Group.find(params[:id])
  127. rescue ActiveRecord::RecordNotFound
  128. render_404
  129. end
  130. def user_count_by_group_id
  131. h = User.joins(:groups).group('group_id').count
  132. h.keys.each do |key|
  133. h[key.to_i] = h.delete(key)
  134. end
  135. h
  136. end
  137. end