verify permission on taxonomy importer #274

This commit is contained in:
leogermani 2019-12-06 16:29:06 -03:00
parent 3175d7e8d8
commit 58621f3538
1 changed files with 29 additions and 19 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
/** /**
* @author: MediaLab-UFG(Vinicius Nunes). * @author: MediaLab-UFG(Vinicius Nunes).
* Term Importer * Term Importer
* *
@ -36,7 +36,7 @@ class Term_Importer extends Importer {
'new_taxonomy' => '' 'new_taxonomy' => ''
]); ]);
} }
public function options_form() { public function options_form() {
ob_start(); ob_start();
?> ?>
@ -55,7 +55,7 @@ class Term_Importer extends Importer {
<div class="help-tooltip-body"> <div class="help-tooltip-body">
<p><?php _e('The character used to separate each column in your CSV (e.g. , or ;)', 'tainacan'); ?></p> <p><?php _e('The character used to separate each column in your CSV (e.g. , or ;)', 'tainacan'); ?></p>
</div> </div>
</div> </div>
</span> </span>
<div class="control is-clearfix"> <div class="control is-clearfix">
<input class="input" type="text" name="delimiter" value="<?php echo $this->get_option('delimiter'); ?>"> <input class="input" type="text" name="delimiter" value="<?php echo $this->get_option('delimiter'); ?>">
@ -78,7 +78,7 @@ class Term_Importer extends Importer {
<p><?php _e('Inform the taxonomy you want to import the terms to.', 'tainacan'); ?></p> <p><?php _e('Inform the taxonomy you want to import the terms to.', 'tainacan'); ?></p>
<p><?php _e('Select an existing taxonomy or create a new one on the fly.', 'tainacan'); ?></p> <p><?php _e('Select an existing taxonomy or create a new one on the fly.', 'tainacan'); ?></p>
</div> </div>
</div> </div>
</span> </span>
<div class="control is-clearfix"> <div class="control is-clearfix">
<div class="select"> <div class="select">
@ -94,25 +94,25 @@ class Term_Importer extends Importer {
} }
?> ?>
</select> </select>
</div> </div>
<input class="input new_taxonomy" type="text" name="new_taxonomy" value="<?php echo $this->get_option('new_taxonomy'); ?>" placeholder="<?php _e('New taxonomy name', 'tainacan'); ?>" > <input class="input new_taxonomy" type="text" name="new_taxonomy" value="<?php echo $this->get_option('new_taxonomy'); ?>" placeholder="<?php _e('New taxonomy name', 'tainacan'); ?>" >
</div> </div>
</div> </div>
<?php <?php
return ob_get_clean(); return ob_get_clean();
} }
public function process_item($index, $collection_definition) { public function process_item($index, $collection_definition) {
return true; return true;
} }
public function create_terms( ) { public function create_terms( ) {
if (($handle = fopen($this->tmp_file, "r")) !== false) { if (($handle = fopen($this->tmp_file, "r")) !== false) {
$file = $handle; $file = $handle;
$this->set_current_step_total( filesize($this->tmp_file) ); $this->set_current_step_total( filesize($this->tmp_file) );
@ -126,7 +126,14 @@ class Term_Importer extends Importer {
$position = $this->get_transient('position') == null ? 0: $this->get_transient('position'); $position = $this->get_transient('position') == null ? 0: $this->get_transient('position');
$last_term = $this->get_transient('last_term') == null ? 0: $this->get_transient('last_term'); $last_term = $this->get_transient('last_term') == null ? 0: $this->get_transient('last_term');
$id_taxonomy= $this->get_transient('new_taxonomy'); $id_taxonomy= $this->get_transient('new_taxonomy');
$taxonomy = \tainacan_taxonomies()->fetch( (int) $id_taxonomy );
if ( $taxonomy instanceof Entities\Taxonomy && ! $taxonomy->can_edit() ) {
$this->add_error_log("You don't have permission to add terms to this taxonomy");
$this->abort();
return false;
}
$position_file = $this->get_in_step_count(); $position_file = $this->get_in_step_count();
fseek($file, $position_file); fseek($file, $position_file);
if (($values = fgetcsv($file, 0, $this->get_option('delimiter'), '"')) !== FALSE) { if (($values = fgetcsv($file, 0, $this->get_option('delimiter'), '"')) !== FALSE) {
@ -144,16 +151,16 @@ class Term_Importer extends Importer {
$this->abort(); $this->abort();
return false; return false;
} }
$term = new \Tainacan\Entities\Term(); $term = new \Tainacan\Entities\Term();
$term->set_name($values[$position]); $term->set_name($values[$position]);
$term->set_description($values[$position+1]); $term->set_description($values[$position+1]);
$term->set_taxonomy($id_taxonomy); $term->set_taxonomy($id_taxonomy);
$term_repo = \Tainacan\Repositories\Terms::get_instance(); $term_repo = \Tainacan\Repositories\Terms::get_instance();
if(end($parent)) if(end($parent))
$term->set_parent(end($parent)); $term->set_parent(end($parent));
if ($term->validate()) { if ($term->validate()) {
$term_insert = $term_repo->insert($term); $term_insert = $term_repo->insert($term);
$last_term = $term_insert->get_id(); $last_term = $term_insert->get_id();
@ -179,19 +186,22 @@ class Term_Importer extends Importer {
$this->add_transient('new_taxonomy', $this->get_option('select_taxonomy')); $this->add_transient('new_taxonomy', $this->get_option('select_taxonomy'));
return false; return false;
} }
if ( $this->get_option('select_taxonomy') == '' && $this->get_option('new_taxonomy') == '' ) { if ( $this->get_option('select_taxonomy') == '' && $this->get_option('new_taxonomy') == '' ) {
$this->abort(); $this->abort();
$this->add_error_log('No taxonomy selected'); $this->add_error_log('No taxonomy selected');
return false; return false;
} }
$tax1 = new Entities\Taxonomy(); $tax1 = new Entities\Taxonomy();
$tax1->set_name($this->get_option('new_taxonomy')); $tax1->set_name($this->get_option('new_taxonomy'));
$tax1->set_allow_insert('yes'); $tax1->set_allow_insert('yes');
$tax1->set_status('publish'); $tax1->set_status('publish');
if ($tax1->validate()) { if ( ! $tax1->get_capabilities()->edit_posts ) {
$this->add_error_log('Error creating taxonomy. Permission denied');
$this->abort();
} elseif ($tax1->validate()) {
$tax_repo = \Tainacan\Repositories\Taxonomies::get_instance(); $tax_repo = \Tainacan\Repositories\Taxonomies::get_instance();
$tax1 = $tax_repo->insert($tax1); $tax1 = $tax_repo->insert($tax1);
$name = $tax1->get_name(); $name = $tax1->get_name();
@ -206,4 +216,4 @@ class Term_Importer extends Importer {
return false; return false;
} }
} }