Skip to content

Commit b1274c9

Browse files
Merge branch '6.0' into 6.1
2 parents 16c3c51 + ca449db commit b1274c9

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
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: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,35 +74,47 @@ public function testGetFormattedValue()
7474
// Set an explicit base URL so we get a reliable value for the test
7575
Director::config()->set('alternate_base_url', 'http://mysite.com');
7676
$fileName = $this->submittedFile->getFileName();
77-
$message = "You don&#039;t have the right permissions to download this file";
77+
$link = 'http://mysite.com/assets/3c01bdbb26/test-SubmittedFileFieldTest.txt';
7878

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

82-
$this->loginWithPermission('ADMIN');
82+
// Userforms submission filled in by non-logged in user being emailed to recipient
83+
$this->logOut();
8384
$this->assertEquals(
8485
sprintf(
85-
'%s - <a href="http://mysite.com/assets/3c01bdbb26/test-SubmittedFileFieldTest.txt" target="_blank">Download File</a>',
86-
$fileName
86+
'%s - <a href="%s" target="_blank">%s</a> - <em>%s</em>',
87+
$fileName,
88+
$link,
89+
'Download File',
90+
'You must be logged in to view this file'
8791
),
8892
$this->submittedFile->getFormattedValue()->value
8993
);
90-
9194
$this->logOut();
92-
$this->loginWithPermission('CMS_ACCESS_CMSMain');
95+
96+
// Logged in CMS user without permissions to view file in the CMS
97+
$this->logInWithPermission('CMS_ACCESS_CMSMain');
9398
$this->assertEquals(
9499
sprintf(
95100
'<i class="icon font-icon-lock"></i> %s - <em>%s</em>',
96101
$fileName,
97-
$message
102+
'You don&#039;t have the right permissions to download this file'
98103
),
99104
$this->submittedFile->getFormattedValue()->value
100105
);
106+
$this->logOut();
101107

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

0 commit comments

Comments
 (0)