use of org.activityinfo.shared.auth.AuthenticatedUser in project activityinfo by bedatadriven.
the class AuthenticationFilter method doFilter.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
authProvider.clear();
String authToken = ((HttpServletRequest) request).getHeader("Authorization");
if (Strings.isNullOrEmpty(authToken)) {
authToken = authTokenFromCookie();
}
if (authToken != null) {
try {
AuthenticatedUser currentUser = authTokenCache.get(authToken);
authProvider.set(currentUser);
LocaleProxy.setLocale(LocaleHelper.getLocaleObject(currentUser));
LOGGER.info("Setting locale to " + currentUser.getUserLocale());
} catch (Exception e) {
authProvider.clear();
}
}
filterChain.doFilter(request, response);
}
use of org.activityinfo.shared.auth.AuthenticatedUser in project activityinfo by bedatadriven.
the class AdminLevelResource method update.
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public Response update(@InjectParam AuthenticatedUser user, UpdatedAdminLevel updatedLevel) throws ParseException {
assertAuthorized(user);
EntityManager em = entityManager.get();
em.getTransaction().begin();
AdminLevel level = entityManager.get().merge(this.level);
level.setName(updatedLevel.getName());
for (LocationType boundLocationType : level.getBoundLocationTypes()) {
boundLocationType.setName(updatedLevel.getName());
}
if (updatedLevel.getEntities() != null) {
for (UpdatedAdminEntity updatedEntity : updatedLevel.getEntities()) {
// check geometry
if (updatedEntity.getGeometry() != null && !isValid(updatedEntity.getGeometry())) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("Geometry must be Polygon or MultiPolygon").build());
}
if (updatedEntity.isDeleted()) {
// mark the entity as deleted. we can't remove it from
// the database because we may have locations which refer to it
// on distant clients
em.find(AdminEntity.class, updatedEntity.getId()).setDeleted(true);
} else if (updatedEntity.isNew()) {
// create new entity
AdminEntity entity = new AdminEntity();
entity.setLevel(level);
if (updatedEntity.getParentId() != null) {
entity.setParent(em.getReference(AdminEntity.class, updatedEntity.getParentId()));
}
entity.setName(updatedEntity.getName());
entity.setCode(updatedEntity.getCode());
entity.setBounds(updatedEntity.getBounds());
entity.setGeometry(updatedEntity.getGeometry());
em.persist(entity);
} else {
// update existing entity
// TODO: bound locations that share this name?
AdminEntity entity = em.find(AdminEntity.class, updatedEntity.getId());
entity.setName(updatedEntity.getName());
entity.setCode(updatedEntity.getCode());
entity.setBounds(updatedEntity.getBounds());
entity.setGeometry(updatedEntity.getGeometry());
}
}
}
int newVersion = level.getVersion() + 1;
level.setVersion(newVersion);
AdminLevelVersion version = new AdminLevelVersion();
version.setLevel(level);
version.setVersion(newVersion);
version.setUser(em.getReference(User.class, user.getId()));
version.setTimeCreated(new Date().getTime());
VersionMetadata metadata = updatedLevel.getVersionMetadata();
if (metadata != null) {
version.setSourceUrl(metadata.getSourceUrl());
version.setSourceFilename(metadata.getSourceFilename());
version.setSourceHash(metadata.getSourceMD5());
version.setMessage(metadata.getMessage());
version.setSourceMetadata(metadata.getSourceMetadata());
}
em.persist(version);
em.getTransaction().commit();
return Response.ok().build();
}
use of org.activityinfo.shared.auth.AuthenticatedUser in project activityinfo by bedatadriven.
the class HostControllerTest method verifyThatRequestWithValidAuthTokensReceiveTheView.
@Test
public void verifyThatRequestWithValidAuthTokensReceiveTheView() throws Exception {
authProvider.set(new AuthenticatedUser(VALID_TOKEN, 3, "akbertram@gmail.com"));
HttpServletRequest req = createMock(HttpServletRequest.class);
expect(req.getServerName()).andReturn("www.activityinfo.org");
expect(req.getHeader("User-Agent")).andReturn(CHROME_USER_AGENT);
replay(req);
Response response = resource.getHostPage(RestMockUtils.mockUriInfo("http://www.activityinfo.org"), req, false);
assertThat(response.getEntity(), instanceOf(Viewable.class));
assertThat(((Viewable) response.getEntity()).getModel(), instanceOf(HostPageModel.class));
}
Aggregations