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();
}
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;
}
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());
}
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());
}
}
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);
}
}
}
Aggregations