Skip to content

Commit

Permalink
fix: user roles
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Mar 1, 2024
1 parent 9eb76a1 commit 4843083
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.google.gson.JsonObject;
import io.supertokens.Main;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -48,7 +50,7 @@ public String getPath() {

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
// API is tenant specific, but uses public tenant storage
// API is tenant specific
JsonObject input = InputParser.parseJsonObjectOrThrowError(req);
String userId = InputParser.parseStringOrThrowError(input, "userId", false);
String role = InputParser.parseStringOrThrowError(input, "role", false);
Expand All @@ -60,9 +62,11 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IO
}

try {
TenantIdentifier tenantIdentifier = getTenantIdentifier(req);
Storage storage = getTenantStorage(req);

boolean didUserAlreadyHaveRole = !UserRoles.addRoleToUser(
this.getTenantStorage(req).withStorage(this.getPublicTenantStorage(req).getStorage()),
userId, role);
tenantIdentifier, storage, userId, role);
JsonObject response = new JsonObject();
response.addProperty("status", "OK");
response.addProperty("didUserAlreadyHaveRole", didUserAlreadyHaveRole);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.google.gson.JsonObject;
import io.supertokens.Main;
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -81,8 +83,10 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IO
}

try {
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage storage = enforcePublicTenantAndGetPublicTenantStorage(req);
boolean createdNewRole = UserRoles.createNewRoleOrModifyItsPermissions(
this.enforcePublicTenantAndGetPublicTenantStorage(req), role, permissions);
appIdentifier, storage, role, permissions);

JsonObject response = new JsonObject();
response.addProperty("status", "OK");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.gson.JsonPrimitive;
import io.supertokens.Main;
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -60,7 +62,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
}

try {
String[] permissions = UserRoles.getPermissionsForRole(this.enforcePublicTenantAndGetPublicTenantStorage(req), role);
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage storage = enforcePublicTenantAndGetPublicTenantStorage(req);

String[] permissions = UserRoles.getPermissionsForRole(appIdentifier, storage, role);
JsonArray arr = new JsonArray();
for (String permission : permissions) {
arr.add(new JsonPrimitive(permission));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.gson.JsonPrimitive;
import io.supertokens.Main;
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -50,8 +52,10 @@ public String getPath() {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
// API is app specific
try {
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage storage = enforcePublicTenantAndGetPublicTenantStorage(req);

String[] roles = UserRoles.getRoles(this.enforcePublicTenantAndGetPublicTenantStorage(req));
String[] roles = UserRoles.getRoles(appIdentifier, storage);
JsonArray arr = new JsonArray();

for (String s : roles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.gson.JsonPrimitive;
import io.supertokens.Main;
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -60,8 +62,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
}

try {
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage storage = enforcePublicTenantAndGetPublicTenantStorage(req);

String[] roles = UserRoles.getRolesThatHavePermission(this.enforcePublicTenantAndGetPublicTenantStorage(req), permission);
String[] roles = UserRoles.getRolesThatHavePermission(appIdentifier, storage, permission);
JsonArray arr = new JsonArray();

for (String s : roles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import io.supertokens.Main;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -48,12 +50,12 @@ public String getPath() {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
// API is tenant specific, but using the public tenant storage
// API is tenant specific
String userId = InputParser.getQueryParamOrThrowError(req, "userId", false);
try {
String[] userRoles = UserRoles.getRolesForUser(
this.getTenantStorage(req).withStorage(this.getPublicTenantStorage(req).getStorage()),
userId);
TenantIdentifier tenantIdentifier = getTenantIdentifier(req);
Storage storage = getTenantStorage(req);
String[] userRoles = UserRoles.getRolesForUser(tenantIdentifier, storage, userId);
JsonArray arr = new JsonArray();
for (String s : userRoles) {
arr.add(new JsonPrimitive(s));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import io.supertokens.Main;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -60,9 +62,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
}

try {
String[] roleUsers = UserRoles.getUsersForRole(
this.getTenantStorage(req).withStorage(this.getPublicTenantStorage(req).getStorage()),
role);
TenantIdentifier tenantIdentifier = getTenantIdentifier(req);
Storage storage = getTenantStorage(req);

String[] roleUsers = UserRoles.getUsersForRole(tenantIdentifier, storage, role);
JsonArray arr = new JsonArray();

for (String s : roleUsers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.google.gson.JsonObject;
import io.supertokens.Main;
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -81,7 +83,10 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
}

try {
UserRoles.deletePermissionsFromRole(this.enforcePublicTenantAndGetPublicTenantStorage(req), role, permissions);
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage storage = enforcePublicTenantAndGetPublicTenantStorage(req);

UserRoles.deletePermissionsFromRole(appIdentifier, storage, role, permissions);
JsonObject response = new JsonObject();
response.addProperty("status", "OK");
super.sendJsonResponse(200, response, resp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.google.gson.JsonObject;
import io.supertokens.Main;
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -58,7 +60,10 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
}

try {
boolean didRoleExist = UserRoles.deleteRole(this.enforcePublicTenantAndGetPublicTenantStorage(req), role);
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage storage = enforcePublicTenantAndGetPublicTenantStorage(req);

boolean didRoleExist = UserRoles.deleteRole(appIdentifier, storage, role);

JsonObject response = new JsonObject();
response.addProperty("status", "OK");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.google.gson.JsonObject;
import io.supertokens.Main;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -60,9 +62,10 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
}

try {
boolean didUserHaveRole = UserRoles.removeUserRole(
this.getTenantStorage(req).withStorage(this.getPublicTenantStorage(req).getStorage()),
userId, role);
TenantIdentifier tenantIdentifier = getTenantIdentifier(req);
Storage storage = getTenantStorage(req);

boolean didUserHaveRole = UserRoles.removeUserRole(tenantIdentifier, storage, userId, role);

JsonObject response = new JsonObject();
response.addProperty("status", "OK");
Expand Down

0 comments on commit 4843083

Please sign in to comment.