| @@ -108,20 +108,46 @@ | ||
| 108 | 108 | |
| 109 | 109 | return $boardUsers; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | + /** | |
| 113 | + * Return associated users and roles only from boards shared with the requester. | |
| 114 | + */ | |
| 112 | 115 | public function memberAssociatedTaskUsers($userId) |
| 113 | 116 | { |
| 114 | 117 | $user = User::find($userId); |
| 115 | - $boards = $user->whichBoards->pluck('id'); | |
| 116 | - $boardUsers = Relation::whereIn('object_id', $boards) | |
| 118 | + $memberBoardIds = array_values(array_unique(array_filter(array_map( | |
| 119 | + 'intval', | |
| 120 | + $user->whichBoards->pluck('id')->toArray() | |
| 121 | + )))); | |
| 122 | + $requesterBoardIds = array_values(array_unique(array_filter(array_map( | |
| 123 | + 'intval', | |
| 124 | + PermissionManager::getBoardIdsForUser(get_current_user_id()) | |
| 125 | + )))); | |
| 126 | + $sharedBoardIds = array_values(array_intersect($memberBoardIds, $requesterBoardIds)); | |
| 127 | + | |
| 128 | + if (!$sharedBoardIds) { | |
| 129 | + return [ | |
| 130 | + 'uniqueUsers' => [], | |
| 131 | + 'userWiseBoardDesignation' => [], | |
| 132 | + ]; | |
| 133 | + } | |
| 134 | + | |
| 135 | + $boardUsers = Relation::whereIn('object_id', $sharedBoardIds) | |
| 117 | 136 | ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER) |
| 118 | 137 | ->pluck('foreign_id')->toArray(); |
| 119 | 138 | $uniqueUsersIds = array_unique($boardUsers); |
| 120 | - $uniqueUsers = User::whereIn('ID', $uniqueUsersIds)->with('whichBoards')->get(); | |
| 139 | + $boardTable = (new Board())->getTable(); | |
| 140 | + $uniqueUsers = User::whereIn('ID', $uniqueUsersIds) | |
| 141 | + ->with(['whichBoards' => function ($query) use ($boardTable, $sharedBoardIds) { | |
| 142 | + $query->whereIn($boardTable . '.id', $sharedBoardIds); | |
| 143 | + }]) | |
| 144 | + ->get(); | |
| 121 | 145 | |
| 122 | 146 | $userWiseBoardDesignation = Relation::query()->whereIn('foreign_id', $uniqueUsersIds) |
| 123 | - ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)->get(); | |
| 147 | + ->whereIn('object_id', $sharedBoardIds) | |
| 148 | + ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER) | |
| 149 | + ->get(); | |
| 124 | 150 | |
| 125 | 151 | $data = array(); |
| 126 | 152 | $data['userWiseBoardDesignation'] = $userWiseBoardDesignation; |
| 127 | 153 | |
| @@ -135,9 +161,9 @@ | ||
| 135 | 161 | } elseif (PermissionManager::isFluentBoardsAdmin($uniqueUser['ID'])) { |
| 136 | 162 | $uniqueUser['is_super'] = true; |
| 137 | 163 | $uniqueUser['is_wpadmin'] = false; |
| 138 | 164 | } else { |
| 139 | - $uniqueUser['all_boards'] = Arr::get($uniqueUser, 'boards'); | |
| 165 | + $uniqueUser['all_boards'] = $uniqueUser->getRelation('whichBoards'); | |
| 140 | 166 | $uniqueUser['is_super'] = false; |
| 141 | 167 | $uniqueUser['is_wpadmin'] = false; |
| 142 | 168 | } |
| 143 | 169 | } |
| @@ -392,38 +418,91 @@ | ||
| 392 | 418 | ]; |
| 393 | 419 | } |
| 394 | 420 | |
| 395 | 421 | |
| 422 | + /** | |
| 423 | + * Get a member's activities scoped to boards the requesting user can access. | |
| 424 | + */ | |
| 396 | 425 | public function getMemberRelatedAcitivies($user_id, $page) |
| 397 | 426 | { |
| 398 | - $activities = Activity::query()->where('created_by', $user_id) | |
| 427 | + $perPage = 40; | |
| 428 | + $user_id = absint($user_id); | |
| 429 | + $page = max(1, absint($page)); | |
| 430 | + $allowedBoardIds = array_values(array_filter(array_map( | |
| 431 | + 'intval', | |
| 432 | + PermissionManager::getBoardIdsForUser(get_current_user_id()) | |
| 433 | + ))); | |
| 434 | + | |
| 435 | + if (empty($allowedBoardIds)) { | |
| 436 | + return [ | |
| 437 | + 'activities' => [], | |
| 438 | + 'pagination' => $this->getEmptyPaginationInfo($page, $perPage), | |
| 439 | + ]; | |
| 440 | + } | |
| 441 | + | |
| 442 | + $allowedTaskIds = Task::query() | |
| 443 | + ->select('id') | |
| 444 | + ->whereIn('board_id', $allowedBoardIds); | |
| 445 | + | |
| 446 | + $activities = Activity::query() | |
| 447 | + ->where('created_by', $user_id) | |
| 448 | + ->where(function ($query) use ($allowedBoardIds, $allowedTaskIds) { | |
| 449 | + $query->where(function ($boardQuery) use ($allowedBoardIds) { | |
| 450 | + $boardQuery->where('object_type', Constant::ACTIVITY_BOARD) | |
| 451 | + ->whereIn('object_id', $allowedBoardIds); | |
| 452 | + })->orWhere(function ($taskQuery) use ($allowedTaskIds) { | |
| 453 | + $taskQuery->where('object_type', Constant::ACTIVITY_TASK) | |
| 454 | + ->whereIn('object_id', $allowedTaskIds); | |
| 455 | + }); | |
| 456 | + }) | |
| 399 | 457 | ->orderBy('created_at', 'desc') |
| 400 | - ->with('user')->paginate(40, ['*'], 'page', $page); | |
| 458 | + ->with('user') | |
| 459 | + ->paginate($perPage, ['*'], 'page', $page); | |
| 401 | 460 | |
| 402 | - $activitiesToShow = array(); | |
| 461 | + $activitiesToShow = []; | |
| 403 | 462 | |
| 404 | 463 | foreach ($activities as $activity) { |
| 405 | 464 | if ($activity->object_type == Constant::ACTIVITY_BOARD) { |
| 406 | 465 | $activity->load('board'); |
| 407 | -// if($activity->settings && $activity->settings['task_id']){ | |
| 408 | -// $activity->task = Task::findOrFail($activity->settings['task_id']); | |
| 409 | -// } | |
| 410 | - if (PermissionManager::userHasPermission($activity->board_id, get_current_user_id())) { | |
| 411 | - $activitiesToShow[] = $activity; | |
| 412 | - } | |
| 466 | + $activitiesToShow[] = $activity; | |
| 413 | 467 | } elseif ($activity->object_type == Constant::ACTIVITY_TASK) { |
| 414 | 468 | $activity->load('task'); |
| 415 | - if ($activity->task && PermissionManager::userHasPermission($activity->task->board_id, get_current_user_id())) { | |
| 416 | - $activitiesToShow[] = $activity; | |
| 417 | - } | |
| 469 | + $activitiesToShow[] = $activity; | |
| 418 | 470 | } |
| 419 | 471 | } |
| 472 | + | |
| 420 | 473 | return [ |
| 421 | - 'activities' => $activitiesToShow, | |
| 422 | - 'pagination' => $activities->toArray(), | |
| 474 | + 'activities' => $activitiesToShow, | |
| 475 | + 'pagination' => $this->getPaginationInfo($activities), | |
| 423 | 476 | ]; |
| 424 | 477 | } |
| 425 | 478 | |
| 479 | + /** | |
| 480 | + * Return pagination metadata without duplicating serialized row data. | |
| 481 | + */ | |
| 482 | + private function getPaginationInfo($paginator) | |
| 483 | + { | |
| 484 | + return [ | |
| 485 | + 'current_page' => $paginator->currentPage(), | |
| 486 | + 'last_page' => $paginator->lastPage(), | |
| 487 | + 'per_page' => (int) $paginator->perPage(), | |
| 488 | + 'total' => $paginator->total(), | |
| 489 | + ]; | |
| 490 | + } | |
| 491 | + | |
| 492 | + /** | |
| 493 | + * Return an empty pagination payload for callers without accessible boards. | |
| 494 | + */ | |
| 495 | + private function getEmptyPaginationInfo($page, $perPage) | |
| 496 | + { | |
| 497 | + return [ | |
| 498 | + 'current_page' => max(1, (int) $page), | |
| 499 | + 'last_page' => 1, | |
| 500 | + 'per_page' => (int) $perPage, | |
| 501 | + 'total' => 0, | |
| 502 | + ]; | |
| 503 | + } | |
| 504 | + | |
| 426 | 505 | private function getTaskById($taskId) |
| 427 | 506 | { |
| 428 | 507 | return Task::findOrFail($taskId); |
| 429 | 508 | } |
| @@ -432,25 +511,23 @@ | ||
| 432 | 511 | { |
| 433 | 512 | if(!$user_id) { |
| 434 | 513 | return []; |
| 435 | 514 | } |
| 436 | - $boardIds = []; | |
| 437 | - $boards = []; | |
| 438 | 515 | $user = User::find($user_id); |
| 439 | 516 | $currentUserId = get_current_user_id(); |
| 440 | - if($currentUserId != $user->ID) { | |
| 441 | - if(!PermissionManager::isAdmin()){ | |
| 442 | - $currentUser = User::find($currentUserId); | |
| 443 | - $currentUserBoardIds = $currentUser->whichBoards->pluck('id')->toArray(); | |
| 444 | - $boardIds = $currentUserBoardIds; | |
| 445 | - } | |
| 517 | + | |
| 518 | + if ($currentUserId == $user->ID || PermissionManager::isAdmin($currentUserId)) { | |
| 519 | + return $user->whichBoards; | |
| 446 | 520 | } |
| 447 | - if (!empty($boardIds)) { | |
| 448 | - $boards = $user->whichBoards()->whereIn('fbs_boards.id', $boardIds)->get(); | |
| 449 | - } else { | |
| 450 | - $boards = $user->whichBoards; | |
| 521 | + | |
| 522 | + $currentUser = User::find($currentUserId); | |
| 523 | + $boardIds = $currentUser ? $currentUser->whichBoards->pluck('id')->toArray() : []; | |
| 524 | + | |
| 525 | + if (empty($boardIds)) { | |
| 526 | + return []; | |
| 451 | 527 | } |
| 452 | - return $boards; | |
| 528 | + | |
| 529 | + return $user->whichBoards()->whereIn('fbs_boards.id', $boardIds)->get(); | |
| 453 | 530 | } |
| 454 | 531 | |
| 455 | 532 | /** |
| 456 | 533 | * Returns four aggregate counts for a member's stats widget. |