use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel in project hub-alert by blackducksoftware.
the class UserActions method createWithoutChecks.
@Override
protected ActionResponse<UserConfig> createWithoutChecks(UserConfig resource) {
try {
String userName = resource.getUsername();
String password = resource.getPassword();
String emailAddress = resource.getEmailAddress();
logger.debug(actionMessageCreator.createStartMessage("user", userName));
UserModel userModel = userAccessor.addUser(userName, password, emailAddress);
Long userId = userModel.getId();
Set<String> configuredRoleNames = resource.getRoleNames();
if (null != configuredRoleNames && !configuredRoleNames.isEmpty()) {
Collection<UserRoleModel> roleNames = roleAccessor.getRoles().stream().filter(role -> configuredRoleNames.contains(role.getName())).collect(Collectors.toList());
authorizationManager.updateUserRoles(userId, roleNames);
}
userModel = userAccessor.getUser(userId).orElse(userModel);
logger.debug(actionMessageCreator.createSuccessMessage("User", userName));
return new ActionResponse<>(HttpStatus.CREATED, convertDatabaseModelToRestModel(userModel));
} catch (AlertException ex) {
logger.error(actionMessageCreator.createErrorMessage("user", resource.getUsername()));
return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, String.format("There was an issue creating the user. %s", ex.getMessage()));
}
}
use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel in project hub-alert by blackducksoftware.
the class RoleActions method createWithoutChecks.
@Override
protected ActionResponse<RolePermissionModel> createWithoutChecks(RolePermissionModel resource) {
String roleName = resource.getRoleName();
Set<PermissionModel> permissions = resource.getPermissions();
PermissionMatrixModel permissionMatrixModel = PermissionModelUtil.convertToPermissionMatrixModel(permissions);
logger.debug(actionMessageCreator.createStartMessage("role", roleName));
UserRoleModel userRoleModel = authorizationManager.createRoleWithPermissions(roleName, permissionMatrixModel);
logger.debug(actionMessageCreator.createSuccessMessage("Role", roleName));
return new ActionResponse<>(HttpStatus.OK, convertDatabaseModelToRestModel(userRoleModel));
}
use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel in project hub-alert by blackducksoftware.
the class DefaultRoleAccessor method getRoles.
@Override
@Transactional(readOnly = true)
public Set<UserRoleModel> getRoles() {
List<RoleEntity> roleList = roleRepository.findAll();
Set<UserRoleModel> userRoles = new LinkedHashSet<>();
for (RoleEntity entity : roleList) {
userRoles.add(new UserRoleModel(entity.getId(), entity.getRoleName(), entity.getCustom(), readPermissionsForRole(entity.getId())));
}
return userRoles;
}
use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel in project hub-alert by blackducksoftware.
the class DefaultRoleAccessorTest method updateUserRolesTest.
@Test
public void updateUserRolesTest() {
final Long userId = 1L;
final String roleName = "roleName";
final Long roleId = 1L;
RoleEntity roleEntity = new RoleEntity(roleName, true);
roleEntity.setId(1L);
UserRoleModel userRoleModel = createUserRoleModel(roleId, roleName, true);
Collection<UserRoleModel> userRoleModelCollection = List.of(userRoleModel);
Mockito.when(roleRepository.findRoleEntitiesByRoleNames(Mockito.any())).thenReturn(List.of(roleEntity));
DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
authorizationUtility.updateUserRoles(userId, userRoleModelCollection);
Mockito.verify(userRoleRepository).bulkDeleteAllByUserId(Mockito.any());
Mockito.verify(userRoleRepository).saveAll(Mockito.any());
}
use of com.synopsys.integration.alert.common.persistence.model.UserRoleModel in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method getUserByUserIdTest.
@Test
public void getUserByUserIdTest() {
final Long userId = 1L;
final Long emptyUserId = 5L;
final Long authenticationTypeId = 1L;
final String roleName = "userName";
UserEntity userEntity = new UserEntity(username, password, emailAddress, authenticationTypeId);
userEntity.setId(1L);
UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
Mockito.when(userRepository.findById(userId)).thenReturn(Optional.of(userEntity));
Mockito.when(userRepository.findById(emptyUserId)).thenReturn(Optional.empty());
createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
Optional<UserModel> userModelOptional = defaultUserAccessor.getUser(userId);
Optional<UserModel> userModelOptionalEmpty = defaultUserAccessor.getUser(emptyUserId);
assertTrue(userModelOptional.isPresent());
assertFalse(userModelOptionalEmpty.isPresent());
UserModel userModel = userModelOptional.get();
testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel);
}
Aggregations