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

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