@Override protected SecurityApiException generateAlreadyExistsException(final KombuchaModelDao entity, final InternalCallContext context) { return new SecurityApiException(ErrorCode.__UNKNOWN_ERROR_CODE); }
public void assertAuthorized(final Annotation annotation) throws AuthorizationException { if (!(annotation instanceof RequiresPermissions)) { return; } final RequiresPermissions requiresPermissions = (RequiresPermissions) annotation; try { securityApi.checkCurrentUserPermissions(ImmutableList.<Permission>copyOf(requiresPermissions.value()), requiresPermissions.logical(), context); } catch (SecurityApiException e) { if (e.getCause() != null && e.getCause() instanceof AuthorizationException) { throw (AuthorizationException) e.getCause(); } else if (e.getCause() != null) { throw new AuthorizationException(e.getCause()); } else { throw new AuthorizationException(e); } } } }
private void testInvalidPermissionScenario(final List<String> permissions) { try { securityApi.addRoleDefinition("failed", permissions, callContext); Assert.fail("Should fail permissions " + permissions + " were invalid"); } catch (SecurityApiException expected) { Assert.assertEquals(expected.getCode(), ErrorCode.SECURITY_INVALID_PERMISSIONS.getCode()); } }
throw new SecurityApiException(ErrorCode.SECURITY_INVALID_PERMISSIONS, curPerm); throw new SecurityApiException(ErrorCode.SECURITY_INVALID_PERMISSIONS, curPerm);
Assert.fail("test should throw SecurityApiException"); } catch (final SecurityApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.__UNKNOWN_ERROR_CODE.getCode());
public void assertAuthorized(final Annotation annotation) throws AuthorizationException { if (!(annotation instanceof RequiresPermissions)) { return; } final RequiresPermissions requiresPermissions = (RequiresPermissions) annotation; try { securityApi.checkCurrentUserPermissions(ImmutableList.<Permission>copyOf(requiresPermissions.value()), requiresPermissions.logical(), context); } catch (SecurityApiException e) { if (e.getCause() != null && e.getCause() instanceof AuthorizationException) { throw (AuthorizationException) e.getCause(); } else if (e.getCause() != null) { throw new AuthorizationException(e.getCause()); } else { throw new AuthorizationException(e); } } } }
@Override public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final DateTime updatedDate = clock.getUTCNow(); final UsersSqlDao usersSqlDao = handle.attach(UsersSqlDao.class); final UserModelDao userModelDao = usersSqlDao.getByUsername(username); if (userModelDao == null) { throw new SecurityApiException(ErrorCode.SECURITY_INVALID_USER, username); } usersSqlDao.invalidate(username, updatedDate.toDate(), updatedBy); return null; } });
private void testInvalidPermissionScenario(final List<String> permissions) { try { securityApi.addRoleDefinition("failed", permissions, callContext); Assert.fail("Should fail permissions " + permissions + " were invalid"); } catch (SecurityApiException expected) { Assert.assertEquals(expected.getCode(), ErrorCode.SECURITY_INVALID_PERMISSIONS.getCode()); } }
@Override public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final RolesPermissionsSqlDao rolesPermissionsSqlDao = handle.attach(RolesPermissionsSqlDao.class); final List<RolesPermissionsModelDao> existingRole = rolesPermissionsSqlDao.getByRoleName(role); if (!existingRole.isEmpty()) { throw new SecurityApiException(ErrorCode.SECURITY_ROLE_ALREADY_EXISTS, role); } for (final String permission : permissions) { rolesPermissionsSqlDao.create(new RolesPermissionsModelDao(role, permission, createdDate, createdBy)); } return null; } });
Assert.fail("test should throw SecurityApiException"); } catch (final SecurityApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.__UNKNOWN_ERROR_CODE.getCode());
@Override public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final DateTime updatedDate = clock.getUTCNow(); final UsersSqlDao usersSqlDao = handle.attach(UsersSqlDao.class); final UserModelDao userModelDao = usersSqlDao.getByUsername(username); if (userModelDao == null) { throw new SecurityApiException(ErrorCode.SECURITY_INVALID_USER, username); } usersSqlDao.updatePassword(username, hashedPasswordBase64, salt.toBase64(), updatedDate.toDate(), updatedBy); return null; } });
@Override public List<UserRolesModelDao> inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final UsersSqlDao usersSqlDao = handle.attach(UsersSqlDao.class); final UserModelDao userModelDao = usersSqlDao.getByUsername(username); if (userModelDao == null) { throw new SecurityApiException(ErrorCode.SECURITY_INVALID_USER, username); } final UserRolesSqlDao userRolesSqlDao = handle.attach(UserRolesSqlDao.class); return userRolesSqlDao.getByUsername(username); } });
@Override public void checkCurrentUserPermissions(final List<Permission> permissions, final Logical logical, final TenantContext context) throws SecurityApiException { final String[] permissionsString = Lists.<Permission, String>transform(permissions, Functions.toStringFunction()).toArray(new String[permissions.size()]); try { final Subject subject = SecurityUtils.getSubject(); if (permissionsString.length == 1) { subject.checkPermission(permissionsString[0]); } else if (Logical.AND.equals(logical)) { subject.checkPermissions(permissionsString); } else if (Logical.OR.equals(logical)) { boolean hasAtLeastOnePermission = false; for (final String permission : permissionsString) { if (subject.isPermitted(permission)) { hasAtLeastOnePermission = true; break; } } // Cause the exception if none match if (!hasAtLeastOnePermission) { subject.checkPermission(permissionsString[0]); } } } catch (final AuthorizationException e) { throw new SecurityApiException(e, ErrorCode.SECURITY_NOT_ENOUGH_PERMISSIONS); } }
final UserModelDao userModelDao = usersSqlDao.getByUsername(username); if (userModelDao == null) { throw new SecurityApiException(ErrorCode.SECURITY_INVALID_USER, username);
@Override public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final UserRolesSqlDao userRolesSqlDao = handle.attach(UserRolesSqlDao.class); for (final String role : roles) { userRolesSqlDao.create(new UserRolesModelDao(username, role, createdDate, createdBy)); } final UsersSqlDao usersSqlDao = handle.attach(UsersSqlDao.class); final UserModelDao userModelDao = usersSqlDao.getByUsername(username); if (userModelDao != null) { throw new SecurityApiException(ErrorCode.SECURITY_USER_ALREADY_EXISTS, username); } usersSqlDao.create(new UserModelDao(username, hashedPasswordBase64, salt.toBase64(), createdDate, createdBy)); return null; } });
@Override protected SecurityApiException generateAlreadyExistsException(final KombuchaModelDao entity, final InternalCallContext context) { return new SecurityApiException(ErrorCode.__UNKNOWN_ERROR_CODE); }
throw new SecurityApiException(ErrorCode.SECURITY_INVALID_PERMISSIONS, curPerm); throw new SecurityApiException(ErrorCode.SECURITY_INVALID_PERMISSIONS, curPerm);
@Override public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final DateTime updatedDate = clock.getUTCNow(); final UsersSqlDao usersSqlDao = handle.attach(UsersSqlDao.class); final UserModelDao userModelDao = usersSqlDao.getByUsername(username); if (userModelDao == null) { throw new SecurityApiException(ErrorCode.SECURITY_INVALID_USER, username); } usersSqlDao.invalidate(username, updatedDate.toDate(), updatedBy); return null; } });
@Override public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final RolesPermissionsSqlDao rolesPermissionsSqlDao = handle.attach(RolesPermissionsSqlDao.class); final List<RolesPermissionsModelDao> existingRole = rolesPermissionsSqlDao.getByRoleName(role); if (!existingRole.isEmpty()) { throw new SecurityApiException(ErrorCode.SECURITY_ROLE_ALREADY_EXISTS, role); } for (final String permission : permissions) { rolesPermissionsSqlDao.create(new RolesPermissionsModelDao(role, permission, createdDate, createdBy)); } return null; } });
@Override public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception { final DateTime updatedDate = clock.getUTCNow(); final UsersSqlDao usersSqlDao = handle.attach(UsersSqlDao.class); final UserModelDao userModelDao = usersSqlDao.getByUsername(username); if (userModelDao == null) { throw new SecurityApiException(ErrorCode.SECURITY_INVALID_USER, username); } usersSqlDao.updatePassword(username, hashedPasswordBase64, salt.toBase64(), updatedDate.toDate(), updatedBy); return null; } });