Ejemplo n.º 1
0
func (c App) LoginPost(email, password string) revel.Result {
	var found, valid bool

	c.Validation.Required(email).Message("Email required")
	c.Validation.Required(password).Message("Password required")

	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(routes.App.Login())
	}

	// check for user in basic table
	UB := user.GetUserBasicByName(c.Txn, email)
	if UB != nil {
		found = true
	} else {
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(routes.App.Login())
	}

	// check for user in auth table
	P := user.UserPass{UB.UserId, email, password}
	U, err := auth.Authenticate(c.Txn, &P)
	if err != nil || U == nil {
		revel.WARN.Println(err)
	} else {
		valid = true
	}

	if found && valid {
		c.Flash.Out["heading"] = "LOGIN PASS"
		c.Flash.Out["message"] = "Login successful for " + email

		c.Session["user"] = UB.UserName
		c.RenderArgs["user_basic"] = UB

		return c.Redirect(routes.User.Result())

	} else {
		c.Flash.Out["heading"] = "LOGIN FAIL"
		c.Flash.Out["message"] = "Login failed for " + email

		c.Validation.Keep()
		c.FlashParams()
		c.Redirect(routes.App.Login())
	}
	return c.Redirect(routes.App.Result())
}
Ejemplo n.º 2
0
func (c App) LoginPost(userlogin *models.UserLogin) revel.Result {
	userlogin.Validate(c.Validation)
	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(routes.App.Login())
	}

	var found, valid bool

	// check for user in basic table
	UB, err := user.GetUserBasicByName(c.Txn, userlogin.Email)
	checkERROR(err)
	if UB != nil {
		found = true
	} else {
		c.Flash.Error("unknown user")
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(routes.App.Login())
	}

	// check for user in auth table
	passed, err := auth.Authenticate(c.Txn, UB.UserId, userlogin.Password)
	checkERROR(err)
	if !passed {
		c.Flash.Error("bad password")
	} else {
		valid = true
	}

	if found && valid {
		c.Session["user"] = UB.UserName
		c.RenderArgs["user_basic"] = UB

		// update visitor info in DB with UserId
		c.updateVisitorWithUserIdPanic()

		delete(c.Session, "v")
		delete(c.RenderArgs, "visitor")
		return c.Redirect(routes.User.Result())

	} else {
		c.Validation.Keep()
		c.FlashParams()
		return c.Redirect(routes.App.Login())
	}
}
Ejemplo n.º 3
0
func testDevDB() {
	for _, up := range dev_users {
		u := user.GetUserBasicByUserId(TestDB, up.UserId)
		if u == nil {
			revel.ERROR.Println("Failed to look up user by id:", up.UserId)
		}
		u = user.GetUserBasicByName(TestDB, up.UserName)
		if u == nil {
			revel.ERROR.Println("Failed to look up user by name:", up.UserName)
		}

		a, err := auth.Authenticate(TestDB, up)
		checkERROR(err)
		if a == nil {
			revel.ERROR.Printf("Failed to authenticate user: %+v\n", *up)
		}
	}
}
Ejemplo n.º 4
0
func testUserDB() {
	for _, up := range dev_users {

		u, err := user.GetUserBasicById(TestDB, up.UserId)
		checkERROR(err)
		if u == nil {
			revel.ERROR.Println("Failed to look up user by id:", up.UserId)
		}

		u, err = user.GetUserBasicByName(TestDB, up.UserName)
		checkERROR(err)
		if u == nil {
			revel.ERROR.Println("Failed to look up user by name:", up.UserName)
		}

		passed, err := auth.Authenticate(TestDB, up.UserId, up.Password)
		checkERROR(err)
		if !passed {
			revel.ERROR.Printf("Failed to authenticate user: %+v\n", *up)
		}
	}
}
Ejemplo n.º 5
0
func (c App) addNewUser(email, password string) (*user.UserBasic, error) {
	// check for user in basic table
	UB := user.GetUserBasicByName(c.Txn, email)
	if UB != nil {
		c.Flash.Out["message"] = "Email: " + email + " already used"
		c.Validation.Keep()
		c.FlashParams()
		return nil, errors.New("UserBasic already in use")
	}

	// uuid := get random number (that isn't used already)
	uuid := int64(1001)
	UB = &user.UserBasic{
		UserId:   uuid,
		UserName: email,
	}

	// check for user in auth table
	UP := &user.UserPass{UB.UserId, email, password}
	UA, err := auth.Authenticate(c.Txn, UP)
	if UA != nil {
		c.Flash.Out["message"] = "Authentication Error"
		c.Validation.Keep()
		c.FlashParams()
		return nil, errors.New("UserAuth already in use")
	}

	// add user to tables

	err = user.AddUserBasic(TestDB, UB)
	checkERROR(err)

	_, err = auth.AddUserAuth(TestDB, UP)
	checkERROR(err)

	return UB, nil
}