Se connecter au serveur LDAP
1: #!/usr/bin/env ruby 2: #################################################################### 3: # 4: # main.rb 5: # 6: #################################################################### 7: require 'rubygems' 8: require 'active_ldap' 9: require 'pp' 10: #################################################################### 11: # 12: # setup connection 13: # 14: #################################################################### 15: ActiveLdap::Base.setup_connection( 16: :host => 'directory.example.com', 17: :port => 10389, 18: :base => 'ou=fil_rouge_ldap_1,dc=example,dc=com', 19: :bind_dn => "uid=admin,ou=system", 20: :password_block => Proc.new { 'secret' }, 21: :allow_anonymous => false, 22: :try_sasl => false, 23: :method => :plain 24: ) 25: #################################################################### 26: # 27: # define the root name 28: # 29: #################################################################### 30: module ANF 31: LABO = 'UMR1492' 32: end 33: #################################################################### 34: # 35: # require all exercices 36: # 37: #################################################################### 38: (2..6).each do |i| require "ex#{i}" end
Créer une entrée de type organizationalUnit correspondant à votre laboratoire, afin de travailler dans votre propre sous-arborescence.
1: class OU < ActiveLdap::Base 2: ldap_mapping( 3: :dn_attribute => 'ou', 4: :prefix => '', 5: :classes => ['top','organizationalUnit'] 6: ) 7: end 8: 9: OU.create(ANF::LABO) unless OU.exists?(ANF::LABO) 10: @labo = OU.find(ANF::LABO) 11: 12: puts "creating '#{ANF::LABO}' organizationalUnit"
En dessous de votre entrée « LABO », créer une entrée de type organizationalUnit que vous nommerez « groups ».
1: OU.create( :ou => 'groups', :parent => @labo ) unless OU.exists?('groups') 2: @groups = OU.find('groups') 3: 4: puts "creating 'groups' organizationalUnit"
En dessous de votre entrée « LABO », créer une entrée de type organizationalUnit que vous nommerez « users ».
1: puts "========= EXO 4 ================" 2: OU.create( :ou => 'users', :parent => @labo ) unless OU.exists?('users') 3: @users = OU.find('users') 4: 5: puts "creating 'users' organizationalUnit"
En dessous de votre entrée « LABO / groups », vous allez créer 10 entrées de type posixGroup.
1: puts "========= EXO 5 ================" 2: class Group < ActiveLdap::Base 3: ldap_mapping( 4: :dn_attribute => 'cn', 5: :prefix => "ou=groups,ou=#{ANF::LABO}", 6: :classes => ['top','posixGroup'] 7: ) 8: has_many( :members, 9: :wrap => 'memberUid', 10: :class_name => 'AnfUser', 11: :primary_key => 'cn' 12: ) 13: end 14: 15: (1..10).each do |number| 16: cn = "group_#{number}" 17: group = Group.new( cn ) 18: group.gidNumber = number 19: unless group.save 20: puts 'failed' 21: puts group.errors.full_messages 22: end 23: end 24: 25: puts "creating 10 groups"
Lister tous les groupes.
1: puts "========= EXO 6 ================" 2: groups = Group.find(:all,'*') 3: groups_cn = groups.map do |g| g.cn end 4: puts "list all groups" 5: ap groups_cn
En dessous de votre entrée « LABO / users », vous allez créer 50 entrées de type account comportant les attributs cn, email et userPassword.
1: puts "========= EXO 7 ================" 2: 3: class AnfUser < ActiveLdap::Base 4: ldap_mapping( 5: :dn_attribute => 'cn', 6: :prefix => "ou=users,ou=#{ANF::LABO}", 7: :classes => [ 'top', 'inetOrgPerson' ] 8: ) 9: belongs_to( :groups, 10: :many => 'memberUid', 11: :class_name => 'Group', 12: :primary_key => 'cn' 13: ) 14: end 15: 16: (1..50).each do |user_no| 17: begin 18: cn = "user.no_#{user_no}" 19: user = AnfUser.new(cn) 20: user.sn = cn 21: user.mail = "#{cn}@example.com" 22: user.userpassword = ActiveLdap::UserPassword::ssha("shinypassword_#{user_no}") 23: user.save 24: rescue => e 25: puts cn 26: pp e 27: puts user.errors.full_messages 28: end 29: end 30: 31: puts "creating 50 users"
Lister tous les utilisateurs.
1: puts "========= EXO 8 ================" 2: users = AnfUser.find(:all,'*') 3: users_cn = users.map do |u| u.cn end 4: puts "list all users" 5: ap users_cn
Retirer les 25 derniers utilisateurs ajoutés.
1: puts "========= EXO 9 ================" 2: groups = Group.all 3: users = Array.new(25) do |no| "user.no_#{no+26}" end 4: users_that_exists = [] 5: users.each do |cn| 6: if AnfUser.exists?(cn) then 7: users_that_exists = users_that_exists << cn 8: puts "removing #{cn}" 9: user = AnfUser.find(cn) 10: AnfUser.destroy(cn) 11: end 12: end 13: groups.each do |group| 14: puts "removing users from #{group.cn}" 15: members = group.members 16: group.members = members - users_that_exists 17: group.save 18: end
Ajouter des utilisateurs dans les groupes.
1: puts "========= EXO 10 ================" 2: puts "adding users (10..25) to group_10" 3: g = Group.find('group_10') 4: members = Array.new(16) do |no| "user.no_#{no+10}" end 5: g.memberUid = members 6: g.save
Lister tous les membres du groupe « group_10 ».
1: puts "========= EXO 11 ================" 2: g = Group.find('group_10') 3: members = g.members.map do |m| m.cn end 4: puts "list all group_10's members" 5: ap members
Lister tous les groupes auquels appartient l'utilisateur « user.no_13 ».
1: puts "========= EXO 12 ================" 2: puts "list all user.no_13's groups" 3: user = AnfUser.find('user.no_13') 4: groups = user.groups.map do |group| group.id end 5: ap groups
Retirer des utilisateurs des groupes.
1: puts "========= EXO 13 ================" 2: puts "remove users from group" 3: groups=Group.all 4: groups.each do |group| 5: members = group.members.map do |member| member.id end 6: puts group.id 7: ap members 8: if group.id == 'group_10' 9: memberUid = group.memberUid.slice(0..4) 10: group.memberUid = memberUid 11: group.save 12: puts "after removing users" 13: group = Group.find('group_10') 14: members = group.members.map do |member| member.id end 15: ap members 16: end 17: end
$Id: mercredi_corrige_ruby_1.txt 664 2012-05-23 22:00:36Z delavennat $