|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright [ 2020 - 2024 ] Matthew Buckton |
| 4 | + * Copyright [ 2024 - 2025 ] MapsMessaging B.V. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 with the Commons Clause |
| 7 | + * (the "License"); you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at: |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * https://commonsclause.com/ |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package io.mapsmessaging.auth; |
| 21 | + |
| 22 | + |
| 23 | +import io.mapsmessaging.security.authorisation.Permission; |
| 24 | +import lombok.Getter; |
| 25 | + |
| 26 | +@Getter |
| 27 | +public enum ServerPermissions implements Permission { |
| 28 | + |
| 29 | + READ("read", "Allows Read access to the resource", 0), |
| 30 | + WRITE("write", "Allows Write access to the resource", 1), |
| 31 | + DELETE("delete", "Allows Delete access to the resource", 2), |
| 32 | + CREATE("create", "Allows Create access to the resource", 3), |
| 33 | + SUBSCRIBE("subscribe", "Allows Subscription access to the topic", 4), |
| 34 | + PUBLISH("publish", "Allows Publish access to the topic", 5), |
| 35 | + PUBLISH_RETAINED("retain", "Allows Retain access to the topic", 6), |
| 36 | + CONNECT("connect", "allows user to connect", 7), |
| 37 | + MANAGE("manage", "allows user to manage resource", 8) |
| 38 | + ; |
| 39 | + |
| 40 | + private final String name; |
| 41 | + private final String description; |
| 42 | + private final long mask; |
| 43 | + |
| 44 | + ServerPermissions(final String name, final String description, final long mask) { |
| 45 | + this.name = name; |
| 46 | + this.description = description; |
| 47 | + this.mask = 1L <<mask; |
| 48 | + } |
| 49 | + |
| 50 | + public static String generateOpenFgaModel() { |
| 51 | + StringBuilder stringBuilder = new StringBuilder(); |
| 52 | + |
| 53 | + stringBuilder.append("model\n"); |
| 54 | + stringBuilder.append(" schema 1.1\n"); |
| 55 | + stringBuilder.append("\n"); |
| 56 | + stringBuilder.append("type user\n"); |
| 57 | + stringBuilder.append("\n"); |
| 58 | + stringBuilder.append("type group\n"); |
| 59 | + stringBuilder.append(" relations\n"); |
| 60 | + stringBuilder.append(" define member: [user]\n"); |
| 61 | + stringBuilder.append("\n"); |
| 62 | + stringBuilder.append("type resource\n"); |
| 63 | + stringBuilder.append(" relations\n"); |
| 64 | + |
| 65 | + for (ServerPermissions serverPermission : ServerPermissions.values()) { |
| 66 | + String permissionName = serverPermission.getName(); |
| 67 | + stringBuilder |
| 68 | + .append(" define ") |
| 69 | + .append(permissionName) |
| 70 | + .append(": [user, group#member]\n"); |
| 71 | + } |
| 72 | + |
| 73 | + return stringBuilder.toString(); |
| 74 | + } |
| 75 | + |
| 76 | + public static void main(String[] args) { |
| 77 | + System.err.println(generateOpenFgaModel()); |
| 78 | + } |
| 79 | +} |
0 commit comments