Exemple #1
0
 public static Result newTask() {
   Form<Task> filledForm = taskForm.bindFromRequest();
   if (filledForm.hasErrors()) {
     return badRequest(views.html.index.render(Task.all(), filledForm));
   } else {
     Task.create(filledForm.get());
     return redirect(routes.Application.tasks());
   }
 }
Exemple #2
0
 /** Handle login form submission. */
 public static Result authenticate() {
   Form<Login> filledLogin = form(Application.Login.class).bindFromRequest();
   if (filledLogin.hasErrors()) {
     return badRequest(login.render(filledLogin));
   } else {
     session("email", filledLogin.get().email);
     return redirect(routes.JobOffers.index());
   }
 }
Exemple #3
0
 public static Result newTask() {
   User user = loadUser();
   Form<Task> filledForm = taskForm.bindFromRequest();
   if (filledForm.hasErrors()) {
     flash("error", "The task could not be saved.");
     return badRequest(main.render("Tasks", null, tasks.render(user.tasks, filledForm)));
   } else {
     Task task = filledForm.get();
     task.owner = user;
     task.save();
     return created(main.render("Tasks", null, tasks.render(user.tasks, taskForm)));
   }
 }
Exemple #4
0
public class Application extends Controller {

  static Form<Task> taskForm = Form.form(Task.class);

  public static Result index() {
    return redirect(routes.Application.tasks());
  }

  public static Result tasks() {
    return ok(views.html.index.render(Task.all(), taskForm));
  }

  public static Result newTask() {
    Form<Task> filledForm = taskForm.bindFromRequest();
    if (filledForm.hasErrors()) {
      return badRequest(views.html.index.render(Task.all(), filledForm));
    } else {
      Task.create(filledForm.get());
      return redirect(routes.Application.tasks());
    }
  }

  public static Result deleteTask(Long id) {
    Task.delete(id);
    return redirect(routes.Application.tasks());
  }
}
Exemple #5
0
@With(DetectUser.class)
@Authenticated(UserAuthenticator.class)
public class TaskController extends Controller {

  static Form<Task> taskForm = Form.form(Task.class);

  public static User loadUser() {
    return User.fetch(session().get("username"));
  }

  public static Result tasks() {
    User user = loadUser();
    return ok(main.render("Tasks", null, tasks.render(user.tasks, taskForm)));
  }

  public static Result newTask() {
    User user = loadUser();
    Form<Task> filledForm = taskForm.bindFromRequest();
    if (filledForm.hasErrors()) {
      flash("error", "The task could not be saved.");
      return badRequest(main.render("Tasks", null, tasks.render(user.tasks, filledForm)));
    } else {
      Task task = filledForm.get();
      task.owner = user;
      task.save();
      return created(main.render("Tasks", null, tasks.render(user.tasks, taskForm)));
    }
  }

  public static Result deleteTask(Long id) {
    Task.delete(id);
    return redirect(routes.TaskController.tasks());
  }
}
  /**
   * Validates fields from the registration form and either creates a new user or communicates any
   * validation errors.
   */
  public static Result submit() {
    Form<User> filledForm = signupForm.bindFromRequest();

    // Check accept conditions
    if (!"true".equals(filledForm.field("accept").value())) {
      filledForm.reject("accept", "You must accept the terms and conditions");
    }

    // Check repeated password
    if (!filledForm.field("password").valueOr("").isEmpty()) {
      if (!filledForm
          .field("password")
          .valueOr("")
          .equals(filledForm.field("repeatPassword").value())) {
        filledForm.reject("repeatPassword", "Passwords do not match");
      }
    }

    // Check if the username and email are valid
    if (!filledForm.hasErrors()) {

      String un = filledForm.get().username;
      String email = filledForm.get().email;

      if (un.equals("admin") || un.equals("guest")) {
        filledForm.reject("username", "This username is already taken");
      }

      try {
        Logger.debug("Finding user " + email);
        User.findByEmail(email);
        filledForm.reject(
            "email", "There is already an account associated with this email address.");
      } catch (Exception e) {
        // continue - the user does not exist
      }
    }

    // Return validation results to user or save user
    if (filledForm.hasErrors()) {
      return badRequest(form.render(filledForm));
    } else {
      User user = filledForm.get(); /* create an object from a form */
      User svUser =
          new User(user.username, user.email, user.password); /* recreate to get save group info */
      svUser.save();
      return ok(summary.render(svUser));
    }
  }