Exemplo n.º 1
0
Arquivo: user.go Projeto: GoNuuts/gogs
func updateUser(e Engine, u *User) error {
	// Organization does not need e-mail.
	if !u.IsOrganization() {
		u.Email = strings.ToLower(u.Email)
		has, err := e.Where("id!=?", u.Id).And("type=?", u.Type).And("email=?", u.Email).Get(new(User))
		if err != nil {
			return err
		} else if has {
			return ErrEmailAlreadyUsed{u.Email}
		}

		if len(u.AvatarEmail) == 0 {
			u.AvatarEmail = u.Email
		}
		u.Avatar = avatar.HashEmail(u.AvatarEmail)
	}

	u.LowerName = strings.ToLower(u.Name)

	if len(u.Location) > 255 {
		u.Location = u.Location[:255]
	}
	if len(u.Website) > 255 {
		u.Website = u.Website[:255]
	}
	if len(u.Description) > 255 {
		u.Description = u.Description[:255]
	}

	u.FullName = base.Sanitizer.Sanitize(u.FullName)
	_, err := e.Id(u.Id).AllCols().Update(u)
	return err
}
Exemplo n.º 2
0
// UpdateUser updates user's information.
func UpdateUser(u *User) error {
	u.Email = strings.ToLower(u.Email)
	has, err := x.Where("id!=?", u.Id).And("type=?", u.Type).And("email=?", u.Email).Get(new(User))
	if err != nil {
		return err
	} else if has {
		return ErrEmailAlreadyUsed{u.Email}
	}

	u.LowerName = strings.ToLower(u.Name)

	if len(u.Location) > 255 {
		u.Location = u.Location[:255]
	}
	if len(u.Website) > 255 {
		u.Website = u.Website[:255]
	}
	if len(u.Description) > 255 {
		u.Description = u.Description[:255]
	}

	if u.AvatarEmail == "" {
		u.AvatarEmail = u.Email
	}
	u.Avatar = avatar.HashEmail(u.AvatarEmail)

	u.FullName = base.Sanitizer.Sanitize(u.FullName)
	_, err = x.Id(u.Id).AllCols().Update(u)
	return err
}
Exemplo n.º 3
0
func TestFetch(t *testing.T) {
	os.Mkdir(TMPDIR, 0755)
	defer os.RemoveAll(TMPDIR)

	hash := avatar.HashEmail("*****@*****.**")
	a := avatar.New(hash, TMPDIR)
	a.UpdateTimeout(time.Millisecond * 200)
}
Exemplo n.º 4
0
Arquivo: tool.go Projeto: noresjo/gogs
// AvatarLink returns avatar link by given e-mail.
func AvatarLink(email string) string {
	if setting.DisableGravatar || setting.OfflineMode {
		return setting.AppSubUrl + "/img/avatar_default.jpg"
	}

	gravatarHash := avatar.HashEmail(email)
	if setting.Service.EnableCacheAvatar {
		return setting.AppSubUrl + "/avatar/" + gravatarHash
	}
	return setting.GravatarSource + gravatarHash
}
Exemplo n.º 5
0
// CreateUser creates record of a new user.
func CreateUser(u *User) (err error) {
	if err = IsUsableName(u.Name); err != nil {
		return err
	}

	isExist, err := IsUserExist(0, u.Name)
	if err != nil {
		return err
	} else if isExist {
		return ErrUserAlreadyExist{u.Name}
	}

	isExist, err = IsEmailUsed(u.Email)
	if err != nil {
		return err
	} else if isExist {
		return ErrEmailAlreadyUsed{u.Email}
	}

	u.LowerName = strings.ToLower(u.Name)
	u.AvatarEmail = u.Email
	u.Avatar = avatar.HashEmail(u.AvatarEmail)
	u.Rands = GetUserSalt()
	u.Salt = GetUserSalt()
	u.EncodePasswd()

	sess := x.NewSession()
	defer sess.Close()
	if err = sess.Begin(); err != nil {
		return err
	}

	if _, err = sess.Insert(u); err != nil {
		sess.Rollback()
		return err
	} else if err = os.MkdirAll(UserPath(u.Name), os.ModePerm); err != nil {
		sess.Rollback()
		return err
	} else if err = sess.Commit(); err != nil {
		return err
	}

	// Auto-set admin for the first user.
	if CountUsers() == 1 {
		u.IsAdmin = true
		u.IsActive = true
		_, err = x.Id(u.Id).AllCols().Update(u)
	}
	return err
}
Exemplo n.º 6
0
// CreateUser creates record of a new user.
func CreateUser(u *User) (err error) {
	if err = IsUsableName(u.Name); err != nil {
		return err
	}

	isExist, err := IsUserExist(0, u.Name)
	if err != nil {
		return err
	} else if isExist {
		return ErrUserAlreadyExist{u.Name}
	}

	u.Email = strings.ToLower(u.Email)
	isExist, err = IsEmailUsed(u.Email)
	if err != nil {
		return err
	} else if isExist {
		return ErrEmailAlreadyUsed{u.Email}
	}

	u.LowerName = strings.ToLower(u.Name)
	u.AvatarEmail = u.Email
	u.Avatar = avatar.HashEmail(u.AvatarEmail)
	u.Rands = GetUserSalt()
	u.Salt = GetUserSalt()
	u.EncodePasswd()
	u.MaxRepoCreation = -1

	sess := x.NewSession()
	defer sess.Close()
	if err = sess.Begin(); err != nil {
		return err
	}

	if _, err = sess.Insert(u); err != nil {
		sess.Rollback()
		return err
	} else if err = os.MkdirAll(UserPath(u.Name), os.ModePerm); err != nil {
		sess.Rollback()
		return err
	}

	return sess.Commit()
}
Exemplo n.º 7
0
func TestFetchMany(t *testing.T) {
	os.Mkdir(TMPDIR, 0755)
	defer os.RemoveAll(TMPDIR)

	log.Println("start")
	var n = 5
	ch := make(chan bool, n)
	for i := 0; i < n; i++ {
		go func(i int) {
			hash := avatar.HashEmail(strconv.Itoa(i) + "*****@*****.**")
			a := avatar.New(hash, TMPDIR)
			a.Update()
			log.Println("finish", hash)
			ch <- true
		}(i)
	}
	for i := 0; i < n; i++ {
		<-ch
	}
	log.Println("end")
}