|
49 | 49 | import io.supertokens.pluginInterface.thirdparty.exception.DuplicateThirdPartyUserException; |
50 | 50 | import io.supertokens.pluginInterface.thirdparty.sqlStorage.ThirdPartySQLStorage; |
51 | 51 | import io.supertokens.pluginInterface.usermetadata.sqlStorage.UserMetadataSQLStorage; |
| 52 | +import io.supertokens.pluginInterface.userroles.exception.DuplicateUserRoleMappingException; |
| 53 | +import io.supertokens.pluginInterface.userroles.exception.UnknownRoleException; |
| 54 | +import io.supertokens.pluginInterface.userroles.sqlStorage.UserRolesSQLStorage; |
52 | 55 | import io.supertokens.storage.postgresql.config.Config; |
53 | 56 | import io.supertokens.storage.postgresql.config.PostgreSQLConfig; |
54 | 57 | import io.supertokens.storage.postgresql.output.Logging; |
|
67 | 70 | import java.util.List; |
68 | 71 |
|
69 | 72 | public class Start implements SessionSQLStorage, EmailPasswordSQLStorage, EmailVerificationSQLStorage, |
70 | | - ThirdPartySQLStorage, JWTRecipeSQLStorage, PasswordlessSQLStorage, UserMetadataSQLStorage { |
| 73 | + ThirdPartySQLStorage, JWTRecipeSQLStorage, PasswordlessSQLStorage, UserMetadataSQLStorage, UserRolesSQLStorage { |
71 | 74 |
|
72 | 75 | private static final Object appenderLock = new Object(); |
73 | 76 | public static boolean silent = false; |
@@ -179,6 +182,7 @@ public <T> T startTransaction(TransactionLogic<T> logic, TransactionIsolationLev |
179 | 182 | if (exceptionMessage == null) { |
180 | 183 | exceptionMessage = ""; |
181 | 184 | } |
| 185 | + |
182 | 186 | // see: https://github.com/supertokens/supertokens-postgresql-plugin/pull/3 |
183 | 187 |
|
184 | 188 | // We set this variable to the current (or cause) exception casted to |
@@ -1442,4 +1446,175 @@ public int deleteUserMetadata(String userId) throws StorageQueryException { |
1442 | 1446 | throw new StorageQueryException(e); |
1443 | 1447 | } |
1444 | 1448 | } |
| 1449 | + |
| 1450 | + @Override |
| 1451 | + public void addRoleToUser(String userId, String role) |
| 1452 | + throws StorageQueryException, UnknownRoleException, DuplicateUserRoleMappingException { |
| 1453 | + |
| 1454 | + try { |
| 1455 | + UserRolesQueries.addRoleToUser(this, userId, role); |
| 1456 | + } catch (SQLException e) { |
| 1457 | + if (e instanceof PSQLException) { |
| 1458 | + PostgreSQLConfig config = Config.getConfig(this); |
| 1459 | + ServerErrorMessage serverErrorMessage = ((PSQLException) e).getServerErrorMessage(); |
| 1460 | + if (isForeignKeyConstraintError(serverErrorMessage, config.getUserRolesTable(), "role")) { |
| 1461 | + throw new UnknownRoleException(); |
| 1462 | + } |
| 1463 | + if (isPrimaryKeyError(serverErrorMessage, config.getUserRolesTable())) { |
| 1464 | + throw new DuplicateUserRoleMappingException(); |
| 1465 | + } |
| 1466 | + } |
| 1467 | + throw new StorageQueryException(e); |
| 1468 | + } |
| 1469 | + |
| 1470 | + } |
| 1471 | + |
| 1472 | + @Override |
| 1473 | + public String[] getRolesForUser(String userId) throws StorageQueryException { |
| 1474 | + try { |
| 1475 | + return UserRolesQueries.getRolesForUser(this, userId); |
| 1476 | + } catch (SQLException e) { |
| 1477 | + throw new StorageQueryException(e); |
| 1478 | + } |
| 1479 | + } |
| 1480 | + |
| 1481 | + @Override |
| 1482 | + public String[] getUsersForRole(String role) throws StorageQueryException { |
| 1483 | + try { |
| 1484 | + return UserRolesQueries.getUsersForRole(this, role); |
| 1485 | + } catch (SQLException e) { |
| 1486 | + throw new StorageQueryException(e); |
| 1487 | + } |
| 1488 | + } |
| 1489 | + |
| 1490 | + @Override |
| 1491 | + public String[] getPermissionsForRole(String role) throws StorageQueryException { |
| 1492 | + try { |
| 1493 | + return UserRolesQueries.getPermissionsForRole(this, role); |
| 1494 | + } catch (SQLException e) { |
| 1495 | + throw new StorageQueryException(e); |
| 1496 | + } |
| 1497 | + } |
| 1498 | + |
| 1499 | + @Override |
| 1500 | + public String[] getRolesThatHavePermission(String permission) throws StorageQueryException { |
| 1501 | + try { |
| 1502 | + return UserRolesQueries.getRolesThatHavePermission(this, permission); |
| 1503 | + } catch (SQLException e) { |
| 1504 | + throw new StorageQueryException(e); |
| 1505 | + } |
| 1506 | + } |
| 1507 | + |
| 1508 | + @Override |
| 1509 | + public boolean deleteRole(String role) throws StorageQueryException { |
| 1510 | + try { |
| 1511 | + return UserRolesQueries.deleteRole(this, role); |
| 1512 | + } catch (SQLException e) { |
| 1513 | + throw new StorageQueryException(e); |
| 1514 | + } |
| 1515 | + } |
| 1516 | + |
| 1517 | + @Override |
| 1518 | + public String[] getRoles() throws StorageQueryException { |
| 1519 | + try { |
| 1520 | + return UserRolesQueries.getRoles(this); |
| 1521 | + } catch (SQLException e) { |
| 1522 | + throw new StorageQueryException(e); |
| 1523 | + } |
| 1524 | + } |
| 1525 | + |
| 1526 | + @Override |
| 1527 | + public boolean doesRoleExist(String role) throws StorageQueryException { |
| 1528 | + try { |
| 1529 | + return UserRolesQueries.doesRoleExist(this, role); |
| 1530 | + } catch (SQLException e) { |
| 1531 | + throw new StorageQueryException(e); |
| 1532 | + } |
| 1533 | + } |
| 1534 | + |
| 1535 | + @Override |
| 1536 | + public int deleteAllRolesForUser(String userId) throws StorageQueryException { |
| 1537 | + try { |
| 1538 | + return UserRolesQueries.deleteAllRolesForUser(this, userId); |
| 1539 | + } catch (SQLException e) { |
| 1540 | + throw new StorageQueryException(e); |
| 1541 | + } |
| 1542 | + } |
| 1543 | + |
| 1544 | + @Override |
| 1545 | + public boolean deleteRoleForUser_Transaction(TransactionConnection con, String userId, String role) |
| 1546 | + throws StorageQueryException { |
| 1547 | + Connection sqlCon = (Connection) con.getConnection(); |
| 1548 | + |
| 1549 | + try { |
| 1550 | + return UserRolesQueries.deleteRoleForUser_Transaction(this, sqlCon, userId, role); |
| 1551 | + } catch (SQLException e) { |
| 1552 | + throw new StorageQueryException(e); |
| 1553 | + } |
| 1554 | + } |
| 1555 | + |
| 1556 | + @Override |
| 1557 | + public boolean createNewRoleOrDoNothingIfExists_Transaction(TransactionConnection con, String role) |
| 1558 | + throws StorageQueryException { |
| 1559 | + Connection sqlCon = (Connection) con.getConnection(); |
| 1560 | + |
| 1561 | + try { |
| 1562 | + return UserRolesQueries.createNewRoleOrDoNothingIfExists_Transaction(this, sqlCon, role); |
| 1563 | + } catch (SQLException e) { |
| 1564 | + throw new StorageQueryException(e); |
| 1565 | + } |
| 1566 | + } |
| 1567 | + |
| 1568 | + @Override |
| 1569 | + public void addPermissionToRoleOrDoNothingIfExists_Transaction(TransactionConnection con, String role, |
| 1570 | + String permission) throws StorageQueryException, UnknownRoleException { |
| 1571 | + Connection sqlCon = (Connection) con.getConnection(); |
| 1572 | + try { |
| 1573 | + UserRolesQueries.addPermissionToRoleOrDoNothingIfExists_Transaction(this, sqlCon, role, permission); |
| 1574 | + } catch (SQLException e) { |
| 1575 | + if (e instanceof PSQLException) { |
| 1576 | + PostgreSQLConfig config = Config.getConfig(this); |
| 1577 | + ServerErrorMessage serverErrorMessage = ((PSQLException) e).getServerErrorMessage(); |
| 1578 | + if (isForeignKeyConstraintError(serverErrorMessage, config.getUserRolesPermissionsTable(), "role")) { |
| 1579 | + throw new UnknownRoleException(); |
| 1580 | + } |
| 1581 | + } |
| 1582 | + |
| 1583 | + throw new StorageQueryException(e); |
| 1584 | + } |
| 1585 | + } |
| 1586 | + |
| 1587 | + @Override |
| 1588 | + public boolean deletePermissionForRole_Transaction(TransactionConnection con, String role, String permission) |
| 1589 | + throws StorageQueryException { |
| 1590 | + Connection sqlCon = (Connection) con.getConnection(); |
| 1591 | + try { |
| 1592 | + return UserRolesQueries.deletePermissionForRole_Transaction(this, sqlCon, role, permission); |
| 1593 | + } catch (SQLException e) { |
| 1594 | + throw new StorageQueryException(e); |
| 1595 | + } |
| 1596 | + } |
| 1597 | + |
| 1598 | + @Override |
| 1599 | + public int deleteAllPermissionsForRole_Transaction(TransactionConnection con, String role) |
| 1600 | + throws StorageQueryException { |
| 1601 | + |
| 1602 | + Connection sqlCon = (Connection) con.getConnection(); |
| 1603 | + try { |
| 1604 | + return UserRolesQueries.deleteAllPermissionsForRole_Transaction(this, sqlCon, role); |
| 1605 | + } catch (SQLException e) { |
| 1606 | + throw new StorageQueryException(e); |
| 1607 | + } |
| 1608 | + } |
| 1609 | + |
| 1610 | + @Override |
| 1611 | + public boolean doesRoleExist_Transaction(TransactionConnection con, String role) throws StorageQueryException { |
| 1612 | + Connection sqlCon = (Connection) con.getConnection(); |
| 1613 | + try { |
| 1614 | + return UserRolesQueries.doesRoleExist_transaction(this, sqlCon, role); |
| 1615 | + } catch (SQLException e) { |
| 1616 | + throw new StorageQueryException(e); |
| 1617 | + } |
| 1618 | + } |
| 1619 | + |
1445 | 1620 | } |
0 commit comments