Skip to content
Browse files

Initial commit showing most operations

  • Loading branch information...
0 parents commit 1129662e3eac4cfc79d2f6d7ebd11c990eebf062 @samus committed Oct 18, 2011
Showing with 163 additions and 0 deletions.
  1. BIN .activeldap.rb.swp
  2. +55 −0 .rvmrc
  3. +1 −0 README
  4. +29 −0 activeldap.rb
  5. +25 −0 netldap1.rb
  6. +53 −0 netldap2.rb
BIN .activeldap.rb.swp
Binary file not shown.
55 .rvmrc
@@ -0,0 +1,55 @@
+#!/usr/bin/env bash
+
+# This is an RVM Project .rvmrc file, used to automatically load the ruby
+# development environment upon cd'ing into the directory
+
+# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
+environment_id="ruby-1.9.2-p290@ruby-ldap-presentation"
+
+#
+# Uncomment following line if you want options to be set only for given project.
+#
+# PROJECT_JRUBY_OPTS=( --1.9 )
+
+#
+# First we attempt to load the desired environment directly from the environment
+# file. This is very fast and efficient compared to running through the entire
+# CLI and selector. If you want feedback on which environment was used then
+# insert the word 'use' after --create as this triggers verbose mode.
+#
+if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
+then
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
+
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
+ then
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
+ fi
+else
+ # If the environment file has not yet been created, use the RVM CLI to select.
+ if ! rvm --create "$environment_id"
+ then
+ echo "Failed to create RVM environment '${environment_id}'."
+ exit 1
+ fi
+fi
+
+#
+# If you use an RVM gemset file to install a list of gems (*.gems), you can have
+# it be automatically loaded. Uncomment the following and adjust the filename if
+# necessary.
+#
+# filename=".gems"
+# if [[ -s "$filename" ]]
+# then
+# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
+# fi
+
+# If you use bundler, this might be useful to you:
+# if command -v bundle && [[ -s Gemfile ]]
+# then
+# bundle install
+# fi
+
+
1 README
@@ -0,0 +1 @@
+Sample code covering simple interaction with with net-ldap and activeldap
29 activeldap.rb
@@ -0,0 +1,29 @@
+require 'active_ldap'
+ActiveLdap::Base.setup_connection :host => 'localhost', :base => 'dc=nodomain',
+ :bind_dn => 'cn=admin,dc=nodomain', :password => 'admin'
+
+
+class Group < ActiveLdap::Base
+ ldap_mapping :dn_attribute => 'cn',
+ :prefix => 'ou=groups', :classes => ['top', 'groupOfNames'],
+ :scope => :one
+ has_many :members,
+ :class_name => "User",
+ :wrap => "member"
+end
+
+class User < ActiveLdap::Base
+ ldap_mapping :dn_attribute => 'cn', :prefix => 'ou=users', :classes => ['top', 'inetorgperson']
+ belongs_to :groups, :class_name => 'Group', :foreign_key=> "dn", :primary_key => 'member'
+end
+
+user1 = User.find 'user1'
+puts "Found: #{user1.cn} #{user1.sn}"
+user1.groups.all.each {|group| puts " Group: #{group.cn}"}
+
+puts "\n\n"
+rubyists = Group.find 'rubyists'
+puts "Found Group: #{rubyists.dn}"
+rubyists.members.each do|member|
+ puts " Member: #{member.cn} #{member.sn}"
+end
25 netldap1.rb
@@ -0,0 +1,25 @@
+require 'net/ldap'
+
+ldap = Net::LDAP.new
+ldap.host = "localhost"
+ldap.port = 389
+ldap.auth "cn=admin,dc=nodomain", "admin"
+if ldap.bind == false
+ puts "auth failed"
+ exit
+else
+ puts "auth succeeded"
+end
+
+
+filter = Net::LDAP::Filter.eq("cn", "user1")
+treebase = "dc=nodomain"
+
+ldap.search(:base => treebase, :filter => filter) do |entry|
+ puts "DN: #{entry.dn} is part of the following groups:"
+ check = Net::LDAP::Filter.eq("member", entry.dn)
+ groups = ldap.search(:base => treebase, :filter => check)
+ groups.each do |group|
+ puts "#{group.cn}"
+ end
+end
53 netldap2.rb
@@ -0,0 +1,53 @@
+require 'net/ldap'
+
+ldap = Net::LDAP.new
+ldap.host = "localhost"
+ldap.port = 389
+ldap.auth "cn=admin,dc=nodomain", "admin"
+if ldap.bind == false
+ puts "auth failed"
+ exit
+else
+ puts "auth succeeded"
+end
+
+#Add a user and later delete him
+new_userdn = "cn=phpcreator,ou=users,dc=nodomain"
+attr = {
+ :cn => 'phpcreator',
+ :objectclass => ['inetorgperson'],
+ :sn => 'Lerdorf',
+ :givenName => 'Rasmus'
+}
+ldap.add(:dn => new_userdn, :attributes => attr)
+
+rubyists_dn = 'cn=rubyists,ou=groups,dc=nodomain'
+groups_base = 'ou=groups,dc=nodomain'
+
+ldap.add_attribute rubyists_dn, :member, new_userdn
+
+rubyists = ldap.search :base => groups_base, :filter => '(cn=rubyists)',
+ :return_result => true
+
+puts "Members of the group:"
+rubyists[0].member.each do |member|
+ puts "member dn: #{member}"
+end
+
+puts "Something is wrong here."
+
+ldap.delete :dn => new_userdn
+
+rubyists = ldap.search :base => groups_base, :filter => '(cn=rubyists)', :return_result => true
+members = rubyists[0].member
+if members.include?(new_userdn)
+ members.delete new_userdn
+ ldap.replace_attribute rubyists_dn, :member, members
+end
+
+puts "There fixed that"
+puts "Here's the membership now"
+
+ldap.search :base => groups_base, :filter=> '(cn=rubyists)' do |rubyists|
+ puts "#{rubyists.member}"
+end

0 comments on commit 1129662

Please sign in to comment.
Something went wrong with that request. Please try again.