Disabling validations when reseting passwords in Devise/Warden - ruby-on-rails

I have a user model as follows:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :invitable,
:recoverable, :rememberable, :trackable, :validatable,
:token_authenticatable, :omniauthable
validates_presence_of :nickname, :unless => :skip_nickname_requirement
end
I have a number of user records in the database with a nil nickname - those individuals were imported from another system and I don't actually have their nickname. Those users are invited to set their passwords via the lost passwords link like http://example.com/users/password/edit?reset_password_token=iAYeQRwWrt8geC8eEXR4, and then when they log in, add their personal details such as nickname, etc.
The problem is that when you go to that reset password you're prompted to enter your password (and again for confirmation). When you submit, validation fails because the nickname is nil.
How do I disable the nickname validation when reseting your password? I don't want to add the nickname textfield on the password reset form.
Thanks in advance for your thoughts!

I don't know if simple validates_presence_of :nickname, allow_blank: true would solve your problem, because you may want it to be blank only for those imported users. Therefore, other way would be to use custom validations.
# app/models/user.rb
class NicknameValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << "validation failed" unless :skip_nickname_requirement?(value)
end
private
def skip_nickname_requirement?(values)
# imported_from_legacy_system? would need to be implemented
value.present? || self.imported_from_legacy_system?
end
end
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :invitable,
:recoverable, :rememberable, :trackable, :validatable,
:token_authenticatable, :omniauthable
validates :nickname, :nickname => true
end

Related

"Validation Error: Password can't be blank" but no password field in form

I have a form to save information to a database for my website. Even though the form does not require a sign in, whenever I try to submit the form, I get an error "Validation failed: Password can't be blank". I'm not sure why this is happening as there is no password field in the form. Here is the code from the controller, the error is from the save line:
def create
#newinstructor = Instructor.new
#newinstructor_app = InstructorApp.where(first_name: params['instructor']['first_name']).where(last_name: params['instructor']['last_name'])
#newinstructor.first_name = params['instructor']['first_name']
#newinstructor.last_name = params['instructor']['last_name']
#newinstructor.story = params['instructor']['story']
#newinstructor.save!
Here is the instructor.rb:
class Instructor < ActiveRecord::Base
has_many :activities
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :activities
has_one :instructor_app
end
You should be able to fix, by following either of the following approaches:
Remove the :validatable option from your Instructor, devise option should now be:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable
OR
add the following method to your Instructor
def password_required?
false
end
Let me know if that helps
It looks like you have Devise modules like :database_authenticatable in your instructor model. You cant see it but Devise expects there to be a password field by default. If there's no login for your instructor form then one option is to simply delete the devise modules. Devise is generally used for the User model if you have one, to authenticate when a user initially signs up and/or logs in

Adding extra validations for Devise validatable

So currently validatable validates the presence of email and password. It can also validate an emails format. However, my user model requires more than just an email and password. I am also requiring a first, last and user name. So in order for me to validate the presence of these attributes I have to use rails validates as shown:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :first_name, presence: true
validates :last_name, presence: true
validates :user_name, presence: true
end
I was wondering if there was a way to add first, last and user name to the validatable action. I have checked the devise.rb file and found the validatable config for a password_length and email_regexp but don't quite know how I can add additional attributes to the validatable function. Obviously this isn't a huge deal, but would be nice for cleaning up code in my User's model. Thank you for any responses to my questions.
While you could potentially monkeypatch Devise::Models::Validatable at runtime it would be rather foolish. Its going to take 5 times more code and potentially break upgrades.
The whole point of the module is to provide models with the basic validations needed for Devise to work out of the box.
What you are adding are validations which are specific to your application. As such it belongs in your application - don't try to stuff it back into the library.
Instead can clean up your model by:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_presence_of :first_name, :last_name, :user_name
end

how to store ldap data in ruby on rails application database?

i have created a web app with rails4 and authentication system is developed with devise_ldap_authenticable gem.
where i am using username for login not email. but i want to store email in my users table.
My user model is
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
##attr_accessible :email, :password, :password_confirmation
# not required for LDAP :recoverable, :registerable, :validatable
devise :ldap_authenticatable, :rememberable, :trackable
validates_uniqueness_of :email, :allow_blank => true
before_save :get_ldap_email
def get_ldap_email
self.email = Devise::LDAP::Adapter.get_ldap_param(self.username, "mail")
end
end
But in users table of email field its storing data like
`email` = '--- !ruby/array:Net::BER::BerIdentifiedArray\ninternal:\n- !ruby/string:Net::BER::BerIdentifiedString\n str: !binary |-\n cy5naG9zaEBzYW1zdW5nLmNvbQ==\n ber_identifier: 4\nivars:\n :#ber_identifier: 49\n'
My log says
LDAP: Requested param mail has value ["s.ghosh#example.com"]
How i will store this value to my users table. where im doing wrong? please help me out.
Just need to add the following then its totally ok
def get_ldap_email
self.email = Devise::LDAP::Adapter.get_ldap_param(self.username, "mail").first
end

Get variable from another table in ROR models

I am trying to get a user who has not filled out their profile to redirect to the edit page.
I have two tables - Users (Devise handles) and Profiles.
Profiles has a row called profile_complete
In my user model I am trying to define a method called profile_complete which takes the boolean of profiles_complete and passes it back.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :profiles, :dependent => :destroy
def profile_complete?
self.profile.profile_complete == true
end
end
But cannot seem to figure out what the line in the profile_complete method is. I have got the other bits working in the correct place but cant get this variable across Any help? Cheers :)
def profile_complete?
self.attributes.values.include?(nil) ? false : true
end

Gem to create PublicKey and PrivateKey?

I'm searching for a Gem that will create a private and public key from a given string, that can then be stored in a database. Can anyone recommend such a Gem?
Devise allows this for a token via token_authenticable, I have not found a gem that does this however, so my models usually looks like:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :token_authenticatable, :confirmable
before_save :ensure_authentication_token
before_save :create_secret_token
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
private
def create_secret_token
self.secret_token = ActiveSupport::SecureRandom.base64(20).tr('+/=', '-_ ').strip.delete("\n") unless self.secret_token
end
end
The secret_token generator line ensures that the +/= characters are replaced and then whitespace is removed. "+/=" characters can be difficult to deal with when using RESTful APIs:
ActiveSupport::SecureRandom.base64(20).tr('+/=', '-_ ').strip.delete("\n")

Resources