Created
July 22, 2010 14:56
-
-
Save mallain/486082 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # LDAP Configuration | |
| development: | |
| host: localhost | |
| port: 636 | |
| base: ou=bar,dc=foo,dc=com | |
| admin_user: cn=admin,dc=foo,dc=com | |
| admin_password: password | |
| ssl: false | |
| production: | |
| host: localhost | |
| port: 636 | |
| base: ou=bar,dc=foo,dc=com | |
| admin_user: cn=admin,dc=foo,dc=com | |
| admin_password: password | |
| ssl: false | |
| test: | |
| host: localhost | |
| port: 636 | |
| base: ou=bar,dc=foo,dc=com | |
| admin_user: cn=admin,dc=foo,dc=com | |
| admin_password: password | |
| ssl: false | |
This file contains hidden or 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
| class LdapConnect | |
| attr_reader :ldap | |
| def initialize(params = {}) | |
| ldap_config = YAML.load_file("#{RAILS_ROOT}/config/ldap.yml")[RAILS_ENV] | |
| ldap_options = params | |
| ldap_options[:encryption] = :simple_tls if ldap_config["ssl"] | |
| @ldap = Net::LDAP.new(ldap_options) | |
| @ldap.host = ldap_config["host"] | |
| @ldap.port = ldap_config["port"] | |
| @ldap.base = ldap_config["base"] | |
| @ldap.auth ldap_config["admin_user"], ldap_config["admin_password"] if params[:admin] | |
| end | |
| def online? | |
| begin | |
| @ldap.bind | |
| rescue Net::LDAP::LdapError => e | |
| false | |
| end | |
| end | |
| end |
This file contains hidden or 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
| class User < ActiveRecord::Base | |
| ## Plugins ## | |
| acts_as_authentic do |c| | |
| c.validate_password_field = false | |
| c.logged_in_timeout = 15.minutes | |
| end | |
| ## Callbacks ## | |
| ## Relations ## | |
| ## Validates ## | |
| validates_presence_of :email | |
| ## Scopes ## | |
| ## Methods ## | |
| def dn | |
| "cn=#{self.email},ou=bar,dc=foo,dc=com" | |
| end | |
| protected | |
| def valid_ldap_credentials?(password_plaintext) | |
| begin | |
| ldap = LdapConnect.new.ldap | |
| ldap.auth self.dn, password_plaintext | |
| ldap.bind # will return false if authentication is NOT successful | |
| rescue Net::LDAP::LdapError => e | |
| #TODO Send a mail to inform the administrator | |
| puts "------------------" | |
| puts "Message: #{e.message}" | |
| puts "------------------" | |
| false | |
| end | |
| end | |
| end |
This file contains hidden or 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
| class UserSession < Authlogic::Session::Base | |
| before_validation :check_if_ldap_is_online | |
| verify_password_method :valid_ldap_credentials? | |
| private | |
| def check_if_ldap_is_online | |
| errors.add(I18n.t('ldap_server_offline')) unless LdapConnect.new.online? | |
| end | |
| end |
This file contains hidden or 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
| class UserSessionsController < ApplicationController | |
| before_filter :require_user, :only => :destroy | |
| def new | |
| @user_session = UserSession.new | |
| end | |
| def create | |
| @user_session = UserSession.new(params[:user_session]) | |
| if @user_session.save | |
| session[:feedback] = nil | |
| session[:agency] = nil | |
| flash[:notice] = I18n.t('login_successful') | |
| redirect_to root_url | |
| else | |
| render :action => :new | |
| end | |
| end | |
| def destroy | |
| current_user_session.destroy | |
| session[:feedback] = nil | |
| session[:agency] = nil | |
| flash[:notice] = I18n.t('logout_successful') | |
| redirect_to root_url | |
| end | |
| end |
This file contains hidden or 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
| class UsersController < ApplicationController | |
| def new | |
| @user = User.new | |
| end | |
| def create | |
| @user = User.new(params[:user]) | |
| if @user.save | |
| flash[:notice] = "Registration successful." | |
| redirect_to root_url | |
| else | |
| render :action => 'new' | |
| end | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My Gist about Authlogic + LDAP