Search in sources :

Example 6 with SecurityApiException

use of org.killbill.billing.security.SecurityApiException in project killbill by killbill.

the class DefaultUserDao method updateRoleDefinition.

@Override
public void updateRoleDefinition(final String role, final List<String> permissions, final String createdBy) throws SecurityApiException {
    final DateTime createdDate = clock.getUTCNow();
    inTransactionWithExceptionHandling(new TransactionCallback<Void>() {

        @Override
        public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception {
            final RolesPermissionsSqlDao rolesPermissionsSqlDao = handle.attach(RolesPermissionsSqlDao.class);
            final List<RolesPermissionsModelDao> existingPermissions = rolesPermissionsSqlDao.getByRoleName(role);
            // A empty list of permissions means we should remove all current permissions
            final Iterable<RolesPermissionsModelDao> toBeDeleted = existingPermissions.isEmpty() ? existingPermissions : Iterables.filter(existingPermissions, new Predicate<RolesPermissionsModelDao>() {

                @Override
                public boolean apply(final RolesPermissionsModelDao input) {
                    return !permissions.contains(input.getPermission());
                }
            });
            final Iterable<String> toBeAdded = Iterables.filter(permissions, new Predicate<String>() {

                @Override
                public boolean apply(final String input) {
                    for (RolesPermissionsModelDao e : existingPermissions) {
                        if (e.getPermission().equals(input)) {
                            return false;
                        }
                    }
                    return true;
                }
            });
            for (RolesPermissionsModelDao d : toBeDeleted) {
                rolesPermissionsSqlDao.unactiveEvent(d.getRecordId(), createdDate, createdBy);
            }
            for (final String permission : toBeAdded) {
                rolesPermissionsSqlDao.create(new RolesPermissionsModelDao(role, permission, createdDate, createdBy));
            }
            return null;
        }
    });
}
Also used : TransactionStatus(org.skife.jdbi.v2.TransactionStatus) DateTime(org.joda.time.DateTime) SecurityApiException(org.killbill.billing.security.SecurityApiException) Handle(org.skife.jdbi.v2.Handle) Predicate(com.google.common.base.Predicate) List(java.util.List)

Aggregations

SecurityApiException (org.killbill.billing.security.SecurityApiException)6 DateTime (org.joda.time.DateTime)4 Handle (org.skife.jdbi.v2.Handle)4 TransactionStatus (org.skife.jdbi.v2.TransactionStatus)4 List (java.util.List)2 SimpleHash (org.apache.shiro.crypto.hash.SimpleHash)2 ByteSource (org.apache.shiro.util.ByteSource)2 Function (com.google.common.base.Function)1 Predicate (com.google.common.base.Predicate)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Permission (org.killbill.billing.security.Permission)1 EntitySqlDaoTransactionalJdbiWrapper (org.killbill.billing.util.entity.dao.EntitySqlDaoTransactionalJdbiWrapper)1 Test (org.testng.annotations.Test)1