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.

users_controller.rb 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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 UsersController < ApplicationController
  18. layout 'admin'
  19. before_filter :require_admin, :except => :show
  20. before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership]
  21. accept_api_auth :index, :show, :create, :update, :destroy
  22. helper :sort
  23. include SortHelper
  24. helper :custom_fields
  25. include CustomFieldsHelper
  26. def index
  27. sort_init 'login', 'asc'
  28. sort_update %w(login firstname lastname mail admin created_on last_login_on)
  29. case params[:format]
  30. when 'xml', 'json'
  31. @offset, @limit = api_offset_and_limit
  32. else
  33. @limit = per_page_option
  34. end
  35. @status = params[:status] || 1
  36. scope = User.logged.status(@status)
  37. scope = scope.like(params[:name]) if params[:name].present?
  38. scope = scope.in_group(params[:group_id]) if params[:group_id].present?
  39. @user_count = scope.count
  40. @user_pages = Paginator.new @user_count, @limit, params['page']
  41. @offset ||= @user_pages.offset
  42. @users = scope.order(sort_clause).limit(@limit).offset(@offset).all
  43. respond_to do |format|
  44. format.html {
  45. @groups = Group.all.sort
  46. render :layout => !request.xhr?
  47. }
  48. format.api
  49. end
  50. end
  51. def show
  52. # show projects based on current user visibility
  53. @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current))
  54. events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
  55. @events_by_day = events.group_by(&:event_date)
  56. unless User.current.admin?
  57. if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?)
  58. render_404
  59. return
  60. end
  61. end
  62. respond_to do |format|
  63. format.html { render :layout => 'base' }
  64. format.api
  65. end
  66. end
  67. def new
  68. @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
  69. @auth_sources = AuthSource.all
  70. end
  71. def create
  72. @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
  73. @user.safe_attributes = params[:user]
  74. @user.admin = params[:user][:admin] || false
  75. @user.login = params[:user][:login]
  76. @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
  77. if @user.save
  78. @user.pref.attributes = params[:pref]
  79. @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
  80. @user.pref.save
  81. @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
  82. Mailer.account_information(@user, params[:user][:password]).deliver if params[:send_information]
  83. respond_to do |format|
  84. format.html {
  85. flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user)))
  86. if params[:continue]
  87. redirect_to new_user_path
  88. else
  89. redirect_to edit_user_path(@user)
  90. end
  91. }
  92. format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
  93. end
  94. else
  95. @auth_sources = AuthSource.all
  96. # Clear password input
  97. @user.password = @user.password_confirmation = nil
  98. respond_to do |format|
  99. format.html { render :action => 'new' }
  100. format.api { render_validation_errors(@user) }
  101. end
  102. end
  103. end
  104. def edit
  105. @auth_sources = AuthSource.all
  106. @membership ||= Member.new
  107. end
  108. def update
  109. @user.admin = params[:user][:admin] if params[:user][:admin]
  110. @user.login = params[:user][:login] if params[:user][:login]
  111. if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
  112. @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
  113. end
  114. @user.safe_attributes = params[:user]
  115. # Was the account actived ? (do it before User#save clears the change)
  116. was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
  117. # TODO: Similar to My#account
  118. @user.pref.attributes = params[:pref]
  119. @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
  120. if @user.save
  121. @user.pref.save
  122. @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
  123. if was_activated
  124. Mailer.account_activated(@user).deliver
  125. elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil?
  126. Mailer.account_information(@user, params[:user][:password]).deliver
  127. end
  128. respond_to do |format|
  129. format.html {
  130. flash[:notice] = l(:notice_successful_update)
  131. redirect_to_referer_or edit_user_path(@user)
  132. }
  133. format.api { render_api_ok }
  134. end
  135. else
  136. @auth_sources = AuthSource.all
  137. @membership ||= Member.new
  138. # Clear password input
  139. @user.password = @user.password_confirmation = nil
  140. respond_to do |format|
  141. format.html { render :action => :edit }
  142. format.api { render_validation_errors(@user) }
  143. end
  144. end
  145. end
  146. def destroy
  147. @user.destroy
  148. respond_to do |format|
  149. format.html { redirect_back_or_default(users_path) }
  150. format.api { render_api_ok }
  151. end
  152. end
  153. def edit_membership
  154. @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
  155. @membership.save
  156. respond_to do |format|
  157. format.html { redirect_to edit_user_path(@user, :tab => 'memberships') }
  158. format.js
  159. end
  160. end
  161. def destroy_membership
  162. @membership = Member.find(params[:membership_id])
  163. if @membership.deletable?
  164. @membership.destroy
  165. end
  166. respond_to do |format|
  167. format.html { redirect_to edit_user_path(@user, :tab => 'memberships') }
  168. format.js
  169. end
  170. end
  171. private
  172. def find_user
  173. if params[:id] == 'current'
  174. require_login || return
  175. @user = User.current
  176. else
  177. @user = User.find(params[:id])
  178. end
  179. rescue ActiveRecord::RecordNotFound
  180. render_404
  181. end
  182. end