Ejemplo n.º 1
0
func (c User) PostCreate(user *models.User, password models.Password) revel.Result {
	if user.CanBeCreatedBy(c.MongoSession, c.ActiveUser) {
		if exists := models.GetUserByEmail(c.MongoSession, user.Email); exists.Email == user.Email {
			msg := fmt.Sprint("Account with ", user.Email, " already exists.")
			c.Validation.Required(user.Email != exists.Email).
				Message(msg)
		} else {
			user.Id = bson.NewObjectId()
		}

		user.Validate(c.Validation)
		user.ValidatePassword(c.Validation, password)

		if c.Validation.HasErrors() {
			c.Validation.Keep()
			c.FlashParams()
			c.Flash.Error("Please correct the errors below.")
			return c.Redirect(User.GetCreate)
		}

		user.Save(c.MongoSession, password)

		c.Session["user"] = user.Email
		c.Flash.Success("Welcome, " + user.String())
		return c.Redirect(Application.Index)
	} else {
		return c.Forbidden("You are not allowed to create user accounts.")
	}
}
Ejemplo n.º 2
0
// Responsible for doing any necessary setup for each web request.
func (c *Application) Setup() revel.Result {
	// If there is an active user session load the User data for this user.
	if email, ok := c.Session["user"]; ok {
		c.ActiveUser = models.GetUserByEmail(c.MongoSession, email)
		c.RenderArgs["ActiveUser"] = c.ActiveUser
	}

	dummyContent()
	return nil
}
Ejemplo n.º 3
0
func (c User) PostLogin(Email, Password string) revel.Result {
	user := models.GetUserByEmail(c.MongoSession, Email)

	if user.Email != "" {
		err := bcrypt.CompareHashAndPassword(user.HashedPassword, []byte(Password))
		if err == nil {
			c.Session["user"] = Email
			c.Flash.Success("Welcome, " + user.String())
			return c.Redirect(Application.Index)
		}
	}

	c.Flash.Out["mail"] = Email
	c.Flash.Error("Incorrect email address or password.")
	return c.Redirect(User.GetLogin)
}