2013-07-10 20:46:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class LegalpadDocumentSignController extends LegalpadController {
|
|
|
|
|
|
|
|
private $id;
|
|
|
|
|
2014-01-15 02:17:18 +01:00
|
|
|
public function shouldRequireLogin() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-10 20:46:39 +02:00
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = $data['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
2014-06-26 03:38:07 +02:00
|
|
|
$viewer = $request->getUser();
|
2013-07-10 20:46:39 +02:00
|
|
|
|
|
|
|
$document = id(new LegalpadDocumentQuery())
|
2014-06-26 03:38:07 +02:00
|
|
|
->setViewer($viewer)
|
2013-07-10 20:46:39 +02:00
|
|
|
->withIDs(array($this->id))
|
|
|
|
->needDocumentBodies(true)
|
|
|
|
->executeOne();
|
|
|
|
if (!$document) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2014-01-15 02:17:18 +01:00
|
|
|
$signer_phid = null;
|
|
|
|
$signature_data = array();
|
2014-06-26 03:38:07 +02:00
|
|
|
if ($viewer->isLoggedIn()) {
|
|
|
|
$signer_phid = $viewer->getPHID();
|
2014-01-15 02:17:18 +01:00
|
|
|
$signature_data = array(
|
2014-06-26 16:16:42 +02:00
|
|
|
'name' => $viewer->getRealName(),
|
|
|
|
'email' => $viewer->loadPrimaryEmailAddress(),
|
|
|
|
);
|
2014-01-15 02:17:18 +01:00
|
|
|
} else if ($request->isFormPost()) {
|
|
|
|
$email = new PhutilEmailAddress($request->getStr('email'));
|
2014-06-26 16:16:42 +02:00
|
|
|
if (strlen($email->getDomainName())) {
|
|
|
|
$email_obj = id(new PhabricatorUserEmail())
|
|
|
|
->loadOneWhere('address = %s', $email->getAddress());
|
|
|
|
if ($email_obj) {
|
|
|
|
return $this->signInResponse();
|
|
|
|
}
|
|
|
|
$external_account = id(new PhabricatorExternalAccountQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withAccountTypes(array('email'))
|
|
|
|
->withAccountDomains(array($email->getDomainName()))
|
|
|
|
->withAccountIDs(array($email->getAddress()))
|
|
|
|
->loadOneOrCreate();
|
|
|
|
if ($external_account->getUserPHID()) {
|
|
|
|
return $this->signInResponse();
|
|
|
|
}
|
|
|
|
$signer_phid = $external_account->getPHID();
|
2014-01-15 02:17:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 16:16:42 +02:00
|
|
|
$signature = null;
|
2014-01-15 02:17:18 +01:00
|
|
|
if ($signer_phid) {
|
2014-06-26 16:16:42 +02:00
|
|
|
// TODO: This is odd and should probably be adjusted after grey/external
|
|
|
|
// accounts work better, but use the omnipotent viewer to check for a
|
|
|
|
// signature so we can pick up anonymous/grey signatures.
|
|
|
|
|
2014-01-17 20:40:26 +01:00
|
|
|
$signature = id(new LegalpadDocumentSignatureQuery())
|
2014-06-26 16:16:42 +02:00
|
|
|
->setViewer(PhabricatorUser::getOmnipotentUser())
|
2014-01-17 20:40:26 +01:00
|
|
|
->withDocumentPHIDs(array($document->getPHID()))
|
|
|
|
->withSignerPHIDs(array($signer_phid))
|
|
|
|
->withDocumentVersions(array($document->getVersions()))
|
|
|
|
->executeOne();
|
2014-06-26 16:16:42 +02:00
|
|
|
|
|
|
|
if ($signature && !$viewer->isLoggedIn()) {
|
|
|
|
return $this->newDialog()
|
|
|
|
->setTitle(pht('Already Signed'))
|
|
|
|
->appendParagraph(pht('You have already signed this document!'))
|
|
|
|
->addCancelButton('/'.$document->getMonogram(), pht('Okay'));
|
|
|
|
}
|
2014-01-15 02:17:18 +01:00
|
|
|
}
|
2013-07-10 20:46:39 +02:00
|
|
|
|
2014-06-26 16:16:42 +02:00
|
|
|
$signed_status = null;
|
2013-07-10 20:46:39 +02:00
|
|
|
if (!$signature) {
|
|
|
|
$has_signed = false;
|
|
|
|
$signature = id(new LegalpadDocumentSignature())
|
2014-01-15 02:17:18 +01:00
|
|
|
->setSignerPHID($signer_phid)
|
2013-07-10 20:46:39 +02:00
|
|
|
->setDocumentPHID($document->getPHID())
|
2014-01-15 02:17:18 +01:00
|
|
|
->setDocumentVersion($document->getVersions())
|
|
|
|
->setSignatureData($signature_data);
|
2014-06-26 16:16:42 +02:00
|
|
|
|
|
|
|
// If the user is logged in, show a notice that they haven't signed.
|
|
|
|
// If they aren't logged in, we can't be as sure, so don't show anything.
|
|
|
|
if ($viewer->isLoggedIn()) {
|
|
|
|
$signed_status = id(new AphrontErrorView())
|
|
|
|
->setSeverity(AphrontErrorView::SEVERITY_WARNING)
|
|
|
|
->setErrors(
|
|
|
|
array(
|
|
|
|
pht('You have not signed this document yet.'),
|
|
|
|
));
|
|
|
|
}
|
2013-07-10 20:46:39 +02:00
|
|
|
} else {
|
|
|
|
$has_signed = true;
|
2014-01-15 02:17:18 +01:00
|
|
|
$signature_data = $signature->getSignatureData();
|
2014-06-26 16:16:42 +02:00
|
|
|
|
|
|
|
// In this case, we know they've signed.
|
|
|
|
$signed_at = $signature->getDateCreated();
|
|
|
|
$signed_status = id(new AphrontErrorView())
|
|
|
|
->setSeverity(AphrontErrorView::SEVERITY_NOTICE)
|
|
|
|
->setErrors(
|
|
|
|
array(
|
|
|
|
pht(
|
|
|
|
'You signed this document on %s.',
|
|
|
|
phabricator_datetime($signed_at, $viewer)),
|
|
|
|
));
|
2013-07-10 20:46:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$e_name = true;
|
|
|
|
$e_email = true;
|
2014-06-26 16:16:42 +02:00
|
|
|
$e_agree = null;
|
|
|
|
|
2013-07-10 20:46:39 +02:00
|
|
|
$errors = array();
|
2014-06-29 15:16:48 +02:00
|
|
|
if ($request->isFormOrHisecPost() && !$has_signed) {
|
|
|
|
|
|
|
|
// Require two-factor auth to sign legal documents.
|
|
|
|
$engine = new PhabricatorAuthSessionEngine();
|
|
|
|
$engine->requireHighSecuritySession(
|
|
|
|
$viewer,
|
|
|
|
$request,
|
|
|
|
'/'.$document->getMonogram());
|
|
|
|
|
2013-07-10 20:46:39 +02:00
|
|
|
$name = $request->getStr('name');
|
|
|
|
$agree = $request->getExists('agree');
|
|
|
|
|
2014-06-26 16:16:42 +02:00
|
|
|
if (!strlen($name)) {
|
2013-07-10 20:46:39 +02:00
|
|
|
$e_name = pht('Required');
|
|
|
|
$errors[] = pht('Name field is required.');
|
2014-06-26 16:16:42 +02:00
|
|
|
} else {
|
|
|
|
$e_name = null;
|
2013-07-10 20:46:39 +02:00
|
|
|
}
|
2014-01-15 02:17:18 +01:00
|
|
|
$signature_data['name'] = $name;
|
2013-07-10 20:46:39 +02:00
|
|
|
|
2014-06-26 16:16:42 +02:00
|
|
|
if ($viewer->isLoggedIn()) {
|
|
|
|
$email = $viewer->loadPrimaryEmailAddress();
|
2013-07-10 20:46:39 +02:00
|
|
|
} else {
|
2014-06-26 16:16:42 +02:00
|
|
|
$email = $request->getStr('email');
|
|
|
|
|
|
|
|
$addr_obj = null;
|
|
|
|
if (!strlen($email)) {
|
|
|
|
$e_email = pht('Required');
|
|
|
|
$errors[] = pht('Email field is required.');
|
|
|
|
} else {
|
|
|
|
$addr_obj = new PhutilEmailAddress($email);
|
|
|
|
$domain = $addr_obj->getDomainName();
|
|
|
|
if (!$domain) {
|
|
|
|
$e_email = pht('Invalid');
|
|
|
|
$errors[] = pht('A valid email is required.');
|
|
|
|
} else {
|
|
|
|
$e_email = null;
|
|
|
|
}
|
2013-07-10 20:46:39 +02:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 02:17:18 +01:00
|
|
|
$signature_data['email'] = $email;
|
2013-07-10 20:46:39 +02:00
|
|
|
|
2014-01-15 02:17:18 +01:00
|
|
|
$signature->setSignatureData($signature_data);
|
2013-07-10 20:46:39 +02:00
|
|
|
|
|
|
|
if (!$agree) {
|
|
|
|
$errors[] = pht(
|
|
|
|
'You must check "I agree to the terms laid forth above."');
|
2014-06-26 16:16:42 +02:00
|
|
|
$e_agree = pht('Required');
|
2013-07-10 20:46:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-26 16:16:42 +02:00
|
|
|
if ($viewer->isLoggedIn()) {
|
|
|
|
$verified = LegalpadDocumentSignature::VERIFIED;
|
|
|
|
} else {
|
|
|
|
$verified = LegalpadDocumentSignature::UNVERIFIED;
|
2014-01-15 02:17:18 +01:00
|
|
|
}
|
|
|
|
$signature->setVerified($verified);
|
|
|
|
|
2013-07-10 20:46:39 +02:00
|
|
|
if (!$errors) {
|
|
|
|
$signature->save();
|
2014-06-26 16:16:42 +02:00
|
|
|
|
|
|
|
// If the viewer is logged in, send them to the document page, which
|
|
|
|
// will show that they have signed the document. Otherwise, send them
|
|
|
|
// to a completion page.
|
|
|
|
if ($viewer->isLoggedIn()) {
|
|
|
|
$next_uri = '/'.$document->getMonogram();
|
2014-01-15 02:17:18 +01:00
|
|
|
} else {
|
2014-06-26 16:16:42 +02:00
|
|
|
$next_uri = $this->getApplicationURI('done/');
|
2014-01-15 02:17:18 +01:00
|
|
|
}
|
2014-06-26 16:16:42 +02:00
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())->setURI($next_uri);
|
2013-07-10 20:46:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$document_body = $document->getDocumentBody();
|
|
|
|
$engine = id(new PhabricatorMarkupEngine())
|
2014-06-26 03:38:07 +02:00
|
|
|
->setViewer($viewer);
|
2013-07-10 20:46:39 +02:00
|
|
|
$engine->addObject(
|
|
|
|
$document_body,
|
|
|
|
LegalpadDocumentBody::MARKUP_FIELD_TEXT);
|
|
|
|
$engine->process();
|
|
|
|
|
2014-06-26 16:16:42 +02:00
|
|
|
$document_markup = $engine->getOutput(
|
|
|
|
$document_body,
|
|
|
|
LegalpadDocumentBody::MARKUP_FIELD_TEXT);
|
|
|
|
|
2013-07-10 20:46:39 +02:00
|
|
|
$title = $document_body->getTitle();
|
|
|
|
|
2014-06-26 03:38:07 +02:00
|
|
|
$manage_uri = $this->getApplicationURI('view/'.$document->getID().'/');
|
|
|
|
|
|
|
|
$can_edit = PhabricatorPolicyFilter::hasCapability(
|
|
|
|
$viewer,
|
|
|
|
$document,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT);
|
|
|
|
|
2013-09-17 18:12:37 +02:00
|
|
|
$header = id(new PHUIHeaderView())
|
2014-06-26 03:38:07 +02:00
|
|
|
->setHeader($title)
|
|
|
|
->addActionLink(
|
|
|
|
id(new PHUIButtonView())
|
|
|
|
->setTag('a')
|
|
|
|
->setIcon(
|
|
|
|
id(new PHUIIconView())
|
|
|
|
->setIconFont('fa-pencil'))
|
|
|
|
->setText(pht('Manage Document'))
|
|
|
|
->setHref($manage_uri)
|
|
|
|
->setDisabled(!$can_edit)
|
|
|
|
->setWorkflow(!$can_edit));
|
2013-07-10 20:46:39 +02:00
|
|
|
|
2014-06-26 16:16:42 +02:00
|
|
|
$content = id(new PHUIDocumentView())
|
|
|
|
->addClass('legalpad')
|
|
|
|
->setHeader($header)
|
2014-06-27 16:20:14 +02:00
|
|
|
->setFontKit(PHUIDocumentView::FONT_SOURCE_SANS)
|
2014-06-26 16:16:42 +02:00
|
|
|
->appendChild(
|
|
|
|
array(
|
|
|
|
$signed_status,
|
|
|
|
$document_markup,
|
|
|
|
));
|
|
|
|
|
|
|
|
if (!$has_signed) {
|
|
|
|
$error_view = null;
|
|
|
|
if ($errors) {
|
|
|
|
$error_view = id(new AphrontErrorView())
|
|
|
|
->setErrors($errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
$signature_form = $this->buildSignatureForm(
|
|
|
|
$document_body,
|
|
|
|
$signature,
|
|
|
|
$e_name,
|
|
|
|
$e_email,
|
|
|
|
$e_agree);
|
|
|
|
|
2014-06-27 16:20:14 +02:00
|
|
|
$subheader = id(new PHUIHeaderView())
|
|
|
|
->setHeader(pht('Agree and Sign Document'))
|
|
|
|
->setBleedHeader(true);
|
|
|
|
|
2014-06-26 16:16:42 +02:00
|
|
|
$content->appendChild(
|
|
|
|
array(
|
2014-06-27 16:20:14 +02:00
|
|
|
$subheader,
|
2014-06-26 16:16:42 +02:00
|
|
|
$error_view,
|
|
|
|
$signature_form,
|
|
|
|
));
|
|
|
|
}
|
2013-07-10 20:46:39 +02:00
|
|
|
|
2014-06-26 03:38:07 +02:00
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
|
|
|
$crumbs->addTextCrumb($document->getMonogram());
|
|
|
|
|
2013-07-10 20:46:39 +02:00
|
|
|
return $this->buildApplicationPage(
|
2014-06-26 03:38:07 +02:00
|
|
|
array(
|
|
|
|
$crumbs,
|
|
|
|
$content,
|
|
|
|
),
|
2013-07-10 20:46:39 +02:00
|
|
|
array(
|
|
|
|
'title' => $title,
|
|
|
|
'pageObjects' => array($document->getPHID()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildSignatureForm(
|
|
|
|
LegalpadDocumentBody $body,
|
|
|
|
LegalpadDocumentSignature $signature,
|
2014-06-26 16:16:42 +02:00
|
|
|
$e_name,
|
|
|
|
$e_email,
|
|
|
|
$e_agree) {
|
2013-07-10 20:46:39 +02:00
|
|
|
|
2014-06-26 03:38:07 +02:00
|
|
|
$viewer = $this->getRequest()->getUser();
|
2013-07-10 20:46:39 +02:00
|
|
|
$data = $signature->getSignatureData();
|
2014-06-26 16:16:42 +02:00
|
|
|
|
2013-07-10 20:46:39 +02:00
|
|
|
$form = id(new AphrontFormView())
|
2014-06-26 03:38:07 +02:00
|
|
|
->setUser($viewer)
|
2013-07-10 20:46:39 +02:00
|
|
|
->appendChild(
|
2013-10-30 23:50:46 +01:00
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setLabel(pht('Name'))
|
|
|
|
->setValue(idx($data, 'name', ''))
|
|
|
|
->setName('name')
|
2014-06-26 16:16:42 +02:00
|
|
|
->setError($e_name));
|
|
|
|
|
|
|
|
if (!$viewer->isLoggedIn()) {
|
|
|
|
$form->appendChild(
|
2013-10-30 23:50:46 +01:00
|
|
|
id(new AphrontFormTextControl())
|
2014-06-26 16:16:42 +02:00
|
|
|
->setLabel(pht('Email'))
|
|
|
|
->setValue(idx($data, 'email', ''))
|
|
|
|
->setName('email')
|
|
|
|
->setError($e_email));
|
|
|
|
}
|
|
|
|
|
|
|
|
$form
|
2013-10-30 23:50:46 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormCheckboxControl())
|
2014-06-26 16:16:42 +02:00
|
|
|
->setError($e_agree)
|
|
|
|
->addCheckbox(
|
|
|
|
'agree',
|
|
|
|
'agree',
|
|
|
|
pht('I agree to the terms laid forth above.'),
|
|
|
|
false))
|
2013-10-30 23:50:46 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2014-06-26 16:16:42 +02:00
|
|
|
->setValue(pht('Sign Document'))
|
|
|
|
->addCancelButton($this->getApplicationURI()));
|
2014-06-26 03:38:07 +02:00
|
|
|
|
2014-06-26 07:02:40 +02:00
|
|
|
return $form;
|
2014-01-15 02:17:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function sendVerifySignatureEmail(
|
|
|
|
LegalpadDocument $doc,
|
|
|
|
LegalpadDocumentSignature $signature) {
|
|
|
|
|
|
|
|
$signature_data = $signature->getSignatureData();
|
|
|
|
$email = new PhutilEmailAddress($signature_data['email']);
|
|
|
|
$doc_link = PhabricatorEnv::getProductionURI($doc->getMonogram());
|
|
|
|
$path = $this->getApplicationURI(sprintf(
|
|
|
|
'/verify/%s/',
|
|
|
|
$signature->getSecretKey()));
|
|
|
|
$link = PhabricatorEnv::getProductionURI($path);
|
|
|
|
|
|
|
|
$body = <<<EOBODY
|
|
|
|
Hi {$signature_data['name']},
|
|
|
|
|
|
|
|
This email address was used to sign a Legalpad document ({$doc_link}).
|
|
|
|
Please verify you own this email address by clicking this link:
|
|
|
|
|
|
|
|
{$link}
|
|
|
|
|
|
|
|
Your signature is invalid until you verify you own the email.
|
|
|
|
EOBODY;
|
|
|
|
|
|
|
|
id(new PhabricatorMetaMTAMail())
|
|
|
|
->addRawTos(array($email->getAddress()))
|
|
|
|
->setSubject(pht('[Legalpad] Signature Verification'))
|
|
|
|
->setBody($body)
|
|
|
|
->setRelatedPHID($signature->getDocumentPHID())
|
|
|
|
->saveAndSend();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function signInResponse() {
|
|
|
|
return id(new Aphront403Response())
|
|
|
|
->setForbiddenText(pht(
|
|
|
|
'The email address specified is associated with an account. '.
|
|
|
|
'Please login to that account and sign this document again.'));
|
2013-07-10 20:46:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|