| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Importer\Permission; |
| 4 |
|
| 5 |
use ImportWP\Common\Importer\Exception\MapperException; |
| 6 |
use ImportWP\Common\Importer\PermissionInterface; |
| 7 |
use ImportWP\Common\Model\ImporterModel; |
| 8 |
|
| 9 |
class Permission implements PermissionInterface |
| 10 |
{ |
| 11 |
private $importer_model; |
| 12 |
|
| 13 |
public function __construct(ImporterModel $importer_model) |
| 14 |
{ |
| 15 |
$this->importer_model = $importer_model; |
| 16 |
} |
| 17 |
|
| 18 |
public function validate($fields, $method, $group_id) |
| 19 |
{ |
| 20 |
$permission_method = false; |
| 21 |
|
| 22 |
if ('INSERT' === $method) { |
| 23 |
$permission_method = 'create'; |
| 24 |
} elseif ('UPDATE' === $method) { |
| 25 |
$permission_method = 'update'; |
| 26 |
} else { |
| 27 |
throw new MapperException(sprintf(__('Not enough permissions to %s record.', 'jc-importer'), 'import')); |
| 28 |
} |
| 29 |
|
| 30 |
if (false === $this->allowed_method($permission_method)) { |
| 31 |
if ('create' === $permission_method) { |
| 32 |
throw new MapperException(__('Not enough permissions to create record. No existing record matched the unique identifier, and Create is disabled. Enable Create, or check that the unique identifier can find the existing record.', 'jc-importer')); |
| 33 |
} |
| 34 |
if ('update' === $permission_method) { |
| 35 |
throw new MapperException(__('Not enough permissions to update record. An existing record matched the unique identifier, and Update is disabled. Enable Update if you intended to modify existing records.', 'jc-importer')); |
| 36 |
} |
| 37 |
throw new MapperException(sprintf(__('Not enough permissions to %s record.', 'jc-importer'), $permission_method)); |
| 38 |
} |
| 39 |
|
| 40 |
$permission_data = $this->importer_model->getPermission($permission_method); |
| 41 |
$fields = $this->validate_group($fields, $group_id, $permission_data); |
| 42 |
|
| 43 |
return $fields; |
| 44 |
} |
| 45 |
|
| 46 |
public function allowed_method($method) |
| 47 |
{ |
| 48 |
$permission_data = $this->importer_model->getPermission($method); |
| 49 |
return isset($permission_data['enabled']) && $permission_data['enabled'] !== true ? false : true; |
| 50 |
} |
| 51 |
|
| 52 |
public function validate_group($fields, $group_id, $permission_data) |
| 53 |
{ |
| 54 |
$permission_type = isset($permission_data['type']) ? $permission_data['type'] : false; |
| 55 |
if (!$permission_type) { |
| 56 |
return $fields; |
| 57 |
} |
| 58 |
|
| 59 |
$permission_fields = is_array($permission_data['fields']) ? $permission_data['fields'] : explode("\n", $permission_data['fields']); |
| 60 |
|
| 61 |
$matches = array(); |
| 62 |
foreach ($permission_fields as $field_search) { |
| 63 |
$matches = array_merge($matches, $this->match_permissions($field_search, $fields)); |
| 64 |
} |
| 65 |
|
| 66 |
if ('include' === $permission_type) { |
| 67 |
return $matches; |
| 68 |
} elseif ('exclude' === $permission_type) { |
| 69 |
$result = array(); |
| 70 |
foreach ($fields as $field_id => $field_value) { |
| 71 |
if (!isset($matches[$field_id])) { |
| 72 |
$result[$field_id] = $field_value; |
| 73 |
} |
| 74 |
} |
| 75 |
return $result; |
| 76 |
} |
| 77 |
|
| 78 |
return $fields; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Match search string against field list |
| 83 |
* |
| 84 |
* @param string $field_search |
| 85 |
* @param array $fields List of field_id => field_value |
| 86 |
* |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
private function match_permissions($field_search, $fields) |
| 90 |
{ |
| 91 |
$result = array(); |
| 92 |
// replaces * with the regex pattern |
| 93 |
$pattern = '[a-zA-Z\d_\-\.]+'; |
| 94 |
if ('*' === $field_search) { |
| 95 |
// * |
| 96 |
return $fields; |
| 97 |
} elseif (1 === preg_match("/^\*{$pattern}/i", $field_search)) { |
| 98 |
$search = substr($field_search, 1); |
| 99 |
$search = str_replace('.', '\.', $search); |
| 100 |
// *_src |
| 101 |
foreach ($fields as $field_id => $field_value) { |
| 102 |
if (1 === preg_match("/^{$pattern}{$search}$/i", $field_id)) { |
| 103 |
$result[$field_id] = $fields[$field_id]; |
| 104 |
} |
| 105 |
} |
| 106 |
} elseif (1 === preg_match("/{$pattern}\*$/i", $field_search)) { |
| 107 |
$search = substr($field_search, 0, -1); |
| 108 |
$search = str_replace('.', '\.', $search); |
| 109 |
// attachment_* |
| 110 |
foreach ($fields as $field_id => $field_value) { |
| 111 |
if (1 === preg_match("/^{$search}{$pattern}$/i", $field_id)) { |
| 112 |
$result[$field_id] = $fields[$field_id]; |
| 113 |
} |
| 114 |
} |
| 115 |
} else { |
| 116 |
if (!isset($fields[$field_search])) { |
| 117 |
return $result; |
| 118 |
} |
| 119 |
$result[$field_search] = $fields[$field_search]; |
| 120 |
} |
| 121 |
return $result; |
| 122 |
} |
| 123 |
} |
| 124 |
|