Search in sources :

Example 16 with User

use of org.activityinfo.server.database.hibernate.entity.User in project activityinfo by bedatadriven.

the class SignUpAddressExistsController method resetPassword.

@POST
@Produces(MediaType.TEXT_HTML)
@Transactional
public Viewable resetPassword(@FormParam("email") String email) {
    try {
        User user = userDAO.get().findUserByEmail(email);
        user.setChangePasswordKey(SecureTokenGenerator.generate());
        user.setDateChangePasswordKeyIssued(new Date());
        mailer.send(new ResetPasswordMessage(user));
        return new SignUpAddressExistsPageModel(email).asEmailSent();
    } catch (NoResultException e) {
        return new SignUpAddressExistsPageModel().asLoginError();
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to send password reset email", e);
        return new SignUpAddressExistsPageModel().asEmailError();
    }
}
Also used : SignUpAddressExistsPageModel(org.activityinfo.server.login.model.SignUpAddressExistsPageModel) User(org.activityinfo.server.database.hibernate.entity.User) NoResultException(javax.persistence.NoResultException) Date(java.util.Date) NoResultException(javax.persistence.NoResultException) ResetPasswordMessage(org.activityinfo.server.mail.ResetPasswordMessage) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Transactional(org.activityinfo.server.database.hibernate.dao.Transactional)

Example 17 with User

use of org.activityinfo.server.database.hibernate.entity.User in project activityinfo by bedatadriven.

the class SignUpController method signUp.

@POST
@Produces(MediaType.TEXT_HTML)
@Transactional
public Response signUp(@FormParam("name") String name, @FormParam("organization") String organization, @FormParam("jobtitle") String jobtitle, @FormParam("email") String email, @FormParam("locale") String locale) {
    LOGGER.info("New user signing up! [name: " + name + ", email: " + email + ", locale: " + locale + ", organization: " + organization + ", job title: " + jobtitle + "]");
    if (!domainProvider.get().isSignUpAllowed()) {
        LOGGER.severe("Blocked attempt to signup via " + domainProvider.get().getHost());
        return Response.status(Status.FORBIDDEN).build();
    }
    // checking parameter values
    try {
        checkParam(name, true);
        checkParam(organization, false);
        checkParam(jobtitle, false);
        checkParam(email, true);
        checkParam(locale, true);
    } catch (IllegalArgumentException e) {
        LOGGER.log(Level.INFO, "User " + name + " (" + email + ") failed to sign up", e);
        return Response.ok(SignUpPageModel.formErrorModel().set(email, name, organization, jobtitle, locale).asViewable()).build();
    }
    try {
        // check duplicate email
        if (userDAO.get().doesUserExist(email)) {
            return Response.ok(new SignUpAddressExistsPageModel(email).asViewable()).type(MediaType.TEXT_HTML).build();
        }
        // persist new user
        User user = UserDAOImpl.createNewUser(email, name, organization, jobtitle, locale);
        userDAO.get().persist(user);
        // send confirmation email
        mailer.send(new SignUpConfirmationMessage(user));
        // return to page with positive result
        return Response.seeOther(new URI("/signUp/sent")).build();
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "User " + name + " (" + email + ") failed to sign up", e);
        entityManager.getTransaction().rollback();
        return Response.ok(SignUpPageModel.genericErrorModel().set(email, name, organization, jobtitle, locale).asViewable()).build();
    }
}
Also used : SignUpAddressExistsPageModel(org.activityinfo.server.login.model.SignUpAddressExistsPageModel) User(org.activityinfo.server.database.hibernate.entity.User) SignUpConfirmationMessage(org.activityinfo.server.mail.SignUpConfirmationMessage) URI(java.net.URI) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Transactional(org.activityinfo.server.database.hibernate.dao.Transactional)

Example 18 with User

use of org.activityinfo.server.database.hibernate.entity.User in project activityinfo by bedatadriven.

the class SiteHistoryProcessor method process.

@Timed(name = "updates.site_history")
public void process(Command<?> cmd, final int userId, final int siteId) {
    assert (cmd instanceof SiteCommand);
    LOGGER.fine("persisting site history (site: " + siteId + ", user: " + userId + ")");
    EntityManager em = entityManager.get();
    // It's important to use getOnlyReference() here rather
    // than find() becuase the site might not actually have
    // been sent to the database at this point
    Site site = em.getReference(Site.class, siteId);
    User user = em.getReference(User.class, userId);
    ChangeType type = ChangeType.getType(cmd);
    if (!type.isNew()) {
        Query q = em.createQuery("select count(*) from SiteHistory where site = :site");
        q.setParameter("site", site);
        Long count = (Long) q.getSingleResult();
        if (count == 0) {
            // update, but first entry -> repair history by adding baseline
            // record with complete site json
            LOGGER.fine("site is not new, but history was empty. Adding baseline record..");
            SiteResult siteResult = dispatcher.execute(GetSites.byId(siteId));
            SiteDTO siteDTO = siteResult.getData().get(0);
            String fulljson = JsonUtil.encodeMap(siteDTO.getProperties()).toString();
            SiteHistory baseline = new SiteHistory();
            baseline.setSite(site);
            baseline.setUser(user);
            baseline.setJson(fulljson);
            baseline.setTimeCreated(new Date().getTime());
            baseline.setInitial(false);
            persist(baseline);
        }
    }
    String json = null;
    if (type.isNewOrUpdate()) {
        Map<String, Object> changeMap = ((SiteCommand) cmd).getProperties().getTransientMap();
        if (!changeMap.isEmpty()) {
            json = JsonUtil.encodeMap(changeMap).toString();
        }
    } else if (type.isDelete()) {
        json = JSON_DELETE;
    }
    if (!Strings.isNullOrEmpty(json)) {
        persistHistory(site, user, type, json);
    }
}
Also used : Site(org.activityinfo.server.database.hibernate.entity.Site) User(org.activityinfo.server.database.hibernate.entity.User) Query(javax.persistence.Query) Date(java.util.Date) EntityManager(javax.persistence.EntityManager) SiteResult(org.activityinfo.legacy.shared.command.result.SiteResult) SiteCommand(org.activityinfo.legacy.shared.command.SiteCommand) SiteDTO(org.activityinfo.legacy.shared.model.SiteDTO) SiteHistory(org.activityinfo.server.database.hibernate.entity.SiteHistory) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 19 with User

use of org.activityinfo.server.database.hibernate.entity.User in project activityinfo by bedatadriven.

the class CommandTestCase method execute.

protected <T extends CommandResult> T execute(Command<T> command) throws CommandException {
    User user = em.find(User.class, AuthenticationModuleStub.getCurrentUser().getUserId());
    assert user != null : "cannot find user id " + AuthenticationModuleStub.getCurrentUser().getUserId() + " in the database, have you " + " called execute() without a @OnDataset annotation?";
    Locale.setDefault(Locale.ENGLISH);
    List<CommandResult> results = servlet.handleCommands(Collections.<Command>singletonList(command));
    // normally each request and so each handleCommand() gets its own
    // EntityManager, but here successive requests in the same test
    // will share an EntityManager, which can be bad if there are
    // collections
    // still living in the first-level cache
    // 
    // I think these command tests should ultimately become real end-to-end
    // tests and so would go through the actual servlet process, but for the
    // moment,
    // we'll just add this work aroudn that clears the cache after each
    // command.
    em.clear();
    CommandResult result = results.get(0);
    if (result instanceof CommandException) {
        throw (CommandException) result;
    }
    return (T) result;
}
Also used : User(org.activityinfo.server.database.hibernate.entity.User) CommandException(org.activityinfo.legacy.shared.exception.CommandException) CommandResult(org.activityinfo.legacy.shared.command.result.CommandResult)

Example 20 with User

use of org.activityinfo.server.database.hibernate.entity.User in project activityinfo by bedatadriven.

the class CommandTestCase2 method execute.

protected <T extends CommandResult> T execute(Command<T> command) throws CommandException {
    User user;
    if (AuthenticationModuleStub.getCurrentUser().getUserId() == 0) {
        user = new User();
        user.setName("Anonymous");
        user.setEmail("Anonymous@anonymous");
    } else {
        user = new User();
        user.setId(AuthenticationModuleStub.getCurrentUser().getUserId());
        user.setEmail("foo@foo.com");
        user.setName("Foo Name");
        user.setLocale("en");
    }
    ThreadLocalLocaleProvider.pushLocale(user.getLocaleObject());
    try {
        RemoteExecutionContext context = new RemoteExecutionContext(injector);
        T result = context.startExecute(command);
        // normally each request and so each handleCommand() gets its own
        // EntityManager, but here successive requests in the same test
        // will share an EntityManager, which can be bad if there are
        // collections
        // still living in the first-level cache
        // 
        // I think these command tests should ultimately become real end-to-end
        // tests and so would go through the actual servlet process, but for the
        // moment,
        // we'll just add this work aroudn that clears the cache after each
        // command.
        injector.getInstance(EntityManager.class).clear();
        return result;
    } finally {
        ThreadLocalLocaleProvider.popLocale();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) User(org.activityinfo.server.database.hibernate.entity.User) RemoteExecutionContext(org.activityinfo.server.endpoint.gwtrpc.RemoteExecutionContext)

Aggregations

User (org.activityinfo.server.database.hibernate.entity.User)51 Test (org.junit.Test)19 Date (java.util.Date)7 EntityManager (javax.persistence.EntityManager)7 NoResultException (javax.persistence.NoResultException)7 AuthenticatedUser (org.activityinfo.legacy.shared.AuthenticatedUser)6 Database (org.activityinfo.server.database.hibernate.entity.Database)6 IOException (java.io.IOException)5 ServletException (javax.servlet.ServletException)4 GET (javax.ws.rs.GET)3 Produces (javax.ws.rs.Produces)3 GetSyncRegionUpdates (org.activityinfo.legacy.shared.command.GetSyncRegionUpdates)3 SyncRegionUpdate (org.activityinfo.legacy.shared.command.result.SyncRegionUpdate)3 OnDataSet (org.activityinfo.server.database.OnDataSet)3 Transactional (org.activityinfo.server.database.hibernate.dao.Transactional)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 POST (javax.ws.rs.POST)2 Filter (org.activityinfo.legacy.shared.command.Filter)2 PivotSites (org.activityinfo.legacy.shared.command.PivotSites)2