Search in sources :

Example 61 with User

use of org.mamute.model.User in project mamute by caelum.

the class DefaultAdminCreator method createDefaultAdmin.

public void createDefaultAdmin() {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    Long result = (Long) session.createQuery("select count(*) from User").uniqueResult();
    if (result == 0) {
        Random random = new Random(System.currentTimeMillis());
        String email = "moderator@mamuteqa.org";
        User newUser = new User(fromTrustedText("moderator"), email).asModerator();
        String number = Long.toString(random.nextLong());
        String password = Digester.md5(number);
        LoginMethod brutalLogin = LoginMethod.brutalLogin(newUser, email, password);
        newUser.add(brutalLogin);
        session.save(brutalLogin);
        session.save(newUser);
        LOG.info("=========================");
        LOG.info("New admin user created!");
        LOG.info("email: " + email);
        LOG.info("password: " + password);
        LOG.info("=========================");
    }
    session.getTransaction().commit();
}
Also used : User(org.mamute.model.User) LoginMethod(org.mamute.model.LoginMethod) Random(java.util.Random) Session(org.hibernate.Session)

Example 62 with User

use of org.mamute.model.User in project mamute by caelum.

the class ForgotPasswordController method validateTokenAndGetUser.

private User validateTokenAndGetUser(Long id, String token) {
    User user = users.loadByIdAndToken(id, token);
    if (user == null) {
        validator.add(messageFactory.build("error", "forgot_password.invalid_token"));
        validator.onErrorRedirectTo(this).forgotPasswordForm();
    }
    return user;
}
Also used : User(org.mamute.model.User)

Example 63 with User

use of org.mamute.model.User in project mamute by caelum.

the class ForgotPasswordController method changePassword.

@Post
public void changePassword(Long id, String token, String password, String passwordConfirmation) {
    User user = validateTokenAndGetUser(id, token);
    boolean passwordUpdated = user.updateForgottenPassword(password, passwordConfirmation);
    if (!passwordUpdated) {
        validator.add(messageFactory.build("error", "forgot_password.password_doesnt_match"));
        validator.onErrorRedirectTo(this).forgotPasswordForm();
    }
    user.touchForgotPasswordToken();
    users.save(user);
    result.include("mamuteMessages", Arrays.asList(messageFactory.build("confirmation", "forgot_password.password_changed")));
    linker.linkTo(ListController.class).home(null);
    result.forwardTo(AuthController.class).login(user.getEmail(), password, linker.get());
}
Also used : User(org.mamute.model.User) Post(br.com.caelum.vraptor.Post)

Example 64 with User

use of org.mamute.model.User in project mamute by caelum.

the class VotingMachine method register.

public void register(Votable votable, Vote current, Class<?> votableType) {
    User voter = current.getAuthor();
    User votableAuthor = votable.getAuthor();
    ReputationEventContext eventContext = votes.contextOf(votable);
    if (votable.getAuthor().getId().equals(voter.getId())) {
        throw new IllegalArgumentException("an author can't vote its own votable");
    }
    Vote previous = votes.previousVoteFor(votable.getId(), voter, votableType);
    boolean shouldCountKarma = voteChecker.shouldCountKarma(voter, votableAuthor, current);
    if (previous != null) {
        ReputationEvent receivedVote = new ReceivedVoteEvent(previous.getType(), votable, eventContext, shouldCountKarma).reputationEvent();
        votableAuthor.descreaseKarma(karmaCalculator.karmaFor(receivedVote));
        ReputationEvent votedAtSomething = new VotedAtSomethingEvent(previous, eventContext).reputationEvent();
        voter.descreaseKarma(karmaCalculator.karmaFor(votedAtSomething));
        reputationEvents.delete(receivedVote);
        reputationEvents.delete(votedAtSomething);
    }
    votable.substitute(previous, current);
    ReputationEvent receivedVote = new ReceivedVoteEvent(current.getType(), votable, eventContext, shouldCountKarma).reputationEvent();
    votableAuthor.increaseKarma(karmaCalculator.karmaFor(receivedVote));
    ReputationEvent votedAtSomething = new VotedAtSomethingEvent(current, eventContext).reputationEvent();
    voter.increaseKarma(karmaCalculator.karmaFor(votedAtSomething));
    reputationEvents.save(receivedVote);
    reputationEvents.save(votedAtSomething);
    if (votable.getVoteCount() <= -5) {
        votable.getQuestion().remove();
        retrieveDownvote.retrieveKarma(votable.getVotes());
    }
}
Also used : Vote(org.mamute.model.Vote) User(org.mamute.model.User) ReceivedVoteEvent(org.mamute.reputation.rules.ReceivedVoteEvent) VotedAtSomethingEvent(org.mamute.reputation.rules.VotedAtSomethingEvent) ReputationEventContext(org.mamute.model.ReputationEventContext) ReputationEvent(org.mamute.model.ReputationEvent)

Example 65 with User

use of org.mamute.model.User in project mamute by caelum.

the class NewsletterMailer method sendTo.

public void sendTo(ScrollableResults results, boolean isTestNewsletter) {
    DateTime pastWeek = new DateTime().minusWeeks(1);
    DateTime twelveHoursAgo = new DateTime().minusHours(12);
    List<News> hotNews = news.hotNews();
    List<Question> hotQuestions = questions.hot(pastWeek, 8);
    List<Question> unanswered = questions.randomUnanswered(pastWeek, twelveHoursAgo, 8);
    LinkToHelper linkToHelper = new NotificationMailer.LinkToHelper(router, brutalEnv);
    String siteName = bundle.getMessage("site.name");
    String date = brutalDateFormat.getInstance("date.joda.newsletter.pattern").print(new DateTime());
    String teste = isTestNewsletter ? bundle.getMessage("newsletter_mail_test") : "";
    while (results.next()) {
        User user = (User) results.get()[0];
        try {
            Email email = templates.template("newsletter_mail", date, siteName, teste).with("hotNews", hotNews).with("hotQuestions", hotQuestions).with("unansweredQuestions", unanswered).with("unsubscribeLink", linkToHelper.unsubscribeLink(user)).with("linkToHelper", linkToHelper).with("l10n", bundle).with("sanitizer", POLICY).with("siteName", siteName).with("date", date).with("logoUrl", env.get("mail_logo_url")).to(user.getName(), user.getEmail());
            email.setCharset("utf-8");
            mailer.send(email);
        } catch (Exception e) {
            LOG.error("could not send email", e);
        }
    }
}
Also used : User(org.mamute.model.User) Email(org.apache.commons.mail.Email) News(org.mamute.model.News) Question(org.mamute.model.Question) LinkToHelper(org.mamute.notification.NotificationMailer.LinkToHelper) DateTime(org.joda.time.DateTime)

Aggregations

User (org.mamute.model.User)97 Test (org.junit.Test)56 Question (org.mamute.model.Question)28 LoggedUser (org.mamute.model.LoggedUser)25 VRaptorTestResult (br.com.caelum.vraptor.test.VRaptorTestResult)9 UserFlow (br.com.caelum.vraptor.test.requestflow.UserFlow)9 Answer (org.mamute.model.Answer)9 LoginMethod (org.mamute.model.LoginMethod)9 Post (br.com.caelum.vraptor.Post)7 Before (org.junit.Before)7 Tag (org.mamute.model.Tag)7 QuestionInformation (org.mamute.model.QuestionInformation)6 Email (org.apache.commons.mail.Email)5 UserPersonalInfo (org.mamute.dto.UserPersonalInfo)5 ReputationEvent (org.mamute.model.ReputationEvent)5 ArrayList (java.util.ArrayList)4 Watcher (org.mamute.model.watch.Watcher)4 DateTime (org.joda.time.DateTime)3 Elements (org.jsoup.select.Elements)3 DaoManager (org.mamute.integration.util.DaoManager)3