Exemplo n.º 1
0
func getUsers(c *bitmonster.Context) error {
	// Obtain the data from the context.
	data := struct {
		Groups []string `json:"groups"`
	}{}

	err := c.Decode(&data)
	// Skip if no context data is available.
	if err != nil && err != bitmonster.ErrNoContextData {
		return err
	}

	// Prepare the data.
	data.Groups = utils.RemoveDuplicateStrings(data.Groups)

	// Obtain the users.
	users, err := GetUsers(data.Groups...)
	if err != nil {
		return err
	}

	// Set the users as return data.
	c.Data(users)

	return nil
}
Exemplo n.º 2
0
func editUser(c *bitmonster.Context) error {
	// Obtain the data from the context.
	data := struct {
		ID string `json:"id"`

		// All these values replace the values of the user:
		Name    string   `json:"name"`
		Email   string   `json:"email"`
		Enabled bool     `json:"enabled"`
		Groups  []string `json:"groups"`
	}{}

	err := c.Decode(&data)
	if err != nil {
		return err
	}

	// Prepare the data.
	data.Groups = utils.RemoveDuplicateStrings(data.Groups)

	// Obtain the user.
	user, err := GetUser(data.ID)
	if err != nil {
		return err
	}

	// Update the user fields.
	user.Name = data.Name
	user.Email = data.Email
	user.Enabled = data.Enabled
	user.Groups = data.Groups

	// Update the user in the database.
	err = UpdateUser(user)
	if err != nil {
		return err
	}

	// Set the updated user as return data.
	c.Data(user)

	return nil
}
Exemplo n.º 3
0
func addUser(c *bitmonster.Context) error {
	// Obtain the data from the context.
	data := struct {
		Username string `json:"username"`
		Name     string `json:"name"`
		EMail    string `json:"email"`
		Password string `json:"password"`

		Enabled bool     `json:"enabled"`
		Groups  []string `json:"groups"`
	}{}

	err := c.Decode(&data)
	if err != nil {
		return err
	}

	// Prepare the data.
	data.Groups = utils.RemoveDuplicateStrings(data.Groups)

	// Create a new user value.
	// Fields are validated by the function.
	user, err := NewUser(data.Username, data.Name, data.EMail, data.Password)
	if err != nil {
		return err
	}

	// Set the additinal data fields.
	user.Enabled = data.Enabled
	user.Groups = data.Groups

	// Add the user to the database.
	err = AddUser(user)
	if err != nil {
		return err
	}

	// Set the newly created user as return data.
	c.Data(user)

	return nil
}