To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / .svn / text-base / users_controller.rb.svn-base @ 442:753f1380d6bc

History | View | Annotate | Download (8.44 KB)

1 0:513646585e45 Chris
# Redmine - project management software
2 441:cbce1fd3b1b7 Chris
# Copyright (C) 2006-2011  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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
18
class UsersController < ApplicationController
19
  layout 'admin'
20
21
  before_filter :require_admin, :except => :show
22 128:07fa8a8b56a8 Chris
  before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership]
23
  accept_key_auth :index, :show, :create, :update, :destroy
24 0:513646585e45 Chris
25
  helper :sort
26
  include SortHelper
27
  helper :custom_fields
28
  include CustomFieldsHelper
29
30
  def index
31
    sort_init 'login', 'asc'
32
    sort_update %w(login firstname lastname mail admin created_on last_login_on)
33
34 119:8661b858af72 Chris
    case params[:format]
35
    when 'xml', 'json'
36
      @offset, @limit = api_offset_and_limit
37
    else
38
      @limit = per_page_option
39
    end
40
41 441:cbce1fd3b1b7 Chris
    scope = User
42
    scope = scope.in_group(params[:group_id].to_i) if params[:group_id].present?
43
44 0:513646585e45 Chris
    @status = params[:status] ? params[:status].to_i : 1
45
    c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
46
47
    unless params[:name].blank?
48
      name = "%#{params[:name].strip.downcase}%"
49
      c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
50
    end
51
52 441:cbce1fd3b1b7 Chris
    @user_count = scope.count(:conditions => c.conditions)
53 119:8661b858af72 Chris
    @user_pages = Paginator.new self, @user_count, @limit, params['page']
54
    @offset ||= @user_pages.current.offset
55 441:cbce1fd3b1b7 Chris
    @users =  scope.find :all,
56 119:8661b858af72 Chris
                        :order => sort_clause,
57 0:513646585e45 Chris
                        :conditions => c.conditions,
58 119:8661b858af72 Chris
                        :limit  =>  @limit,
59
                        :offset =>  @offset
60 0:513646585e45 Chris
61 441:cbce1fd3b1b7 Chris
    respond_to do |format|
62
      format.html {
63
        @groups = Group.all.sort
64
        render :layout => !request.xhr?
65
      }
66 119:8661b858af72 Chris
      format.api
67 441:cbce1fd3b1b7 Chris
    end
68 0:513646585e45 Chris
  end
69
70
  def show
71 14:1d32c0a0efbf Chris
    # show projects based on current user visibility
72 441:cbce1fd3b1b7 Chris
    @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current))
73 0:513646585e45 Chris
74
    events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
75
    @events_by_day = events.group_by(&:event_date)
76
77
    unless User.current.admin?
78
      if !@user.active? || (@user != User.current  && @memberships.empty? && events.empty?)
79
        render_404
80
        return
81
      end
82
    end
83 119:8661b858af72 Chris
84
    respond_to do |format|
85
      format.html { render :layout => 'base' }
86
      format.api
87
    end
88 0:513646585e45 Chris
  end
89
90 37:94944d00e43c chris
  def new
91 119:8661b858af72 Chris
    @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
92 37:94944d00e43c chris
    @auth_sources = AuthSource.find(:all)
93
  end
94
95
  verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
96
  def create
97 119:8661b858af72 Chris
    @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
98
    @user.safe_attributes = params[:user]
99 37:94944d00e43c chris
    @user.admin = params[:user][:admin] || false
100
    @user.login = params[:user][:login]
101 119:8661b858af72 Chris
    @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
102 37:94944d00e43c chris
103
    # TODO: Similar to My#account
104
    @user.pref.attributes = params[:pref]
105
    @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
106
107
    if @user.save
108
      @user.pref.save
109 119:8661b858af72 Chris
      @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
110 37:94944d00e43c chris
111 119:8661b858af72 Chris
      Mailer.deliver_account_information(@user, params[:user][:password]) if params[:send_information]
112
113
      respond_to do |format|
114
        format.html {
115
          flash[:notice] = l(:notice_successful_create)
116
          redirect_to(params[:continue] ?
117
            {:controller => 'users', :action => 'new'} :
118
            {:controller => 'users', :action => 'edit', :id => @user}
119
          )
120
        }
121
        format.api  { render :action => 'show', :status => :created, :location => user_url(@user) }
122
      end
123 0:513646585e45 Chris
    else
124 37:94944d00e43c chris
      @auth_sources = AuthSource.find(:all)
125 119:8661b858af72 Chris
      # Clear password input
126
      @user.password = @user.password_confirmation = nil
127 37:94944d00e43c chris
128 119:8661b858af72 Chris
      respond_to do |format|
129
        format.html { render :action => 'new' }
130
        format.api  { render_validation_errors(@user) }
131
      end
132 0:513646585e45 Chris
    end
133
  end
134
135
  def edit
136
    @auth_sources = AuthSource.find(:all)
137
    @membership ||= Member.new
138 37:94944d00e43c chris
  end
139
140
  verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
141
  def update
142
    @user.admin = params[:user][:admin] if params[:user][:admin]
143
    @user.login = params[:user][:login] if params[:user][:login]
144 119:8661b858af72 Chris
    if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
145
      @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
146 37:94944d00e43c chris
    end
147 119:8661b858af72 Chris
    @user.safe_attributes = params[:user]
148 37:94944d00e43c chris
    # Was the account actived ? (do it before User#save clears the change)
149
    was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
150
    # TODO: Similar to My#account
151
    @user.pref.attributes = params[:pref]
152
    @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
153
154
    if @user.save
155
      @user.pref.save
156 119:8661b858af72 Chris
      @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
157 37:94944d00e43c chris
158
      if was_activated
159
        Mailer.deliver_account_activated(@user)
160 119:8661b858af72 Chris
      elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil?
161
        Mailer.deliver_account_information(@user, params[:user][:password])
162 37:94944d00e43c chris
      end
163 119:8661b858af72 Chris
164
      respond_to do |format|
165
        format.html {
166
          flash[:notice] = l(:notice_successful_update)
167
          redirect_to :back
168
        }
169
        format.api  { head :ok }
170
      end
171 37:94944d00e43c chris
    else
172
      @auth_sources = AuthSource.find(:all)
173
      @membership ||= Member.new
174 119:8661b858af72 Chris
      # Clear password input
175
      @user.password = @user.password_confirmation = nil
176 37:94944d00e43c chris
177 119:8661b858af72 Chris
      respond_to do |format|
178
        format.html { render :action => :edit }
179
        format.api  { render_validation_errors(@user) }
180
      end
181 37:94944d00e43c chris
    end
182 0:513646585e45 Chris
  rescue ::ActionController::RedirectBackError
183
    redirect_to :controller => 'users', :action => 'edit', :id => @user
184
  end
185 37:94944d00e43c chris
186 128:07fa8a8b56a8 Chris
  verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
187
  def destroy
188
    @user.destroy
189
    respond_to do |format|
190
      format.html { redirect_to(users_url) }
191
      format.api  { head :ok }
192
    end
193
  end
194
195 0:513646585e45 Chris
  def edit_membership
196
    @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
197
    @membership.save if request.post?
198
    respond_to do |format|
199 14:1d32c0a0efbf Chris
      if @membership.valid?
200
        format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
201
        format.js {
202
          render(:update) {|page|
203
            page.replace_html "tab-content-memberships", :partial => 'users/memberships'
204
            page.visual_effect(:highlight, "member-#{@membership.id}")
205
          }
206
        }
207
      else
208
        format.js {
209
          render(:update) {|page|
210
            page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
211
          }
212
        }
213
      end
214
    end
215 0:513646585e45 Chris
  end
216
217
  def destroy_membership
218
    @membership = Member.find(params[:membership_id])
219
    if request.post? && @membership.deletable?
220
      @membership.destroy
221
    end
222
    respond_to do |format|
223
      format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
224
      format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
225
    end
226
  end
227 119:8661b858af72 Chris
228
  private
229
230
  def find_user
231
    if params[:id] == 'current'
232
      require_login || return
233
      @user = User.current
234
    else
235
      @user = User.find(params[:id])
236
    end
237
  rescue ActiveRecord::RecordNotFound
238
    render_404
239
  end
240 0:513646585e45 Chris
end