001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.permissions;
020
021import com.plotsquared.core.configuration.Settings;
022import org.checkerframework.checker.index.qual.NonNegative;
023import org.checkerframework.checker.nullness.qual.NonNull;
024import org.checkerframework.checker.nullness.qual.Nullable;
025
026/**
027 * Any object which can hold permissions
028 */
029public interface PermissionHolder {
030
031    /**
032     * Check if the owner of the profile has a given (global) permission
033     *
034     * @param permission Permission
035     * @return {@code true} if the owner has the given permission, else {@code false}
036     */
037    default boolean hasPermission(final @NonNull String permission) {
038        return hasPermission(null, permission);
039    }
040
041    /**
042     * Check if the owner of the profile has a given (global) permission
043     *
044     * @param permission Permission
045     * @return {@code true} if the owner has the given permission, else {@code false}
046     */
047    default boolean hasPermission(final @NonNull Permission permission) {
048        return hasPermission(permission.toString());
049    }
050
051    /**
052     * Check if the owner of the profile has a given (global) keyed permission. Checks both {@code permission.key}
053     * and {@code permission.*}
054     *
055     * @param permission Permission
056     * @param key        Permission "key"
057     * @return {@code true} if the owner has the given permission, else {@code false}
058     * @since 6.0.10
059     */
060    default boolean hasKeyedPermission(
061            final @NonNull String permission,
062            final @NonNull String key
063    ) {
064        return hasKeyedPermission(null, permission, key);
065    }
066
067    /**
068     * Check the highest permission a PlotPlayer has within a specified range.<br>
069     * - Excessively high values will lag<br>
070     * - The default range that is checked is {@link Settings.Limit#MAX_PLOTS}<br>
071     *
072     * @param stub  The permission stub to check e.g. for `plots.plot.#` the stub is `plots.plot`
073     * @param range The range to check
074     * @return The highest permission they have within that range
075     */
076    @NonNegative
077    default int hasPermissionRange(
078            final @NonNull Permission stub,
079            @NonNegative final int range
080    ) {
081        return hasPermissionRange(stub.toString(), range);
082    }
083
084    /**
085     * Check the highest permission a PlotPlayer has within a specified range.<br>
086     * - Excessively high values will lag<br>
087     * - The default range that is checked is {@link Settings.Limit#MAX_PLOTS}<br>
088     *
089     * @param stub  The permission stub to check e.g. for `plots.plot.#` the stub is `plots.plot`
090     * @param range The range to check
091     * @return The highest permission they have within that range
092     */
093    @NonNegative
094    default int hasPermissionRange(
095            final @NonNull String stub,
096            @NonNegative final int range
097    ) {
098        if (hasPermission(Permission.PERMISSION_ADMIN.toString())) {
099            return Integer.MAX_VALUE;
100        }
101        String[] nodes = stub.split("\\.");
102        StringBuilder builder = new StringBuilder();
103        for (int i = 0; i < (nodes.length - 1); i++) {
104            builder.append(nodes[i]).append(".");
105            if (!stub.equals(builder + Permission.PERMISSION_STAR.toString())) {
106                if (hasPermission(builder + Permission.PERMISSION_STAR.toString())) {
107                    return Integer.MAX_VALUE;
108                }
109            }
110        }
111        if (hasPermission(stub + ".*")) {
112            return Integer.MAX_VALUE;
113        }
114        for (int i = range; i > 0; i--) {
115            if (hasPermission(stub + "." + i)) {
116                return i;
117            }
118        }
119        return 0;
120    }
121
122    /**
123     * Checks if the owner of the profile has a permission, and optionally send the no permission message if applicable.
124     *
125     * @param permission Permission
126     * @param notify     If to notify the permission holder
127     * @return {@code true} if the owner has the given permission, else {@code false}
128     */
129    default boolean hasPermission(@NonNull Permission permission, boolean notify) {
130        return hasPermission(permission.toString(), notify);
131    }
132
133    /**
134     * Checks if the owner of the profile has a permission, and optionally send the no permission message if applicable.
135     *
136     * @param permission Permission
137     * @param notify     If to notify the permission holder
138     * @return {@code true} if the owner has the given permission, else {@code false}
139     */
140    boolean hasPermission(@NonNull String permission, boolean notify);
141
142    /**
143     * Check if the owner of the profile has a given permission
144     *
145     * @param world      World name
146     * @param permission Permission
147     * @return {@code true} if the owner has the given permission, else {@code false}
148     */
149    boolean hasPermission(@Nullable String world, @NonNull String permission);
150
151    /**
152     * Check if the owner of the profile has a given keyed permission. Checks both {@code permission.key}
153     * and {@code permission.*}
154     *
155     * @param world      World name
156     * @param permission Permission
157     * @param key        Permission "key"
158     * @return {@code true} if the owner has the given permission, else {@code false}
159     * @since 6.0.10
160     */
161    boolean hasKeyedPermission(@Nullable String world, @NonNull String permission, @NonNull String key);
162
163}