use of org.javacord.api.entity.permission.PermissionType in project Javacord by BtoBastian.
the class RegularServerChannelImpl method getEffectiveOverwrittenPermissions.
@Override
public Permissions getEffectiveOverwrittenPermissions(User user) {
PermissionsBuilder builder = new PermissionsBuilder(PermissionsImpl.EMPTY_PERMISSIONS);
Server server = getServer();
Role everyoneRole = server.getEveryoneRole();
Permissions everyoneRolePermissionOverwrites = getOverwrittenPermissions(everyoneRole);
for (PermissionType type : PermissionType.values()) {
if (everyoneRolePermissionOverwrites.getState(type) == PermissionState.DENIED) {
builder.setState(type, PermissionState.DENIED);
}
if (everyoneRolePermissionOverwrites.getState(type) == PermissionState.ALLOWED) {
builder.setState(type, PermissionState.ALLOWED);
}
}
List<Role> rolesOfUser = new ArrayList<>(server.getRoles(user));
rolesOfUser.remove(everyoneRole);
List<Permissions> permissionOverwrites = rolesOfUser.stream().map(this::getOverwrittenPermissions).collect(Collectors.toList());
for (Permissions permissions : permissionOverwrites) {
for (PermissionType type : PermissionType.values()) {
if (permissions.getState(type) == PermissionState.DENIED) {
builder.setState(type, PermissionState.DENIED);
}
}
}
for (Permissions permissions : permissionOverwrites) {
for (PermissionType type : PermissionType.values()) {
if (permissions.getState(type) == PermissionState.ALLOWED) {
builder.setState(type, PermissionState.ALLOWED);
}
}
}
for (PermissionType type : PermissionType.values()) {
Permissions permissions = overwrittenUserPermissions.getOrDefault(user.getId(), PermissionsImpl.EMPTY_PERMISSIONS);
if (permissions.getState(type) == PermissionState.DENIED) {
builder.setState(type, PermissionState.DENIED);
}
if (permissions.getState(type) == PermissionState.ALLOWED) {
builder.setState(type, PermissionState.ALLOWED);
}
}
return builder.build();
}
use of org.javacord.api.entity.permission.PermissionType in project Javacord by BtoBastian.
the class RegularServerChannel method getEffectivePermissions.
/**
* Gets the effective permissions of a user in this channel.
* The returned permission object will only have {@link PermissionState#ALLOWED} and
* {@link PermissionState#DENIED} states!
* It takes into account global permissions and the effective overwritten permissions of a user.
* Remember, that some permissions affect others!
* E.g. a user who has {@link PermissionType#SEND_MESSAGES} but not {@link PermissionType#READ_MESSAGES} cannot
* send messages, even though he has the {@link PermissionType#SEND_MESSAGES} permission.
*
* @param user The user.
* @return The effective permissions of the user in this channel.
*/
default Permissions getEffectivePermissions(User user) {
if (getServer().isOwner(user)) {
return getServer().getPermissions(user);
}
PermissionsBuilder builder = new PermissionsBuilder(getServer().getPermissions(user));
Permissions effectiveOverwrittenPermissions = getEffectiveOverwrittenPermissions(user);
Arrays.stream(PermissionType.values()).filter(type -> effectiveOverwrittenPermissions.getState(type) != PermissionState.UNSET).forEachOrdered(type -> builder.setState(type, effectiveOverwrittenPermissions.getState(type)));
Arrays.stream(PermissionType.values()).filter(type -> builder.getState(type) == PermissionState.UNSET).forEachOrdered(type -> builder.setState(type, PermissionState.DENIED));
return builder.build();
}
Aggregations