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

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

Related

How To Recover a Password with Devise ( Ruby on Rails)

I'm trying to recover a user's password with devise, but it generates the following error
undefined method `reset_password_sent_at=' for #<User:0x007fb78cfafb68>
Can anyone help me with this, since I'm new to Ruby on Rails?
What is the best way to recover a password and email the user using Devise? Thank you very much...
I'm use devise (2.2.3)
User.rb
require 'digest/md5'
class User < ActiveRecord::Base
# Setup accessible (or protected) attributes for your model
belongs_to :shop
before_create :compute_email_md5
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
:recoverable,
:rememberable,
:trackable,
:validatable,
:token_authenticatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email,
:email_md5,
:password,
:password_confirmation,
:shop_id,
:role,
:terms,
:name,
:notify_on_order_received
validates :terms, :acceptance => true, :on => :create
end
THE SOLUTION IS
add reset_password_sent_at column to user table
As you've discovered, passord recovery requires that the model have a reset_password_sent_at column. Adding it via migration should solve this problem.
As for the reason this is happening, I'm guessing you added password recovery (the :recoverable module) after initially generating your Devise-enabled model (User). That's why Devise's generator didn't create that column for you.

alias_attribute and devise causing stack level too deep error

I am running into some slightly tricky issues with a legacy db. Everything seems to work fine if I simply change the "password" column name in the db to "encrypted_password"; however, I need to leave the db in place.
So I decided to use
alias_attribute :encrypted_password, :password
Now I get a "stack level too deep" error in the console.
My user model:
class User < ActiveRecord::Base
require "digest/sha1"
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :encryptable, :encryptor => :old_cakephp_auth
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :events
before_create :add_default_values
#alias_attribute :created_at, :created
#alias_attribute :updated_at, :updated
alias_attribute :encrypted_password, :password
def add_default_values
self.created = Time.now
self.updated = Time.now
self.image = ""
self.name = self.email.split("#").first
#make normal user
self.role_id = 2
self.username = self.email.split("#").first + rand(100000000000000).to_s
self.website = ""
end
def valid_password?(password)
return false if encrypted_password.blank?
Devise.secure_compare(Digest::SHA1.hexdigest(self.password_salt+password), self.encrypted_password)
end
end
Ideas? Thanks!!! :)
I imagine that this is due to devise reserving the word password for their own use (and it in turn calling encrypted_password. Try renaming it to pword and see if the error still occurs. If it doesn't, you'll have to find another name to call the aliased password.
I should say that this is just an assumption. Let me know if it helps.

Devise official wiki for omniauth with facebook not working?

I followed the devise official wiki to integrate devise with facebook through omniauth.
I am able to login with my facebook account but somehow I am unable to save additional data from facebook's omniauth callback.
Note that I literally copied everything on this guide
Please look at my current user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,
:fb_raw
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token.extra.raw_info
if user = User.where(:email => data.email).first
user
else # Create a user with a stub password.
User.create!(:email => data.email, :password => Devise.friendly_token[0,20])
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && \
session["devise.facebook_data"]["extra"]["raw_info"]
# Here you can save all the info you want including networks and education
user.email = data["email"]
user.fb_raw = data['raw_info']
end
end
end
end
The problem here is that self.new_with_session is never invoked. I placed a throw exception clause right before super.tab to prove that my hypothesis.
Does anyone know a work around this?
The new_with_session is call on DeviseRegistrationController so it's not in OmniauthCallbacksController so it's normal to never be called during the login by Facebook.

Disabling validations when reseting passwords in Devise/Warden

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

Model attribute is not being set when using devise?

I used devise to create a user model. It contains all the default stuff from devise as well as 2 more database columns: first_name and last_name. Pretty simple stuff.
I am coding some tests to try it out:
#user = User.new(:first_name => "Ken", :email => 'myemail#gmail.com', :password => 'apassword')
#user.valid?
puts "user's first name: #{#user.first_name}"
puts #user.errors
In my model, I have this:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
validates_presence_of :first_name
end
For some reason, "first_name" is not getting set when I pass it in the constructor. The password and email are getting set, but not first_name.
Does anyone know why? I have played a bit with rails, and it works in other models. Why not with devise?
In your User model, write this:
attr_accessible :first_name, :last_name
and you are good to go.

Resources