Skip to content

Commit 56449e1

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-95647' into 2.2.7-develop-pr53
2 parents 2951789 + add1451 commit 56449e1

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
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
@@ -120,8 +120,7 @@ public function extractValue(RequestInterface $request)
120120
}
121121

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

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

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

178187
$errors = [];
179188
$attribute = $this->getAttribute();
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;
@@ -195,11 +204,13 @@ public function validateValue($value)
195204
}
196205

197206
if ($toUpload) {
198-
$errors = array_merge($errors, $this->_validateByRules($value));
207+
$errors = array_merge($errors, $this->_validateByRules($fileData));
199208
}
200209

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

205216
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
@@ -1142,4 +1142,24 @@ define([
11421142
.call($.validator.prototype, '30', el1, null)).toEqual(false);
11431143
});
11441144
});
1145+
1146+
describe('Testing validate-forbidden-extensions', function () {
1147+
it('validate-forbidden-extensions', function () {
1148+
var el1 = $('<input type="text" value="" ' +
1149+
'class="validate-extensions" data-validation-params="php,phtml">').get(0);
1150+
1151+
expect($.validator.methods['validate-forbidden-extensions']
1152+
.call($.validator.prototype, 'php', el1, null)).toEqual(false);
1153+
expect($.validator.methods['validate-forbidden-extensions']
1154+
.call($.validator.prototype, 'php,phtml', el1, null)).toEqual(false);
1155+
expect($.validator.methods['validate-forbidden-extensions']
1156+
.call($.validator.prototype, 'html', el1, null)).toEqual(true);
1157+
expect($.validator.methods['validate-forbidden-extensions']
1158+
.call($.validator.prototype, 'html,png', el1, null)).toEqual(true);
1159+
expect($.validator.methods['validate-forbidden-extensions']
1160+
.call($.validator.prototype, 'php,html', el1, null)).toEqual(false);
1161+
expect($.validator.methods['validate-forbidden-extensions']
1162+
.call($.validator.prototype, 'html,php', el1, null)).toEqual(false);
1163+
});
1164+
});
11451165
});

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/web/mage/validation.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,27 @@
881881
},
882882
$.mage.__('Please enter a valid number in this field.')
883883
],
884+
'validate-forbidden-extensions': [
885+
function (v, elem) {
886+
var forbiddenExtensions = $(elem).attr('data-validation-params'),
887+
forbiddenExtensionsArray = forbiddenExtensions.split(','),
888+
extensionsArray = v.split(','),
889+
result = true;
890+
891+
this.validateExtensionsMessage = $.mage.__('Forbidden extensions has been used. Avoid usage of ') +
892+
forbiddenExtensions;
893+
894+
$.each(extensionsArray, function (key, extension) {
895+
if (forbiddenExtensionsArray.indexOf(extension) !== -1) {
896+
result = false;
897+
}
898+
});
899+
900+
return result;
901+
}, function () {
902+
return this.validateExtensionsMessage;
903+
}
904+
],
884905
'validate-digits-range': [
885906
function (v, elm, param) {
886907
var numValue, dataAttrRange, classNameRange, result, range, m, classes, ii;

0 commit comments

Comments
 (0)