PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.95.2
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.95.2
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / app / Http / Controllers / UserController.php

UserController.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.95.2, at app/Http/Controllers/UserController.php

304 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\PermissionManager;
9 use FluentBoards\App\Services\UserService;
10 use FluentBoards\Framework\Http\Request\Request;
11 use FluentCrm\App\Models\Subscriber;
12
13
14 class UserController extends Controller
15 {
16 private UserService $userService;
17
18 public function __construct(UserService $userService)
19 {
20 parent::__construct();
21 $this->userService = $userService;
22 }
23
24 public function allFluentBoardsUsers()
25 {
26 return [
27 'users' => $this->userService->allFluentBoardsUsers(),
28 'boards' => Board::select('id', 'title')->orderBy('title', 'ASC')->get()
29 ];
30 }
31
32 public function memberAssociatedTaskUsers($user_id)
33 {
34 $user_id = absint($user_id);
35 try {
36 $uniqueUsers = $this->userService->memberAssociatedTaskUsers($user_id);
37
38 return $this->sendSuccess([
39 'users' => $uniqueUsers['uniqueUsers'],
40 'userWiseBoardDesignation' => $uniqueUsers['userWiseBoardDesignation'],
41 ], 200);
42 } catch (\Exception $e) {
43 return $this->sendError($e->getMessage(), 404);
44 }
45 }
46
47 public function searchFluentBoardsUser(Request $request)
48 {
49 try {
50 $search_input = $request->getSafe('searchInput', 'sanitize_text_field', '');
51
52 $boardUsers = $this->userService->searchFluentBoardsUser($search_input);
53
54 return $this->sendSuccess($boardUsers, 200);
55 } catch (\Exception $e) {
56 return $this->sendError($e->getMessage(), 404);
57 }
58 }
59
60 public function searchMemberUser(Request $request, $user_id)
61 {
62 $user_id = absint($user_id);
63 try {
64 $search_input = $request->getSafe('searchInput', 'sanitize_text_field', '');
65 $searchResult = $this->userService->searchMemberUser($search_input, $user_id);
66
67 return $this->sendSuccess([
68 'users' => $searchResult,
69 ], 200);
70 } catch (\Exception $e) {
71 return $this->sendError($e->getMessage(), 404);
72 }
73 }
74
75 public function getMemberAssociatedTasks(Request $request, $user_id)
76 {
77 $user_id = absint($user_id);
78 // Sanitize boardIds array
79 $rawBoardIds = $request->getSafe('boardIds');
80 $boardIds = [];
81 if (is_array($rawBoardIds)) {
82 $boardIds = array_filter(array_map('intval', $rawBoardIds));
83 }
84
85 $requestData = [
86 'page' => $request->getSafe('page', 'intval', 1),
87 'taskType' => $request->getSafe('taskType', 'sanitize_text_field'),
88 'boardIds' => $boardIds,
89 'orderBy' => $request->getSafe('orderBy', 'sanitize_text_field'),
90 'order' => $request->getSafe('order', 'sanitize_text_field'),
91 ];
92 try {
93 return $this->sendSuccess(
94 $this->userService->getMemberAssociatedTasks($user_id, $requestData)
95 , 200);
96 } catch (\Exception $e) {
97 return $this->sendError($e->getMessage(), 404);
98 }
99 }
100
101 public function getMemberRelatedAcitivies(Request $request, $user_id)
102 {
103 $user_id = absint($user_id);
104 $page = $request->getSafe('page', 'intval', 1);
105 try {
106 return $this->sendSuccess(
107 $this->userService->getMemberRelatedAcitivies($user_id, $page)
108 , 200);
109 } catch (\Exception $e) {
110 return $this->sendError($e->getMessage(), 404);
111 }
112 }
113
114 public function getMemberInfo($user_id)
115 {
116 if(!PermissionManager::isFluentBoardsUser($user_id)) {
117 return $this->sendError(
118 [
119 'message' => __('You are not authorized to access this resource', 'fluent-boards'),
120 'code' => 'fluent_boards_unauthorized',
121 'status' => 403
122 ],
123 403
124 );
125 }
126
127 $user_id = absint($user_id);
128 $user = User::findOrFail($user_id);
129
130 $user = Helper::sanitizeUserCollections($user);
131
132 $user->fbs_role = PermissionManager::isFluentBoardsAdmin($user_id) ? 'fbs_admin' : 'member';
133
134 $user->is_wp_admin = user_can($user_id, 'manage_options') ? 'yes' : 'no';
135
136 if (defined('FLUENTCRM')) {
137 $subscriber = Subscriber::where('user_id', $user_id)->first();
138 $user->fluentcrm_subscriber = $subscriber ?? null;
139 }
140
141 return [
142 'user' => $user
143 ];
144 }
145
146 public function getMemberBoards($user_id)
147 {
148 $user_id = absint($user_id);
149 try {
150 return $this->sendSuccess(
151 [
152 'boards' => $this->userService->getMemberBoards($user_id)
153 ], 200);
154 } catch (\Exception $e) {
155 return $this->sendError($e->getMessage(), 404);
156 }
157 }
158
159 public function updateDisplayName(Request $request, $user_id)
160 {
161 $user_id = absint($user_id);
162 $currentUserId = get_current_user_id();
163
164 if ($currentUserId !== $user_id && !PermissionManager::isAdmin($currentUserId)) {
165 return $this->sendError(
166 __('You do not have permission to update this display name', 'fluent-boards'),
167 403
168 );
169 }
170
171 $displayName = $request->getSafe('display_name', 'sanitize_text_field');
172
173 if(!$displayName) {
174 return $this->sendError('Display name is required', 400);
175 }
176
177 $updateResult = wp_update_user([
178 'ID' => $user_id,
179 'display_name' => $displayName,
180 ]);
181
182 if (is_wp_error($updateResult)) {
183 return $this->sendError(
184 $updateResult->get_error_message(),
185 400
186 );
187 }
188
189 $user = User::findOrFail($user_id);
190 $user = Helper::sanitizeUserCollections($user);
191 $user->fbs_role = PermissionManager::isFluentBoardsAdmin($user_id) ? 'fbs_admin' : 'member';
192 $user->is_wp_admin = user_can($user_id, 'manage_options') ? 'yes' : 'no';
193
194 if (defined('FLUENTCRM')) {
195 $subscriber = Subscriber::where('user_id', $user_id)->first();
196 $user->fluentcrm_subscriber = $subscriber ?? null;
197 }
198
199 return $this->sendSuccess([
200 'message' => __('Display name has been updated', 'fluent-boards'),
201 'user' => $user,
202 ], 200);
203
204 }
205
206 public function updateProfilePhoto(Request $request, $user_id)
207 {
208 $user_id = absint($user_id);
209 $currentUserId = get_current_user_id();
210
211 // Only the user themself OR an admin can change the profile picture
212 if ($currentUserId !== $user_id && !PermissionManager::isAdmin($currentUserId)) {
213 return $this->sendError(
214 __('You do not have permission to update this profile photo', 'fluent-boards'),
215 403
216 );
217 }
218
219 // We’ll use native WordPress upload handling
220 if (empty($_FILES['photo']) || !empty($_FILES['photo']['error'])) {
221 return $this->sendError(
222 __('No photo uploaded or upload error', 'fluent-boards'),
223 400
224 );
225 }
226
227 // Limit file size to 2MB for profile photos
228 $maxSize = 2 * 1024 * 1024;
229 if ($_FILES['photo']['size'] > $maxSize) {
230 return $this->sendError(
231 __('Photo must be under 2MB', 'fluent-boards'),
232 400
233 );
234 }
235
236 $file = $_FILES['photo'];
237
238 // Validate MIME type server-side (client-sent type is spoofable)
239 $fileType = wp_check_filetype($file['name'], [
240 'jpg|jpeg|jpe' => 'image/jpeg',
241 'gif' => 'image/gif',
242 'png' => 'image/png',
243 'webp' => 'image/webp',
244 ]);
245
246 if (!$fileType['type']) {
247 return $this->sendError(
248 __('Invalid image type', 'fluent-boards'),
249 400
250 );
251 }
252
253 // Load WordPress upload helpers
254 if (!function_exists('wp_handle_upload')) {
255 require_once ABSPATH . 'wp-admin/includes/file.php';
256 }
257
258 $overrides = [
259 'test_form' => false,
260 'mimes' => [
261 'jpg|jpeg|jpe' => 'image/jpeg',
262 'gif' => 'image/gif',
263 'png' => 'image/png',
264 'webp' => 'image/webp',
265 ],
266 ];
267
268 $uploaded = wp_handle_upload($file, $overrides);
269
270 if (isset($uploaded['error'])) {
271 return $this->sendError(
272 $uploaded['error'],
273 400
274 );
275 }
276
277 $photoUrl = esc_url_raw($uploaded['url']);
278
279 // Store custom profile photo in user meta
280 update_user_meta($user_id, 'fbs_profile_photo', $photoUrl);
281
282 // Reload user and sanitize same as in getMemberInfo()
283 $user = User::findOrFail($user_id);
284 $user = Helper::sanitizeUserCollections($user);
285
286 // Override photo field if your sanitizer does not already use the meta
287 $user->photo = $photoUrl;
288
289 $user->fbs_role = PermissionManager::isFluentBoardsAdmin($user_id) ? 'fbs_admin' : 'member';
290 $user->is_wp_admin = user_can($user_id, 'manage_options') ? 'yes' : 'no';
291
292 if (defined('FLUENTCRM')) {
293 $subscriber = Subscriber::where('user_id', $user_id)->first();
294 $user->fluentcrm_subscriber = $subscriber ?? null;
295 }
296
297 return $this->sendSuccess([
298 'message' => __('Profile photo has been updated', 'fluent-boards'),
299 'photo_url' => $photoUrl,
300 'user' => $user,
301 ], 200);
302 }
303 }
304