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

332 lines 8.7 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 (static::isAdmin($userId)) {
144 return true; // if task some admin we will allow him.
145 }
146
147 if (!$boardId) {
148 return false;
149 }
150
151 /* now, if user is board admin or board member then will allow him */
152 return Relation::where('object_id', $boardId)
153 ->where('foreign_id', $userId)
154 ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
155 ->exists();
156 }
157
158 public static function isFluentBoardsAdmin($userId = null): bool
159 {
160 if (!$userId) {
161 $userId = get_current_user_id();
162 if (!$userId) {
163 return false;
164 }
165 }
166
167 $isExists = (bool)Meta::where('object_id', $userId)
168 ->where('object_type', Constant::FLUENT_BOARD_ADMIN)
169 ->exists();
170
171 if (!$isExists) {
172 return false;
173 }
174
175 return true;
176 }
177
178 public static function isFluentBoardsUser($userId = null): bool
179 {
180 if (!$userId) {
181 $userId = get_current_user_id();
182 if (!$userId) {
183 return false;
184 }
185 }
186
187 if (static::isAdmin($userId)) { // boardAdmin is super admin and this function returns if loggedUser is task superadmin or not.
188 return true; // if board super admin we will allow him.
189 }
190
191 $isExists = (bool)Relation::where('foreign_id', $userId)
192 ->whereNotNull('object_id')
193 ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
194 ->exists();
195 if (!$isExists) {
196 return false;
197 }
198
199 return true;
200 }
201
202 public static function isAdmin($userId = null, $useCache = true)
203 {
204
205 static $cache = [];
206
207 if (isset($cache[$userId]) && $useCache) {
208 return $cache[$userId];
209 }
210
211 /*
212 * Two types of admin we have
213 * 1. WordPress admin
214 * 2. FluentBoard admin
215 * if user is WordPress admin then we will allow him
216 * if user is FluentBoard admin then we will allow him
217 */
218
219 if (!$userId) {
220 $userId = get_current_user_id();
221 if (!$userId) {
222 return false;
223 }
224 }
225
226 if (!$userId) {
227 return false;
228 }
229
230 if (user_can($userId, 'manage_options')) { // WordPress Administrator
231
232 $cache[$userId] = true;
233
234 return true;
235 }
236
237 if (static::isFluentBoardsAdmin($userId)) { // FluentBoard Plugin Administrator
238 $cache[$userId] = true;
239 return true;
240 }
241
242 $cache[$userId] = false;
243 return false;
244 }
245
246 /**
247 * Get array of board Ids for logged-in user
248 * @param null $userId
249 * @return array
250 */
251 public static function getBoardIdsForUser($userId = null, $boardId = null)
252 {
253 if (!$userId) {
254 $userId = get_current_user_id();
255 if (!$userId) {
256 return [];
257 }
258 }
259
260 if (static::isAdmin($userId)) {
261 if ($boardId) {
262 return Board::where('archived_at', null)->where('id', $boardId)->pluck('id')->toArray();
263 }
264 return Board::where('archived_at', null)->pluck('id')->toArray();
265 }
266
267 return Relation::where('foreign_id', $userId)
268 ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
269 ->pluck('object_id')->toArray();
270 }
271
272 public static function getTaskIdsWatchByUser($userId = null)
273 {
274 if (!$userId) {
275 $userId = get_current_user_id();
276 if (!$userId) {
277 return [];
278 }
279 }
280
281 return Relation::where('foreign_id', $userId)
282 ->where('object_type', Constant::OBJECT_TYPE_USER_TASK_WATCH)
283 ->pluck('object_id')->toArray();
284 }
285
286 public static function userCan($permission, $userId = null)
287 {
288 if (!$userId) {
289 $userId = get_current_user_id();
290 if (!$userId) {
291 return false;
292 }
293 }
294
295 if (static::isAdmin($userId)) {
296 return true;
297 }
298 // if user has edit_posts capability then we will allow him
299 if (user_can($userId, 'edit_posts')) {
300 return true;
301 }
302 }
303
304 public static function hasAppAccess($userId = null)
305 {
306 return !!is_user_logged_in();
307 }
308
309 public static function getAll_WP_Admins($searchquery = '')
310 {
311 $args = array(
312 'role' => '',
313 'capability' => 'manage_options',
314 'search' => '*' . $searchquery . '*',
315 );
316
317 return get_users($args);
318 }
319
320 public static function isWPAdmin($userId = null)
321 {
322 if (!$userId) {
323 $userId = get_current_user_id();
324 if (!$userId) {
325 return false;
326 }
327 }
328
329 return user_can($userId, 'manage_options');
330 }
331 }
332