Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 9a6029196b
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 131 lines (99 sloc) 3.354 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#!/usr/bin/ruby -w

require 'rubygems'
require 'net/ldap'
require "pp"
require "singleton"

class SRoboLDAP
    
  include Singleton
   
  class << self

    attr_accessor :key,:ldappwd

    def dummy= (arg)
        @dummy = arg
    end

    def dummy?
        @dummy || false
    end

  end

    def ldap_groups(auth_hash, user_search)
        return dummy_groups auth_hash, user_search if self.class.dummy?
        ldap = Net::LDAP.new :host => "localhost",
             :port => 389,
             :auth => {
                   :method => :simple,
                   :username => "uid=" + auth_hash["username"] + ",ou=users,o=sr",
                   :password => auth_hash["password"]
             }
        if ldap.bind
            treebase= "ou=groups,o=sr"
            filter = "memberUid=" + user_search
            groups = []
            ldap.search(:base => treebase, :filter => filter) do |entry|
                entry.each do |attribute, value|
                    if attribute.to_s == "cn"
                        groups << value.to_s
                    end
                    puts attribute, value
                end
            end

            return groups
        end

        return nil

    end


    def dummy_groups(auth_hash, user_search)
        return ["team1"]
    end

    def ldap_user_details(auth_hash, user_search)
        return dummy_get_ldap_user_details auth_hash, user_search if self.class.dummy?
   
        ldap = Net::LDAP.new :host => "localhost",
             :port => 389,
             :auth => {
                   :method => :simple,
                   :username => "uid=" + auth_hash["username"] + ",ou=users,o=sr",
                   :password => auth_hash["password"]
             }
        if ldap.bind
            treebase="ou=users,o=sr"
            filter="uid=" + user_search
            result = {}
            ldap.search(:base => treebase, :filter => filter) do |entry|
                entry.each do |attribute, values|
                    result[attribute] = values.each.next
                end
            end

            correct_entry = ""
    
            for i in 1..21
                treebase = "ou=groups,o=sr"
                filter = "memberUid=" + user_search

                ldap.search(:base => treebase, :filter => filter) do |entry|
                    entry.each do |attribute, values|
                        if attribute == "cn" && values.next =~ "^college-"
                            correct_entry = entry
                        end

                    end

                end
                
            end


            correct_entry.each do |attribute, values|
                if attribute == "description"
                    result["school"] = values.next
                end
            end

            return result
        else
            puts "losing"
            p ldap.get_operation_result
        end
    end

    private
    def dummy_get_ldap_user_details(auth_hash, user_search)
        return {:uidnumber=>"2096",
         :uid=>"sphippen",
         :gidnumber=>"1999",
         :homedirectory=>"/home/sphippen",
         :sn=>"phippen",
         :cn=>"sam",
         :dn=>"uid=sphippen,ou=users,o=sr",
         :loginshell=>"/bin/bash",
         :objectclass=>"inetOrgPerson",
         :mail=>"samphippen@gmail.com"}
    end

  end




Something went wrong with that request. Please try again.