Class: UserController

Inherits:
ApplicationController show all
Defined in:
app/controllers/user_controller.rb

Constant Summary

Constants included from Localization

Localization::LOCALIZED_STRINGS

Instance Method Summary collapse

Methods inherited from ApplicationController

in_place_edit_for, #initialize

Methods included from ApplicationHelper

#back_or_link_to, #detour?, #detour_to, #display_notice, #h, #image_button_to, #image_detour_to, #image_link_to, #image_link_to_remote, #insert, #record, #resolution_image, #t, #update_task, #with_detour

Methods included from Localization

#l, load_localized_strings, #valid_language?

Constructor Details

This class inherits a constructor from ApplicationController

Instance Method Details

#change_passwordObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/user_controller.rb', line 53

def change_password
  return if generate_filled_in
  params['user'].delete('form')
  begin
    @user.change_password(params['user']['password'], params['user']['password_confirmation'])
    @user.save!
    rescue Exception => ex
    report_exception ex
    flash.now[:notice] = 'Your password could not be changed at this time. Please retry.'
    render and return
  end
  begin
    UserNotify.deliver_change_password(@user, params['user']['password'])
    rescue Exception => ex
    report_exception ex
  end
  
end

#deleteObject



131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/user_controller.rb', line 131

def delete
  @user = current_user || User.find_by_id( session[:user_id] )
  begin
    @user.update_attribute( :deleted, true )
    logout
    rescue Exception => ex
    flash.now[:notice] = "Error: #{ex}."
    redirect_back_or_default :action => 'welcome'
  end
end

#editObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/user_controller.rb', line 106

def edit
  return if generate_filled_in
  if params['user']['form']
    form = params['user'].delete('form')
    begin
      case form
      when "edit"
        changeable_fields = ['first_name', 'last_name', 'email']
        @user.attributes = params['user'].delete_if { |k,v| not changeable_fields.include?(k) }
        @user.save
        flash.now['notice'] = "User has been updated."
      when "change_password"
        change_password
      when "delete"
        delete
      else
        raise "unknown edit action"
      end
      rescue Exception => ex
      logger.warn ex
      logger.warn ex.backtrace
    end
  end
end

#forgot_passwordObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/user_controller.rb', line 72

def forgot_password
  if authenticated_user?
    flash[:notice] = 'You are currently logged in. You may change your password now.'
    redirect_to :action => 'change_password'
    return
  end
  
  return if generate_blank_form
  
  if params['user']['email'].empty?
    flash.now[:notice] = 'Please enter a valid email address.'
  elsif (user = User.find_by_email(params['user']['email'])).nil?
    flash.now[:notice] = "We could not find a user with the email address #{CGI.escapeHTML(params['user']['email'])}"
  else
    begin
      User.transaction do
        key = user.generate_security_token
        url = url_for(:action => 'change_password')
        url += "?user[id]=#{user.id}&key=#{key}"
        UserNotify.deliver_forgot_password(user, url)
        flash['notice'] = "Instructions on resetting your password have been emailed to #{CGI.escapeHTML(params['user']['email'])}."
        unless authenticated_user?
          redirect_to :action => 'login'
          return
        end
        redirect_back_or_default :action => 'welcome'
      end
      rescue Exception => ex
      report_exception ex
      flash.now[:notice] = "Your password could not be emailed to #{CGI.escapeHTML(params['user']['email'])}"
    end
  end
end

#loginObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/user_controller.rb', line 4

def 
  return if generate_blank_form
  remember_me = params[:user].delete(:autologin)
  @user = User.new(params['user'])
  user = User.authenticate(params['user']['login'], params['user']['password'])
  if user
    self.current_user = user
    flash[:notice] = 'Login succeeded'
    if remember_me && remember_me == '1'
      user.generate_security_token
      cookies[:autologin] = {:value => user.id.to_s, :expires =>90.days.from_now}
      cookies[:token]     = {:value => user.security_token, :expires =>90.days.from_now}
    end
    back_or_redirect_to :controller => 'welcome', :action => :index
  else
    @login = params['user']['login']
    flash[:notice] = 'Login failed'
  end
end

#logoutObject



46
47
48
49
50
51
# File 'app/controllers/user_controller.rb', line 46

def logout
  session[:user_id] = nil
  self.current_user = nil
  cookies.delete :autologin
  redirect_to :action => 'login'
end

#set_groupObject



147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/user_controller.rb', line 147

def set_group
  @group = Group.find(params[:group_id])
  @user = User.find(params[:id])
  if params[:value] == 'true'
    @group.users << @user unless @group.users.include? @user 
  else
    @group.users.delete @user
  end
  @users = User.find(:all)
end

#signupObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/user_controller.rb', line 24

def 
  return if generate_blank_form
  params['user'].delete('form')
  @user = User.new(params['user'])
  begin
    User.transaction do
      @user.password_needs_confirmation = true
      if @user.save
        key = @user.generate_security_token
        url = url_for(:action => 'welcome')
        url += "?user[id]=#{@user.id}&key=#{key}"
        UserNotify.(@user, params['user']['password'], url)
        flash[:notice] = 'Signup successful! Please check your registered email account to verify your account registration and continue with the login.'
        redirect_to :action => 'login'
      end
    end
    rescue Exception => ex
    report_exception ex
    flash[:notice] = 'Error creating account: confirmation email not sent'
  end
end

#toggle_work_lock_monitoringObject



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/user_controller.rb', line 158

def toggle_work_lock_monitoring
  @user = User.find(params[:id])
  already_monitoring = @user.work_lock_subscribers.include? current_user
  if already_monitoring
    @user.work_lock_subscribers.delete current_user
    action = 'stopped'
  else
    @user.work_lock_subscribers << current_user
    action = 'started'
  end
  flash[:notice] = "Monitoring #{action}"
  UserNotify.deliver_monitoring(@user, current_user, action)
end

#welcomeObject



142
143
144
145
# File 'app/controllers/user_controller.rb', line 142

def welcome
  flash.keep
  back_or_redirect_to :controller => 'welcome'
end