Commit 0623ae19 authored by Michel Felipe de Oliveira Ferreira's avatar Michel Felipe de Oliveira Ferreira
Browse files

Refactory to new feature that show a specific message when a user inactive try login

parent 23a10a5f
......@@ -46,8 +46,12 @@ class AccountController < ApplicationController
self.current_user = plugins_alternative_authentication
self.current_user ||= User.authenticate(params[:user][:login], params[:user][:password], environment) if params[:user]
begin
self.current_user ||= User.authenticate(params[:user][:login], params[:user][:password], environment) if params[:user]
rescue NoosferoExceptions::UserInactive => e
session[:notice] = e.message
return
end
if logged_in?
check_join_in_community(self.current_user)
......
......@@ -120,11 +120,17 @@ class User < ActiveRecord::Base
validates_inclusion_of :terms_accepted, :in => [ '1' ], :if => lambda { |u| ! u.terms_of_use.blank? }, :message => N_('{fn} must be checked in order to signup.').fix_i18n
scope :has_login?, lambda { |login,email,environment_id|
where('login = ? OR email = ?', login, email).
where(environment_id: environment_id)
}
# Authenticates a user by their login name or email and unencrypted password. Returns the user or nil.
def self.authenticate(login, password, environment = nil)
environment ||= Environment.default
u = self.first :conditions => ['(login = ? OR email = ?) AND environment_id = ? AND activated_at IS NOT NULL',
login, login, environment.id] # need to get the salt
u = self.has_login?(login, login, environment.id)
u = u.first if u.is_a?(ActiveRecord::Relation)
u && u.authenticated?(password) ? u : nil
end
......@@ -237,6 +243,12 @@ class User < ActiveRecord::Base
end
def authenticated?(password)
unless self.activated?
message = _('The user "%{login}" is not active!') % {login: self.login}
raise NoosferoExceptions::UserInactive.new(message, self)
end
result = (crypted_password == encrypt(password))
if (encryption_method != User.system_encryption_method) && result
self.password_type = User.system_encryption_method.to_s
......@@ -275,8 +287,14 @@ class User < ActiveRecord::Base
# current password.
# * Saves the record unless it is a new one.
def change_password!(current, new, confirmation)
unless self.authenticated?(current)
self.errors.add(:current_password, _('does not match.'))
begin
unless self.authenticated?(current)
self.errors.add(:current_password, _('does not match.'))
raise IncorrectPassword
end
rescue NoosferoExceptions::UserInactive => e
self.errors.add(:current_password, e.message)
raise IncorrectPassword
end
self.force_change_password!(new, confirmation)
......@@ -393,3 +411,15 @@ class User < ActiveRecord::Base
Delayed::Job.enqueue(UserActivationJob.new(self.id), {:priority => 0, :run_at => (NOOSFERO_CONF['hours_until_user_activation_check'] || 72).hours.from_now})
end
end
module NoosferoExceptions
class UserInactive < ActiveRecord::ActiveRecordError
attr_reader :user
def initialize(message, user = nil)
@user = user
super(message)
end
end
end
......@@ -40,6 +40,14 @@ class AccountControllerTest < ActionController::TestCase
post :login, :user => { :login => 'fake', :password => 'fake' }
end
should 'fail login if a user is inactive and show a warning message' do
user = User.create!(login: 'testuser', email: 'test@email.com', password:'test', password_confirmation:'test', activation_code: nil)
post :login, :user => { :login => 'testuser', :password => 'test' }
assert_match 'not active', session[:notice]
assert_nil session[:user]
end
def test_should_fail_login_and_not_redirect
@request.env["HTTP_REFERER"] = 'bli'
post :login, :user => {:login => 'johndoe', :password => 'bad password'}
......@@ -273,8 +281,9 @@ class AccountControllerTest < ActionController::TestCase
assert_template 'invalid_change_password_code'
end
should 'require password confirmation correctly to enter new pasword' do
should 'require password confirmation correctly to enter new password' do
user = create_user('testuser', :email => 'testuser@example.com', :password => 'test', :password_confirmation => 'test')
user.activate
change = ChangePassword.create!(:requestor => user.person)
post :new_password, :code => change.code, :change_password => { :password => 'onepass', :password_confirmation => 'another_pass' }
......@@ -749,6 +758,8 @@ class AccountControllerTest < ActionController::TestCase
get :activate
assert_nil assigns(:message)
post :login, :user => {:login => 'testuser', :password => 'test123'}
assert_match 'not active', session[:notice]
assert_nil session[:user]
end
......@@ -758,6 +769,8 @@ class AccountControllerTest < ActionController::TestCase
get :activate, :activation_code => 'wrongcode'
assert_nil assigns(:message)
post :login, :user => {:login => 'testuser', :password => 'test123'}
assert_match 'not active', session[:notice]
assert_nil session[:user]
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment