Ejemplo n.º 1
0
func Example() {
	var config = uuid.StateSaverConfig{SaveReport: true, SaveSchedule: 30 * time.Minute}
	uuid.SetupFileSystemStateSaver(config)
	u1 := uuid.NewV1()
	fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1)

	uP, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
	u3 := uuid.NewV3(uP, uuid.Name("test"))

	u4 := uuid.NewV4()
	fmt.Printf("version %d variant %x: %s\n", u4.Version(), u4.Variant(), u4)

	u5 := uuid.NewV5(uuid.NamespaceURL, uuid.Name("test"))

	if uuid.Equal(u1, u3) {
		fmt.Printf("Will never happen")
	}

	fmt.Printf(uuid.Formatter(u5, uuid.CurlyHyphen))

	uuid.SwitchFormat(uuid.BracketHyphen)
}
Ejemplo n.º 2
0
func (c *UserController) Register() {
	// get
	c.setupView("user/register")
	if c.Ctx.Input.Method() == "POST" {

		flash := beego.NewFlash()
		valid := validation.Validation{}

		firstname := c.GetString("firstname")
		lastname := c.GetString("lastname")
		username := c.GetString("username")
		email := c.GetString("email")
		password := c.GetString("password")
		passwordConfirm := c.GetString("password_confirm")

		// password validation
		valid.Required(passwordConfirm, "password_confirm")
		r, err := regexp.Compile(strings.Join([]string{"^", password, "$"}, ""))
		if err != nil {
			fmt.Printf("There is a problem with your regexp.")
			return
		}
		valid.Match(passwordConfirm, r, "password_confirm")

		config := uuid.StateSaverConfig{SaveReport: true, SaveSchedule: 30 * time.Minute}
		uuid.SetupFileSystemStateSaver(config)
		u1 := uuid.NewV4()

		user := &models.AuthUser{
			Firstname: firstname,
			Lastname:  lastname,
			Username:  username,
			Email:     email,
			Password:  password,
			Reg_key:   u1.String(),
		}

		b, err := valid.Valid(user)
		if err != nil {
			fmt.Println(err)
		}
		if !b {
			errormap := []string{}
			for _, err := range valid.Errors {
				errormap = append(errormap, "Validation failed on "+err.Key+": "+err.Message+"\n")
			}
			flash.Error("Invalid data!")
			flash.Store(&c.Controller)
			c.Data["Errors"] = errormap
			fmt.Println(errormap)
			return
		}

		hash, hashErr := bcrypt.GenerateFromPassword([]byte(password), 1)
		if hashErr != nil {
			c.Abort("401")
		}
		user.Password = string(hash)

		o := orm.NewOrm()
		o.Using("default")

		if created, id, err := o.ReadOrCreate(user, "Username"); err == nil {
			if created {
				fmt.Println("New user registered. Id:", id)
				flash.Notice("Welcome, " + username + "!")
				flash.Store(&c.Controller)

				link := "http://*****:*****@mail.com", "Start Go")
				msg.SetHeader("To", email)
				msg.SetHeader("Subject", "Account Verification for Start Go")
				msg.SetBody("text/html", "To verify your account, please click on the link: <a href=\""+link+"\">"+link+"</a>")
				m := gomail.NewMailer(host, "youremail@mail", "yourpassword", port)

				if errMail := m.Send(msg); errMail != nil {
					fmt.Println("Email was not sent!")
					fmt.Println(errMail)
				}

				return
			} else {
				flash.Error("Invalid data!")
				flash.Store(&c.Controller)
				errormap := []string{}
				errormap = append(errormap, "User already exist")
				c.Data["Errors"] = errormap
				return
			}
		}
	}
}
Ejemplo n.º 3
0
func ExampleSetupFileSystemStateSaver() {
	var config = uuid.StateSaverConfig{SaveReport: true, SaveSchedule: 30 * time.Minute}
	uuid.SetupFileSystemStateSaver(config)
	u1 := uuid.NewV1()
	fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1)
}