/
PermissionUtil.java
644 lines (582 loc) · 29.8 KB
/
PermissionUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*
* Copyright 2015-2018 Austin Keener & Michael Ritter & Florian Spieß
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core.utils;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.entities.impl.GuildImpl;
import org.apache.commons.collections4.CollectionUtils;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
public class PermissionUtil
{
/**
* Checks if one given Member can interact with a 2nd given Member - in a permission sense (kick/ban/modify perms).
* This only checks the Role-Position and does not check the actual permission (kick/ban/manage_role/...)
*
* @param issuer
* The member that tries to interact with 2nd member
* @param target
* The member that is the target of the interaction
*
* @throws IllegalArgumentException
* if any of the provided parameters is {@code null}
* or the provided entities are not from the same guild
*
* @return True, if issuer can interact with target in guild
*/
public static boolean canInteract(Member issuer, Member target)
{
Checks.notNull(issuer, "Issuer Member");
Checks.notNull(target, "Target Member");
Guild guild = issuer.getGuild();
if (!guild.equals(target.getGuild()))
throw new IllegalArgumentException("Provided members must both be Member objects of the same Guild!");
if(guild.getOwner().equals(issuer))
return true;
if(guild.getOwner().equals(target))
return false;
List<Role> issuerRoles = issuer.getRoles();
List<Role> targetRoles = target.getRoles();
return !issuerRoles.isEmpty() && (targetRoles.isEmpty() || canInteract(issuerRoles.get(0), targetRoles.get(0)));
}
/**
* Checks if a given Member can interact with a given Role - in a permission sense (kick/ban/modify perms).
* This only checks the Role-Position and does not check the actual permission (kick/ban/manage_role/...)
*
* @param issuer
* The member that tries to interact with the role
* @param target
* The role that is the target of the interaction
*
* @throws IllegalArgumentException
* if any of the provided parameters is {@code null}
* or the provided entities are not from the same guild
*
* @return True, if issuer can interact with target
*/
public static boolean canInteract(Member issuer, Role target)
{
Checks.notNull(issuer, "Issuer Member");
Checks.notNull(target, "Target Role");
Guild guild = issuer.getGuild();
if (!guild.equals(target.getGuild()))
throw new IllegalArgumentException("Provided Member issuer and Role target must be from the same Guild!");
if(guild.getOwner().equals(issuer))
return true;
List<Role> issuerRoles = issuer.getRoles();
return !issuerRoles.isEmpty() && canInteract(issuerRoles.get(0), target);
}
/**
* Checks if one given Role can interact with a 2nd given Role - in a permission sense (kick/ban/modify perms).
* This only checks the Role-Position and does not check the actual permission (kick/ban/manage_role/...)
*
* @param issuer
* The role that tries to interact with 2nd role
* @param target
* The role that is the target of the interaction
*
* @throws IllegalArgumentException
* if any of the provided parameters is {@code null}
* or the provided entities are not from the same guild
*
* @return True, if issuer can interact with target
*/
public static boolean canInteract(Role issuer, Role target)
{
Checks.notNull(issuer, "Issuer Role");
Checks.notNull(target, "Target Role");
if(!issuer.getGuild().equals(target.getGuild()))
throw new IllegalArgumentException("The 2 Roles are not from same Guild!");
return target.getPosition() < issuer.getPosition();
}
/**
* Check whether the provided {@link net.dv8tion.jda.core.entities.Member Member} can use the specified {@link net.dv8tion.jda.core.entities.Emote Emote}.
*
* <p>If the specified Member is not in the emote's guild or the emote provided is fake this will return false.
* Otherwise it will check if the emote is restricted to any roles and if that is the case if the Member has one of these.
*
* <p>In the case of an {@link net.dv8tion.jda.core.entities.Emote#isAnimated() animated} Emote, this will
* check if the issuer is the currently logged in account, and then check if the account has
* {@link net.dv8tion.jda.core.entities.SelfUser#isNitro() nitro}, and return false if it doesn't.
* <br>For other accounts, this method will not take into account whether the emote is animated, as there is
* no real way to check if the Member can interact with them.
*
* <br><b>Note</b>: This is not checking if the issuer owns the Guild or not.
*
* @param issuer
* The member that tries to interact with the Emote
* @param emote
* The emote that is the target interaction
*
* @throws IllegalArgumentException
* if any of the provided parameters is {@code null}
* or the provided entities are not from the same guild
*
* @return True, if the issuer can interact with the emote
*/
public static boolean canInteract(Member issuer, Emote emote)
{
Checks.notNull(issuer, "Issuer Member");
Checks.notNull(emote, "Target Emote");
if (!issuer.getGuild().equals(emote.getGuild()))
throw new IllegalArgumentException("The issuer and target are not in the same Guild");
// We don't need to check based on the fact it is animated if it's a BOT account
// because BOT accounts cannot have nitro, and have access to animated Emotes naturally.
if (emote.isAnimated() && !issuer.getUser().isBot())
{
// This is a currently logged in client, meaning we can check if they have nitro or not.
// If this isn't the currently logged in account, we just check it like a normal emote,
// since there is no way to verify if they have nitro or not.
if (issuer.getUser() instanceof SelfUser)
{
// If they don't have nitro, we immediately return
// false, otherwise we proceed with the remaining checks.
if (!((SelfUser)issuer.getUser()).isNitro())
return false;
}
}
return (emote.getRoles().isEmpty() // Emote restricted to roles -> check if the issuer has them
|| CollectionUtils.containsAny(issuer.getRoles(), emote.getRoles()));
}
/**
* Checks whether the specified {@link net.dv8tion.jda.core.entities.Emote Emote} can be used by the provided
* {@link net.dv8tion.jda.core.entities.User User} in the {@link net.dv8tion.jda.core.entities.MessageChannel MessageChannel}.
*
* @param issuer
* The user that tries to interact with the Emote
* @param emote
* The emote that is the target interaction
* @param channel
* The MessageChannel this emote should be interacted within
* @param botOverride
* Whether bots can use non-managed emotes in other guilds
*
* @throws IllegalArgumentException
* if any of the provided parameters is {@code null}
* or the provided entities are not from the same guild
*
* @return True, if the issuer can interact with the emote within the specified MessageChannel
*/
public static boolean canInteract(User issuer, Emote emote, MessageChannel channel, boolean botOverride)
{
Checks.notNull(issuer, "Issuer Member");
Checks.notNull(emote, "Target Emote");
Checks.notNull(channel, "Target Channel");
if (emote.isFake() || !emote.getGuild().isMember(issuer))
return false; // cannot use an emote if you're not in its guild
Member member = emote.getGuild().getMemberById(issuer.getIdLong());
if (!canInteract(member, emote))
return false;
// external means it is available outside of its own guild - works for bots or if its managed
// currently we cannot check whether other users have nitro, we assume no here
final boolean external = emote.isManaged() || (issuer.isBot() && botOverride) || isNitro(issuer);
switch (channel.getType())
{
case TEXT:
TextChannel text = (TextChannel) channel;
member = text.getGuild().getMemberById(issuer.getIdLong());
return emote.getGuild().equals(text.getGuild()) // within the same guild
|| (external && member.hasPermission(text, Permission.MESSAGE_EXT_EMOJI)); // in different guild
default:
return external; // In Group or Private it only needs to be external
}
}
private static boolean isNitro(User issuer)
{
return issuer instanceof SelfUser && ((SelfUser) issuer).isNitro();
}
/**
* Checks whether the specified {@link net.dv8tion.jda.core.entities.Emote Emote} can be used by the provided
* {@link net.dv8tion.jda.core.entities.User User} in the {@link net.dv8tion.jda.core.entities.MessageChannel MessageChannel}.
*
* @param issuer
* The user that tries to interact with the Emote
* @param emote
* The emote that is the target interaction
* @param channel
* The MessageChannel this emote should be interacted within
*
* @throws IllegalArgumentException
* if any of the provided parameters is {@code null}
* or the provided entities are not from the same guild
*
* @return True, if the issuer can interact with the emote within the specified MessageChannel
*/
public static boolean canInteract(User issuer, Emote emote, MessageChannel channel)
{
return canInteract(issuer, emote, channel, true);
}
/**
* Checks to see if the {@link net.dv8tion.jda.core.entities.Member Member} has the specified {@link net.dv8tion.jda.core.Permission Permissions}
* in the specified {@link net.dv8tion.jda.core.entities.Guild Guild}. This method properly deals with Owner status.
*
* <p><b>Note:</b> this is based on effective permissions, not literal permissions. If a member has permissions that would
* enable them to do something without the literal permission to do it, this will still return true.
* <br>Example: If a member has the {@link net.dv8tion.jda.core.Permission#ADMINISTRATOR} permission, they will be able to
* {@link net.dv8tion.jda.core.Permission#MANAGE_SERVER} as well, even without the literal permissions.
*
* @param member
* The {@link net.dv8tion.jda.core.entities.Member Member} whose permissions are being checked.
* @param permissions
* The {@link net.dv8tion.jda.core.Permission Permissions} being checked for.
*
* @throws IllegalArgumentException
* if any of the provided parameters is null
*
* @return True -
* if the {@link net.dv8tion.jda.core.entities.Member Member} effectively has the specified {@link net.dv8tion.jda.core.Permission Permissions}.
*/
public static boolean checkPermission(Member member, Permission... permissions)
{
Checks.notNull(member, "Member");
Checks.notNull(permissions, "Permissions");
long effectivePerms = getEffectivePermission(member);
return isApplied(effectivePerms, Permission.ADMINISTRATOR.getRawValue())
|| isApplied(effectivePerms, Permission.getRaw(permissions));
}
/**
* Checks to see if the {@link net.dv8tion.jda.core.entities.Member Member} has the specified {@link net.dv8tion.jda.core.Permission Permissions}
* in the specified {@link net.dv8tion.jda.core.entities.Channel Channel}. This method properly deals with
* {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverrides} and Owner status.
*
* <p><b>Note:</b> this is based on effective permissions, not literal permissions. If a member has permissions that would
* enable them to do something without the literal permission to do it, this will still return true.
* <br>Example: If a member has the {@link net.dv8tion.jda.core.Permission#ADMINISTRATOR} permission, they will be able to
* {@link net.dv8tion.jda.core.Permission#MESSAGE_WRITE} in every channel.
*
* @param member
* The {@link net.dv8tion.jda.core.entities.Member Member} whose permissions are being checked.
* @param channel
* The {@link net.dv8tion.jda.core.entities.Channel Channel} being checked.
* @param permissions
* The {@link net.dv8tion.jda.core.Permission Permissions} being checked for.
*
* @throws IllegalArgumentException
* if any of the provided parameters is {@code null}
* or the provided entities are not from the same guild
*
* @return True -
* if the {@link net.dv8tion.jda.core.entities.Member Member} effectively has the specified {@link net.dv8tion.jda.core.Permission Permissions}.
*/
public static boolean checkPermission(Channel channel, Member member, Permission... permissions)
{
Checks.notNull(channel, "Channel");
Checks.notNull(member, "Member");
Checks.notNull(permissions, "Permissions");
GuildImpl guild = (GuildImpl) channel.getGuild();
checkGuild(guild, member.getGuild(), "Member");
// if (guild.getOwner().equals(member) // Admin or owner? If yes: no need to iterate
// || guild.getPublicRole().hasPermission(Permission.ADMINISTRATOR)
// || member.getRoles().stream().anyMatch(role -> role.hasPermission(Permission.ADMINISTRATOR)))
// return true; // can be removed as getEffectivePermissions calculates these cases in
long effectivePerms = getEffectivePermission(channel, member);
return isApplied(effectivePerms, Permission.ADMINISTRATOR.getRawValue())
|| isApplied(effectivePerms, Permission.getRaw(permissions));
}
/**
* Gets the {@code long} representation of the effective permissions allowed for this {@link net.dv8tion.jda.core.entities.Member Member}
* in this {@link net.dv8tion.jda.core.entities.Guild Guild}. This can be used in conjunction with
* {@link net.dv8tion.jda.core.Permission#getPermissions(long) Permission.getPermissions(int)} to easily get a list of all
* {@link net.dv8tion.jda.core.Permission Permissions} that this member has in this {@link net.dv8tion.jda.core.entities.Guild Guild}.
*
* <p><b>This only returns the Guild-level permissions!</b>
*
* @param member
* The {@link net.dv8tion.jda.core.entities.Member Member} whose permissions are being checked.
*
* @throws IllegalArgumentException
* if any of the provided parameters is {@code null}
* or the provided entities are not from the same guild
*
* @return The {@code long} representation of the literal permissions that
* this {@link net.dv8tion.jda.core.entities.Member Member} has in this {@link net.dv8tion.jda.core.entities.Guild Guild}.
*/
public static long getEffectivePermission(Member member)
{
Checks.notNull(member, "Member");
if (member.isOwner())
return Permission.ALL_PERMISSIONS;
//Default to binary OR of all global permissions in this guild
long permission = member.getGuild().getPublicRole().getPermissionsRaw();
for (Role role : member.getRoles())
{
permission |= role.getPermissionsRaw();
if (isApplied(permission, Permission.ADMINISTRATOR.getRawValue()))
return Permission.ALL_PERMISSIONS;
}
return permission;
}
/**
* Gets the {@code long} representation of the effective permissions allowed for this {@link net.dv8tion.jda.core.entities.Member Member}
* in this {@link net.dv8tion.jda.core.entities.Channel Channel}. This can be used in conjunction with
* {@link net.dv8tion.jda.core.Permission#getPermissions(long) Permission.getPermissions(long)} to easily get a list of all
* {@link net.dv8tion.jda.core.Permission Permissions} that this member can use in this {@link net.dv8tion.jda.core.entities.Channel Channel}.
* <br>This functions very similarly to how {@link net.dv8tion.jda.core.entities.Role#getPermissionsRaw() Role.getPermissionsRaw()}.
*
* @param channel
* The {@link net.dv8tion.jda.core.entities.Channel Channel} being checked.
* @param member
* The {@link net.dv8tion.jda.core.entities.Member Member} whose permissions are being checked.
*
* @throws IllegalArgumentException
* if any of the provided parameters is {@code null}
* or the provided entities are not from the same guild
*
* @return The {@code long} representation of the effective permissions that this {@link net.dv8tion.jda.core.entities.Member Member}
* has in this {@link net.dv8tion.jda.core.entities.Channel Channel}.
*/
public static long getEffectivePermission(Channel channel, Member member)
{
Checks.notNull(channel, "Channel");
Checks.notNull(member, "Member");
Checks.check(channel.getGuild().equals(member.getGuild()), "Provided channel and provided member are not of the same guild!");
if (member.isOwner())
{
// Owner effectively has all permissions
return Permission.ALL_PERMISSIONS;
}
long permission = getEffectivePermission(member) | getExplicitPermission(channel, member);
if (isApplied(permission, Permission.ADMINISTRATOR.getRawValue()))
return Permission.ALL_PERMISSIONS;
AtomicLong allow = new AtomicLong(0);
AtomicLong deny = new AtomicLong(0);
getExplicitOverrides(channel, member, allow, deny);
permission = apply(permission, allow.get(), deny.get());
if (!isApplied(permission, Permission.VIEW_CHANNEL.getRawValue()))
{
//When the permission to view the channel is not applied it is not granted
// This means that we have no access to this channel at all
return 0;
}
final long managePerms = Permission.MANAGE_PERMISSIONS.getRawValue();
final long manageChannel = Permission.MANAGE_CHANNEL.getRawValue();
if ((permission & (managePerms | manageChannel)) != 0)
{
// In channels, MANAGE_CHANNEL and MANAGE_PERMISSIONS grant full text/voice permissions
permission |= Permission.ALL_TEXT_PERMISSIONS | Permission.ALL_VOICE_PERMISSIONS;
}
return apply(permission, allow.get(), deny.get());
}
/**
* Gets the {@code long} representation of the effective permissions allowed for this {@link net.dv8tion.jda.core.entities.Role Role}
* in this {@link net.dv8tion.jda.core.entities.Channel Channel}. This can be used in conjunction with
* {@link net.dv8tion.jda.core.Permission#getPermissions(long) Permission.getPermissions(long)} to easily get a list of all
* {@link net.dv8tion.jda.core.Permission Permissions} that this role can use in this {@link net.dv8tion.jda.core.entities.Channel Channel}.
*
* @param channel
* The {@link net.dv8tion.jda.core.entities.Channel Channel} in which permissions are being checked.
* @param role
* The {@link net.dv8tion.jda.core.entities.Role Role} whose permissions are being checked.
*
* @throws IllegalArgumentException
* if any of the provided parameters is {@code null}
* or the provided entities are not from the same guild
*
* @return The {@code long} representation of the effective permissions that this {@link net.dv8tion.jda.core.entities.Role Role}
* has in this {@link net.dv8tion.jda.core.entities.Channel Channel}
*/
public static long getEffectivePermission(Channel channel, Role role)
{
Checks.notNull(channel, "Channel");
Checks.notNull(role, "Role");
Guild guild = channel.getGuild();
if (!guild.equals(role.getGuild()))
throw new IllegalArgumentException("Provided channel and role are not of the same guild!");
long permissions = role.getPermissionsRaw() | guild.getPublicRole().getPermissionsRaw();
PermissionOverride publicOverride = channel.getPermissionOverride(guild.getPublicRole());
PermissionOverride roleOverride = channel.getPermissionOverride(role);
if (publicOverride != null)
{
permissions &= ~publicOverride.getDeniedRaw();
permissions |= publicOverride.getAllowedRaw();
}
if (roleOverride != null)
{
permissions &= ~roleOverride.getDeniedRaw();
permissions |= roleOverride.getAllowedRaw();
}
return permissions;
}
/**
* Retrieves the explicit permissions of the specified {@link net.dv8tion.jda.core.entities.Member Member}
* in its hosting {@link net.dv8tion.jda.core.entities.Guild Guild}.
* <br>This method does not calculate the owner in.
*
* <p>All permissions returned are explicitly granted to this Member via its {@link net.dv8tion.jda.core.entities.Role Roles}.
* <br>Permissions like {@link net.dv8tion.jda.core.Permission#ADMINISTRATOR Permission.ADMINISTRATOR} do not
* grant other permissions in this value.
*
* @param member
* The non-null {@link net.dv8tion.jda.core.entities.Member Member} for which to get implicit permissions
*
* @throws IllegalArgumentException
* If the specified member is {@code null}
*
* @return Primitive (unsigned) long value with the implicit permissions of the specified member
*
* @since 3.1
*/
public static long getExplicitPermission(Member member)
{
Checks.notNull(member, "Member");
final Guild guild = member.getGuild();
long permission = guild.getPublicRole().getPermissionsRaw();
for (Role role : member.getRoles())
permission |= role.getPermissionsRaw();
return permission;
}
/**
* Retrieves the explicit permissions of the specified {@link net.dv8tion.jda.core.entities.Member Member}
* in its hosting {@link net.dv8tion.jda.core.entities.Guild Guild} and specific {@link net.dv8tion.jda.core.entities.Channel Channel}.
* <br>This method does not calculate the owner in.
* <b>Allowed permissions override denied permissions of {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverrides}!</b>
*
* <p>All permissions returned are explicitly granted to this Member via its {@link net.dv8tion.jda.core.entities.Role Roles}.
* <br>Permissions like {@link net.dv8tion.jda.core.Permission#ADMINISTRATOR Permission.ADMINISTRATOR} do not
* grant other permissions in this value.
* <p>This factor in all {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverrides} that affect this member
* and only grants the ones that are explicitly given.
*
* @param channel
* The target channel of which to check {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverrides}
* @param member
* The non-null {@link net.dv8tion.jda.core.entities.Member Member} for which to get implicit permissions
*
* @throws IllegalArgumentException
* If any of the arguments is {@code null}
* or the specified entities are not from the same {@link net.dv8tion.jda.core.entities.Guild Guild}
*
* @return Primitive (unsigned) long value with the implicit permissions of the specified member in the specified channel
*
* @since 3.1
*/
public static long getExplicitPermission(Channel channel, Member member)
{
Checks.notNull(channel, "Channel");
Checks.notNull(member, "Member");
final Guild guild = member.getGuild();
checkGuild(channel.getGuild(), guild, "Member");
long permission = guild.getPublicRole().getPermissionsRaw();
AtomicLong allow = new AtomicLong(0);
AtomicLong deny = new AtomicLong(0);
// populates allow/deny
getExplicitOverrides(channel, member, allow, deny);
return apply(permission, allow.get(), deny.get());
}
/**
* Retrieves the explicit permissions of the specified {@link net.dv8tion.jda.core.entities.Role Role}
* in its hosting {@link net.dv8tion.jda.core.entities.Guild Guild} and specific {@link net.dv8tion.jda.core.entities.Channel Channel}.
* <br><b>Allowed permissions override denied permissions of {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverrides}!</b>
*
* <p>All permissions returned are explicitly granted to this Role.
* <br>Permissions like {@link net.dv8tion.jda.core.Permission#ADMINISTRATOR Permission.ADMINISTRATOR} do not
* grant other permissions in this value.
* <p>This factor in existing {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverrides} if possible.
*
* @param channel
* The target channel of which to check {@link net.dv8tion.jda.core.entities.PermissionOverride PermissionOverrides}
* @param role
* The non-null {@link net.dv8tion.jda.core.entities.Role Role} for which to get implicit permissions
*
* @throws IllegalArgumentException
* If any of the arguments is {@code null}
* or the specified entities are not from the same {@link net.dv8tion.jda.core.entities.Guild Guild}
*
* @return Primitive (unsigned) long value with the implicit permissions of the specified role in the specified channel
*
* @since 3.1
*/
public static long getExplicitPermission(Channel channel, Role role)
{
Checks.notNull(channel, "Channel");
Checks.notNull(role, "Role");
final Guild guild = role.getGuild();
checkGuild(channel.getGuild(), guild, "Role");
long permission = role.getPermissionsRaw() | guild.getPublicRole().getPermissionsRaw();
PermissionOverride override = channel.getPermissionOverride(guild.getPublicRole());
if (override != null)
permission = apply(permission, override.getAllowedRaw(), override.getDeniedRaw());
if (role.isPublicRole())
return permission;
override = channel.getPermissionOverride(role);
return override == null
? permission
: apply(permission, override.getAllowedRaw(), override.getDeniedRaw());
}
/**
* Pushes all deny/allow values to the specified BiConsumer
* <br>First parameter is allow, second is deny
*/
private static void getExplicitOverrides(Channel channel, Member member, AtomicLong allow, AtomicLong deny)
{
PermissionOverride override = channel.getPermissionOverride(member.getGuild().getPublicRole());
long allowRaw = 0;
long denyRaw = 0;
if (override != null)
{
denyRaw = override.getDeniedRaw();
allowRaw = override.getAllowedRaw();
}
long allowRole = 0;
long denyRole = 0;
// create temporary bit containers for role cascade
for (Role role : member.getRoles())
{
override = channel.getPermissionOverride(role);
if (override != null)
{
// important to update role cascade not others
denyRole |= override.getDeniedRaw();
allowRole |= override.getAllowedRaw();
}
}
// Override the raw values of public role then apply role cascade
allowRaw = (allowRaw & ~denyRole) | allowRole;
denyRaw = (denyRaw & ~allowRole) | denyRole;
override = channel.getPermissionOverride(member);
if (override != null)
{
// finally override the role cascade with member overrides
final long oDeny = override.getDeniedRaw();
final long oAllow = override.getAllowedRaw();
allowRaw = (allowRaw & ~oDeny) | oAllow;
denyRaw = (denyRaw & ~oAllow) | oDeny;
// this time we need to exclude new allowed bits from old denied ones and OR the new denied bits as final overrides
}
// set as resulting values
allow.set(allowRaw);
deny.set(denyRaw);
}
/*
* Check whether the specified permission is applied in the bits
*/
private static boolean isApplied(long permissions, long perms)
{
return (permissions & perms) == perms;
}
private static long apply(long permission, long allow, long deny)
{
permission &= ~deny; //Deny everything that the cascade of roles denied.
permission |= allow; //Allow all the things that the cascade of roles allowed
// The allowed bits override the denied ones!
return permission;
}
private static void checkGuild(Guild o1, Guild o2, String name)
{
Checks.check(o1.equals(o2),
"Specified %s is not in the same guild! (%s / %s)", name, o1.toString(), o2.toString());
}
}