To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / controllers / users_controller.rb @ 440:6253d777aa12
History | View | Annotate | Download (9.33 KB)
| 1 |
# Redmine - project management software
|
|---|---|
| 2 |
# Copyright (C) 2006-2009 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 |
|
| 23 |
helper :sort
|
| 24 |
include SortHelper
|
| 25 |
helper :custom_fields
|
| 26 |
include CustomFieldsHelper
|
| 27 |
|
| 28 |
def index |
| 29 |
sort_init 'login', 'asc' |
| 30 |
sort_update %w(login firstname lastname mail admin created_on last_login_on)
|
| 31 |
|
| 32 |
@status = params[:status] ? params[:status].to_i : 1 |
| 33 |
c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status]) |
| 34 |
|
| 35 |
unless params[:name].blank? |
| 36 |
name = "%#{params[:name].strip.downcase}%"
|
| 37 |
c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
|
| 38 |
end
|
| 39 |
|
| 40 |
@user_count = User.count(:conditions => c.conditions) |
| 41 |
@user_pages = Paginator.new self, @user_count, |
| 42 |
per_page_option, |
| 43 |
params['page']
|
| 44 |
@users = User.find :all,:order => sort_clause, |
| 45 |
:conditions => c.conditions,
|
| 46 |
:limit => @user_pages.items_per_page, |
| 47 |
:offset => @user_pages.current.offset |
| 48 |
|
| 49 |
render :layout => !request.xhr?
|
| 50 |
end
|
| 51 |
|
| 52 |
def show |
| 53 |
@user = User.find(params[:id]) |
| 54 |
|
| 55 |
if @user.ssamr_user_detail != nil |
| 56 |
@description = @user.ssamr_user_detail.description |
| 57 |
|
| 58 |
if @user.ssamr_user_detail.institution_type != nil |
| 59 |
# institution_type is true for listed institutions
|
| 60 |
if (@user.ssamr_user_detail.institution_type) |
| 61 |
@institution_name = Institution.find(@user.ssamr_user_detail.institution_id).name |
| 62 |
else
|
| 63 |
@institution_name = @user.ssamr_user_detail.other_institution |
| 64 |
end
|
| 65 |
end
|
| 66 |
end
|
| 67 |
|
| 68 |
# show projects based on current user visibility
|
| 69 |
@memberships = @user.memberships.all(:conditions => Project.visible_by(User.current)) |
| 70 |
|
| 71 |
events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) |
| 72 |
@events_by_day = events.group_by(&:event_date) |
| 73 |
|
| 74 |
unless User.current.admin? |
| 75 |
if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) |
| 76 |
render_404 |
| 77 |
return
|
| 78 |
end
|
| 79 |
end
|
| 80 |
render :layout => 'base' |
| 81 |
|
| 82 |
rescue ActiveRecord::RecordNotFound |
| 83 |
render_404 |
| 84 |
end
|
| 85 |
|
| 86 |
def new |
| 87 |
@notification_options = User::MAIL_NOTIFICATION_OPTIONS |
| 88 |
@notification_option = Setting.default_notification_option |
| 89 |
|
| 90 |
@user = User.new(:language => Setting.default_language) |
| 91 |
@auth_sources = AuthSource.find(:all) |
| 92 |
|
| 93 |
@ssamr_user_details = SsamrUserDetail.new |
| 94 |
end
|
| 95 |
|
| 96 |
verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } |
| 97 |
def create |
| 98 |
@notification_options = User::MAIL_NOTIFICATION_OPTIONS |
| 99 |
@notification_option = Setting.default_notification_option |
| 100 |
|
| 101 |
@user = User.new(params[:user]) |
| 102 |
@user.admin = params[:user][:admin] || false |
| 103 |
@user.login = params[:user][:login] |
| 104 |
@user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id |
| 105 |
|
| 106 |
# TODO: Similar to My#account
|
| 107 |
@user.mail_notification = params[:notification_option] || 'only_my_events' |
| 108 |
@user.pref.attributes = params[:pref] |
| 109 |
@user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
| 110 |
|
| 111 |
@ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details]) |
| 112 |
|
| 113 |
# associates the 2 objects
|
| 114 |
@user.ssamr_user_detail = @ssamr_user_details |
| 115 |
|
| 116 |
if @user.save |
| 117 |
@user.pref.save
|
| 118 |
|
| 119 |
@ssamr_user_details.save!
|
| 120 |
|
| 121 |
@user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : []) |
| 122 |
|
| 123 |
Mailer.deliver_account_information(@user, params[:password]) if params[:send_information] |
| 124 |
flash[:notice] = l(:notice_successful_create) |
| 125 |
redirect_to(params[:continue] ? {:controller => 'users', :action => 'new'} : |
| 126 |
{:controller => 'users', :action => 'edit', :id => @user})
|
| 127 |
return
|
| 128 |
else
|
| 129 |
@auth_sources = AuthSource.find(:all) |
| 130 |
@notification_option = @user.mail_notification |
| 131 |
|
| 132 |
render :action => 'new' |
| 133 |
end
|
| 134 |
end
|
| 135 |
|
| 136 |
def edit |
| 137 |
@user = User.find(params[:id]) |
| 138 |
@notification_options = @user.valid_notification_options |
| 139 |
@notification_option = @user.mail_notification |
| 140 |
|
| 141 |
@ssamr_user_details = @user.ssamr_user_detail |
| 142 |
|
| 143 |
if @user.ssamr_user_detail == nil |
| 144 |
@selected_institution_id = nil |
| 145 |
else
|
| 146 |
@selected_institution_id = @user.ssamr_user_detail.institution_id.to_i |
| 147 |
end
|
| 148 |
|
| 149 |
@auth_sources = AuthSource.find(:all) |
| 150 |
@membership ||= Member.new |
| 151 |
end
|
| 152 |
|
| 153 |
verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
| 154 |
def update |
| 155 |
@user = User.find(params[:id]) |
| 156 |
|
| 157 |
@notification_options = @user.valid_notification_options |
| 158 |
@notification_option = @user.mail_notification |
| 159 |
|
| 160 |
@user.admin = params[:user][:admin] if params[:user][:admin] |
| 161 |
@user.login = params[:user][:login] if params[:user][:login] |
| 162 |
if params[:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) |
| 163 |
@user.password, @user.password_confirmation = params[:password], params[:password_confirmation] |
| 164 |
end
|
| 165 |
@user.group_ids = params[:user][:group_ids] if params[:user][:group_ids] |
| 166 |
@user.attributes = params[:user] |
| 167 |
# Was the account actived ? (do it before User#save clears the change)
|
| 168 |
was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) |
| 169 |
# TODO: Similar to My#account
|
| 170 |
@user.mail_notification = params[:notification_option] || 'only_my_events' |
| 171 |
@user.pref.attributes = params[:pref] |
| 172 |
@user.pref[:no_self_notified] = (params[:no_self_notified] == '1') |
| 173 |
|
| 174 |
if @user.ssamr_user_detail == nil |
| 175 |
@ssamr_user_details = SsamrUserDetail.new() |
| 176 |
@user.ssamr_user_detail = @ssamr_user_details |
| 177 |
else
|
| 178 |
@ssamr_user_details = @user.ssamr_user_detail |
| 179 |
end
|
| 180 |
|
| 181 |
if params[:ssamr_user_details].nil? or params[:ssamr_user_details].empty? |
| 182 |
@ssamr_user_details.description = @user.ssamr_user_detail.description |
| 183 |
@ssamr_user_details.institution_id = @user.ssamr_user_detail.institution_id |
| 184 |
@ssamr_user_details.other_institution = @user.ssamr_user_detail.other_institution |
| 185 |
@ssamr_user_details.institution_type = @user.ssamr_user_detail.institution_type |
| 186 |
|
| 187 |
else
|
| 188 |
@ssamr_user_details.description = params[:ssamr_user_details][:description] |
| 189 |
@ssamr_user_details.institution_id = params[:ssamr_user_details][:institution_id] |
| 190 |
@ssamr_user_details.other_institution = params[:ssamr_user_details][:other_institution] |
| 191 |
@ssamr_user_details.institution_type = params[:ssamr_user_details][:institution_type] |
| 192 |
end
|
| 193 |
|
| 194 |
if @user.save |
| 195 |
@user.pref.save
|
| 196 |
@user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : []) |
| 197 |
|
| 198 |
if was_activated
|
| 199 |
Mailer.deliver_account_activated(@user) |
| 200 |
elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil? |
| 201 |
Mailer.deliver_account_information(@user, params[:password]) |
| 202 |
end
|
| 203 |
flash[:notice] = l(:notice_successful_update) |
| 204 |
redirect_to :back
|
| 205 |
else
|
| 206 |
@auth_sources = AuthSource.find(:all) |
| 207 |
@membership ||= Member.new |
| 208 |
|
| 209 |
render :action => :edit |
| 210 |
end
|
| 211 |
rescue ::ActionController::RedirectBackError |
| 212 |
redirect_to :controller => 'users', :action => 'edit', :id => @user |
| 213 |
end
|
| 214 |
|
| 215 |
def edit_membership |
| 216 |
@user = User.find(params[:id]) |
| 217 |
@membership = Member.edit_membership(params[:membership_id], params[:membership], @user) |
| 218 |
@membership.save if request.post? |
| 219 |
respond_to do |format|
|
| 220 |
if @membership.valid? |
| 221 |
format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
|
| 222 |
format.js {
|
| 223 |
render(:update) {|page|
|
| 224 |
page.replace_html "tab-content-memberships", :partial => 'users/memberships' |
| 225 |
page.visual_effect(:highlight, "member-#{@membership.id}") |
| 226 |
} |
| 227 |
} |
| 228 |
else
|
| 229 |
format.js {
|
| 230 |
render(:update) {|page|
|
| 231 |
page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', '))) |
| 232 |
} |
| 233 |
} |
| 234 |
end
|
| 235 |
end
|
| 236 |
end
|
| 237 |
|
| 238 |
def destroy_membership |
| 239 |
@user = User.find(params[:id]) |
| 240 |
@membership = Member.find(params[:membership_id]) |
| 241 |
if request.post? && @membership.deletable? |
| 242 |
@membership.destroy
|
| 243 |
end
|
| 244 |
respond_to do |format|
|
| 245 |
format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
|
| 246 |
format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
|
| 247 |
end
|
| 248 |
end
|
| 249 |
end
|