This patch adds user invitations, a way for existing users on a site

to send an email to their friends and invite them to create an account
on the site.  The feature is enabled or disabled in the site Settings.
(Operation/Settings.pm)

It is implemented as a new operation, Invite (Operation/Invite.pm,
Help/Invite.pm, i18n/English/Invite.pm), and the option is displayed
as an option on the user's account screen. (Operation/Shared.pm).
The form is templated, and lives in the Invite namespace.  Once
the invitation is submitted, if the user's email address is not
already in WebGUI, an email is sent and a record is stored in
the userInvitations table.

When the friend gets the invitation, they are taken to the account
creation screen, which conveniently has their email address already
filled in.  This required changes in the Auth modules (Auth.pm, Auth/*.pm),
and ProfileField.pm.  The latter was so that profile fields can have
their values manually set.  The former changes handle inserting the
email address, bypassing the anonymous registration check, and
updating the record in ther userInvitations table.

I refactored some code out of the AdminConsole for finding the url
back to the site and added it to Session/Url.pm.  The method is
called getBackToSiteUrl.
This commit is contained in:
Colin Kuskie 2007-06-10 16:38:43 +00:00
parent 32f7866f3b
commit 21c4fcb75f
18 changed files with 586 additions and 28 deletions

View file

@ -10,6 +10,7 @@
- add: User profile data table is now a flat table, one column for each
field.
- add: Posts can now have Metadata (United Knowledge)
- add: Users can now invite others to create an account (United Knowledge)
- add: Calendar events now allow attachments
- add: Calendar events now allow setting view permissions
- add: WebGUI::Paginator now capable of more efficient SQL paginations using

View file

@ -0,0 +1,47 @@
#PBtmpl00000userInvite1
#url: userInviteTemplate
#title:User Invite Template
#menuTitle:Default User Invite Template
#namespace:userInvite
#create
<h1><tmpl_var title></h1>
<tmpl_var formHeader>
<table width="100%" cellspacing="1" cellpadding="2" border="0">
<tbody>
<tr>
<td class="tableData">
<tmpl_var emailAddressLabel>
</td>
<td class="tableData">
<tmpl_var emailAddressForm>
</td>
</tr>
<tr>
<td class="tableData">
<tmpl_var subjectLabel>
</td>
<td class="tableData">
<tmpl_var subjectForm>
</td>
</tr>
<tr>
<td class="tableData">
<tmpl_var messageLabel>
</td>
<td class="tableData">
<tmpl_var messageForm>
</td>
</tr>
<tr>
<td class="tableData">
&nbsp;
</td>
<td class="tableData">
<tmpl_var submitButton>
</td>
</tr>
</tbody>
</table>
<tmpl_var formFooter>

View file

@ -26,6 +26,7 @@ fixProfileDataWithoutFields($session);
buildNewUserProfileTable($session);
addAttachmentsToEvents($session);
addMetaDataPostsToCS($session);
addUserInvitations($session);
finish($session); # this line required
@ -118,6 +119,35 @@ sub addMetaDataPostsToCS {
}
#----------------------------------------------------------------------------
sub addUserInvitations {
my $session = shift;
my $db = $session->db;
print "\tAdding the ability for users's to invite others to the site... " unless $quiet;
##Add settings
$session->setting->add('userInvitationsEnabled', 0);
$session->setting->add('userInvitationsEmailExists', 'This email address exists in our system. This means that your friend is already a member of the site. The invitation will not be sent.');
##Create table for tracking invitations
$session->db->write(<<EOSQL);
CREATE TABLE userInvitations (
inviteId VARCHAR(22) BINARY NOT NULL,
userId VARCHAR(22) BINARY NOT NULL,
dateSent DATE,
email VARCHAR(255) NOT NULL,
newUserId VARCHAR(22) BINARY,
dateCreated DATE,
PRIMARY KEY (inviteId)
)
EOSQL
print "OK!\n" unless $quiet;
}
#----------------------------------------------------------------------------
sub buildNewUserProfileTable {

View file

@ -509,20 +509,8 @@ sub render {
if (scalar(@tags)) {
$var{versionTags} = \@tags;
}
if (defined $self->session->asset) {
my $importNode = WebGUI::Asset->getImportNode($self->session);
my $importNodeLineage = $importNode->get("lineage");
my $media = WebGUI::Asset->getMedia($self->session);
my $mediaLineage = $media->get("lineage");
my $assetLineage = $self->session->asset->get("lineage");
if ($assetLineage =~ /^$importNodeLineage/ || $assetLineage eq "000001" || $assetLineage =~ /^$mediaLineage/ || ($self->session->asset->get("state") ne "published" && $self->session->asset->get("state") ne "archived")) {
$var{"backtosite.url"} = WebGUI::Asset->getDefault($self->session)->getUrl;
} else {
$var{"backtosite.url"} = $self->session->asset->getContainer->getUrl;
}
} else {
$var{"backtosite.url"} = $self->session->url->page();
}
$var{"backtosite.url"} = $self->session->url->getBackToSiteURL();
$var{"application_loop"} = $self->getAdminFunction;
return $self->session->style->process(WebGUI::Asset::Template->new($self->session,$self->session->setting->get("AdminConsoleTemplate"))->process(\%var),"PBtmpl0000000000000137");
}

View file

@ -178,9 +178,23 @@ sub createAccount {
$vars->{'create.form.header'} .= WebGUI::Form::hidden($self->session,{"name"=>"method","value"=>$method});
#User Defined Options
my $userInvitation = $self->session->setting->get('userInvitationsEnabled');
$self->session->errorHandler->warn('invite = '.$userInvitation);
$vars->{'create.form.profile'} = [];
foreach my $field (@{WebGUI::ProfileField->getRegistrationFields($self->session)}) {
my ($id, $formField, $label) = ($field->getId, $field->formField, $field->getLabel);
my $id = $field->getId;
my $label = $field->getLabel;
my $emailAddress = {};
if ($field->get('fieldName') eq "email" && $userInvitation ) {
$self->session->errorHandler->warn('Email address field');
my $code = $self->session->form->get('code')
|| $self->session->form->get('uniqueUserInvitationCode');
$self->session->errorHandler->warn('Code:'.$code);
($emailAddress) = $self->session->db->quickArray('select email from userInvitations where inviteId=?',[$code]);
$self->session->errorHandler->warn('Email address :'.$emailAddress);
$vars->{'create.form.header'} .= WebGUI::Form::hidden($self->session, {name=>"uniqueUserInvitationCode", value=>$code});
}
my $formField = $field->formField(undef, undef, undef, undef, $emailAddress); ##Manually set the field
my $required = $field->isRequired;
# Old-style field loop.
@ -279,7 +293,20 @@ sub createAccountSave {
} else {
$self->session->http->setStatus(201,"Account Registration Successful");
}
##Finalize the record in the user invitation table.
my $inviteId = $self->session->form->get('uniqueUserInvitationCode');
if ($inviteId) {
$self->session->db->setRow(
'userInvitations',
'inviteId',
{
inviteId => $inviteId,
newUserId => $u->userId,
dateCreated => WebGUI::DateTime->new($self->session, time)->toMysqlDate,
},
);
}
return undef;
}

View file

@ -223,7 +223,8 @@ sub createAccount {
my $vars;
if ($self->session->user->userId ne "1") {
return $self->displayAccount;
} elsif (!$self->session->setting->get("anonymousRegistration")) {
}
elsif (!$self->session->setting->get("anonymousRegistration") && !$self->session->setting->get('userInvitationsEnabled')) {
return $self->displayLogin;
}

View file

@ -135,11 +135,13 @@ sub createAccount {
my $confirm = shift || $self->session->form->process("confirm");
my $vars = shift || {};
if ($self->session->user->userId ne "1") {
return $self->displayAccount;
} elsif (!$self->session->setting->get("anonymousRegistration")) {
return $self->displayLogin;
}
$self->session->errorHandler->warn('WebGUI::Auth::createAccount called');
if ($self->session->user->userId ne "1") {
return $self->displayAccount;
}
elsif (!$self->session->setting->get("anonymousRegistration") && !$self->session->setting->get('userInvitationsEnabled')) {
return $self->displayLogin;
}
my $i18n = WebGUI::International->new($self->session);
$vars->{'create.message'} = $message if ($message);
$vars->{useCaptcha} = $self->session->setting->get("webguiUseCaptcha");
@ -170,7 +172,7 @@ sub createAccountSave {
return $self->displayAccount if ($self->session->user->userId ne "1");
#Make sure anonymous registration is enabled
unless ($self->session->setting->get("anonymousRegistration")) {
unless ($self->session->setting->get("anonymousRegistration") || $self->session->setting->get("userInvitationsEnabled")) {
$self->session->errorHandler->security($i18n->get("no registration hack", "AuthWebGUI"));
return $self->displayLogin;
}

54
lib/WebGUI/Help/Invite.pm Normal file
View file

@ -0,0 +1,54 @@
package WebGUI::Help::Invite;
our $HELP = {
'invite form template' => {
title => 'invite form template title',
body => 'invite form template body',
variables => [
{
name => 'inviteFormError',
required => 1,
},
{
name => 'formHeader',
required => 1,
},
{
name => 'formFooter',
required => 1,
},
{
name => 'title',
},
{
name => 'emailAddressLabel',
},
{
name => 'emailAddressForm',
},
{
name => 'subjectLabel',
},
{
name => 'subjectForm',
},
{
name => 'messageLabel',
},
{
name => 'messageForm',
},
{
name => 'submitButton',
},
],
fields => [
],
related => [
]
},
};
1; ##All perl modules must return true

View file

@ -403,6 +403,16 @@ our $HELP = {
description => 'Enable passive profiling description',
namespace => 'WebGUI',
},
{
title => 'Enable user invitations',
description => 'Enable user invitations description',
namespace => 'WebGUI',
},
{
title => 'user invitations email exists',
description => 'user invitations email exists description',
namespace => 'WebGUI',
},
{
title => '164',
description => '164 description',

View file

@ -164,6 +164,10 @@ sub getOperations {
'viewInbox' => 'WebGUI::Operation::Inbox',
'viewInboxMessage' => 'WebGUI::Operation::Inbox',
'inviteUser' => 'WebGUI::Operation::Invite',
'inviteUserSave' => 'WebGUI::Operation::Invite',
'acceptInvite' => 'WebGUI::Operation::Invite',
'copyLDAPLink' => 'WebGUI::Operation::LDAPLink',
'deleteLDAPLink' => 'WebGUI::Operation::LDAPLink',
'editLDAPLink' => 'WebGUI::Operation::LDAPLink',

View file

@ -0,0 +1,186 @@
package WebGUI::Operation::Invite;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2006 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use WebGUI::Session;
use WebGUI::User;
use WebGUI::Form;
use WebGUI::Mail::Send;
use WebGUI::Operation::Auth;
=head1 NAME
Package WebGUI::Operation::Invite
=head1 DESCRIPTION
Operation handler for handling user invitations.
=cut
#-------------------------------------------------------------------
=head2 www_inviteUser ( )
Form for inviting a user.
=cut
sub www_inviteUser {
my $session = shift;
return $session->privilege->insufficient() unless ($session->user->isInGroup(2));
my $formError = shift;
my $vars = {};
my $i18n = WebGUI::International->new($session, 'Invite');
$vars->{inviteFormError} = $i18n->get($formError);
$vars->{formHeader} = WebGUI::Form::formHeader($session).WebGUI::Form::hidden($session, {name => "op", value => "inviteUserSave"});
$vars->{formFooter} = WebGUI::Form::formFooter($session, {});
$vars->{title} = $i18n->get('invite a friend title');
$vars->{emailAddressLabel} = $i18n->get('480', 'WebGUI');
$vars->{emailAddressForm} = WebGUI::Form::email(
$session,
{
name => "invite_email",
value => $session->form->get('invite_email'),
},
);
$vars->{subjectLabel} = $i18n->get('229', 'WebGUI');
$vars->{subjectForm} = WebGUI::Form::text(
$session,
{
name => "invite_subject",
value => $session->form->get('invite_subject'),
},
);
$vars->{messageLabel} = $i18n->get('351', 'WebGUI');
$vars->{messageForm} = WebGUI::Form::textarea(
$session,
{
name => "invite_message",
value => $session->form->get('invite_message') || $i18n->get('default invite'),
},
);
$vars->{submitButton} = WebGUI::Form::submit(
$session,
{value => $i18n->get('submit', 'WebGUI')},
);
my $output = WebGUI::Asset::Template->new($session,"PBtmpl00000userInvite1")->process($vars);
return $session->style->userStyle($output);
}
#-------------------------------------------------------------------
=head2 www_inviteUserSave ( )
Post process the form, check for required fields, handle inviting users who are already
members (determined by email address) and send the email.
=cut
sub www_inviteUserSave {
my $session = shift;
return $session->privilege->insufficient() unless ($session->user->isInGroup(2));
#Mandatory field checks
my $hisEmailAddress = $session->form->get('invite_email');
return www_inviteUser($session, 'missing email') unless $hisEmailAddress;
my $message = $session->form->get('invite_message');
return www_inviteUser($session, 'missing message') unless $message;
my $subject = $session->form->get('invite_subject');
return www_inviteUser($session, 'missing subject') unless $subject;
my $i18n = WebGUI::International->new($session, 'Invite');
#User existance check.
my $existingUser = WebGUI::User->newByEmail($session, $hisEmailAddress);
use Data::Dumper;
if (defined $existingUser) {
my $output = sprintf qq!<h1>%s</h1>\n<p>%s</p><a href="%s">%s</a>!,
$i18n->get('already a member'),
$session->setting->get('userInvitationsEmailExists'),
$session->url->getBackToSiteURL(),
$i18n->get('493', 'WebGUI');
return $session->style->userStyle($output);
}
my $myEmailAddress = $session->user->profileField('email');
my $invitation = WebGUI::Mail::Send->create(
$session,
{
to => $hisEmailAddress,
from => $myEmailAddress,
subject => $subject,
},
);
##No sneaky attack paths...
$message = WebGUI::HTML::filter($message);
##Append the invitation url.
my $inviteId = $session->id->generate();
my $inviteUrl = $session->url->append($session->url->getSiteURL, 'op=acceptInvite;code='.$inviteId);
$message .= "\n$inviteUrl\n";
##Create the invitation record.
$session->db->setRow(
'userInvitations',
'inviteId',
{
userId => $session->user->userId,
dateSent => WebGUI::DateTime->new($session, time)->toMysqlDate,
email => $hisEmailAddress,
},
$inviteId,
);
$invitation->addText($message);
$invitation->send;
my $output = sprintf qq!<p>%s</p><a href="%s">%s</a>!,
$i18n->get('invitation sent'),
$session->url->getBackToSiteURL(),
$i18n->get('493', 'WebGUI');
return $session->style->userStyle($output);
}
#-------------------------------------------------------------------
=head2 www_acceptInvite ( )
Validate the invitation code. If valid, send the user over to the
create account page. Otherwise, scourge and flay them.
=cut
sub www_acceptInvite {
my $session = shift;
return $session->privilege->insufficient() if ($session->user->isInGroup(2));
my $i18n = WebGUI::International->new($session, 'Invite');
my $inviteId = $session->form->get('code');
my ($validInviteId) = $session->db->quickArray('select userId from userInvitations where inviteId=?',[$inviteId]);
if (!$validInviteId) {
my $output = sprintf qq!<h1>%s</h1>\n<p>%s</p><a href="%s">%s</a>!,
$i18n->get('invalid invite code'),
$i18n->get('invalid invite code message'),
$session->url->getBackToSiteURL(),
$i18n->get('493', 'WebGUI');
return $session->style->userStyle($output);
}
##Everything looks good. Sign them up!
my $auth = WebGUI::Operation::Auth::getInstance($session);
return $session->style->userStyle($auth->createAccount());
}
1;

View file

@ -368,6 +368,22 @@ sub definition {
defaultValue=>$session->setting->get("passiveProfilingEnabled"),
extras=>'onchange="alert(\''.$i18n->get("Illegal Warning").'\')" '
});
push(@fields, {
tab=>"user",
fieldType=>"yesNo",
name=>"userInvitationsEnabled",
label=>$i18n->get("Enable user invitations"),
hoverHelp=>$i18n->get("Enable user invitations description"),
defaultValue=>$session->setting->get("userInvitationsEnabled"),
});
push(@fields, {
tab=>"user",
fieldType=>"textarea",
name=>"userInvitationsEmailExists",
label=>$i18n->get("user invitations email exists"),
hoverHelp=>$i18n->get("user invitations email exists description"),
defaultValue=>$session->setting->get("userInvitationsEmailExists"),
});
# auth settings
my $options;
foreach (@{$session->config->get("authMethods")}) {

View file

@ -68,6 +68,11 @@ is in group Admin (3). Returns the user to the List Database Links screen.
push(@array, {'options.display' => '<a href="'.$session->url->page('op=redeemSubscriptionCode').'">'.$i18n->get('redeem code', 'Subscription').'</a>'});
}
if ($session->setting->get('userInvitationsEnabled')) {
push @array, {
'options.display' => sprintf('<a href=%s>%s</a>', $session->url->page('op=inviteUser'), $i18n->get('invite a friend')),
};
}
my %logout;
$logout{'options.display'} = '<a href="'.$session->url->page('op=auth;method=logout').'">'.$i18n->get(64).'</a>';
push(@array,\%logout);

View file

@ -39,7 +39,7 @@ Operation for creating, deleting, editing and many other user related functions.
#-------------------------------------------------------------------
=head2 _submenu ( session, workarea [, title, help] )
=head2 _submenu ( session, properties )
Internal utility routine for setting up the Admin Console for User functions.

View file

@ -211,6 +211,7 @@ sub formField {
my $withWrapper = shift;
my $u = shift;
my $skipDefault = shift;
my $assignedValue = shift;
my $default;
if ($skipDefault) {
}
@ -231,6 +232,9 @@ sub formField {
$default = WebGUI::Operation::Shared::secureEval($self->session,$properties->{dataDefault});
}
$properties->{value} = $default;
if (defined $assignedValue) {
$properties->{value} = $assignedValue;
}
if ($withWrapper == 1) {
return WebGUI::Form::DynamicField->new($self->session,%{$properties})->displayFormWithWrapper;
} elsif ($withWrapper == 2) {

View file

@ -182,6 +182,36 @@ sub gateway {
return $url;
}
#-------------------------------------------------------------------
=head2 getBackToSiteURL ( )
Tries to return a URL to take the user back to the last page they were at before
using an operation or other function.
=cut
sub getBackToSiteURL {
my $self = shift;
my $url;
if (defined $self->session->asset) {
my $importNode = WebGUI::Asset->getImportNode($self->session);
my $importNodeLineage = $importNode->get("lineage");
my $media = WebGUI::Asset->getMedia($self->session);
my $mediaLineage = $media->get("lineage");
my $assetLineage = $self->session->asset->get("lineage");
if ($assetLineage =~ /^$importNodeLineage/ || $assetLineage eq "000001" || $assetLineage =~ /^$mediaLineage/ || ($self->session->asset->get("state") ne "published" && $self->session->asset->get("state") ne "archived")) {
$url = WebGUI::Asset->getDefault($self->session)->getUrl;
} else {
$url = $self->session->asset->getContainer->getUrl;
}
} else {
$url = $self->session->url->page();
}
return $url;
}
#-------------------------------------------------------------------
=head2 getRefererUrl ( )

View file

@ -0,0 +1,128 @@
package WebGUI::i18n::English::Invite;
our $I18N = {
'invite a friend title' => {
message => q|Invite A Friend|,
lastUpdated => 1181103900,
},
'default invite' => {
message => q|I'm a member of a site that I thought you would find very useful, so I'm sending this invitation hoping you'll join me here. Click on the link below to register.|,
lastUpdated => 1181106351,
},
'missing email' => {
message => q|The invitation cannot be sent because you did not enter an email address.|,
lastUpdated => 1181409056,
},
'missing message' => {
message => q|Your invitiation must have a message.|,
lastUpdated => 1181409432,
},
'missing subject' => {
message => q|Your invitation must have a subject.|,
lastUpdated => 1181409433,
},
'already a member' => {
message => q|Already a member.|,
lastUpdated => 1181410226,
},
'invitation sent' => {
message => q|Your invitation has been sent.|,
lastUpdated => 1181410226,
},
'invalid invite code' => {
message => q|Invalid invitation code|,
lastUpdated => 1181428043,
},
'invalid invite code message' => {
message => q|The invitation code in your URL is invalid.|,
lastUpdated => 1181410226,
},
'already a member message' => {
message => q|The invitation code in your URL is invalid.|,
lastUpdated => 1181410226,
context => q|This message is displayed when someone who is already signed up tries to use an invite code.|,
},
'invite form template title' => {
message => q|User Invitation Form Template|,
lastUpdated => 1181492752,
},
'invite form template body' => {
message => q|This template is used to customize and display the form that users fill out to invite friends to create an account.|,
lastUpdated => 1181492842,
},
'inviteFormError' => {
message => q|Any errors from submitting the form. Error messages are internationalized.|,
lastUpdated => 1181492842,
},
'formHeader' => {
message => q|HTML code for starting the form.|,
lastUpdated => 1181492842,
},
'formFooter' => {
message => q|HTML code for ending the form.|,
lastUpdated => 1181492842,
},
'title' => {
message => q|An internationalized title for the form.|,
lastUpdated => 1181492842,
},
'emailAddressLabel' => {
message => q|An internationalized label for the email address field.|,
lastUpdated => 1181492842,
},
'emailAddressForm' => {
message => q|HTML code for the email address field.|,
lastUpdated => 1181492842,
},
'subjectLabel' => {
message => q|An internationalized label for the subject field.|,
lastUpdated => 1181492842,
},
'subjectForm' => {
message => q|HTML code for the subject field.|,
lastUpdated => 1181492842,
},
'messageLabel' => {
message => q|An internationalized label for the message field.|,
lastUpdated => 1181492842,
},
'messageForm' => {
message => q|HTML code for the message field.|,
lastUpdated => 1181492842,
},
'submitButton' => {
message => q|HTML code for the submit button, with internationalized label.|,
lastUpdated => 1181492842,
},
'topicName' => {
message => q|User Invitations.|,
lastUpdated => 1181493546,
},
};
1;

View file

@ -3676,10 +3676,9 @@ and tracked by WebGUI.|,
lastUpdated => 1163457062,
},
'Enable passive profiling description' => {
message => q|Used in conjunction with Metadata, this keeps a record of every wobject viewed by
a user.|,
lastUpdated => 1167189802,
'Enable user invitations description' => {
message => q|Enable users to send emails to their friends, inviting them to come and create an account on this site.|,
lastUpdated => 1181017746,
},
'164 description' => {
@ -3788,6 +3787,17 @@ Select which of the configured LDAP connections to use to authenticate users.
lastUpdated => 1089039511
},
'Enable passive profiling description' => {
message => q|Used in conjunction with Metadata, this keeps a record of every wobject viewed by
a user.|,
lastUpdated => 1167189802,
},
'Enable user invitations' => {
message => q|Enable user invitations?|,
lastUpdated => 1181017730
},
'Illegal Warning' => {
message => q|Enabling this feature is illegal in some countries, like Australia. In addition, some countries require you to add a warning to your site if you use this feature. Consult your local authorities for local laws. Plain Black Corporation is not responsible for your illegal activities, regardless of ignorance or malice.|,
lastUpdated => 1089039511
@ -4077,6 +4087,21 @@ Get a copy of wget and use this: <code>wget -p -r --html-extension -k http://the
lastUpdated => 1161388472,
},
'invite a friend' => {
message => q|Invite a friend|,
lastUpdated => 1181019679,
},
'user invitations email exists' => {
message => q|Email exists message|,
lastUpdated => 1181277915
},
'user invitations email exists description' => {
message => q|This is the message displayed to users who try to invite someone whose email address already exists in the system.|,
lastUpdated => 1181277914,
},
};
1;