Skip to content

Commit 3fe5d1f

Browse files
committed
Add per-request override for users-unrestricted-roles
1 parent fee9fb8 commit 3fe5d1f

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

accounts/src/main/java/org/restheart/accounts/AccountsInitializer.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.restheart.plugins.Initializer;
1212
import org.restheart.plugins.Inject;
1313
import org.restheart.plugins.RegisterPlugin;
14+
import org.restheart.accounts.util.RequestOverrides;
1415
import org.restheart.security.ACLRegistry;
1516
import org.restheart.security.predicates.BsonRequestWhitelistPredicate;
1617
import org.slf4j.Logger;
@@ -72,8 +73,10 @@ public void init() {
7273
if (!(r instanceof MongoRequest mr)) return false;
7374

7475
// Roles configured via `users-unrestricted-roles` (e.g. an admin console
75-
// role) bypass this restriction entirely — see AccountsConfigData.
76-
var exemptRoles = conf.usersUnrestrictedRoles();
76+
// role) bypass this restriction entirely — see AccountsConfigData. Reads
77+
// the per-team override first (set by e.g. TeamConfigInterceptor), falling
78+
// back to the node-level YAML config.
79+
var exemptRoles = RequestOverrides.usersUnrestrictedRoles(mr, conf);
7780
if (exemptRoles != null && exemptRoles.stream().anyMatch(r::isAccountInRole)) {
7881
return false;
7982
}

accounts/src/main/java/org/restheart/accounts/util/RequestOverrides.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.restheart.accounts.oauth.OAuthConfig;
55
import org.restheart.exchange.ServiceRequest;
66

7+
import java.util.List;
8+
79
/**
810
* Reads per-request override parameters and returns the effective values,
911
* falling back to the plugin's static configuration.
@@ -66,6 +68,12 @@
6668
* <td>Google OAuth client secret for this team</td>
6769
* <td>{@code null}</td>
6870
* </tr>
71+
* <tr>
72+
* <td>{@code override-accounts-users-unrestricted-roles}</td>
73+
* <td>Roles exempt from the {@code /users} self-service write restriction
74+
* (see {@code AccountsInitializer})</td>
75+
* <td>{@link AccountsConfigData#usersUnrestrictedRoles()}</td>
76+
* </tr>
6977
* </table>
7078
*
7179
* <h2>Multi-team usage (restheart-cloud)</h2>
@@ -139,6 +147,11 @@ public final class RequestOverrides {
139147
/** Team role for the user who creates a team (override for multi-team). */
140148
public static final String OWNERSHIP_ROLE = "override-accounts-ownership-role";
141149

150+
// ── Users self-service write restriction override ───────────────────────
151+
152+
/** Roles exempt from the {@code /users} self-service write restriction (override for multi-team). */
153+
public static final String USERS_UNRESTRICTED_ROLES = "override-accounts-users-unrestricted-roles";
154+
142155
private RequestOverrides() {}
143156

144157
// ── Accessor methods ──────────────────────────────────────────────────────
@@ -201,6 +214,11 @@ public static String ownershipRole(ServiceRequest<?> req, AccountsConfigData con
201214
return str(req, OWNERSHIP_ROLE, conf.ownershipRole());
202215
}
203216

217+
/** Effective roles exempt from the {@code /users} self-service write restriction. */
218+
public static List<String> usersUnrestrictedRoles(ServiceRequest<?> req, AccountsConfigData conf) {
219+
return list(req, USERS_UNRESTRICTED_ROLES, conf.usersUnrestrictedRoles());
220+
}
221+
204222
/**
205223
* Per-team Google OAuth config, or {@code null} if not overridden.
206224
* When non-null, this takes precedence over the static {@link OAuthConfig}.
@@ -228,4 +246,13 @@ private static boolean bool(ServiceRequest<?> req, String key, boolean defaultVa
228246
if (v instanceof String s) return Boolean.parseBoolean(s);
229247
return defaultValue;
230248
}
249+
250+
@SuppressWarnings("unchecked")
251+
private static List<String> list(ServiceRequest<?> req, String key, List<String> defaultValue) {
252+
var v = req.attachedParam(key);
253+
if (v instanceof List<?> l && !l.isEmpty()) {
254+
return (List<String>) l;
255+
}
256+
return defaultValue;
257+
}
231258
}

0 commit comments

Comments
 (0)