Skip to content

Commit c8bf90c

Browse files
Merge pull request #1236 from creative-commoners/pulls/5.15/you-must-log-in
FIX Renable email link to submitted file
2 parents c0eb6d6 + f1510dc commit c8bf90c

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

code/Model/Submission/SubmittedFileField.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use SilverStripe\Control\Director;
77
use SilverStripe\ORM\FieldType\DBField;
88
use SilverStripe\Versioned\Versioned;
9+
use SilverStripe\Security\Member;
10+
use SilverStripe\Security\Security;
911

1012
/**
1113
* A file uploaded on a {@link UserDefinedForm} and attached to a single
@@ -41,27 +43,40 @@ public function getFormattedValue()
4143
{
4244
$name = $this->getFileName();
4345
$link = $this->getLink(false);
44-
$title = _t(__CLASS__ . '.DOWNLOADFILE', 'Download File');
45-
$message = _t(__CLASS__ . '.INSUFFICIENTRIGHTS', 'You don\'t have the right permissions to download this file');
46-
$file = $this->getUploadedFileFromDraft();
47-
4846
if ($link) {
49-
if ($file->canView()) {
47+
$title = _t(__CLASS__ . '.DOWNLOADFILE', 'Download File');
48+
$file = $this->getUploadedFileFromDraft();
49+
if (!$file->canView()) {
50+
if (Security::getCurrentUser()) {
51+
// Logged in CMS user without permissions to view file in the CMS
52+
$default = 'You don\'t have the right permissions to download this file';
53+
$message = _t(__CLASS__ . '..INSUFFICIENTRIGHTS', $default);
54+
return DBField::create_field('HTMLText', sprintf(
55+
'<i class="icon font-icon-lock"></i> %s - <em>%s</em>',
56+
htmlspecialchars($name, ENT_QUOTES),
57+
htmlspecialchars($message, ENT_QUOTES)
58+
));
59+
} else {
60+
// Userforms submission filled in by non-logged in user being emailed to recipient
61+
$message = _t(__CLASS__ . '.YOUMUSTBELOGGEDIN', 'You must be logged in to view this file');
62+
return DBField::create_field('HTMLText', sprintf(
63+
'%s - <a href="%s" target="_blank">%s</a> - <em>%s</em>',
64+
htmlspecialchars($name, ENT_QUOTES),
65+
htmlspecialchars($link, ENT_QUOTES),
66+
htmlspecialchars($title, ENT_QUOTES),
67+
htmlspecialchars($message, ENT_QUOTES)
68+
));
69+
}
70+
} else {
71+
// Logged in CMS user with permissions to view file in the CMS
5072
return DBField::create_field('HTMLText', sprintf(
5173
'%s - <a href="%s" target="_blank">%s</a>',
5274
htmlspecialchars($name, ENT_QUOTES),
5375
htmlspecialchars($link, ENT_QUOTES),
5476
htmlspecialchars($title, ENT_QUOTES)
5577
));
56-
} else {
57-
return DBField::create_field('HTMLText', sprintf(
58-
'<i class="icon font-icon-lock"></i> %s - <em>%s</em>',
59-
htmlspecialchars($name, ENT_QUOTES),
60-
htmlspecialchars($message, ENT_QUOTES)
61-
));
6278
}
6379
}
64-
6580
return false;
6681
}
6782

lang/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ en:
328328
one: 'A Submitted File Field'
329329
other: '{count} Submitted File Fields'
330330
SINGULARNAME: 'Submitted File Field'
331+
YOUMUSTBELOGGEDIN: 'You must be logged in to view this file'
331332
has_one_UploadedFile: 'Uploaded file'
332333
SilverStripe\UserForms\Model\Submission\SubmittedForm:
333334
PLURALNAME: 'Submitted Forms'

tests/php/Model/SubmittedFileFieldTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,34 +73,45 @@ public function testGetFormattedValue()
7373
// Set an explicit base URL so we get a reliable value for the test
7474
Director::config()->set('alternate_base_url', 'http://mysite.com');
7575
$fileName = $this->submittedFile->getFileName();
76-
$message = "You don&#039;t have the right permissions to download this file";
76+
$link = 'http://mysite.com/assets/3c01bdbb26/test-SubmittedFileFieldTest.txt';
7777

7878
$this->file->CanViewType = 'OnlyTheseUsers';
7979
$this->file->write();
8080

81-
$this->loginWithPermission('ADMIN');
81+
// Userforms submission filled in by non-logged in user being emailed to recipient
82+
$this->logOut();
8283
$this->assertEquals(
8384
sprintf(
84-
'%s - <a href="http://mysite.com/assets/3c01bdbb26/test-SubmittedFileFieldTest.txt" target="_blank">Download File</a>',
85-
$fileName
85+
'%s - <a href="%s" target="_blank">%s</a> - <em>%s</em>',
86+
$fileName,
87+
$link,
88+
'Download File',
89+
'You must be logged in to view this file'
8690
),
8791
$this->submittedFile->getFormattedValue()->value
8892
);
8993

90-
$this->loginWithPermission('CMS_ACCESS_CMSMain');
94+
// Logged in CMS user without permissions to view file in the CMS
95+
$this->logInWithPermission('CMS_ACCESS_CMSMain');
9196
$this->assertEquals(
9297
sprintf(
9398
'<i class="icon font-icon-lock"></i> %s - <em>%s</em>',
9499
$fileName,
95-
$message
100+
'You don&#039;t have the right permissions to download this file'
96101
),
97102
$this->submittedFile->getFormattedValue()->value
98103
);
99104

100-
$store = Injector::inst()->get(AssetStore::class);
101-
$this->assertFalse(
102-
$store->canView($fileName, $this->file->getHash()),
103-
'Users without canView rights on the file should not have been session granted access to it'
105+
// Logged in CMS user with permissions to view file in the CMS
106+
$this->loginWithPermission('ADMIN');
107+
$this->assertEquals(
108+
sprintf(
109+
'%s - <a href="%s" target="_blank">%s</a>',
110+
$fileName,
111+
$link,
112+
'Download File'
113+
),
114+
$this->submittedFile->getFormattedValue()->value
104115
);
105116
}
106117
}

0 commit comments

Comments
 (0)