Search in sources :

Example 1 with PermissionType

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();
}
Also used : Role(org.javacord.api.entity.permission.Role) Server(org.javacord.api.entity.server.Server) PermissionType(org.javacord.api.entity.permission.PermissionType) Permissions(org.javacord.api.entity.permission.Permissions) ArrayList(java.util.ArrayList) PermissionsBuilder(org.javacord.api.entity.permission.PermissionsBuilder)

Example 2 with PermissionType

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();
}
Also used : PermissionType(org.javacord.api.entity.permission.PermissionType) Arrays(java.util.Arrays) PermissionsBuilder(org.javacord.api.entity.permission.PermissionsBuilder) Collection(java.util.Collection) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Permissionable(org.javacord.api.entity.Permissionable) Permissions(org.javacord.api.entity.permission.Permissions) DiscordEntity(org.javacord.api.entity.DiscordEntity) PermissionState(org.javacord.api.entity.permission.PermissionState) User(org.javacord.api.entity.user.User) Map(java.util.Map) Optional(java.util.Optional) NoSuchElementException(java.util.NoSuchElementException) Collections(java.util.Collections) Permissions(org.javacord.api.entity.permission.Permissions) PermissionsBuilder(org.javacord.api.entity.permission.PermissionsBuilder)

Aggregations

PermissionType (org.javacord.api.entity.permission.PermissionType)2 Permissions (org.javacord.api.entity.permission.Permissions)2 PermissionsBuilder (org.javacord.api.entity.permission.PermissionsBuilder)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 DiscordEntity (org.javacord.api.entity.DiscordEntity)1 Permissionable (org.javacord.api.entity.Permissionable)1 PermissionState (org.javacord.api.entity.permission.PermissionState)1 Role (org.javacord.api.entity.permission.Role)1 Server (org.javacord.api.entity.server.Server)1 User (org.javacord.api.entity.user.User)1