Add API calls to manage tags

This commit is contained in:
Frederic Guillot 2016-12-17 17:02:29 -05:00
parent ddeb89e2c6
commit 1186104469
10 changed files with 189 additions and 4 deletions

View File

@ -4,6 +4,7 @@ Version 1.0.36 (unreleased)
New features: New features:
* Add slideshow for images * Add slideshow for images
* Add API calls to manage tags
Improvements: Improvements:

View File

@ -23,13 +23,13 @@ class ProjectAuthorization extends Base
protected function checkProjectPermission($class, $method, $project_id) protected function checkProjectPermission($class, $method, $project_id)
{ {
if (empty($project_id)) { if (empty($project_id)) {
throw new AccessDeniedException('Project not found'); throw new AccessDeniedException('Project Not Found');
} }
$role = $this->projectUserRoleModel->getUserRole($project_id, $this->userSession->getId()); $role = $this->projectUserRoleModel->getUserRole($project_id, $this->userSession->getId());
if (! $this->apiProjectAuthorization->isAllowed($class, $method, $role)) { if (! $this->apiProjectAuthorization->isAllowed($class, $method, $role)) {
throw new AccessDeniedException('Project access denied'); throw new AccessDeniedException('Project Access Denied');
} }
} }
} }

View File

@ -0,0 +1,23 @@
<?php
namespace Kanboard\Api\Authorization;
/**
* Class TagAuthorization
*
* @package Kanboard\Api\Authorization
* @author Frederic Guillot
*/
class TagAuthorization extends ProjectAuthorization
{
public function check($class, $method, $tag_id)
{
if ($this->userSession->isLogged()) {
$tag = $this->tagModel->getById($tag_id);
if (! empty($tag)) {
$this->checkProjectPermission($class, $method, $tag['project_id']);
}
}
}
}

View File

@ -10,10 +10,10 @@ namespace Kanboard\Api\Authorization;
*/ */
class TaskAuthorization extends ProjectAuthorization class TaskAuthorization extends ProjectAuthorization
{ {
public function check($class, $method, $category_id) public function check($class, $method, $task_id)
{ {
if ($this->userSession->isLogged()) { if ($this->userSession->isLogged()) {
$this->checkProjectPermission($class, $method, $this->taskFinderModel->getProjectId($category_id)); $this->checkProjectPermission($class, $method, $this->taskFinderModel->getProjectId($task_id));
} }
} }
} }

View File

@ -0,0 +1,44 @@
<?php
namespace Kanboard\Api\Procedure;
use Kanboard\Api\Authorization\ProjectAuthorization;
use Kanboard\Api\Authorization\TagAuthorization;
/**
* Class TagProcedure
*
* @package Kanboard\Api\Procedure
* @author Frederic Guillot
*/
class TagProcedure extends BaseProcedure
{
public function getAllTags()
{
return $this->tagModel->getAll();
}
public function getTagsByProject($project_id)
{
ProjectAuthorization::getInstance($this->container)->check($this->getClassName(), 'getTagsByProject', $project_id);
return $this->tagModel->getAllByProject($project_id);
}
public function createTag($project_id, $tag)
{
ProjectAuthorization::getInstance($this->container)->check($this->getClassName(), 'createTag', $project_id);
return $this->tagModel->findOrCreateTag($project_id, $tag);
}
public function updateTag($tag_id, $tag)
{
TagAuthorization::getInstance($this->container)->check($this->getClassName(), 'updateTag', $tag_id);
return $this->tagModel->update($tag_id, $tag);
}
public function removeTag($tag_id)
{
TagAuthorization::getInstance($this->container)->check($this->getClassName(), 'removeTag', $tag_id);
return $this->tagModel->remove($tag_id);
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Kanboard\Api\Procedure;
use Kanboard\Api\Authorization\TaskAuthorization;
/**
* Class TaskTagProcedure
*
* @package Kanboard\Api\Procedure
* @author Frederic Guillot
*/
class TaskTagProcedure extends BaseProcedure
{
public function setTaskTags($project_id, $task_id, array $tags)
{
TaskAuthorization::getInstance($this->container)->check($this->getClassName(), 'setTaskTags', $task_id);
return $this->taskTagModel->save($project_id, $task_id, $tags);
}
public function getTaskTags($task_id)
{
TaskAuthorization::getInstance($this->container)->check($this->getClassName(), 'getTaskTags', $task_id);
return $this->taskTagModel->getList($task_id);
}
}

View File

@ -10,6 +10,7 @@ use Kanboard\Api\Procedure\CategoryProcedure;
use Kanboard\Api\Procedure\ColumnProcedure; use Kanboard\Api\Procedure\ColumnProcedure;
use Kanboard\Api\Procedure\CommentProcedure; use Kanboard\Api\Procedure\CommentProcedure;
use Kanboard\Api\Procedure\ProjectFileProcedure; use Kanboard\Api\Procedure\ProjectFileProcedure;
use Kanboard\Api\Procedure\TagProcedure;
use Kanboard\Api\Procedure\TaskExternalLinkProcedure; use Kanboard\Api\Procedure\TaskExternalLinkProcedure;
use Kanboard\Api\Procedure\TaskFileProcedure; use Kanboard\Api\Procedure\TaskFileProcedure;
use Kanboard\Api\Procedure\GroupProcedure; use Kanboard\Api\Procedure\GroupProcedure;
@ -25,6 +26,7 @@ use Kanboard\Api\Procedure\SwimlaneProcedure;
use Kanboard\Api\Procedure\TaskMetadataProcedure; use Kanboard\Api\Procedure\TaskMetadataProcedure;
use Kanboard\Api\Procedure\TaskProcedure; use Kanboard\Api\Procedure\TaskProcedure;
use Kanboard\Api\Procedure\TaskLinkProcedure; use Kanboard\Api\Procedure\TaskLinkProcedure;
use Kanboard\Api\Procedure\TaskTagProcedure;
use Kanboard\Api\Procedure\UserProcedure; use Kanboard\Api\Procedure\UserProcedure;
use Pimple\Container; use Pimple\Container;
use Pimple\ServiceProviderInterface; use Pimple\ServiceProviderInterface;
@ -71,9 +73,11 @@ class ApiProvider implements ServiceProviderInterface
->withObject(new TaskLinkProcedure($container)) ->withObject(new TaskLinkProcedure($container))
->withObject(new TaskExternalLinkProcedure($container)) ->withObject(new TaskExternalLinkProcedure($container))
->withObject(new TaskMetadataProcedure($container)) ->withObject(new TaskMetadataProcedure($container))
->withObject(new TaskTagProcedure($container))
->withObject(new UserProcedure($container)) ->withObject(new UserProcedure($container))
->withObject(new GroupProcedure($container)) ->withObject(new GroupProcedure($container))
->withObject(new GroupMemberProcedure($container)) ->withObject(new GroupMemberProcedure($container))
->withObject(new TagProcedure($container))
->withBeforeMethod('beforeProcedure') ->withBeforeMethod('beforeProcedure')
; ;

View File

@ -210,6 +210,8 @@ class AuthenticationProvider implements ServiceProviderInterface
$acl->add('TaskLinkProcedure', '*', Role::PROJECT_MEMBER); $acl->add('TaskLinkProcedure', '*', Role::PROJECT_MEMBER);
$acl->add('TaskExternalLinkProcedure', array('createExternalTaskLink', 'updateExternalTaskLink', 'removeExternalTaskLink'), Role::PROJECT_MEMBER); $acl->add('TaskExternalLinkProcedure', array('createExternalTaskLink', 'updateExternalTaskLink', 'removeExternalTaskLink'), Role::PROJECT_MEMBER);
$acl->add('TaskProcedure', '*', Role::PROJECT_MEMBER); $acl->add('TaskProcedure', '*', Role::PROJECT_MEMBER);
$acl->add('TaskTagProcedure', array('setTaskTags'), Role::PROJECT_MEMBER);
$acl->add('TagProcedure', array('createTag', 'updateTag', 'removeTag'), Role::PROJECT_MEMBER);
return $acl; return $acl;
} }

View File

@ -0,0 +1,58 @@
<?php
require_once __DIR__.'/BaseProcedureTest.php';
class TagProcedureTest extends BaseProcedureTest
{
protected $projectName = 'My project with tags';
public function testAll()
{
$this->assertCreateTeamProject();
$this->assertCreateTag();
$this->assertGetProjectTags();
$this->assertGetAllTags();
$this->assertUpdateTag();
$this->assertRemoveTag();
}
public function assertCreateTag()
{
$this->assertNotFalse($this->app->createTag($this->projectId, 'some tag'));
}
public function assertGetProjectTags()
{
$tags = $this->app->getTagsByProject($this->projectId);
$this->assertCount(1, $tags);
$this->assertEquals('some tag', $tags[0]['name']);
}
public function assertGetAllTags()
{
$tags = $this->app->getAllTags();
$this->assertCount(1, $tags);
$this->assertEquals('some tag', $tags[0]['name']);
}
public function assertUpdateTag()
{
$tags = $this->app->getAllTags();
$this->assertCount(1, $tags);
$this->assertTrue($this->app->updateTag($tags[0]['id'], 'another tag'));
$tags = $this->app->getAllTags();
$this->assertCount(1, $tags);
$this->assertEquals('another tag', $tags[0]['name']);
}
public function assertRemoveTag()
{
$tags = $this->app->getAllTags();
$this->assertCount(1, $tags);
$this->assertTrue($this->app->removeTag($tags[0]['id']));
$tags = $this->app->getAllTags();
$this->assertCount(0, $tags);
}
}

View File

@ -0,0 +1,27 @@
<?php
require_once __DIR__.'/BaseProcedureTest.php';
class TaskTagProcedureTest extends BaseProcedureTest
{
protected $projectName = 'My project with tasks and tags';
public function testAll()
{
$this->assertCreateTeamProject();
$this->assertCreateTask();
$this->assertSetTaskTags();
$this->assertGetTaskTags();
}
public function assertSetTaskTags()
{
$this->assertTrue($this->app->setTaskTags($this->projectId, $this->taskId, array('tag1', 'tag2')));
}
public function assertGetTaskTags()
{
$tags = $this->app->getTaskTags($this->taskId);
$this->assertEquals(array('tag1', 'tag2'), array_values($tags));
}
}