コード例 #1
0
ファイル: users.go プロジェクト: ovh/tat
// Create a new user, record Username, Fullname and Email
// A mail is sent to ask user for validation
func (u *UsersController) Create(ctx *gin.Context) {
	var userJSON tat.UserCreateJSON
	ctx.Bind(&userJSON)
	var userIn tat.User
	userIn.Username = u.computeUsername(userJSON)
	userIn.Fullname = strings.TrimSpace(userJSON.Fullname)
	userIn.Email = strings.TrimSpace(userJSON.Email)
	callback := strings.TrimSpace(userJSON.Callback)

	if len(userIn.Username) < 3 || len(userIn.Fullname) < 3 || len(userIn.Email) < 7 {
		err := fmt.Errorf("Invalid username (%s) or fullname (%s) or email (%s)", userIn.Username, userIn.Fullname, userIn.Email)
		AbortWithReturnError(ctx, http.StatusInternalServerError, err)
		return
	}

	if err := u.checkAllowedDomains(userJSON); err != nil {
		ctx.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
		return
	}

	user := tat.User{}
	foundEmail, errEmail := userDB.FindByEmail(&user, userJSON.Email)
	foundUsername, errUsername := userDB.FindByUsername(&user, userJSON.Username)
	foundFullname, errFullname := userDB.FindByFullname(&user, userJSON.Fullname)

	if foundEmail || foundUsername || foundFullname || errEmail != nil || errUsername != nil || errFullname != nil {
		e := fmt.Errorf("Please check your username, email or fullname. If you are already registered, please reset your password")
		AbortWithReturnError(ctx, http.StatusBadRequest, e)
		return
	}

	tokenVerify, err := userDB.Insert(&userIn)
	if err != nil {
		log.Errorf("Error while InsertUser %s", err)
		ctx.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	go userDB.SendVerifyEmail(userIn.Username, userIn.Email, tokenVerify, callback)

	info := ""
	if viper.GetBool("username_from_email") {
		info = fmt.Sprintf(" Note that configuration of Tat forced your username to %s", userIn.Username)
	}
	ctx.JSON(http.StatusCreated, gin.H{"info": fmt.Sprintf("please check your mail to validate your account.%s", info)})
}
コード例 #2
0
ファイル: user.go プロジェクト: ovh/tat
// TrustUsername create user is not already registered
func TrustUsername(user *tat.User, username string) error {

	var userCheck = tat.User{}
	found, errCheck := FindByUsername(&userCheck, username)

	if errCheck != nil {
		return fmt.Errorf("Error with DB Backend: %s", errCheck)
	} else if errCheck == nil && !found {

		user.Username = username
		setEmailAndFullnameFromTrustedUsername(user)

		tokenVerify, err := Insert(user)
		if err != nil {
			return fmt.Errorf("TrustUsername, Error while Insert user %s : %s", username, err.Error())
		}

		// force default group and topics, even if it should be done in Verify
		CheckDefaultGroup(user, true)
		CheckTopics(user, true)

		if _, _, err = Verify(user, username, tokenVerify); err != nil {
			return fmt.Errorf("TrustUsername, Error while verify : %s", err.Error())
		}

		log.Infof("User %s created by TrustUsername", username)
	}

	// ok, user is checked, get all fields now
	//return FindByUsername(user, username)
	found, err := FindByUsername(user, username)
	if !found || err != nil {
		return fmt.Errorf("TrustUsername, Error while find username:%s err:%s", username, err.Error())
	}

	return nil
}
コード例 #3
0
ファイル: users.go プロジェクト: ovh/tat
// Reset send a mail asking user to confirm reset password
func (u *UsersController) Reset(ctx *gin.Context) {
	var userJSON userResetJSON
	ctx.Bind(&userJSON)
	var userIn tat.User
	userIn.Username = strings.TrimSpace(userJSON.Username)
	userIn.Email = strings.TrimSpace(userJSON.Email)
	callback := strings.TrimSpace(userJSON.Callback)

	if len(userIn.Username) < 3 || len(userIn.Email) < 7 {
		err := fmt.Errorf("Invalid username (%s) or email (%s)", userIn.Username, userIn.Email)
		AbortWithReturnError(ctx, http.StatusInternalServerError, err)
		return
	}

	tokenVerify, err := userDB.AskReset(&userIn)
	if err != nil {
		log.Errorf("Error while AskReset %s", err)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	go userDB.SendAskResetEmail(userIn.Username, userIn.Email, tokenVerify, callback)
	ctx.JSON(http.StatusCreated, gin.H{"info": "please check your mail to validate your account"})
}