Example #1
0
func ExampleNew() {
	// Provided by the user, usually at registration.
	plain := "alberto"
	// p contains the encoded password, which can
	// be stored in the database.
	p := password.New(plain)
	// This prints the encoded password.
	fmt.Println(p)
	// This will print the same as the previous line
	// but its type will be string. It might be useful
	// for some storage drivers that expect values of
	// type string.
	fmt.Println(p.String())
}
Example #2
0
// Create creates a new user user with the given parameters, previously checking that
// the given username and email aren't already in use. Note that the user is only created,
// no implicit sign in is performed.
func Create(ctx *app.Context, username string, email string, pw string) (app.User, error) {
	if err := validateNewUsername(ctx, username); err != nil {
		return nil, err
	}
	addr, err := validateNewEmail(ctx, email)
	if err != nil {
		return nil, err
	}
	user, userIface := newEmptyUser(ctx)
	setUserValue(user, "Username", username)
	setUserValue(user, "NormalizedUsername", Normalize(username))
	setUserValue(user, "Email", addr)
	setUserValue(user, "NormalizedEmail", Normalize(addr))
	setUserValue(user, "Password", password.New(pw))
	setUserValue(user, "Created", time.Now().UTC())
	if _, err = ctx.Orm().Insert(userIface); err != nil {
		return nil, err
	}
	return userIface.(app.User), nil
}
Example #3
0
func registerUser(ctx *app.Context) {
	username := ctx.RequireIndexValue(0)
	userVal, _ := newEmptyUser(ctx)
	updating := false
	if ctx.Orm().MustOne(ByUsername(username), userVal.Interface()) {
		// Updating existing user
		updating = true
	} else {
		// Creating a new one
		userVal = newUser(ctx, username)
	}
	var askPassword bool
	ctx.ParseParamValue("p", &askPassword)
	if !updating || askPassword {
		password1, err := speakeasy.Ask("Password:"******"Confirm Password:"******"passwords don't match"))
		}
		setUserValue(userVal, "Password", password.New(password1))
	}
	var admin bool
	ctx.ParseParamValue("s", &admin)
	setUserValue(userVal, "Admin", admin)

	var email string
	ctx.ParseParamValue("e", &email)
	if email != "" {
		setUserValue(userVal, "Email", email)
	}

	ctx.Orm().MustSave(userVal.Interface())
	ctx.Logger().Infof("saved user as %+v", userVal.Interface())
}
Example #4
0
func saveNewUser(ctx *app.Context, user reflect.Value) {
	setUserValue(user, "Password", password.New(string(getUserValue(user, "Password").(password.Password))))
	setUserValue(user, "Created", time.Now().UTC())
	ctx.Orm().MustInsert(user.Interface())
	ctx.MustSignIn(asGondolaUser(user))
}