Skip to content

Commit 3de87a3

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-95646' into 2.1.16-develop-pr60
2 parents be4dcea + 1350681 commit 3de87a3

File tree

6 files changed

+70
-12
lines changed

6 files changed

+70
-12
lines changed

app/code/Magento/Eav/Model/Attribute/Data/File.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ public function extractValue(RequestInterface $request)
119119
}
120120

121121
/**
122-
* Validate file by attribute validate rules
123-
* Return array of errors
122+
* Validate file by attribute validate rules and return array of errors.
124123
*
125124
* @param array $value
126125
* @return string[]
@@ -146,7 +145,7 @@ protected function _validateByRules($value)
146145
return $this->_fileValidator->getMessages();
147146
}
148147

149-
if (!is_uploaded_file($value['tmp_name'])) {
148+
if (!empty($value['tmp_name']) && !is_uploaded_file($value['tmp_name'])) {
150149
return [__('"%1" is not a valid file.', $label)];
151150
}
152151

@@ -173,13 +172,23 @@ public function validateValue($value)
173172
if ($this->getIsAjaxRequest()) {
174173
return true;
175174
}
175+
$fileData = $value;
176+
177+
if (is_string($value) && !empty($value)) {
178+
$dir = $this->_directory->getAbsolutePath($this->getAttribute()->getEntityType()->getEntityTypeCode());
179+
$fileData = [
180+
'size' => filesize($dir . $value),
181+
'name' => $value,
182+
'tmp_name' => $dir . $value,
183+
];
184+
}
176185

177186
$errors = [];
178187
$attribute = $this->getAttribute();
179188
$label = $attribute->getStoreLabel();
180189

181190
$toDelete = !empty($value['delete']) ? true : false;
182-
$toUpload = !empty($value['tmp_name']) ? true : false;
191+
$toUpload = !empty($value['tmp_name']) || is_string($value) && !empty($value) ? true : false;
183192

184193
if (!$toUpload && !$toDelete && $this->getEntity()->getData($attribute->getAttributeCode())) {
185194
return true;
@@ -194,11 +203,13 @@ public function validateValue($value)
194203
}
195204

196205
if ($toUpload) {
197-
$errors = array_merge($errors, $this->_validateByRules($value));
206+
$errors = array_merge($errors, $this->_validateByRules($fileData));
198207
}
199208

200209
if (count($errors) == 0) {
201210
return true;
211+
} elseif (is_string($value) && !empty($value)) {
212+
$this->_directory->delete($dir . $value);
202213
}
203214

204215
return $errors;

dev/tests/js/jasmine/tests/lib/mage/validation.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,24 @@ define([
132132
).toEqual(false);
133133
});
134134
});
135+
136+
describe('Testing validate-forbidden-extensions', function () {
137+
it('validate-forbidden-extensions', function () {
138+
var el1 = $('<input type="text" value="" ' +
139+
'class="validate-extensions" data-validation-params="php,phtml">').get(0);
140+
141+
expect($.validator.methods['validate-forbidden-extensions']
142+
.call($.validator.prototype, 'php', el1, null)).toEqual(false);
143+
expect($.validator.methods['validate-forbidden-extensions']
144+
.call($.validator.prototype, 'php,phtml', el1, null)).toEqual(false);
145+
expect($.validator.methods['validate-forbidden-extensions']
146+
.call($.validator.prototype, 'html', el1, null)).toEqual(true);
147+
expect($.validator.methods['validate-forbidden-extensions']
148+
.call($.validator.prototype, 'html,png', el1, null)).toEqual(true);
149+
expect($.validator.methods['validate-forbidden-extensions']
150+
.call($.validator.prototype, 'php,html', el1, null)).toEqual(false);
151+
expect($.validator.methods['validate-forbidden-extensions']
152+
.call($.validator.prototype, 'html,php', el1, null)).toEqual(false);
153+
});
154+
});
135155
});

lib/internal/Magento/Framework/Data/Form/Element/Text.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
/**
8-
* Form text element
9-
*
10-
* @author Magento Core Team <core@magentocommerce.com>
11-
*/
127
namespace Magento\Framework\Data\Form\Element;
138

149
use Magento\Framework\Escaper;
1510

11+
/**
12+
* Form text element
13+
*/
1614
class Text extends AbstractElement
1715
{
1816
/**
@@ -65,7 +63,8 @@ public function getHtmlAttributes()
6563
'placeholder',
6664
'data-form-part',
6765
'data-role',
68-
'data-action'
66+
'data-validation-params',
67+
'data-action',
6968
];
7069
}
7170
}

lib/internal/Magento/Framework/Filesystem/Driver/File.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,13 @@ public function fileUnlock($resource)
843843
*/
844844
public function getAbsolutePath($basePath, $path, $scheme = null)
845845
{
846+
// check if the path given is already an absolute path containing the
847+
// basepath. so if the basepath starts at position 0 in the path, we
848+
// must not concatinate them again because path is already absolute.
849+
if (0 === strpos($path, $basePath)) {
850+
return $this->getScheme($scheme) . $path;
851+
}
852+
846853
return $this->getScheme($scheme) . $basePath . ltrim($this->fixSeparator($path), '/');
847854
}
848855

lib/internal/Magento/Framework/Filesystem/Test/Unit/Driver/FileTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function dataProviderForTestGetAbsolutePath()
4040
['/root/path/', 'sub', '/root/path/sub'],
4141
['/root/path/', '/sub', '/root/path/sub'],
4242
['/root/path/', '../sub', '/root/path/../sub'],
43-
['/root/path/', '/root/path/sub', '/root/path/root/path/sub'],
43+
['/root/path/', '/root/path/sub', '/root/path/sub'],
4444
];
4545
}
4646

lib/web/mage/validation.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,27 @@
782782
$.mage.__('The value is not within the specified range.'),
783783
true
784784
],
785+
'validate-forbidden-extensions': [
786+
function (v, elem) {
787+
var forbiddenExtensions = $(elem).attr('data-validation-params'),
788+
forbiddenExtensionsArray = forbiddenExtensions.split(','),
789+
extensionsArray = v.split(','),
790+
result = true;
791+
792+
this.validateExtensionsMessage = $.mage.__('Forbidden extensions has been used. Avoid usage of ') +
793+
forbiddenExtensions;
794+
795+
$.each(extensionsArray, function (key, extension) {
796+
if (forbiddenExtensionsArray.indexOf(extension) !== -1) {
797+
result = false;
798+
}
799+
});
800+
801+
return result;
802+
}, function () {
803+
return this.validateExtensionsMessage;
804+
}
805+
],
785806
'validate-range': [
786807
function (v, elm) {
787808
var minValue, maxValue;

0 commit comments

Comments
 (0)