PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.45
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.45
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 / Services / PermissionManager.php

PermissionManager.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.45, at app/Services/PermissionManager.php

374 lines 10.0 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\Services;
4
5 use FluentBoards\App\Models\Board;
6 use FluentBoards\App\Models\Relation;
7 use FluentBoards\App\Models\Meta;
8 use FluentBoards\App\Services\Constant;
9
10 class PermissionManager
11 {
12 public static function getFormattedBoardPermissions()
13 {
14 return [
15 'board_admin' => 'Board Admin',
16 'create_tasks' => 'Create Tasks',
17 'edit_tasks' => 'Edit Tasks',
18 'delete_tasks' => 'Delete Tasks',
19 ];
20 }
21
22 public static function getTopLevelPermissions()
23 {
24 return [
25 'all_board_admin' => 'All Board Admin',
26 'create_boards' => 'Create Boards',
27 'edit_boards' => 'Edit Boards',
28 'delete_boards' => 'Delete Boards',
29 'can_manage_permissions' => 'Can Manage Permissions',
30 ];
31 }
32
33 public static function getBoardPermissions($boardId, $userId)
34 {
35 if (user_can($userId, 'manage_options')) {
36 return array_keys(self::getFormattedBoardPermissions());
37 }
38
39 $globalPermissions = self::getTopUserPermission($userId);
40
41 if (in_array('all_board_admin', $globalPermissions)) {
42 return array_keys(self::getFormattedBoardPermissions());
43 }
44
45 // The user does not have global permission now we are checking for board permission
46
47 $isUserOn = Relation::query()->where('board_id', $boardId)->where('user_id', $userId)->exists();
48
49 $permissions = [
50
51 ];
52
53 if (in_array('board_admin', $permissions)) {
54 return array_keys(self::getFormattedBoardPermissions());
55 }
56
57 return $permissions;
58 }
59
60 private static function getTopUserPermission($userId)
61 {
62 $allAccesses = ['all_board_admin'];
63
64 $userPermission = ['create_boards', 'edit_boards', 'delete_boards', 'can_manage_permissions'];
65
66 return array_intersect($allAccesses, $userPermission);
67 }
68
69 public static function userHasBoardAccess($boardId, $userId = null, $permissions = [])
70 {
71 if (!$userId) {
72 $userId = get_current_user_id();
73 }
74
75 if (!$userId) {
76 return false;
77 }
78
79 $userPermissions = self::getBoardPermissions($boardId, $userId);
80
81 if (!$permissions) {
82 return false;
83 }
84
85 return (bool)array_intersect($permissions, $userPermissions);
86 }
87
88 public static function isBoardManager($boardId, $userId = null, $useCache = false)
89 {
90 if (!$userId) {
91 $userId = get_current_user_id();
92 if (!$userId) {
93 return false;
94 }
95 }
96
97 if (static::isAdmin($userId, $useCache)) {
98 return true;
99 }
100
101 $boardUser = Relation::where('object_id', $boardId)
102 ->where('foreign_id', $userId)
103 ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
104 ->first();
105
106 if (!$boardUser) {
107 return false;
108 }
109
110 return (bool)$boardUser->settings ? $boardUser->settings['is_admin'] : null;
111
112 }
113
114 public static function userHasAnyBoardAccess($userId = null): bool
115 {
116 if (!$userId) {
117 $userId = get_current_user_id();
118 }
119
120 if (!$userId) {
121 return false;
122 }
123
124 if (static::isAdmin($userId)) {
125 return true;
126 }
127
128 return Relation::where('foreign_id', $userId)
129 ->where('object_type', 'board_user')
130 ->exists();
131 }
132
133 /// this is function currently being used , we will go permission way next time
134 public static function userHasPermission($boardId, $userId = null)
135 {
136 if (!$userId) {
137 $userId = get_current_user_id();
138 if (!$userId) {
139 return false;
140 }
141 }
142
143 // if boardId is not provided then we will return false
144 if (!$boardId) {
145 return false;
146 }
147
148 if (static::isAdmin($userId)) {
149 return true; // if task some admin we will allow him.
150 }
151
152
153 /* now, if user is board admin or board member then will allow him */
154 return Relation::where('object_id', $boardId)
155 ->where('foreign_id', $userId)
156 ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
157 ->exists();
158 }
159
160 public static function isFluentBoardsAdmin($userId = null): bool
161 {
162 if (!$userId) {
163 $userId = get_current_user_id();
164 if (!$userId) {
165 return false;
166 }
167 }
168
169 $isExists = (bool)Meta::where('object_id', $userId)
170 ->where('object_type', Constant::FLUENT_BOARD_ADMIN)
171 ->exists();
172
173 if (!$isExists) {
174 return false;
175 }
176
177 return true;
178 }
179
180 public static function isFluentBoardsUser($userId = null): bool
181 {
182 if (!$userId) {
183 $userId = get_current_user_id();
184 if (!$userId) {
185 return false;
186 }
187 }
188
189 if (static::isAdmin($userId)) { // boardAdmin is super admin and this function returns if loggedUser is task superadmin or not.
190 return true; // if board super admin we will allow him.
191 }
192
193 $isExists = (bool)Relation::where('foreign_id', $userId)
194 ->whereNotNull('object_id')
195 ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
196 ->exists();
197 if (!$isExists) {
198 return false;
199 }
200
201 return true;
202 }
203
204 public static function isAdmin($userId = null, $useCache = true)
205 {
206
207 static $cache = [];
208
209 if (isset($cache[$userId]) && $useCache) {
210 return $cache[$userId];
211 }
212
213 /*
214 * Two types of admin we have
215 * 1. WordPress admin
216 * 2. FluentBoard admin
217 * if user is WordPress admin then we will allow him
218 * if user is FluentBoard admin then we will allow him
219 */
220
221 if (!$userId) {
222 $userId = get_current_user_id();
223 if (!$userId) {
224 return false;
225 }
226 }
227
228 if (!$userId) {
229 return false;
230 }
231
232 if (user_can($userId, 'manage_options')) { // WordPress Administrator
233
234 $cache[$userId] = true;
235
236 return true;
237 }
238
239 if (static::isFluentBoardsAdmin($userId)) { // FluentBoard Plugin Administrator
240 $cache[$userId] = true;
241 return true;
242 }
243
244 $cache[$userId] = false;
245 return false;
246 }
247
248 /**
249 * Get array of board Ids for logged-in user
250 * @param null $userId
251 * @return array
252 */
253 public static function getBoardIdsForUser($userId = null, $boardId = null)
254 {
255 if (!$userId) {
256 $userId = get_current_user_id();
257 if (!$userId) {
258 return [];
259 }
260 }
261
262 if (static::isAdmin($userId)) {
263 if ($boardId) {
264 return Board::where('archived_at', null)->where('id', $boardId)->pluck('id')->toArray();
265 }
266 return Board::where('archived_at', null)->pluck('id')->toArray();
267 }
268
269 return Relation::where('foreign_id', $userId)
270 ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
271 ->pluck('object_id')->toArray();
272 }
273
274 public static function getTaskIdsWatchByUser($userId = null)
275 {
276 if (!$userId) {
277 $userId = get_current_user_id();
278 if (!$userId) {
279 return [];
280 }
281 }
282
283 return Relation::where('foreign_id', $userId)
284 ->where('object_type', Constant::OBJECT_TYPE_USER_TASK_WATCH)
285 ->pluck('object_id')->toArray();
286 }
287
288 public static function userCan($permission, $userId = null)
289 {
290 if (!$userId) {
291 $userId = get_current_user_id();
292 if (!$userId) {
293 return false;
294 }
295 }
296
297 if (static::isAdmin($userId)) {
298 return true;
299 }
300 // if user has edit_posts capability then we will allow him
301 if (user_can($userId, 'edit_posts')) {
302 return true;
303 }
304 }
305
306 public static function hasAppAccess($userId = null)
307 {
308 return !!is_user_logged_in();
309 }
310
311 public static function getAll_WP_Admins($searchquery = '')
312 {
313 $args = array(
314 'role' => '',
315 'capability' => 'manage_options',
316 'search' => '*' . $searchquery . '*',
317 );
318
319 return get_users($args);
320 }
321
322 public static function isWPAdmin($userId = null)
323 {
324 if (!$userId) {
325 $userId = get_current_user_id();
326 if (!$userId) {
327 return false;
328 }
329 }
330
331 return user_can($userId, 'manage_options');
332 }
333
334 public static function userHasBoardPermission($boardId, $requestMethod, $userId = null)
335 {
336 // Get the current user if no user ID is provided
337 if (!$userId) {
338 $userId = get_current_user_id();
339 if (!$userId) {
340 return false; // Return false if no user is logged in
341 }
342 }
343
344 // Ensure the board ID is valid
345 if (!$boardId) {
346 return false; // Return false if no board ID is provided
347 }
348
349 // Admins have full permissions, allow access immediately
350 if (static::isAdmin()) {
351 return true;
352 }
353
354 // Fetch user permissions for the given board
355 $boardPermissions = Relation::where('object_id', $boardId)
356 ->where('foreign_id', $userId)
357 ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
358 ->first();
359
360 // If no permissions are found, deny access
361 if (!$boardPermissions) {
362 return false;
363 }
364
365 // Allow read-only access for GET requests
366 if ($requestMethod === 'GET') {
367 return true;
368 }
369
370 // Deny access if the user is a viewer only and trying to modify data
371 return !($boardPermissions->settings['is_viewer_only'] ?? false);
372 }
373 }
374