PageRenderTime 1412ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/admin/users_controller.rb

http://github.com/fatfreecrm/fat_free_crm
Ruby | 153 lines | 87 code | 28 blank | 38 comment | 12 complexity | bd375f1ce06788b0e7591263304d2d95 MD5 | raw file
Possible License(s): AGPL-3.0
  1. # frozen_string_literal: true
  2. # Copyright (c) 2008-2013 Michael Dvorkin and contributors.
  3. #
  4. # Fat Free CRM is freely distributable under the terms of MIT license.
  5. # See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
  6. #------------------------------------------------------------------------------
  7. class Admin::UsersController < Admin::ApplicationController
  8. before_action :setup_current_tab, only: %i[index show]
  9. load_resource except: [:create]
  10. # GET /admin/users
  11. # GET /admin/users.xml HTML
  12. #----------------------------------------------------------------------------
  13. def index
  14. @users = get_users(page: params[:page])
  15. respond_with(@users)
  16. end
  17. # GET /admin/users/1
  18. # GET /admin/users/1.xml
  19. #----------------------------------------------------------------------------
  20. def show
  21. respond_with(@user)
  22. end
  23. # GET /admin/users/new
  24. # GET /admin/users/new.xml AJAX
  25. #----------------------------------------------------------------------------
  26. def new
  27. respond_with(@user)
  28. end
  29. # GET /admin/users/1/edit AJAX
  30. #----------------------------------------------------------------------------
  31. def edit
  32. @previous = User.find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
  33. respond_with(@user)
  34. end
  35. # POST /admin/users
  36. # POST /admin/users.xml AJAX
  37. #----------------------------------------------------------------------------
  38. def create
  39. @user = User.new(user_params)
  40. @user.suspend_if_needs_approval
  41. @user.save
  42. respond_with(@user)
  43. end
  44. # PUT /admin/users/1
  45. # PUT /admin/users/1.xml AJAX
  46. #----------------------------------------------------------------------------
  47. def update
  48. @user = User.find(params[:id])
  49. @user.attributes = user_params
  50. @user.save
  51. respond_with(@user)
  52. end
  53. # GET /admin/users/1/confirm AJAX
  54. #----------------------------------------------------------------------------
  55. def confirm
  56. respond_with(@user)
  57. end
  58. # DELETE /admin/users/1
  59. # DELETE /admin/users/1.xml AJAX
  60. #----------------------------------------------------------------------------
  61. def destroy
  62. flash[:warning] = t(:msg_cant_delete_user, @user.full_name) unless @user.destroyable?(current_user) && @user.destroy
  63. respond_with(@user)
  64. end
  65. # POST /users/auto_complete/query AJAX
  66. #----------------------------------------------------------------------------
  67. # Handled by Admin::ApplicationController :auto_complete
  68. # PUT /admin/users/1/suspend
  69. # PUT /admin/users/1/suspend.xml AJAX
  70. #----------------------------------------------------------------------------
  71. def suspend
  72. @user.update_attribute(:suspended_at, Time.now) if @user != current_user
  73. respond_with(@user)
  74. end
  75. # PUT /admin/users/1/reactivate
  76. # PUT /admin/users/1/reactivate.xml AJAX
  77. #----------------------------------------------------------------------------
  78. def reactivate
  79. @user.update_attribute(:suspended_at, nil)
  80. respond_with(@user)
  81. end
  82. protected
  83. def user_params
  84. return {} unless params[:user]
  85. params[:user][:password_confirmation] = nil if params[:user][:password_confirmation].blank?
  86. params[:user][:email].try(:strip!)
  87. params[:user][:alt_email].try(:strip!)
  88. params[:user].permit(
  89. :admin,
  90. :username,
  91. :email,
  92. :first_name,
  93. :last_name,
  94. :title,
  95. :company,
  96. :alt_email,
  97. :phone,
  98. :mobile,
  99. :aim,
  100. :yahoo,
  101. :google,
  102. :skype,
  103. :password,
  104. :password_confirmation,
  105. group_ids: []
  106. )
  107. end
  108. private
  109. #----------------------------------------------------------------------------
  110. def get_users(options = {})
  111. self.current_page = options[:page] if options[:page]
  112. self.current_query = params[:query] if params[:query]
  113. @search = klass.ransack(params[:q])
  114. @search.build_grouping unless @search.groupings.any?
  115. wants = request.format
  116. scope = User.by_id
  117. scope = scope.merge(@search.result)
  118. scope = scope.text_search(current_query) if current_query.present?
  119. scope = scope.paginate(page: current_page) if wants.html? || wants.js? || wants.xml?
  120. scope
  121. end
  122. def setup_current_tab
  123. set_current_tab('admin/users')
  124. end
  125. end