Search in sources :

Example 6 with UserGroupRecord

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

the class UserGroupPermissionServiceTest method testUpdateUserGroupPermissionWhenGroupRecordIsPresentForGivenName.

@Test
public void testUpdateUserGroupPermissionWhenGroupRecordIsPresentForGivenName() {
    UserGroupPermission userGroupPermission = mockUserGroupPermissionWithNameAndRoleId("name", "roleId");
    Optional<UserGroupRecord> userGroupRecord = getUserGroup();
    Mockito.when(userGroupDao.getUserGroupByName("name")).thenReturn(userGroupRecord);
    userGroupPermissionService.updateUserGroupPermission("safeBoId", userGroupPermission, "user", OffsetDateTime.MAX);
    Mockito.verify(userGroupDao).updateUserGroupPermission(Mockito.any(UserGroupPermissionRecord.class));
}
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 7 with UserGroupRecord

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

the class UserGroupPermissionServiceTest method testGrantUserGroupPermissionWhenUserGroupRecordIsPresentForGivenName.

@Test
public void testGrantUserGroupPermissionWhenUserGroupRecordIsPresentForGivenName() {
    UserGroupPermission userGroupPermission = mockUserGroupPermissionWithNameAndRoleId("name", "roleId");
    Role role = Mockito.mock(Role.class);
    Mockito.when(roleService.getRoleById("roleId")).thenReturn(Optional.of(role));
    Optional<UserGroupRecord> userGroupRecord = getUserGroup();
    Mockito.when(userGroupDao.getUserGroupByName("name")).thenReturn(userGroupRecord);
    userGroupPermissionService.grantUserGroupPermission("safeBoxId", userGroupPermission, "user", OffsetDateTime.MAX);
    Mockito.verify(userGroupDao).createUserGroupPermission(Mockito.any(UserGroupPermissionRecord.class));
}
Also used : Role(com.nike.cerberus.domain.Role) UserGroupRecord(com.nike.cerberus.record.UserGroupRecord) UserGroupPermissionRecord(com.nike.cerberus.record.UserGroupPermissionRecord) UserGroupPermission(com.nike.cerberus.domain.UserGroupPermission) Test(org.junit.Test)

Example 8 with UserGroupRecord

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

the class UserGroupPermissionServiceTest method testUpdateUserGroupPermissionsWhenGroupRecordIsPresentForGivenName.

@Test
public void testUpdateUserGroupPermissionsWhenGroupRecordIsPresentForGivenName() {
    UserGroupPermission userGroupPermission = mockUserGroupPermissionWithNameAndRoleId("name", "roleId");
    Set<UserGroupPermission> userGroupPermissions = new HashSet<>();
    userGroupPermissions.add(userGroupPermission);
    Optional<UserGroupRecord> userGroupRecord = getUserGroup();
    Mockito.when(userGroupDao.getUserGroupByName("name")).thenReturn(userGroupRecord);
    userGroupPermissionService.updateUserGroupPermissions("safeBoId", userGroupPermissions, "user", OffsetDateTime.MAX);
    Mockito.verify(userGroupDao).updateUserGroupPermission(Mockito.any(UserGroupPermissionRecord.class));
}
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 9 with UserGroupRecord

use of com.nike.cerberus.record.UserGroupRecord 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 10 with UserGroupRecord

use of com.nike.cerberus.record.UserGroupRecord 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)

Aggregations

UserGroupRecord (com.nike.cerberus.record.UserGroupRecord)11 UserGroupPermission (com.nike.cerberus.domain.UserGroupPermission)8 UserGroupPermissionRecord (com.nike.cerberus.record.UserGroupPermissionRecord)8 Test (org.junit.Test)7 Role (com.nike.cerberus.domain.Role)3 Transactional (org.springframework.transaction.annotation.Transactional)2