Search in sources :

Example 1 with UserGroupPermissionRecord

use of com.nike.cerberus.record.UserGroupPermissionRecord in project cerberus by Nike-Inc.

the class UserGroupPermissionServiceTest method testGetUserGroupPermissionsForGivenSafeBoxIdWhenUserGroupPermissionRecordPresent.

@Test
public void testGetUserGroupPermissionsForGivenSafeBoxIdWhenUserGroupPermissionRecordPresent() {
    UserGroupPermissionRecord userGroupPermissionRecord = getUserGroupPermissionRecord();
    List<UserGroupPermissionRecord> userGroupPermissionRecords = new ArrayList<>();
    userGroupPermissionRecords.add(userGroupPermissionRecord);
    Mockito.when(userGroupDao.getUserGroupPermissions("safeBoxId")).thenReturn(userGroupPermissionRecords);
    Optional<UserGroupRecord> userGroupRecord = getUserGroup();
    Mockito.when(userGroupDao.getUserGroup("id")).thenReturn(userGroupRecord);
    Set<UserGroupPermission> userGroupPermissions = userGroupPermissionService.getUserGroupPermissions("safeBoxId");
    Assert.assertFalse(userGroupPermissions.isEmpty());
    Assert.assertEquals(1, userGroupPermissions.size());
}
Also used : UserGroupRecord(com.nike.cerberus.record.UserGroupRecord) UserGroupPermissionRecord(com.nike.cerberus.record.UserGroupPermissionRecord) UserGroupPermission(com.nike.cerberus.domain.UserGroupPermission) Test(org.junit.Test)

Example 2 with UserGroupPermissionRecord

use of com.nike.cerberus.record.UserGroupPermissionRecord in project cerberus by Nike-Inc.

the class UserGroupPermissionService method updateUserGroupPermission.

/**
 * Updates a user group permission.
 *
 * @param safeDepositBoxId The safe deposit box id
 * @param userGroupPermission The user group permission
 * @param user The user making the changes
 * @param dateTime The time of the changes
 */
@Transactional
public void updateUserGroupPermission(final String safeDepositBoxId, final UserGroupPermission userGroupPermission, final String user, final OffsetDateTime dateTime) {
    final Optional<UserGroupRecord> possibleUserGroupRecord = userGroupDao.getUserGroupByName(userGroupPermission.getName());
    if (possibleUserGroupRecord.isEmpty()) {
        String msg = "Unable to update permissions for user group name that doesn't exist.";
        throw ApiException.newBuilder().withApiErrors(CustomApiError.createCustomApiError(DefaultApiError.ENTITY_NOT_FOUND, msg)).withExceptionMessage(msg).build();
    }
    UserGroupPermissionRecord record = new UserGroupPermissionRecord();
    record.setSdboxId(safeDepositBoxId);
    record.setUserGroupId(possibleUserGroupRecord.get().getId());
    record.setRoleId(userGroupPermission.getRoleId());
    record.setLastUpdatedBy(user);
    record.setLastUpdatedTs(dateTime);
    userGroupDao.updateUserGroupPermission(record);
}
Also used : UserGroupRecord(com.nike.cerberus.record.UserGroupRecord) UserGroupPermissionRecord(com.nike.cerberus.record.UserGroupPermissionRecord) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with UserGroupPermissionRecord

use of com.nike.cerberus.record.UserGroupPermissionRecord in project cerberus by Nike-Inc.

the class UserGroupPermissionService method grantUserGroupPermission.

/**
 * Grants a user group permission.
 *
 * @param safeDepositBoxId The safe deposit box id
 * @param userGroupPermission The user group permission
 * @param user The user making the changes
 * @param dateTime The time of the changes
 */
@Transactional
public void grantUserGroupPermission(final String safeDepositBoxId, final UserGroupPermission userGroupPermission, final String user, final OffsetDateTime dateTime) {
    final Optional<UserGroupRecord> possibleUserGroupRecord = userGroupDao.getUserGroupByName(userGroupPermission.getName());
    final Optional<Role> role = roleService.getRoleById(userGroupPermission.getRoleId());
    if (role.isEmpty()) {
        throw ApiException.newBuilder().withApiErrors(DefaultApiError.USER_GROUP_ROLE_ID_INVALID).build();
    }
    String userGroupId;
    if (possibleUserGroupRecord.isPresent()) {
        userGroupId = possibleUserGroupRecord.get().getId();
    } else {
        userGroupId = uuidSupplier.get();
        UserGroupRecord userGroupRecord = new UserGroupRecord();
        userGroupRecord.setId(userGroupId);
        userGroupRecord.setName(userGroupPermission.getName());
        userGroupRecord.setCreatedBy(user);
        userGroupRecord.setLastUpdatedBy(user);
        userGroupRecord.setCreatedTs(dateTime);
        userGroupRecord.setLastUpdatedTs(dateTime);
        userGroupDao.createUserGroup(userGroupRecord);
    }
    UserGroupPermissionRecord permissionsRecord = new UserGroupPermissionRecord();
    permissionsRecord.setId(uuidSupplier.get());
    permissionsRecord.setUserGroupId(userGroupId);
    permissionsRecord.setRoleId(userGroupPermission.getRoleId());
    permissionsRecord.setSdboxId(safeDepositBoxId);
    permissionsRecord.setCreatedBy(user);
    permissionsRecord.setLastUpdatedBy(user);
    permissionsRecord.setCreatedTs(dateTime);
    permissionsRecord.setLastUpdatedTs(dateTime);
    userGroupDao.createUserGroupPermission(permissionsRecord);
}
Also used : UserGroupRecord(com.nike.cerberus.record.UserGroupRecord) Role(com.nike.cerberus.domain.Role) UserGroupPermissionRecord(com.nike.cerberus.record.UserGroupPermissionRecord) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with UserGroupPermissionRecord

use of com.nike.cerberus.record.UserGroupPermissionRecord in project cerberus by Nike-Inc.

the class UserGroupPermissionService method getUserGroupPermissions.

public Set<UserGroupPermission> getUserGroupPermissions(final String safeDepositBoxId) {
    final Set<UserGroupPermission> permissionsSet = Sets.newHashSet();
    final List<UserGroupPermissionRecord> permissionRecords = userGroupDao.getUserGroupPermissions(safeDepositBoxId);
    permissionRecords.forEach(r -> {
        final Optional<UserGroupRecord> userGroupRecord = userGroupDao.getUserGroup(r.getUserGroupId());
        if (userGroupRecord.isPresent()) {
            UserGroupPermission permission = new UserGroupPermission();
            permission.setId(r.getId());
            permission.setRoleId(r.getRoleId());
            permission.setName(userGroupRecord.get().getName());
            permission.setCreatedBy(r.getCreatedBy());
            permission.setCreatedTs(r.getCreatedTs());
            permission.setLastUpdatedBy(r.getLastUpdatedBy());
            permission.setLastUpdatedTs(r.getLastUpdatedTs());
            permissionsSet.add(permission);
        }
    });
    return permissionsSet;
}
Also used : UserGroupRecord(com.nike.cerberus.record.UserGroupRecord) UserGroupPermissionRecord(com.nike.cerberus.record.UserGroupPermissionRecord) UserGroupPermission(com.nike.cerberus.domain.UserGroupPermission)

Aggregations

UserGroupPermissionRecord (com.nike.cerberus.record.UserGroupPermissionRecord)4 UserGroupRecord (com.nike.cerberus.record.UserGroupRecord)4 UserGroupPermission (com.nike.cerberus.domain.UserGroupPermission)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Role (com.nike.cerberus.domain.Role)1 Test (org.junit.Test)1