func (lgc *Logic) AddUser(user *graylog.User) (int, error) {
	// client side validation
	if err := validator.CreateValidator.Struct(user); err != nil {
		return 400, err
	}

	// Check a given username has already used.
	ok, err := lgc.HasUser(user.Username)
	if err != nil {
		return 500, err
	}
	if ok {
		return 400, fmt.Errorf(
			`the user "%s" has already existed`, user.Username)
	}

	// check role exists
	if user.Roles != nil {
		if sc, err := lgc.checkUserRoles(user.Roles.ToList()); err != nil {
			return sc, err
		}
	}

	user.SetDefaultValues()
	user.Password = encryptPassword(user.Password)
	// Add a user
	if err := lgc.store.AddUser(user); err != nil {
		return 500, err
	}
	return 201, nil
}
// AddUser adds a user to the Server.
AddUser adds a user to the Server.
// AddUser adds a user to the Server .
func (lgc *Logic) AddUser(user *graylog.User) (int, error) {
	if err := validator.CreateValidator.Struct(user); err != nil {
		return 400, err
	}
	ok, err := lgc.HasUser(user.Username)
	if err != nil {
		return 500, err
	}
	if ok {
		return 400, fmt.Errorf(
			`the user "%s" has already existed`, user.Username)
	}
	if user.Roles != nil {
		if sc, err := lgc.checkUserRoles(user.Roles.ToList()); err != nil {
			return sc, err
		}
	}
	user.SetDefaultValues()
	user.Password = encryptPassword(user.Password)
	if err := lgc.store.AddUser(user); err != nil {
		return 500, err
	}
	return 201, nil
}
func ( lgc * Logic ) AddUser ( user * graylog . User ) ( int , error ) { if err := validator . CreateValidator . Struct ( user ) ; err != nil { return 400 , err } ok , err := lgc . HasUser ( user . Username ) if err != nil { return 500 , err } if ok { return 400 , fmt . Errorf ( `the user "%s" has already existed` , user . Username ) } if user . Roles != nil { if sc , err := lgc . checkUserRoles ( user . Roles . ToList ( ) ) ; err != nil { return sc , err } } user . SetDefaultValues ( ) user . Password = encryptPassword ( user . Password ) if err := lgc . store . AddUser ( user ) ; err != nil { return 500 , err } return 201 , nil }
AddUser adds a user to the Server.
AddUser adds a user to the Server .
// client side validation // Check a given username has already used. // check role exists // Add a user