Esempio n. 1
0
File: groups.go Progetto: ovh/tat
func (*GroupsController) preCheckUser(ctx *gin.Context, paramJSON *tat.ParamGroupUserJSON) (tat.Group, error) {
	user := tat.User{}
	found, err := userDB.FindByUsername(&user, paramJSON.Username)
	if err != nil {
		ctx.AbortWithError(http.StatusInternalServerError, err)
		return tat.Group{}, err
	}

	if !found {
		e := errors.New("username " + paramJSON.Username + " does not exist")
		ctx.AbortWithError(http.StatusInternalServerError, e)
		return tat.Group{}, e
	}

	group, errfinding := groupDB.FindByName(paramJSON.Groupname)
	if errfinding != nil {
		ctx.AbortWithError(http.StatusInternalServerError, errfinding)
		return tat.Group{}, errfinding
	}

	if isTatAdmin(ctx) { // if Tat admin, ok
		return *group, nil
	}

	if !groupDB.IsUserAdmin(group, getCtxUsername(ctx)) {
		e := fmt.Errorf("user %s is not admin on group %s", user.Username, group.Name)
		ctx.AbortWithError(http.StatusInternalServerError, e)
		return tat.Group{}, e
	}

	return *group, nil
}
Esempio n. 2
0
File: groups.go Progetto: 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, "")
}
Esempio n. 3
0
File: groups.go Progetto: ovh/tat
// Delete deletes requested group
// only for Tat admin
func (g *GroupsController) Delete(ctx *gin.Context) {
	groupName, err := GetParam(ctx, "group")
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Group in query"})
		return
	}

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

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

	c := tat.TopicCriteria{}
	c.Skip = 0
	c.Limit = 10
	c.Group = groupToDelete.Name

	count, topics, err := topicDB.ListTopics(&c, &user, false, false, false)
	if err != nil {
		log.Errorf("Error while getting topics associated to group %s:%s", groupToDelete.Name, err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Errorf("Error while getting topics associated to group")})
		return
	}

	if len(topics) > 0 {
		e := fmt.Sprintf("Group %s associated to %d topic, you can't delete it", groupToDelete.Name, count)
		log.Errorf(e)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Errorf(e)})
		return
	}

	if err = groupDB.Delete(groupToDelete, &user); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error while deleting Group: %s", err.Error())})
		return
	}
	ctx.JSON(http.StatusOK, "")
}
Esempio n. 4
0
File: user.go Progetto: ovh/tat
// AddDefaultGroup add default group to user
func AddDefaultGroup(user *tat.User) error {
	groupname := viper.GetString("default_group")

	// no default group
	if groupname == "" {
		return nil
	}

	tatGroup, errfinding := group.FindByName(groupname)
	if errfinding != nil {
		e := fmt.Errorf("Error while fetching default group : %s", errfinding.Error())
		return e
	}
	err := group.AddUser(tatGroup, "Tat", user.Username)
	if err != nil {
		e := fmt.Errorf("Error while adding user to default group : %s", err.Error())
		return e
	}
	return nil
}