use of org.springframework.security.access.AccessDeniedException in project motech by motech.
the class UserControllerTest method shouldReturnEmptyListForUsersWithoutAccess.
@Test
public void shouldReturnEmptyListForUsersWithoutAccess() throws Exception {
when(motechUserService.getUsers()).thenThrow(new AccessDeniedException("denied"));
controller.perform(get("/users")).andExpect(status().isOk()).andExpect(content().string("[]"));
}
use of org.springframework.security.access.AccessDeniedException in project motech by motech.
the class EntityServiceImpl method getEntityDraft.
@Override
@Transactional
public EntityDraft getEntityDraft(Long entityId, String username) {
Entity entity = allEntities.retrieveById(entityId);
assertEntityExists(entity, entityId);
if (entity instanceof EntityDraft) {
return (EntityDraft) entity;
}
if (username == null) {
throw new AccessDeniedException("Cannot save draft - no user");
}
// get the draft
EntityDraft draft = allEntityDrafts.retrieve(entity, username);
if (draft == null) {
draft = allEntityDrafts.create(entity, username);
}
return draft;
}
use of org.springframework.security.access.AccessDeniedException in project irida by phac-nml.
the class AnnouncementServiceImplIT method testUserMarkAnnouncementAsReadSuccess.
@Test
@WithMockUser(username = "user3", roles = "USER")
public void testUserMarkAnnouncementAsReadSuccess() {
final Announcement a = announcementService.read(2L);
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
final User user = userService.getUserByUsername(auth.getName());
try {
announcementService.markAnnouncementAsReadByUser(a, user);
} catch (AccessDeniedException e) {
fail("User should be able able to mark announcement as read.");
} catch (EntityExistsException e) {
fail("Failed for unknown reason, stack trace follows:");
e.printStackTrace();
}
}
use of org.springframework.security.access.AccessDeniedException in project ma-core-public by infiniteautomation.
the class UrlSecurityFilter method doFilter.
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// Assume an http request.
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
boolean foundMapping = false;
User user = Common.getHttpUser();
String msg;
String uri = request.getRequestURI();
for (UriMappingDefinition uriDef : ModuleRegistry.getDefinitions(UriMappingDefinition.class)) {
if (matcher.match(uriDef.getPath(), uri)) {
boolean allowed = true;
foundMapping = true;
switch(uriDef.getPermission()) {
case ADMINISTRATOR:
if ((user == null) || (!Permissions.hasAdmin(user)))
allowed = false;
break;
case DATA_SOURCE:
if ((user == null) || (!user.isDataSourcePermission()))
allowed = false;
break;
case USER:
if (user == null) {
allowed = false;
}
break;
case CUSTOM:
try {
allowed = uriDef.hasCustomPermission(user);
} catch (PermissionException e) {
allowed = false;
}
break;
case ANONYMOUS:
break;
}
if (!allowed) {
if (user == null) {
msg = "Denying access to page where user isn't logged in, uri=" + uri + ", remote host ip= " + request.getRemoteHost();
} else {
msg = "Denying access to page where user hasn't sufficient permission, user=" + user.getUsername() + ", uri=" + uri + ", remote host ip= " + request.getRemoteHost();
}
LOG.warn(msg);
throw new AccessDeniedException(msg);
}
break;
}
}
// if not set then check our other definitions
if (!foundMapping) {
for (ControllerMappingDefinition uriDef : ModuleRegistry.getDefinitions(ControllerMappingDefinition.class)) {
if (matcher.match(uriDef.getPath(), uri)) {
boolean allowed = true;
foundMapping = true;
switch(uriDef.getPermission()) {
case ADMINISTRATOR:
if ((user == null) || (!Permissions.hasAdmin(user)))
allowed = false;
break;
case DATA_SOURCE:
if ((user == null) || (!user.isDataSourcePermission()))
allowed = false;
break;
case USER:
if (user == null) {
allowed = false;
}
break;
case CUSTOM:
try {
allowed = uriDef.hasCustomPermission(user);
} catch (PermissionException e) {
allowed = false;
}
break;
case ANONYMOUS:
break;
}
if (!allowed) {
if (user == null) {
msg = "Denying access to page where user isn't logged in, uri=" + uri + ", remote host ip= " + request.getRemoteHost();
} else {
msg = "Denying access to page where user hasn't sufficient permission, user=" + user.getUsername() + ", uri=" + uri + ", remote host ip= " + request.getRemoteHost();
}
LOG.info(msg);
throw new AccessDeniedException(msg);
}
break;
}
}
}
// if not set then check our other definitions
if (!foundMapping) {
for (UrlMappingDefinition uriDef : ModuleRegistry.getDefinitions(UrlMappingDefinition.class)) {
if (matcher.match(uriDef.getUrlPath(), uri)) {
boolean allowed = true;
foundMapping = true;
switch(uriDef.getPermission()) {
case ADMINISTRATOR:
if ((user == null) || (!Permissions.hasAdmin(user)))
allowed = false;
break;
case DATA_SOURCE:
if ((user == null) || (!user.isDataSourcePermission()))
allowed = false;
break;
case USER:
if (user == null) {
allowed = false;
}
break;
case ANONYMOUS:
break;
}
if (!allowed) {
if (user == null) {
msg = "Denying access to page where user isn't logged in, uri=" + uri + ", remote host ip= " + request.getRemoteHost();
} else {
msg = "Denying access to page where user hasn't sufficient permission, user=" + user.getUsername() + ", uri=" + uri + ", remote host ip= " + request.getRemoteHost();
}
LOG.info(msg);
throw new AccessDeniedException(msg);
}
break;
}
}
}
filterChain.doFilter(servletRequest, servletResponse);
}
use of org.springframework.security.access.AccessDeniedException in project cloudbreak by hortonworks.
the class AutoScaleClusterCommonService method setCluster.
private AutoscaleClusterResponse setCluster(PeriscopeUser user, AutoscaleClusterRequest json, Long clusterId) {
Ambari ambari = ambariConverter.convert(json);
Long stackId = json.getStackId();
boolean access = clusterSecurityService.hasAccess(user, ambari, stackId);
if (!access) {
String host = ambari.getHost();
LOGGER.info("Illegal access to Ambari cluster '{}' from user '{}'", host, user.getEmail());
throw new AccessDeniedException(String.format("Accessing Ambari cluster '%s' is not allowed", host));
} else {
Cluster cluster = clusterRequestConverter.convert(json);
if (!hasAmbariConnectionDetailsSpecified(json)) {
AmbariStack ambariStack = new AmbariStack(ambari, stackId, null);
cluster = clusterService.create(cluster, user, ambariStack, PENDING);
} else {
AmbariStack resolvedAmbari = clusterSecurityService.tryResolve(ambari);
cluster = clusterId == null ? clusterService.create(cluster, user, resolvedAmbari, RUNNING) : clusterService.update(clusterId, resolvedAmbari, cluster.isAutoscalingEnabled());
}
createHistoryAndNotification(cluster);
return createClusterJsonResponse(cluster);
}
}
Aggregations