Search in sources :

Example 6 with AuthenticatedUser

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AuthenticatedUser(org.activityinfo.shared.auth.AuthenticatedUser) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 7 with AuthenticatedUser

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();
}
Also used : EntityManager(javax.persistence.EntityManager) UpdatedAdminEntity(org.activityinfo.server.endpoint.rest.model.UpdatedAdminEntity) UpdatedAdminEntity(org.activityinfo.server.endpoint.rest.model.UpdatedAdminEntity) NewAdminEntity(org.activityinfo.server.endpoint.rest.model.NewAdminEntity) AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) AuthenticatedUser(org.activityinfo.shared.auth.AuthenticatedUser) User(org.activityinfo.server.database.hibernate.entity.User) WebApplicationException(javax.ws.rs.WebApplicationException) AdminLevel(org.activityinfo.server.database.hibernate.entity.AdminLevel) NewAdminLevel(org.activityinfo.server.endpoint.rest.model.NewAdminLevel) UpdatedAdminLevel(org.activityinfo.server.endpoint.rest.model.UpdatedAdminLevel) VersionMetadata(org.activityinfo.server.endpoint.rest.model.VersionMetadata) AdminLevelVersion(org.activityinfo.server.database.hibernate.entity.AdminLevelVersion) LocationType(org.activityinfo.server.database.hibernate.entity.LocationType) Date(java.util.Date) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 8 with AuthenticatedUser

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));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) HostPageModel(org.activityinfo.server.login.model.HostPageModel) Viewable(com.sun.jersey.api.view.Viewable) AuthenticatedUser(org.activityinfo.shared.auth.AuthenticatedUser) Test(org.junit.Test)

Aggregations

AuthenticatedUser (org.activityinfo.shared.auth.AuthenticatedUser)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 User (org.activityinfo.server.database.hibernate.entity.User)2 Viewable (com.sun.jersey.api.view.Viewable)1 IOException (java.io.IOException)1 Date (java.util.Date)1 EntityManager (javax.persistence.EntityManager)1 ServletException (javax.servlet.ServletException)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1 MockEventBus (org.activityinfo.client.MockEventBus)1 ClientSideAuthProvider (org.activityinfo.client.authentication.ClientSideAuthProvider)1 CommandCache (org.activityinfo.client.dispatch.CommandCache)1 CachingDispatcher (org.activityinfo.client.dispatch.remote.cache.CachingDispatcher)1 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)1 AdminLevel (org.activityinfo.server.database.hibernate.entity.AdminLevel)1 AdminLevelVersion (org.activityinfo.server.database.hibernate.entity.AdminLevelVersion)1 Authentication (org.activityinfo.server.database.hibernate.entity.Authentication)1