| 1 | package Gtk2::Net::LDAP::Widgets::DistinguishedName; | ||
| 2 | #---[ pod head ]---{{{ | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Gtk2::Net::LDAP::Widgets::DistinguishedName - helper class for DN processing | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | This class is mostly used by other components to analyze and process LDAP | ||
| 11 | Distinguished Names and isn't meant to be used directly. Read the source in | ||
| 12 | case of any needs to do that. | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | #---}}} | ||
| 17 | use utf8; | ||
| 18 | use strict; | ||
| 19 | use vars qw(@ISA $VERSION); | ||
| 20 | |||
| 21 | use Net::LDAP; | ||
| 22 | use Net::LDAP::Util qw(ldap_explode_dn); | ||
| 23 | use Data::Dumper; | ||
| 24 | |||
| 25 | #@ISA = qw(Gtk2::TreeView); | ||
| 26 | |||
| 27 | our $VERSION = "2.0.1"; | ||
| 28 | |||
| 29 | our $dntext; | ||
| 30 | our $dn; | ||
| 31 | |||
| 32 | use overload | ||
| 33 | q{""} => 'to_string'; | ||
| 34 | |||
| 35 | # by OLO | ||
| 36 | # czw mar 17 17:51:34 CET 2005 | ||
| 37 | # Constructor: | ||
| 38 | sub new { | ||
| 39 | my $class = shift; | ||
| 40 | my $self = {}; | ||
| 41 | $self->{dntext} = shift; | ||
| 42 | $self->{dn} = _dn_normalize($self->{dntext}); | ||
| 43 | |||
| 44 | bless $self, $class; | ||
| 45 | } | ||
| 46 | |||
| 47 | # by OLO | ||
| 48 | # czw mar 17 17:51:20 CET 2005 | ||
| 49 | # Conversion of self to string: | ||
| 50 | sub to_string { | ||
| 51 | my $self = shift; | ||
| 52 | return $self->{dntext}; | ||
| 53 | } | ||
| 54 | |||
| 55 | #---[ sub isDescendant ]---{{{ | ||
| 56 | |||
| 57 | =head2 isDescendant | ||
| 58 | |||
| 59 | =over 4 | ||
| 60 | |||
| 61 | =item isDescendant ( possible_ancestor ) | ||
| 62 | |||
| 63 | Checks whether this DN is a child of another DN (possible_ancestor). | ||
| 64 | |||
| 65 | C<possible_ancestor> a distinguished name being tested for ancestorness in | ||
| 66 | relation to current DN :) | ||
| 67 | |||
| 68 | =back | ||
| 69 | |||
| 70 | =cut | ||
| 71 | sub isDescendant($) { | ||
| 72 | my $self = shift; | ||
| 73 | my $possible_ancestor = shift; | ||
| 74 | my $mydn = $self->{dn}; | ||
| 75 | my $hisdn = $possible_ancestor->{dn}; | ||
| 76 | |||
| 77 | #print "Testing if $mydn is a child of $hisdn...\n"; | ||
| 78 | if (rindex($mydn, $hisdn) > 0) { | ||
| 79 | #print "...it is.\n"; | ||
| 80 | return 1; | ||
| 81 | } else { | ||
| 82 | #print "...it isn't.\n"; | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | #---}}} | ||
| 87 | |||
| 88 | #---[ sub getRdn ]---{{{ | ||
| 89 | |||
| 90 | =head2 getRdn | ||
| 91 | |||
| 92 | =over 4 | ||
| 93 | |||
| 94 | =item getRdn ( possible_ancestor ) | ||
| 95 | |||
| 96 | Returns the RDN of this DN in relation to a potential ancestor's DN ( the C<possible_ancestor> argument). | ||
| 97 | If the C<possible_ancestor> isn't an ancestor in fact, the returned RDN will be empty. | ||
| 98 | |||
| 99 | C<possible_ancestor> a distinguished name of potential ancestor | ||
| 100 | |||
| 101 | =back | ||
| 102 | |||
| 103 | =cut | ||
| 104 | sub getRdn($) { | ||
| 105 | my $self = shift; | ||
| 106 | my $possible_ancestor = shift; | ||
| 107 | if (! defined($possible_ancestor)) { | ||
| 108 | $possible_ancestor = Gtk2::Net::LDAP::Widgets::DistinguishedName->new(''); | ||
| 109 | } | ||
| 110 | my $mydn = $self->{dn}; | ||
| 111 | my $hisdn = $possible_ancestor->{dn}; | ||
| 112 | my $rdn = ''; | ||
| 113 | |||
| 114 | if (rindex($mydn, $hisdn) > 0) { | ||
| 115 | # we're a descendant | ||
| 116 | #print "$mydn is a descendant of $hisdn\n"; | ||
| 117 | |||
| 118 | # compute the RDN: | ||
| 119 | $rdn = substr($mydn, 0, rindex($mydn, $hisdn)); | ||
| 120 | #print " ...so its RDN is $rdn\n"; | ||
| 121 | } | ||
| 122 | $rdn =~ s/,+$//; | ||
| 123 | $rdn =~ s/^,+//; | ||
| 124 | return $rdn; | ||
| 125 | } | ||
| 126 | #---}}} | ||
| 127 | |||
| 128 | #---[ sub getLength ]---{{{ | ||
| 129 | |||
| 130 | =head2 getLength | ||
| 131 | |||
| 132 | =over 4 | ||
| 133 | |||
| 134 | =item getLength ( ) | ||
| 135 | |||
| 136 | Returns the count of RDN components that this DN consists of. | ||
| 137 | |||
| 138 | =back | ||
| 139 | |||
| 140 | =cut | ||
| 141 | sub getLength { | ||
| 142 | my $self = shift; | ||
| 143 | my @exploded_dn = @{ ldap_explode_dn($self->{dn}) }; | ||
| 144 | return scalar(@exploded_dn); | ||
| 145 | } | ||
| 146 | #---}}} | ||
| 147 | |||
| 148 | #---[ sub compareTo ]---{{{ | ||
| 149 | |||
| 150 | =head2 compareTo | ||
| 151 | |||
| 152 | =over 4 | ||
| 153 | |||
| 154 | =item compareTo ( ) | ||
| 155 | |||
| 156 | Compares this DN and another DN using normalized forms of both. Return values | ||
| 157 | meanings are the same as with the "eq" Perl operator. | ||
| 158 | |||
| 159 | =back | ||
| 160 | |||
| 161 | =cut | ||
| 162 | sub compareTo($) { | ||
| 163 | my $self = shift; | ||
| 164 | my $him = shift; | ||
| 165 | return ($self->{dn} eq $him->{dn}); | ||
| 166 | } | ||
| 167 | #---}}} | ||
| 168 | |||
| 169 | |||
| 170 | # by OLO | ||
| 171 | # czw kwi 14 09:46:05 CEST 2005 | ||
| 172 | # internal function used to normalize DNs for proper DN comparisons | ||
| 173 | sub _dn_normalize { | ||
| 174 | my $dn = shift; | ||
| 175 | $dn = lc($dn); | ||
| 176 | $dn =~ s/\s//g; | ||
| 177 | $dn = Net::LDAP::Util::canonical_dn($dn, casefold => 'lower'); | ||
| 178 | return $dn; | ||
| 179 | } | ||
| 180 | |||
| 181 | 1; | ||
| 182 | __END__ |