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 / .svn / pristine / c5 / c5ac85b1b901fa66f9ca32fcaa2fc8702a7953a0.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (7.12 KB)

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