refactored WebGUI::LDAPLink to use session api

This commit is contained in:
JT Smith 2006-01-12 05:24:52 +00:00
parent c250321c4c
commit 65a40d75ac
6 changed files with 78 additions and 67 deletions

View file

@ -116,7 +116,7 @@ sub addUserForm {
-name=>"authLDAP_ldapConnection",
-label=>WebGUI::International::get("ldapConnection",'AuthLDAP'),
-hoverHelp=>WebGUI::International::get("ldapConnection description",'AuthLDAP'),
-options=>WebGUI::LDAPLink::getList(),
-options=>WebGUI::LDAPLink->getList($self->session,),
-value=>[$ldapConnection],
-extras=>q|onchange="this.form.authLDAP_ldapUrl.value=ldapValue[this.options[this.selectedIndex].value];"|
);
@ -209,7 +209,7 @@ sub createAccount {
if($self->session->form->process("connection")) {
$self->session->scratch->set("ldapConnection",$self->session->form->process("connection"));
$self->{_connection} = WebGUI::LDAPLink::get($self->session->form->process("connection"));
$self->{_connection} = WebGUI::LDAPLink->new($self->session,$self->session->form->process("connection"))->get;
}
my $connection = $self->{_connection};
$vars->{'create.message'} = $_[0] if ($_[0]);
@ -218,7 +218,7 @@ sub createAccount {
my $url = $self->session->url->page("op=auth;method=createAccount;connection=");
$vars->{'create.form.ldapConnection'} = WebGUI::Form::selectBox({
name=>"ldapConnection",
options=>WebGUI::LDAPLink::getList(),
options=>WebGUI::LDAPLink->getList($self->session,),
value=>[$connection->{ldapLinkId}],
extras=>qq|onchange="location.href='$url'+this.options[this.selectedIndex].value"|
});
@ -362,7 +362,7 @@ sub editUserSettingsForm {
my $f = WebGUI::HTMLForm->new($self->session);
my $ldapConnection = WebGUI::Form::selectBox({
name=>"ldapConnection",
options=>WebGUI::LDAPLink::getList(),
options=>WebGUI::LDAPLink->getList($self->session,),
value=>[$self->session->setting->get("ldapConnection")]
});
my $ldapConnectionLabel = WebGUI::International::get("ldapConnection",'AuthLDAP');
@ -411,7 +411,7 @@ sub new {
my $userId = $_[1];
my @callable = ('createAccount','deactivateAccount','displayAccount','displayLogin','login','logout','createAccountSave','deactivateAccountConfirm');
my $self = WebGUI::Auth->new($authMethod,$userId,\@callable);
$self->{_connection} = WebGUI::LDAPLink::get(($self->session->scratch->get("ldapConnection") || $self->session->setting->get("ldapConnection")));
$self->{_connection} = WebGUI::LDAPLink->new($self->session,($self->session->scratch->get("ldapConnection") || $self->session->setting->get("ldapConnection")))->get;
bless $self, $class;
}

View file

@ -116,7 +116,7 @@ Renders a database connection picker control.
sub toHtml {
my $self = shift;
$self->get("options") = WebGUI::LDAPLink::getList();
$self->get("options") = WebGUI::LDAPLink->getList($self->session,);
return $self->SUPER::toHtml();
}
@ -130,7 +130,7 @@ Creates a series of hidden fields representing the data in the list.
sub toHtmlAsHidden {
my $self = shift;
$self->get("options") = WebGUI::LDAPLink::getList();
$self->get("options") = WebGUI::LDAPLink->getList($self->session,);
return $self->SUPER::toHtmlAsHidden();
}

View file

@ -18,8 +18,6 @@ package WebGUI::LDAPLink;
use strict;
use Tie::CPHash;
use WebGUI::International;
use WebGUI::Session;
use WebGUI::SQL;
use Net::LDAP;
=head1 NAME
@ -33,10 +31,10 @@ This package contains utility methods for WebGUI's ldap link system.
=head1 SYNOPSIS
use WebGUI::LDAPLink;
$hashRef = WebGUI::LDAPLink::getList();
%ldapLink = WebGUI::LDAPLink::get($ldapLinkId);
$hashRef = WebGUI::LDAPLink->getList($self->session,);
%ldapLink = WebGUI::LDAPLink->new($self->session,$ldapLinkId)->get;
$ldapLink = WebGUI::LDAPLink->new($ldapLinkId);
$ldapLink = WebGUI::LDAPLink->new($self->session,$ldapLinkId);
$connection = $ldapLink->authenticate();
$ldapLink->disconnect;
@ -57,52 +55,66 @@ cannot be established
=cut
sub bind {
my $class = shift;
my $self = shift;
my ($uri, $ldap, $auth, $result, $error);
if (defined $class->{_connection}) {
return $class->{_connection};
if (defined $self->{_connection}) {
return $self->{_connection};
}
my $ldapUrl = $class->{_ldapLink}->{ldapUrl};
my $connectDn = $class->{_ldapLink}->{connectDn};
my $identifier = $class->{_ldapLink}->{identifier};
my $ldapUrl = $self->{_ldapLink}->{ldapUrl};
my $connectDn = $self->{_ldapLink}->{connectDn};
my $identifier = $self->{_ldapLink}->{identifier};
if($ldapUrl eq "") {
$class->{_error} = 100;
$self->{_error} = 100;
return 0;
} elsif ($connectDn eq "") {
$class->{_error} = 101;
$self->{_error} = 101;
return 0;
} elsif ($identifier eq "") {
$class->{_error} = 102;
$self->{_error} = 102;
return 0;
}
if($uri = URI->new($ldapUrl)) {
unless($ldap = Net::LDAP->new($uri->host, (port=>($uri->port || 389)))){
$class->{_error} = 103;
$self->{_error} = 103;
return 0;
}
$auth = $ldap->bind(dn=>$connectDn, password=>$identifier);
if ($auth->code == 48 || $auth->code == 49){
$class->{_error} = 104;
$self->{_error} = 104;
}elsif($auth->code > 0){
$class->{_error} = $auth->code;
$self->{_error} = $auth->code;
}
$class->{_connection} = $ldap;
$self->{_connection} = $ldap;
}else{
$class->{_error} = 105;
$self->{_error} = 105;
return 0;
}
return $class->{_connection};
return $self->{_connection};
}
#-------------------------------------------------------------------
sub DESTROY {
my $class = shift;
$class->unbind;
my $self = shift;
$self->unbind;
undef $self;
}
#-------------------------------------------------------------------
=head2 get ( )
Returns the list of LDAP connection properties.
=cut
sub get {
my $self = shift;
return $self->{_ldapLink};
}
#-------------------------------------------------------------------
@ -118,8 +130,8 @@ A valid ldap error code.
=cut
sub getErrorMessage {
my $class = shift;
my $errorCode = $_[0] || $class->{_error};
my $self = shift;
my $errorCode = $_[0] || $self->{_error};
return "" unless $errorCode;
my $i18nCode = "LDAPLink_".$errorCode;
return WebGUI::International::get($i18nCode,"AuthLDAP");
@ -127,41 +139,27 @@ sub getErrorMessage {
#-------------------------------------------------------------------
=head2 getList ( )
=head2 getList ( session )
Returns a hash reference containing all ldap links. The format is:
ldapLinkId => ldapLinkName
Returns a hash reference containing all ldap links. The format is: ldapLinkId => ldapLinkName. This is a class method.
=head3 session
A reference to the current session.
=cut
sub getList {
my $class = shift;
my $session = shift;
my %list;
tie %list, "Tie::IxHash";
%list = $self->session->db->buildHash("select ldapLinkId, ldapLinkName from ldapLink order by ldapLinkName");
%list = $session->db->buildHash("select ldapLinkId, ldapLinkName from ldapLink order by ldapLinkName");
return \%list;
}
#-------------------------------------------------------------------
=head2 get ( ldapLinkId )
Returns a hashRef containing a single ldap link.
=head3 ldapLinkId
A valid ldapLinkId
=cut
sub get {
my %hash;
tie %hash, 'Tie::CPHash';
%hash = $self->session->db->quickHash("select * from ldapLink where ldapLinkId=".$self->session->db->quote($_[0]));
return \%hash;
}
#-------------------------------------------------------------------
=head2 unbind ( )
Disconnect cleanly from the current databaseLink.
@ -169,20 +167,24 @@ Disconnect cleanly from the current databaseLink.
=cut
sub unbind {
my ($class, $value);
$class = shift;
my ($self, $value);
$self = shift;
$value = shift;
if (defined $class->{_connection}) {
$class->{_connection}->unbind;
if (defined $self->{_connection}) {
$self->{_connection}->unbind;
}
}
#-------------------------------------------------------------------
=head2 new ( ldapLinkId )
=head2 new ( session, ldapLinkId )
Constructor.
=head3 session
A reference to the current session.
=head3 ldapLinkId
The ldapLinkId of the ldapLink you're creating an object reference for.
@ -190,12 +192,13 @@ The ldapLinkId of the ldapLink you're creating an object reference for.
=cut
sub new {
my ($class, $ldapLinkId, $ldapLink);
$class = shift;
my ( $ldapLinkId, $ldapLink);
my $class = shift;
my $session = shift;
$ldapLinkId = shift;
return undef unless $ldapLinkId;
$ldapLink = get($ldapLinkId);
bless {_ldapLinkId => $ldapLinkId, _ldapLink => $ldapLink }, $class;
$ldapLink = $session->db->quickHash("select * from ldapLink where ldapLinkId=".$session->db->quote($ldapLinkId));
bless {_session=>$session, _ldapLinkId => $ldapLinkId, _ldapLink => $ldapLink }, $class;
}
#-------------------------------------------------------------------
@ -217,8 +220,8 @@ sub new {
sub getProperty {
my $self = shift;
my $ldap = $self->bind;
my $dn = $_[0];
my $property = $_[1];
my $dn = shift;
my $property = shift;
return [] unless($ldap && $dn && $property);
my $results = [];
my $msg = $ldap->search(

View file

@ -230,7 +230,7 @@ sub www_listLDAPLinks {
.'</td>';
$row[$i] .= '<td valign="top" class="tableData">'.$data->{ldapLinkName}.'</td>';
my $ldapLink = WebGUI::LDAPLink->new($data->{ldapLinkId});
my $ldapLink = WebGUI::LDAPLink->new($session,$data->{ldapLinkId});
my $status = WebGUI::International::get("LDAPLink_1078","AuthLDAP");
if($ldapLink->bind) {
$status = WebGUI::International::get("LDAPLink_1079","AuthLDAP");

View file

@ -342,7 +342,7 @@ sub isInGroup {
if($self->authMethod eq "LDAP") {
my $auth = WebGUI::Auth->new($session,"LDAP",$uid);
my $params = $auth->getParams();
my $ldapLink = WebGUI::LDAPLink->new($session,$params->{ldapConnection});
my $ldapLink = WebGUI::LDAPLink->new($self->session,$session,$params->{ldapConnection});
if($ldapLink ne "") {
my $people = [];
if($group->get("ldapRecursiveProperty")) {