| @@ -4,20 +4,21 @@ | ||
| 4 | 4 | |
| 5 | 5 | defined('ABSPATH') || exit; |
| 6 | 6 | |
| 7 | 7 | use FluentBoards\App\Models\Board; |
| 8 | +use FluentBoards\App\Models\Label; | |
| 9 | +use FluentBoards\App\Models\Stage; | |
| 8 | 10 | use FluentBoards\App\Services\BoardService; |
| 9 | 11 | use FluentBoards\App\Services\Helper; |
| 10 | 12 | use FluentBoards\App\Services\PermissionManager; |
| 11 | 13 | use FluentBoards\App\Services\StageService; |
| 12 | 14 | use FluentBoards\App\Services\LabelService; |
| 13 | -use FluentBoards\Framework\Support\Arr; | |
| 14 | 15 | |
| 15 | 16 | |
| 16 | 17 | /** |
| 17 | - * Contacts Class - PHP APi Wrapper | |
| 18 | + * Boards Class - PHP API Wrapper | |
| 18 | 19 | * |
| 19 | - * Contacts API Wrapper Class that can be used as <code>FluentBoardsApi('boards')</code> to get the class instance | |
| 20 | + * Boards API Wrapper Class that can be used as <code>FluentBoardsApi('boards')</code> to get the class instance | |
| 20 | 21 | * |
| 21 | 22 | * @package FluentBoards\App\Api\Classes |
| 22 | 23 | * @namespace FluentBoards\App\Api\Classes |
| 23 | 24 | * |
| @@ -26,16 +27,8 @@ | ||
| 26 | 27 | class Boards |
| 27 | 28 | { |
| 28 | 29 | private $instance = null; |
| 29 | 30 | |
| 30 | - private $allowedInstanceMethods = [ | |
| 31 | - 'all', | |
| 32 | - 'get', | |
| 33 | - 'find', | |
| 34 | - 'first', | |
| 35 | - 'paginate' | |
| 36 | - ]; | |
| 37 | - | |
| 38 | 31 | public function __construct(Board $instance) |
| 39 | 32 | { |
| 40 | 33 | $this->instance = $instance; |
| 41 | 34 | } |
| @@ -80,11 +73,21 @@ | ||
| 80 | 73 | if (empty($board_id)) { |
| 81 | 74 | return []; |
| 82 | 75 | } |
| 83 | 76 | |
| 77 | + if (!$this->canReadBoard($board_id)) { | |
| 78 | + return false; | |
| 79 | + } | |
| 80 | + | |
| 84 | 81 | return Board::with('stages')->where('id', $board_id)->get(); |
| 85 | 82 | } |
| 86 | 83 | |
| 84 | + /** | |
| 85 | + * Create a board only when the current user can create boards. | |
| 86 | + * | |
| 87 | + * @param array $data | |
| 88 | + * @return Board|false | |
| 89 | + */ | |
| 87 | 90 | public function create($data) |
| 88 | 91 | { |
| 89 | 92 | if (empty($data['title'])) { |
| 90 | 93 | return false; |
| @@ -89,8 +92,12 @@ | ||
| 89 | 92 | if (empty($data['title'])) { |
| 90 | 93 | return false; |
| 91 | 94 | } |
| 92 | 95 | |
| 96 | + if (!PermissionManager::userHasBoardCreationPermission()) { | |
| 97 | + return false; | |
| 98 | + } | |
| 99 | + | |
| 93 | 100 | $boardData = $this->boardSanitizeAndValidate($data); |
| 94 | 101 | |
| 95 | 102 | $boardService = new BoardService(); |
| 96 | 103 | $labelService = new LabelService(); |
| @@ -113,10 +120,108 @@ | ||
| 113 | 120 | |
| 114 | 121 | return $board; |
| 115 | 122 | } |
| 116 | 123 | |
| 124 | + /** | |
| 125 | + * Get non-archived stages for a board the current user can read. | |
| 126 | + * | |
| 127 | + * @param int|string $board_id | |
| 128 | + * @return array|false | |
| 129 | + */ | |
| 130 | + public function getStages($board_id) | |
| 131 | + { | |
| 132 | + if (empty($board_id)) { | |
| 133 | + return []; | |
| 134 | + } | |
| 135 | + | |
| 136 | + if (!$this->canReadBoard($board_id)) { | |
| 137 | + return false; | |
| 138 | + } | |
| 139 | + | |
| 140 | + return Stage::where('board_id', $board_id)->where('archived_at', null)->orderBy('position', 'asc')->get(); | |
| 141 | + } | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 117 | 145 | private function boardSanitizeAndValidate($data) |
| 118 | 146 | { |
| 119 | 147 | return Helper::sanitizeBoard($data); |
| 120 | 148 | } |
| 149 | + | |
| 150 | + /** | |
| 151 | + * Block raw model proxy calls so board access cannot be bypassed. | |
| 152 | + * | |
| 153 | + * @param string $method | |
| 154 | + * @param array $params | |
| 155 | + * @throws \Exception | |
| 156 | + */ | |
| 157 | + public function __call($method, $params) | |
| 158 | + { | |
| 159 | + throw new \Exception(sprintf('Method %s does not exist.', esc_html($method))); | |
| 160 | + } | |
| 161 | + | |
| 162 | + public function getLabels($boardId) | |
| 163 | + { | |
| 164 | + $board = Board::findOrFail($boardId); | |
| 165 | + | |
| 166 | + if (!$board) { | |
| 167 | + return false; | |
| 168 | + } | |
| 169 | + | |
| 170 | + if (!$this->canReadBoard($boardId)) { | |
| 171 | + return false; | |
| 172 | + } | |
| 173 | + | |
| 174 | + return Label::where('board_id', $board->id)->orderBy('created_at', 'ASC')->get(); | |
| 175 | + } | |
| 176 | + | |
| 177 | + public function createLabel($boardId, $data) | |
| 178 | + { | |
| 179 | + if (empty($data['bg_color'])) | |
| 180 | + { | |
| 181 | + return false; | |
| 182 | + } | |
| 183 | + | |
| 184 | + if (empty($data['color'])) | |
| 185 | + { | |
| 186 | + $data['color'] = '#1B2533'; | |
| 187 | + } | |
| 188 | + | |
| 189 | + if (!$this->canWriteBoard($boardId)) { | |
| 190 | + return false; | |
| 191 | + } | |
| 192 | + | |
| 193 | + $labelData = $this->labelSanitize($data); | |
| 194 | + | |
| 195 | + $labelService = new LabelService(); | |
| 196 | + return $labelService->createLabel($labelData, $boardId); | |
| 197 | + } | |
| 198 | + | |
| 199 | + private function labelSanitize($data) | |
| 200 | + { | |
| 201 | + return Helper::sanitizeLabel($data); | |
| 202 | + } | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * Check if the current user can read a board. | |
| 206 | + * | |
| 207 | + * @param int $boardId The board ID to check access for | |
| 208 | + * @return bool True if user has access, false otherwise | |
| 209 | + */ | |
| 210 | + private function canReadBoard($boardId) | |
| 211 | + { | |
| 212 | + return PermissionManager::userHasBoardPermission($boardId, 'GET'); | |
| 213 | + } | |
| 214 | + | |
| 215 | + /** | |
| 216 | + * Check if the current user can write to a board. | |
| 217 | + * | |
| 218 | + * @param int $boardId The board ID to check access for | |
| 219 | + * @return bool True if user can write, false otherwise | |
| 220 | + */ | |
| 221 | + private function canWriteBoard($boardId) | |
| 222 | + { | |
| 223 | + return PermissionManager::userHasBoardPermission($boardId, 'POST'); | |
| 224 | + } | |
| 225 | + | |
| 121 | 226 | |
| 122 | 227 | } |