| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Board; |
| 6 |
use FluentBoards\App\Models\User; |
| 7 |
use FluentBoards\App\Services\Helper; |
| 8 |
use FluentBoards\App\Services\MCPAccessService; |
| 9 |
use FluentBoards\App\Services\PermissionManager; |
| 10 |
use FluentBoards\App\Services\UserService; |
| 11 |
use FluentBoards\Framework\Http\Request\Request; |
| 12 |
use FluentCrm\App\Models\Subscriber; |
| 13 |
|
| 14 |
|
| 15 |
class UserController extends Controller |
| 16 |
{ |
| 17 |
private UserService $userService; |
| 18 |
|
| 19 |
public function __construct(UserService $userService) |
| 20 |
{ |
| 21 |
parent::__construct(); |
| 22 |
$this->userService = $userService; |
| 23 |
} |
| 24 |
|
| 25 |
public function allFluentBoardsUsers() |
| 26 |
{ |
| 27 |
return [ |
| 28 |
'users' => $this->userService->allFluentBoardsUsers(), |
| 29 |
'boards' => Board::select('id', 'title')->orderBy('title', 'ASC')->get() |
| 30 |
]; |
| 31 |
} |
| 32 |
|
| 33 |
public function memberAssociatedTaskUsers($user_id) |
| 34 |
{ |
| 35 |
$user_id = absint($user_id); |
| 36 |
try { |
| 37 |
$uniqueUsers = $this->userService->memberAssociatedTaskUsers($user_id); |
| 38 |
|
| 39 |
return $this->sendSuccess([ |
| 40 |
'users' => $uniqueUsers['uniqueUsers'], |
| 41 |
'userWiseBoardDesignation' => $uniqueUsers['userWiseBoardDesignation'], |
| 42 |
], 200); |
| 43 |
} catch (\Exception $e) { |
| 44 |
return $this->sendError($e->getMessage(), 404); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public function searchFluentBoardsUser(Request $request) |
| 49 |
{ |
| 50 |
try { |
| 51 |
$search_input = $request->getSafe('searchInput', 'sanitize_text_field', ''); |
| 52 |
|
| 53 |
$boardUsers = $this->userService->searchFluentBoardsUser($search_input); |
| 54 |
|
| 55 |
return $this->sendSuccess($boardUsers, 200); |
| 56 |
} catch (\Exception $e) { |
| 57 |
return $this->sendError($e->getMessage(), 404); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public function searchMemberUser(Request $request, $user_id) |
| 62 |
{ |
| 63 |
$user_id = absint($user_id); |
| 64 |
try { |
| 65 |
$search_input = $request->getSafe('searchInput', 'sanitize_text_field', ''); |
| 66 |
$searchResult = $this->userService->searchMemberUser($search_input, $user_id); |
| 67 |
|
| 68 |
return $this->sendSuccess([ |
| 69 |
'users' => $searchResult, |
| 70 |
], 200); |
| 71 |
} catch (\Exception $e) { |
| 72 |
return $this->sendError($e->getMessage(), 404); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public function getMemberAssociatedTasks(Request $request, $user_id) |
| 77 |
{ |
| 78 |
$user_id = absint($user_id); |
| 79 |
// Sanitize boardIds array |
| 80 |
$rawBoardIds = $request->getSafe('boardIds'); |
| 81 |
$boardIds = []; |
| 82 |
if (is_array($rawBoardIds)) { |
| 83 |
$boardIds = array_filter(array_map('intval', $rawBoardIds)); |
| 84 |
} |
| 85 |
|
| 86 |
$perPage = max(1, min(absint($request->getSafe('per_page', 'intval', 15)), 50)); |
| 87 |
|
| 88 |
$requestData = [ |
| 89 |
'page' => $request->getSafe('page', 'intval', 1), |
| 90 |
'taskType' => $request->getSafe('taskType', 'sanitize_text_field'), |
| 91 |
'boardIds' => $boardIds, |
| 92 |
'orderBy' => $request->getSafe('orderBy', 'sanitize_text_field'), |
| 93 |
'order' => $request->getSafe('order', 'sanitize_text_field'), |
| 94 |
'per_page' => $perPage |
| 95 |
]; |
| 96 |
try { |
| 97 |
return $this->sendSuccess( |
| 98 |
$this->userService->getMemberAssociatedTasks($user_id, $requestData) |
| 99 |
, 200); |
| 100 |
} catch (\Exception $e) { |
| 101 |
return $this->sendError($e->getMessage(), 404); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Return profile task counts using the same categories shown on the dashboard. |
| 107 |
*/ |
| 108 |
public function getMemberTaskCounts(Request $request, $user_id) |
| 109 |
{ |
| 110 |
$user_id = absint($user_id); |
| 111 |
$rawBoardIds = $request->getSafe('boardIds'); |
| 112 |
$boardIds = is_array($rawBoardIds) |
| 113 |
? array_filter(array_map('intval', $rawBoardIds)) |
| 114 |
: []; |
| 115 |
|
| 116 |
try { |
| 117 |
return $this->sendSuccess([ |
| 118 |
'counts' => $this->userService->getMemberTaskCounts($user_id, $boardIds), |
| 119 |
], 200); |
| 120 |
} catch (\Exception $e) { |
| 121 |
return $this->sendError($e->getMessage(), 404); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
public function getMemberRelatedAcitivies(Request $request, $user_id) |
| 126 |
{ |
| 127 |
$user_id = absint($user_id); |
| 128 |
$page = $request->getSafe('page', 'intval', 1); |
| 129 |
try { |
| 130 |
return $this->sendSuccess( |
| 131 |
$this->userService->getMemberRelatedAcitivies($user_id, $page) |
| 132 |
, 200); |
| 133 |
} catch (\Exception $e) { |
| 134 |
return $this->sendError($e->getMessage(), 404); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public function getMemberInfo($user_id) |
| 139 |
{ |
| 140 |
if(!PermissionManager::isFluentBoardsUser($user_id)) { |
| 141 |
return $this->sendError( |
| 142 |
[ |
| 143 |
'message' => __('You are not authorized to access this resource', 'fluent-boards'), |
| 144 |
'code' => 'fluent_boards_unauthorized', |
| 145 |
'status' => 403 |
| 146 |
], |
| 147 |
403 |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
$user_id = absint($user_id); |
| 152 |
$user = User::findOrFail($user_id); |
| 153 |
|
| 154 |
$user = Helper::sanitizeUserCollections($user); |
| 155 |
|
| 156 |
$user->fbs_role = PermissionManager::isFluentBoardsAdmin($user_id) ? 'fbs_admin' : 'member'; |
| 157 |
|
| 158 |
$user->is_wp_admin = user_can($user_id, 'manage_options') ? 'yes' : 'no'; |
| 159 |
|
| 160 |
if (defined('FLUENTCRM')) { |
| 161 |
$subscriber = Subscriber::where('user_id', $user_id)->first(); |
| 162 |
$user->fluentcrm_subscriber = $subscriber ?? null; |
| 163 |
} |
| 164 |
|
| 165 |
return [ |
| 166 |
'user' => $user |
| 167 |
]; |
| 168 |
} |
| 169 |
|
| 170 |
public function getMemberBoards($user_id) |
| 171 |
{ |
| 172 |
$user_id = absint($user_id); |
| 173 |
try { |
| 174 |
return $this->sendSuccess( |
| 175 |
[ |
| 176 |
'boards' => $this->userService->getMemberBoards($user_id) |
| 177 |
], 200); |
| 178 |
} catch (\Exception $e) { |
| 179 |
return $this->sendError($e->getMessage(), 404); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
public function getMemberStats($user_id) |
| 184 |
{ |
| 185 |
$user_id = absint($user_id); |
| 186 |
|
| 187 |
return $this->sendSuccess( |
| 188 |
$this->userService->getMemberStats($user_id), |
| 189 |
200 |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
/** Update the authenticated member's display name. */ |
| 194 |
public function updateDisplayName(Request $request, $user_id) |
| 195 |
{ |
| 196 |
try { |
| 197 |
return $this->sendSuccess([ |
| 198 |
'message' => __('Display name has been updated', 'fluent-boards'), |
| 199 |
'user' => $this->userService->updateDisplayName($user_id, $request->get('display_name')), |
| 200 |
]); |
| 201 |
} catch (\Exception $e) { |
| 202 |
return $this->sendError(['message' => $e->getMessage()], $e->getCode() === 403 ? 403 : 400); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/** Upload a profile photo for the authenticated member. */ |
| 207 |
public function updateProfilePhoto(Request $request, $user_id) |
| 208 |
{ |
| 209 |
try { |
| 210 |
return $this->sendSuccess([ |
| 211 |
'message' => __('Profile photo has been updated', 'fluent-boards'), |
| 212 |
'user' => $this->userService->updateProfilePhoto($user_id, $_FILES['photo'] ?? []), |
| 213 |
]); |
| 214 |
} catch (\Exception $e) { |
| 215 |
return $this->sendError(['message' => $e->getMessage()], $e->getCode() === 403 ? 403 : 400); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
public function getMcpConnections(Request $request, $id) |
| 220 |
{ |
| 221 |
if (!current_user_can('list_app_passwords', (int) $id)) { |
| 222 |
return $this->sendError(['message' => __('You are not allowed to list application passwords.', 'fluent-boards')], 403); |
| 223 |
} |
| 224 |
|
| 225 |
return $this->sendSuccess(MCPAccessService::getProfileState((int) $id)); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Create an MCP connection key. The password is only ever in this response. |
| 230 |
*/ |
| 231 |
public function createMcpConnection(Request $request, $id) |
| 232 |
{ |
| 233 |
$result = MCPAccessService::createConnection((int) $id, (string) $request->get('name')); |
| 234 |
|
| 235 |
if (is_wp_error($result)) { |
| 236 |
$data = $result->get_error_data(); |
| 237 |
return $this->sendError(['message' => $result->get_error_message()], $data['status'] ?? 400); |
| 238 |
} |
| 239 |
|
| 240 |
nocache_headers(); |
| 241 |
|
| 242 |
return $this->sendSuccess($result); |
| 243 |
} |
| 244 |
|
| 245 |
public function revokeMcpConnection(Request $request, $id, $uuid) |
| 246 |
{ |
| 247 |
if (!current_user_can('delete_app_password', (int) $id, $uuid)) { |
| 248 |
return $this->sendError(['message' => __('You are not allowed to delete this application password.', 'fluent-boards')], 403); |
| 249 |
} |
| 250 |
|
| 251 |
$result = MCPAccessService::revokeConnection((int) $id, sanitize_text_field($uuid)); |
| 252 |
|
| 253 |
if (is_wp_error($result)) { |
| 254 |
$data = $result->get_error_data(); |
| 255 |
return $this->sendError(['message' => $result->get_error_message()], $data['status'] ?? 400); |
| 256 |
} |
| 257 |
|
| 258 |
return $this->sendSuccess([ |
| 259 |
'message' => __('Connection revoked.', 'fluent-boards'), |
| 260 |
]); |
| 261 |
} |
| 262 |
} |
| 263 |
|