Using custom authlogic error messages - ruby-on-rails

I am using the authlogic gem for user validation on one of my sites. All is going well, but I am wondering if it's possible to change the error message that gets returned when the user types in an invalid email address.
Thanks!

authlogic has a special setting for this purpose:
class UserSession < Authlogic::Session::Base
generalize_credentials_error_messages true
end
The error message will be the same: "Email/Password combination is not valid", whether the password or email is bad. You can change the text of the message specifying a string instead of true:
generalize_credentials_error_messages "Try again"

You can override the settings for email validation with validates_format_of_email_field_options. However, if you only want to change the message you can merge options with merge_validates_format_of_email_field_options so that only the options you specify are overridden. You specify settings in your User controller like so:
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.merge_validates_format_of_email_field_options :message => 'My message'
end
end
You can also change the settings for length and uniqueness validations. There are also a lot more other settings, take a look at the documentation, in the ::Config sections of each module you can find settings and their default values and how to override them.
Alternatively you can use localization and set error_messages.email_invalid (that's what the plugin looks for before setting it to the default English sentence, also useful if you are building an international application).

Override Authlogic error messages by changing in en.yml file
It works for me.
en:
authlogic:
error_messages:
login_blank: "Please enter the email address."
login_not_found: "This email address is already in the system. Please choose a different email address."
login_invalid: "Please enter a valid email address."

Related

Prohibit passwords from being the same as the account ID or user ID [Ruby on Rails]

I have a password policy need to apply at my ROR application.
Prohibit passwords from being the same as the account ID or user ID.
For example: System will prompt error if I use the wording contain of 'alex' if my username is
alex#google.com
Use custom validation, in your User model
validate :password_content
def password_content
self.errors.add(:password, "cannot contain username") if password.include?(user_name[/[^#]+/])
end
It will throw a validation error if password contains user_name. Hope this helps!
You can make use of include?
if password.inlcude? user_name[/[^#]+/]
do something
end

Custom Form Validation in Django-Allauth

I want to do some extra validation on fields in django-allauth. For example I want to prevent using free email addresses. So I want to run this method on signup
def clean_email(self):
email_domain = self.cleaned_data['email'].split('#')[1]
if email_domain in self.bad_domains:
raise forms.ValidationError(_("Registration using free email addresses is prohibited. Please supply a different email address."))
Similarly I want to run custom validation on different fields other than email address. How can I perform this?
There are some adapters on the allauth configuration. For example this one:
ACCOUNT_ADAPTER (="allauth.account.adapter.DefaultAccountAdapter")
Specifies the adapter class to use, allowing you to alter certain default behaviour.
You can specify a new adapter by overriding the default one. Just override the clean_email method.
class MyCoolAdapter(DefaultAccountAdapter):
def clean_email(self, email):
"""
Validates an email value. You can hook into this if you want to
(dynamically) restrict what email addresses can be chosen.
"""
*** here goes your code ***
return email
Then modify the ACCOUNT_ADAPTER on the settings.py
ACCOUNT_ADAPTER = '**app**.MyCoolAdapter'
Check the default behavior on:
https://github.com/pennersr/django-allauth/blob/master/allauth/account/adapter.py

Don't allow a user to change email address using devise with Rails

I'm using devise to allow users to log in to a site. The authentication key is set to a username, which is to be unique. Also to be unique is the email address provided. It seems that somehow devise has already figured out that the email address should be unique. So that's good.
Now I want to let people change their passwords. I link over to my edit_user_registration_path, but notice that the user is allowed to change their email address. One option is to set reconfirmable to false... but I don't think I want to allow users to change their email addresses at all.
I think I could just remove the field from the devise view, but theoretically a carefully crafted PUT method could still let them change their email address. Is there a way to stop this field from being mutable? Or is it better to just let the email address be reconfirmable?
See http://trak3r.blogspot.com/2007/03/immutable-activerecord-attributes.html
So
class User
def email=(address)
if new_record?
write_attribute(:email, address)
else
raise 'email is immutable!'
end
end
end
In my case, the "answer" was to simply allow people to edit their email addresses. There are enough use cases in which somebody could legitimately want to change their email address that I figured there was no harm in trying to stop it.

How check if an email exist without send in Ruby? Use of email_veracity_checker

I'm trying to create a ruby script that checks the real existence of some e-mail.
I've found email_veracity_checker but I don't understand how implement a simple test.
The "Readme" file only says the following:
# Add following entry in your config/environment.rb
config.gem 'email_veracity_checker', :lib => "email_check"
How to use:
#first param is user email address
#second sender address
#third param is domain address
#Note: It's not sending email, at the end point they quit connection.
EmailCheck.run("kiran#joshsoftware.com","no-reply#joshsoftware.com","joshsoftware.com").valid?
It's return can be true or false"
Can someone help me?
Thanks
What have you tried? It looks pretty simple.
What it does is setting up an SMTP-connection, just like you do when you send a mail. At a certain point, in an SMTP-connection, your server says something like this:
I'ld like to send a mail to ...#....com
Most of the servers will return an error when this address doesn't exist. After this, a normal SMTP-connection would be free to send the actual message body. This class, however, closes the connection at this point, so no mail will be sent.
However, before sending the quoted message, it needs to specify the sender's mail. So that's why you need to specify it. Besides that, you also need to define the owner's mail domain, because it's needed for the EHLO payload.
What about doing something like this (I'm using mongo_mapper, so this may be different in your situation):
class User
include MongoMapper::document
...
key mail, String, :required => true
...
validate :validate_mail
...
def validate_mail
if !EmailCheck.run(self.mail, "no-reply#yourdomain.com", self.mail.split('#')[1]).valid?
errors.add :mail, "is invalid."
end
end
end
If you need it for a ruby script, you can do something like this. Just require the email check found on lib of that gem.
require 'email_check.rb'
is_valid = EmailCheck.run("kiran#joshsoftware.com", "no-reply#joshsoftware.com", "joshsoftware.com").valid?
puts "is valid: #{is_valid}"

How to override the Devise Passwords Controller?

How to override the Devise Passwords Controller:
https://github.com/plataformatec/devise/blob/master/app/controllers/devise/passwords_controller.rb
I want to do the following:
downcase all emails submitted for password reset. All the emails in the DB as lowercase. If a user tried to reset a valid email with any characters uppercase, the reset fails to find a user. And devise doesn't even give a error message saying no user found
If no user is found in def create, I want to add a flash that says, no user found, did you enter the right email?
How can I accomplish the 2 items above? I believe that required overriding the devise password controller. How do I do that? Or if you have a better solution that's even cleaner, I would like to hear it.
Thanks
the devise initializer has a option to make any field case insensitive:
config.case_insensitive_keys = [ :email ]
If I remember correctly it was added in the newer version and if you don't see some related comments in you initializer then you should upgrade your devise gem using bundle upgrade devise. I am using version 1.3.3.
And this version also shows an error "Email not found" if an invalid email is entered.
If you not getting the error message add <%= devise_error_messages! %> to your view. You can customize the error messages by editing config/locales/devise.en.yml

Resources