Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement TagDetachCommand
Fixes #23856
  • Loading branch information
vboctor committed Jan 27, 2018
1 parent e3cb7eb commit 65dedb2
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 6 deletions.
116 changes: 116 additions & 0 deletions core/commands/TagDetachCommand.php
@@ -0,0 +1,116 @@
<?php
# MantisBT - A PHP based bugtracking system

# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.

require_api( 'authentication_api.php' );
require_api( 'bug_api.php' );
require_api( 'constant_inc.php' );
require_api( 'config_api.php' );
require_api( 'helper_api.php' );
require_api( 'user_api.php' );

use Mantis\Exceptions\ClientException;

/**
* A command that detaches a tag from an issue.
*
* {
* "query": { "issue_id" => 1234 },
* "payload": {
* "tags": [
* {
* "id": 1
* },
* {
* "name": "tag2"
* },
* {
* "id": 3,
* "name": "tag3"
* }
* ]
* }
* }
*/
class TagDetachCommand extends Command {
/**
* @var integer issue id
*/
private $issue_id;

/**
* @var integer logged in user id
*/
private $user_id;

/**
* @var array Array of tag ids to be detached.
*/
private $tagsToDetach = array();

/**
* @param array $p_data The command data.
*/
function __construct( array $p_data ) {
parent::__construct( $p_data );
}

/**
* Validate the data.
*/
function validate() {
$this->issue_id = helper_parse_issue_id( $this->query( 'issue_id' ) );
$this->user_id = auth_get_current_user_id();

$t_tags = $this->payload( 'tags', array() );
if( !is_array( $t_tags ) || empty( $t_tags ) ) {
throw new ClientException( 'Invalid tags array', ERROR_INVALID_FIELD_VALUE, array( 'tags' ) );
}

foreach( $t_tags as $t_tag ) {
if( isset( $t_tag['id'] ) ) {
$this->tagsToDetach[] = (int)$t_tag['id'];
} else if( isset( $t_tag['name'] ) ) {
$t_tag_row = tag_get_by_name( $t_tag['name'] );
if( $t_tag_row === false ) {
throw new ClientException(
sprintf( "Tag '%s' not found", $t_tag['name'] ),
ERROR_INVALID_FIELD_VALUE,
array( 'tags' ) );
} else {
$this->tagsToDetach[] = (int)$t_tag_row['id'];
}
} else {
# invalid tag with no id or name.
throw new ClientException( "Invalid tag with no id or name", ERROR_INVALID_FIELD_VALUE, array( 'tags' ) );
}
}
}

/**
* Process the command.
*
* @returns array Command response
*/
protected function process() {
foreach( $this->tagsToDetach as $t_tag_id ) {
if( tag_bug_is_attached( $t_tag_id, $this->issue_id ) ) {
tag_bug_detach( $t_tag_id, $this->issue_id );
event_signal( 'EVENT_TAG_DETACHED', array( $this->issue_id, array( $t_tag_id ) ) );
}
}
}
}

7 changes: 6 additions & 1 deletion core/tag_api.php
Expand Up @@ -835,7 +835,12 @@ function tag_bug_detach( $p_tag_id, $p_bug_id, $p_add_history = true, $p_user_id
$t_detach_level = config_get( 'tag_detach_threshold' );
}

access_ensure_bug_level( $t_detach_level, $p_bug_id, $t_user_id );
if( !access_has_bug_level( $t_detach_level, $p_bug_id, $t_user_id ) ) {
throw new ClientException(
sprintf( "Access denied to detach '%s'", $t_tag_row['name'] ),
ERROR_ACCESS_DENIED
);
}

db_param_push();
$t_query = 'DELETE FROM {bug_tag} WHERE tag_id=' . db_param() . ' AND bug_id=' . db_param();
Expand Down
18 changes: 13 additions & 5 deletions tag_detach.php
Expand Up @@ -26,24 +26,32 @@
* @uses form_api.php
* @uses gpc_api.php
* @uses print_api.php
* @uses tag_api.php
*/

require_once( 'core.php' );
require_api( 'event_api.php' );
require_api( 'form_api.php' );
require_api( 'gpc_api.php' );
require_api( 'print_api.php' );
require_api( 'tag_api.php' );

form_security_validate( 'tag_detach' );

$f_tag_id = gpc_get_int( 'tag_id' );
$f_bug_id = gpc_get_int( 'bug_id' );

tag_bug_detach( $f_tag_id, $f_bug_id );

event_signal( 'EVENT_TAG_DETACHED', array( $f_bug_id, array( $f_tag_id ) ) );
$t_data = array(
'query' => array( 'issue_id' => $f_bug_id ),
'payload' => array(
'tags' => array(
array(
'id' => $f_tag_id
)
)
)
);

$t_command = new TagDetachCommand( $t_data );
$t_command->execute();

form_security_purge( 'tag_detach' );

Expand Down

0 comments on commit 65dedb2

Please sign in to comment.