Example #1
0
File: groups.go Project: ovh/tat
// Update a group
// only for Tat admin
func (g *GroupsController) Update(ctx *gin.Context) {
	var paramJSON groupUpdateJSON
	ctx.Bind(&paramJSON)

	group, err := GetParam(ctx, "group")
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Group in query"})
		return
	}

	groupToUpdate, err := groupDB.FindByName(group)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Group"})
		return
	}

	if paramJSON.Name != groupToUpdate.Name {
		groupnameExists := groupDB.IsGroupnameExists(paramJSON.Name)

		if groupnameExists {
			ctx.JSON(http.StatusBadRequest, gin.H{"error": "Group Name already exists"})
			return
		}
	}

	user, err := PreCheckUser(ctx)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Error while fetching user"})
		return
	}

	err = groupDB.Update(groupToUpdate, paramJSON.Name, paramJSON.Description, &user)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Error while update group"})
		return
	}

	if paramJSON.Name != groupToUpdate.Name {
		if err := topicDB.ChangeGroupnameOnTopics(groupToUpdate.Name, paramJSON.Name); err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{"error": "Error while update group on topics"})
			return
		}
	}

	ctx.JSON(http.StatusCreated, "")
}
Example #2
0
File: topics.go Project: ovh/tat
// preCheckGroup checks if group exists and is admin on topic
func (t *TopicsController) preCheckGroup(ctx *gin.Context, paramJSON *tat.ParamTopicGroupJSON) (*tat.Topic, error) {
	if groupExists := groupDB.IsGroupnameExists(paramJSON.Groupname); !groupExists {
		return nil, tat.NewError(http.StatusNotFound, "groupname %s does not exist", paramJSON.Groupname)
	}
	return t.preCheckUserAdminOnTopic(ctx, paramJSON.Topic)
}