|
| 1 | +package com.gewia.common.scope; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * A scope is a mechanism to limit permissions of a person or application. |
| 8 | + * |
| 9 | + * <p> |
| 10 | + * This implementation is the reference implementation of scopes for the 'Gewia' project. |
| 11 | + * |
| 12 | + * Examples: |
| 13 | + * user.email.read.self - let's the user read his own email address |
| 14 | + * user.email.write.self - let's the user change his own email address |
| 15 | + * user.email.write.all - let's the user change every email address of every user |
| 16 | + * user.email.write.steve - let's the user change the email address of "steve"; this is a custom behaviour |
| 17 | + * and not part of the official specification |
| 18 | + * </p> |
| 19 | + * @since 1.0 |
| 20 | + */ |
| 21 | +public abstract class Scope { |
| 22 | + |
| 23 | + protected final List<ScopePart> scopeParts = new ArrayList<>(); |
| 24 | + |
| 25 | + /** |
| 26 | + * Gets the currently available scope parts. |
| 27 | + * |
| 28 | + * <p> |
| 29 | + * Whether the returned list is readonly or even a copy will be specified by |
| 30 | + * the implementation. |
| 31 | + * </p> |
| 32 | + * |
| 33 | + * @return all scope parts |
| 34 | + * |
| 35 | + * @since 1.0 |
| 36 | + */ |
| 37 | + public abstract List<ScopePart> getScopeParts(); |
| 38 | + |
| 39 | + /** |
| 40 | + * Adds the given <i>scopeParts</i> at the end of the scope. |
| 41 | + * |
| 42 | + * @param scopeParts the scope parts to add |
| 43 | + * |
| 44 | + * @return this |
| 45 | + * |
| 46 | + * @since 1.0 |
| 47 | + */ |
| 48 | + public Scope addScopeParts(String... scopeParts) { |
| 49 | + for (String scopePart : scopeParts) this.addScopePart(new ScopePart(scopePart)); |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Adds the given <i>scopePart</i> at the end of the scope. |
| 55 | + * |
| 56 | + * @param scopePart the scope part to add |
| 57 | + * |
| 58 | + * @return this |
| 59 | + * |
| 60 | + * @since 1.0 |
| 61 | + */ |
| 62 | + public Scope addScopePart(String scopePart) { |
| 63 | + return this.addScopePart(new ScopePart(scopePart)); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Adds the given <i>scopePart</i> at the end of the scope. |
| 68 | + * |
| 69 | + * @param scopePart the scope part to add |
| 70 | + * |
| 71 | + * @return this |
| 72 | + * |
| 73 | + * @since 1.0 |
| 74 | + */ |
| 75 | + public abstract Scope addScopePart(ScopePart scopePart); |
| 76 | + |
| 77 | + /** |
| 78 | + * Removes the scope part at the given <i>index</i>. |
| 79 | + * |
| 80 | + * @param index the index used to remove the scope part |
| 81 | + * |
| 82 | + * @return this |
| 83 | + * @throws IndexOutOfBoundsException when the index is greater than the amount scope parts |
| 84 | + * |
| 85 | + * @since 1.0 |
| 86 | + */ |
| 87 | + public abstract Scope removeScopePart(int index); |
| 88 | + |
| 89 | + /** |
| 90 | + * Gets the scope part at the given <i>index</i>. |
| 91 | + * |
| 92 | + * @param index the index used to get the scope part |
| 93 | + * |
| 94 | + * @return this |
| 95 | + * @throws IndexOutOfBoundsException when the index is greater than the amount scope parts |
| 96 | + * |
| 97 | + * @since 1.0 |
| 98 | + */ |
| 99 | + public abstract ScopePart getScopePart(int index); |
| 100 | + |
| 101 | + /** |
| 102 | + * Gets the amount of scope parts of this scope. |
| 103 | + * |
| 104 | + * @return the amount of scope parts |
| 105 | + * |
| 106 | + * @since 1.0 |
| 107 | + */ |
| 108 | + public abstract int getScopePartsSize(); |
| 109 | + |
| 110 | + @Override |
| 111 | + public String toString() { |
| 112 | + StringBuilder sb = new StringBuilder(); |
| 113 | + |
| 114 | + this.getScopeParts().forEach(scopePart -> { |
| 115 | + if (sb.length() != 0) sb.append("."); |
| 116 | + |
| 117 | + sb.append(scopePart); |
| 118 | + }); |
| 119 | + |
| 120 | + return sb.toString(); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments