diff --git a/docs/upgrades/upgrade_7.9.3-7.9.4.pl b/docs/upgrades/upgrade_7.9.3-7.9.4.pl index d50cd002d..150096d42 100644 --- a/docs/upgrades/upgrade_7.9.3-7.9.4.pl +++ b/docs/upgrades/upgrade_7.9.3-7.9.4.pl @@ -35,6 +35,8 @@ my $session = start(); # this line required # upgrade functions go here addWikiSubKeywords($session); addSynopsistoEachWikiPage($session); +dropVisitorAddressBooks($session); +alterAddressBookTable($session); finish($session); # this line required @@ -79,6 +81,27 @@ sub addSynopsistoEachWikiPage { print "DONE!\n" unless $quiet; } +#---------------------------------------------------------------------------- +sub dropVisitorAddressBooks { + my $session = shift; + print "\tDrop AddressBooks owned by Visitor... " unless $quiet; + my $sth = $session->db->read(q|SELECT addressBookId FROM addressBook where userId='1'|); + BOOK: while (my ($addressBookId) = $sth->array) { + my $book = eval { WebGUI::Shop::AddressBook->new($session, $addressBookId); }; + next BOOK if Exception::Class->caught(); + $book->delete; + } + print "DONE!\n" unless $quiet; +} + +#---------------------------------------------------------------------------- +sub alterAddressBookTable { + my $session = shift; + print "\tDrop sessionId from the Address Book database table... " unless $quiet; + # and here's our code + $session->db->write("ALTER TABLE addressBook DROP COLUMN sessionId"); + print "DONE!\n" unless $quiet; +} # -------------- DO NOT EDIT BELOW THIS LINE -------------------------------- diff --git a/lib/WebGUI/Asset/Sku/EMSBadge.pm b/lib/WebGUI/Asset/Sku/EMSBadge.pm index 21adf7063..751c9321d 100644 --- a/lib/WebGUI/Asset/Sku/EMSBadge.pm +++ b/lib/WebGUI/Asset/Sku/EMSBadge.pm @@ -404,7 +404,7 @@ sub view { ; # instanciate address - my $address = WebGUI::Shop::AddressBook->newBySession($self->session)->getAddress($form->get("addressId")) if ($form->get("addressId")); + my $address = WebGUI::Shop::AddressBook->newByUserId($self->session)->getAddress($form->get("addressId")) if ($form->get("addressId")); # build the form that the user needs to fill out with badge holder information $vars{formHeader} = WebGUI::Form::formHeader($session, {action => $self->getUrl}) diff --git a/lib/WebGUI/Content/Shop.pm b/lib/WebGUI/Content/Shop.pm index f52b4c8a9..b3713223a 100644 --- a/lib/WebGUI/Content/Shop.pm +++ b/lib/WebGUI/Content/Shop.pm @@ -88,7 +88,7 @@ sub www_address { my $session = shift; my $output = undef; my $method = "www_". ( $session->form->get("method") || "view"); - my $cart = WebGUI::Shop::AddressBook->newBySession($session); + my $cart = WebGUI::Shop::AddressBook->newByUserId($session); if ($cart->can($method)) { $output = $cart->$method(); diff --git a/lib/WebGUI/Shop/AddressBook.pm b/lib/WebGUI/Shop/AddressBook.pm index 916381bf7..27f712d8d 100644 --- a/lib/WebGUI/Shop/AddressBook.pm +++ b/lib/WebGUI/Shop/AddressBook.pm @@ -56,22 +56,31 @@ sub addAddress { #------------------------------------------------------------------- -=head2 create ( session ) +=head2 create ( session, userId ) -Constructor. Creates a new address book for this user or session if no user is logged in. +Constructor. Creates a new address book for this user. =head3 session A reference to the current session. +=head3 userId + +The userId for the user. Throws an exception if it is Visitor. Defaults to the session +user if omitted. + =cut sub create { - my ($class, $session) = @_; + my ($class, $session, $userId) = @_; unless (defined $session && $session->isa("WebGUI::Session")) { WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session."); } - my $id = $session->db->setRow("addressBook", "addressBookId", {addressBookId=>"new", userId=>$session->user->userId, sessionId=>$session->getId}); + $userId ||= $session->user->userId; + if ($userId eq '1') { + WebGUI::Error::InvalidParam->throw(error=>"Visitor cannot have an address book."); + } + my $id = $session->db->setRow("addressBook", "addressBookId", {addressBookId=>"new", userId=>$userId}); return $class->new($session, $id); } @@ -252,22 +261,30 @@ sub new { #------------------------------------------------------------------- -=head2 newBySession ( session ) +=head2 newByUserId ( session, userId ) -Constructor. Creates a new address book for this user if they don't have one. If the user is not logged in creates an address book attached to the session if there isn't one for the session. In any case returns a reference to the address book. +Constructor. Creates a new address book for this user if they don't have one. In any case returns a reference to the address book. =head3 session A reference to the current session. +=head3 userId + +The userId for the user. Throws an exception if it is Visitor. Defaults to the session +user if omitted. + =cut -sub newBySession { - my ($class, $session) = @_; +sub newByUserId { + my ($class, $session, $userId) = @_; unless (defined $session && $session->isa("WebGUI::Session")) { WebGUI::Error::InvalidObject->throw(expected=>"WebGUI::Session", got=>(ref $session), error=>"Need a session."); } - my $userId = $session->user->userId; + $userId ||= $session->user->userId; + if ($userId eq '1') { + WebGUI::Error::InvalidParam->throw(error=>"Visitor cannot have an address book."); + } # check to see if this user or his session already has an address book my @ids = $session->db->buildArray("select addressBookId from addressBook where (userId<>'1' and userId=?) or sessionId=?",[$session->user->userId, $session->getId]); diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index ad46939e6..5c9b2b0b9 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -263,7 +263,7 @@ sub getAddressBook { my $self = shift; my $id = id $self; unless (exists $addressBookCache{$id}) { - $addressBookCache{$id} = WebGUI::Shop::AddressBook->newBySession($self->session); + $addressBookCache{$id} = WebGUI::Shop::AddressBook->newByUserId($self->session); } return $addressBookCache{$id}; }