Created
March 3, 2012 16:53
-
-
Save johnrails/1966956 to your computer and use it in GitHub Desktop.
custom devise session controller for active directory users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self.authenticate_user(username,password) | |
ldap = Net::LDAP.new | |
ldap.host = ActiveDirectory.first.host | |
ldap.port = 389 | |
ldap.auth username, password | |
if ldap.bind | |
{:results => "successful", :message => "user authenticated"} | |
else | |
{ :results => "error", :message => ldap.get_operation_result.message} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create | |
user = User.find_by_username(params[:user][:username]) | |
password = params[:user][:password] | |
if user && user.user_type == "active directory" | |
results = ActiveDirectory.authenticate_user(user.username,password) | |
if results.fetch(:results) == "successful" | |
begin | |
user.password = password | |
user.password_confirmation = password | |
user.save | |
rescue Exception => e | |
logger.debug "~~~~~~~~~~~~~~~~~~~~~~~~EXCEPTION::#{e}" | |
end | |
if user.save! | |
super | |
else | |
redirect_to root_path, :error => "There was a problem signing in." | |
end | |
else | |
redirect_to root_path | |
flash[:error] = "Invalid username or password" | |
end | |
else | |
super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment