Class: RailsConnector::DefaultUserController

Inherits:
ApplicationController
  • Object
show all
Includes:
Crm::Callbacks, ReCaptcha::AppHelper
Defined in:
app/controllers/rails_connector/default_user_controller.rb

Overview

This class provides a default controller implementation for user functionality. It should be customized by subclassing.

To change how all actions contacting the WebCRM behave in case of an WebCRM error, override on_crm_error in your subclassed controller. See Crm::Callbacks for details.

To override what attributes are writable by the user when registering or editing profiles, use editable_attributes_on_register and editable_attributes_on_edit, respectively. This can be done in your rails_connector.rb or in UserController directly.

By default, users can submit their first name, last name, email and company name.

Direct Known Subclasses

UserController

Instance Method Summary collapse

Methods included from Crm::Callbacks

#after_authenticate, #after_logout, #after_register, #before_authenticate, #before_logout, #before_register, #on_crm_error

Instance Method Details

#editObject

Lets the user change his/her user details.



133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/rails_connector/default_user_controller.rb', line 133

def edit
  @user = Infopark::Crm::Contact.find(current_user.id)
  if request.post? || request.put?
    @user.load(sanitize_user_params(params[:user], self.class.editable_attributes_on_edit))
    @user.save
    flash[:notice] = tcon('edit_successful')
    redirect_to(:action => 'profile')
  end
rescue ActiveResource::ResourceInvalid
  flash.now[:error] = tcon('edit_failed')
end

#edit_passwordObject

Lets the user change his/her password.

Validates the new password using validate_edit_password_params_for.



149
150
151
152
153
154
155
156
157
158
159
# File 'app/controllers/rails_connector/default_user_controller.rb', line 149

def edit_password
  if request.post?
    validate_edit_password_params_for(params[:user])
    @user = Infopark::Crm::Contact.authenticate(current_user., params[:user][:old_password])
    @user.password_set(params[:user][:new_password])
    flash[:notice] = tcon('edit_password_successful')
    redirect_to(:action => "profile")
  end
rescue ActiveResource::ResourceInvalid, Infopark::Crm::Errors::AuthenticationFailed
  flash.now[:error] = tcon('edit_password_failed')
end

#forgot_passwordObject

Lets the user request a new password (double opt-in).

Uses the ConfirmationMailer for sending out the confirmation message.



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/controllers/rails_connector/default_user_controller.rb', line 165

def forgot_password
  if request.post?
    user = Infopark::Crm::Contact.search(:params => {:login => params[:user][:login]}).first
    if user
      confirmation_link = set_password_url_for(user)
      ConfirmationMailer.reset_password(user.email, confirmation_link).deliver
      flash[:notice] = tcon('reset_password_successful')
      redirect_to(:action => "forgot_password")
    else
      flash.now[:error] = tcon('request_password_failed')
    end
  end
end

#loginObject

Logs a CRM user in.

After successful login, user attributes are stored in session[:user].

To change which fields are stored in the session use RailsConnector::Configuration.store_user_attrs_in_session.

Use current_user for a Contact object of the attributes stored in the session.

The user will be redirected to the path given in the return_to param. If no return_to param is set, the user will be redirected to the profile page.

If you merely want to change what happens before or after a user is authenticated, do not override this method but override before_authenticate or after_authenticate.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/rails_connector/default_user_controller.rb', line 67

def 
  if request.post?
    @user = Infopark::Crm::Contact.new(params[:user] || {:login => nil, :password => nil})
    before_authenticate
    @user = Infopark::Crm::Contact.authenticate(@user., @user.password)
    if @user
      after_authenticate
      flash[:notice] = tcon('login_successful')
      self.current_user = @user
      redirect_to params[:return_to].blank? ?
        user_path(:action => 'profile') :
        params[:return_to]
    else
      flash.now[:error] = tcon('login_failed')
    end
  end
rescue Infopark::Crm::Errors::AuthenticationFailed, ActiveResource::ResourceInvalid
  flash.now[:error] = tcon('login_failed')
ensure
  @user.password = nil if @user
end

#logoutObject

Logs the user out by setting session[:user] to nil.

To change the behavior before or after invalidating the session, override before_logout or after_logout.



94
95
96
97
98
99
# File 'app/controllers/rails_connector/default_user_controller.rb', line 94

def logout
  before_logout
  self.current_user = nil
  after_logout
  redirect_to params[:return_to].blank? ? root_path : params[:return_to]
end

#newObject

Creates a WebCRM user.

The user login is automatically set to his/her e-mail.

If you merely want to change what happens before or after a user is registered, do not override this method but override before_register or after_register.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/rails_connector/default_user_controller.rb', line 108

def new
  @user = Infopark::Crm::Contact.new
  # Load some default attributes so that form_for is working
  @user.load(Crm::CONTACT_DEFAULT_ATTRS.merge(sanitize_user_params(params[:user],
      self.class.editable_attributes_on_register)))
  if request.post?
    if RailsConnector::Configuration.use_recaptcha_on_user_registration &&
        !validate_recap(params, @user.errors)
      raise ActiveResource::ResourceInvalid, "captcha failed"
    end
    before_register
    register
    after_register
    redirect_to(:action => "register_pending")
  end
rescue ActiveResource::ResourceInvalid
  flash.now[:error] = tcon('registration_failed')
end

#profileObject

Displays a profile page containing links to all available actions



49
50
# File 'app/controllers/rails_connector/default_user_controller.rb', line 49

def profile
end

#register_pendingObject



128
129
# File 'app/controllers/rails_connector/default_user_controller.rb', line 128

def register_pending
end

#set_passwordObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/controllers/rails_connector/default_user_controller.rb', line 180

def set_password
  if request.get? && params[:token].blank?
    flash[:error] = tcon('token_url_invalid')
  elsif request.post?
    if params[:user][:new_password].blank?
      flash.now[:error] = tcon('password_cannot_be_empty')
    elsif params[:user][:new_password] != params[:user][:new_password_confirm]
      flash.now[:error] = tcon('password_does_not_match_confirmation')
    else
      Infopark::Crm::Contact.password_set(params[:user][:new_password], params[:user][:token])
      flash[:notice] = tcon('password_set')
      redirect_to(:action => 'login')
    end
  end
rescue ActiveResource::ResourceNotFound => e
  flash[:error] = tcon('set_password_failed')
end