PageRenderTime 160ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Api/Base.php

https://gitlab.com/x33n/kanboard
PHP | 113 lines | 91 code | 16 blank | 6 comment | 14 complexity | ada513cd38e6c7762a51d94a4c932fcf MD5 | raw file
  1. <?php
  2. namespace Api;
  3. use JsonRPC\AuthenticationFailure;
  4. use JsonRPC\AccessDeniedException;
  5. /**
  6. * Base class
  7. *
  8. * @package api
  9. * @author Frederic Guillot
  10. */
  11. abstract class Base extends \Core\Base
  12. {
  13. private $user_allowed_procedures = array(
  14. 'getMe',
  15. 'getMyDashboard',
  16. 'getMyActivityStream',
  17. 'createMyPrivateProject',
  18. 'getMyProjectsList',
  19. );
  20. private $both_allowed_procedures = array(
  21. 'getTimezone',
  22. 'getVersion',
  23. 'getDefaultTaskColor',
  24. 'getDefaultTaskColors',
  25. 'getColorList',
  26. 'getProjectById',
  27. 'getTask',
  28. 'getTaskByReference',
  29. 'getAllTasks',
  30. 'openTask',
  31. 'closeTask',
  32. 'moveTaskPosition',
  33. 'createTask',
  34. 'updateTask',
  35. 'getBoard',
  36. );
  37. public function checkProcedurePermission($is_user, $procedure)
  38. {
  39. $is_both_procedure = in_array($procedure, $this->both_allowed_procedures);
  40. $is_user_procedure = in_array($procedure, $this->user_allowed_procedures);
  41. if ($is_user && ! $is_both_procedure && ! $is_user_procedure) {
  42. throw new AccessDeniedException('Permission denied');
  43. }
  44. else if (! $is_user && ! $is_both_procedure && $is_user_procedure) {
  45. throw new AccessDeniedException('Permission denied');
  46. }
  47. }
  48. public function checkProjectPermission($project_id)
  49. {
  50. if ($this->userSession->isLogged() && ! $this->projectPermission->isUserAllowed($project_id, $this->userSession->getId())) {
  51. throw new AccessDeniedException('Permission denied');
  52. }
  53. }
  54. public function checkTaskPermission($task_id)
  55. {
  56. if ($this->userSession->isLogged()) {
  57. $this->checkProjectPermission($this->taskFinder->getProjectId($task_id));
  58. }
  59. }
  60. protected function formatTask($task)
  61. {
  62. if (! empty($task)) {
  63. $task['url'] = $this->helper->url->to('task', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id']), '', true);
  64. $task['color'] = $this->color->getColorProperties($task['color_id']);
  65. }
  66. return $task;
  67. }
  68. protected function formatTasks($tasks)
  69. {
  70. if (! empty($tasks)) {
  71. foreach ($tasks as &$task) {
  72. $task = $this->formatTask($task);
  73. }
  74. }
  75. return $tasks;
  76. }
  77. protected function formatProject($project)
  78. {
  79. if (! empty($project)) {
  80. $project['url'] = array(
  81. 'board' => $this->helper->url->to('board', 'show', array('project_id' => $project['id']), '', true),
  82. 'calendar' => $this->helper->url->to('calendar', 'show', array('project_id' => $project['id']), '', true),
  83. 'list' => $this->helper->url->to('listing', 'show', array('project_id' => $project['id']), '', true),
  84. );
  85. }
  86. return $project;
  87. }
  88. protected function formatProjects($projects)
  89. {
  90. if (! empty($projects)) {
  91. foreach ($projects as &$project) {
  92. $project = $this->formatProject($project);
  93. }
  94. }
  95. return $projects;
  96. }
  97. }