Parent

OpenNebula::LdapAuth

Public Class Methods

new(options) click to toggle source
# File lib/opennebula/ldap_auth.rb, line 23
def initialize(options)
    @options={
        :host => 'localhost',
        :port => 389,
        :user => nil,
        :password => nil,
        :base => nil,
        :auth_method => :simple,
        :user_field => 'cn',
        :user_group_field => 'dn',
        :group_field => 'member'
    }.merge(options)

    ops={}

    if @options[:user]
        ops[:auth] = {
            :method => @options[:auth_method],
            :username => @options[:user],
            :password => @options[:password]
        }
    end

    ops[:host]=@options[:host] if @options[:host]
    ops[:port]=@options[:port].to_i if @options[:port]
    ops[:encryption]=@options[:encryption] if @options[:encryption]

    @ldap=Net::LDAP.new(ops)
end

Public Instance Methods

authenticate(user, password) click to toggle source
# File lib/opennebula/ldap_auth.rb, line 87
def authenticate(user, password)
    ldap=@ldap.clone

    auth={
        :method => @options[:auth_method],
        :username => user,
        :password => password
    }

    if ldap.bind(auth)
        true
    else
        false
    end
end
find_user(name) click to toggle source
# File lib/opennebula/ldap_auth.rb, line 53
def find_user(name)
    begin
        result=@ldap.search(
            :base => @options[:base],
            :filter => "#{@options[:user_field]}=#{name}")

        if result && result.first
            [result.first.dn, result.first[@options[:user_group_field]]]
        else
            result=@ldap.search(:base => name)

            if result && result.first
                [name, result.first[@options[:user_group_field]]]
            else
                [nil, nil]
            end
        end
    rescue
        [nil, nil]
    end
end
is_in_group?(user, group) click to toggle source
# File lib/opennebula/ldap_auth.rb, line 75
def is_in_group?(user, group)
    result=@ldap.search(
                :base   => group,
                :filter => "(#{@options[:group_field]}=#{user.first})")

    if result && result.first
        true
    else
        false
    end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.