| @@ -106,9 +106,9 @@ | ||
| 106 | 106 | if (!$boardUser) { |
| 107 | 107 | return false; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - return (bool)$boardUser->settings ? $boardUser->settings['is_admin'] : null; | |
| 110 | + return (bool)($boardUser->settings[Constant::IS_BOARD_ADMIN] ?? false); | |
| 111 | 111 | |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | public static function userHasAnyBoardAccess($userId = null): bool |
| @@ -139,15 +139,17 @@ | ||
| 139 | 139 | return false; |
| 140 | 140 | } |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | + // if boardId is not provided then we will return false | |
| 144 | + if (!$boardId) { | |
| 145 | + return false; | |
| 146 | + } | |
| 147 | + | |
| 143 | 148 | if (static::isAdmin($userId)) { |
| 144 | 149 | return true; // if task some admin we will allow him. |
| 145 | 150 | } |
| 146 | 151 | |
| 147 | - if (!$boardId) { | |
| 148 | - return false; | |
| 149 | - } | |
| 150 | 152 | |
| 151 | 153 | /* now, if user is board admin or board member then will allow him */ |
| 152 | 154 | return Relation::where('object_id', $boardId) |
| 153 | 155 | ->where('foreign_id', $userId) |
| @@ -198,8 +200,45 @@ | ||
| 198 | 200 | |
| 199 | 201 | return true; |
| 200 | 202 | } |
| 201 | 203 | |
| 204 | + public static function userCanAccessMemberProfile($targetUserId, $userId = null): bool | |
| 205 | + { | |
| 206 | + $targetUserId = intval($targetUserId); | |
| 207 | + | |
| 208 | + if (!$userId) { | |
| 209 | + $userId = get_current_user_id(); | |
| 210 | + } | |
| 211 | + | |
| 212 | + $userId = intval($userId); | |
| 213 | + | |
| 214 | + if (!$targetUserId || !$userId) { | |
| 215 | + return false; | |
| 216 | + } | |
| 217 | + | |
| 218 | + if ($userId === $targetUserId || static::isAdmin($userId)) { | |
| 219 | + return true; | |
| 220 | + } | |
| 221 | + | |
| 222 | + if (!static::isFluentBoardsUser($targetUserId)) { | |
| 223 | + return false; | |
| 224 | + } | |
| 225 | + | |
| 226 | + $boardIds = Relation::where('foreign_id', $userId) | |
| 227 | + ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER) | |
| 228 | + ->pluck('object_id') | |
| 229 | + ->toArray(); | |
| 230 | + | |
| 231 | + if (empty($boardIds)) { | |
| 232 | + return false; | |
| 233 | + } | |
| 234 | + | |
| 235 | + return Relation::where('foreign_id', $targetUserId) | |
| 236 | + ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER) | |
| 237 | + ->whereIn('object_id', $boardIds) | |
| 238 | + ->exists(); | |
| 239 | + } | |
| 240 | + | |
| 202 | 241 | public static function isAdmin($userId = null, $useCache = true) |
| 203 | 242 | { |
| 204 | 243 | |
| 205 | 244 | static $cache = []; |
| @@ -244,9 +283,9 @@ | ||
| 244 | 283 | } |
| 245 | 284 | |
| 246 | 285 | /** |
| 247 | 286 | * Get array of board Ids for logged-in user |
| 248 | - * @param null $userId | |
| 287 | + * @param int $userId | |
| 249 | 288 | * @return array |
| 250 | 289 | */ |
| 251 | 290 | public static function getBoardIdsForUser($userId = null, $boardId = null) |
| 252 | 291 | { |
| @@ -305,9 +344,9 @@ | ||
| 305 | 344 | { |
| 306 | 345 | return !!is_user_logged_in(); |
| 307 | 346 | } |
| 308 | 347 | |
| 309 | - public static function getAll_WP_Admins($searchquery = '') | |
| 348 | + public static function getAll_WP_Admins($searchquery = '', $number = null) | |
| 310 | 349 | { |
| 311 | 350 | $args = array( |
| 312 | 351 | 'role' => '', |
| 313 | 352 | 'capability' => 'manage_options', |
| @@ -313,7 +352,87 @@ | ||
| 313 | 352 | 'capability' => 'manage_options', |
| 314 | 353 | 'search' => '*' . $searchquery . '*', |
| 315 | 354 | ); |
| 316 | 355 | |
| 356 | + // Optionally bound the result set (e.g. for selector popovers) so an | |
| 357 | + // empty search can't serialize every admin-capable user on large sites. | |
| 358 | + if ($number !== null) { | |
| 359 | + $args['number'] = (int) $number; | |
| 360 | + } | |
| 361 | + | |
| 317 | 362 | return get_users($args); |
| 363 | + } | |
| 364 | + | |
| 365 | + public static function isWPAdmin($userId = null) | |
| 366 | + { | |
| 367 | + if (!$userId) { | |
| 368 | + $userId = get_current_user_id(); | |
| 369 | + if (!$userId) { | |
| 370 | + return false; | |
| 371 | + } | |
| 372 | + } | |
| 373 | + | |
| 374 | + return user_can($userId, 'manage_options'); | |
| 375 | + } | |
| 376 | + | |
| 377 | + public static function userHasBoardPermission($boardId, $requestMethod, $userId = null) | |
| 378 | + { | |
| 379 | + // Get the current user if no user ID is provided | |
| 380 | + if (!$userId) { | |
| 381 | + $userId = get_current_user_id(); | |
| 382 | + if (!$userId) { | |
| 383 | + return false; // Return false if no user is logged in | |
| 384 | + } | |
| 385 | + } | |
| 386 | + | |
| 387 | + // Ensure the board ID is valid | |
| 388 | + if (!$boardId) { | |
| 389 | + return false; // Return false if no board ID is provided | |
| 390 | + } | |
| 391 | + | |
| 392 | + // Admins have full permissions, allow access immediately | |
| 393 | + if (static::isAdmin($userId)) { | |
| 394 | + return true; | |
| 395 | + } | |
| 396 | + | |
| 397 | + // Fetch user permissions for the given board | |
| 398 | + $boardPermissions = Relation::where('object_id', $boardId) | |
| 399 | + ->where('foreign_id', $userId) | |
| 400 | + ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER) | |
| 401 | + ->first(); | |
| 402 | + | |
| 403 | + // If no permissions are found, deny access | |
| 404 | + if (!$boardPermissions) { | |
| 405 | + return false; | |
| 406 | + } | |
| 407 | + | |
| 408 | + // Allow read-only access for GET requests | |
| 409 | + if ($requestMethod === 'GET') { | |
| 410 | + return true; | |
| 411 | + } | |
| 412 | + | |
| 413 | + // Deny access if the user is a viewer only and trying to modify data | |
| 414 | + return !($boardPermissions->settings['is_viewer_only'] ?? false); | |
| 415 | + } | |
| 416 | + | |
| 417 | + public static function userHasBoardCreationPermission($userId = null) | |
| 418 | + { | |
| 419 | + if (!$userId) { | |
| 420 | + $userId = get_current_user_id(); | |
| 421 | + if (!$userId) { | |
| 422 | + return false; | |
| 423 | + } | |
| 424 | + } | |
| 425 | + | |
| 426 | + if (static::isAdmin($userId)) { | |
| 427 | + return apply_filters('fluent_boards/can_create_board', true, $userId); | |
| 428 | + } | |
| 429 | + | |
| 430 | + // Check if "allow members to create boards" setting is enabled | |
| 431 | + $generalSettings = fluent_boards_get_option('general_settings', []); | |
| 432 | + if (!empty($generalSettings['allow_members_to_create_boards']) && $generalSettings['allow_members_to_create_boards'] !== 'false' && static::userHasAnyBoardAccess($userId)) { | |
| 433 | + return apply_filters('fluent_boards/can_create_board', true, $userId); | |
| 434 | + } | |
| 435 | + | |
| 436 | + return apply_filters('fluent_boards/can_create_board', false, $userId); | |
| 318 | 437 | } |
| 319 | 438 | } |