Exemplo n.º 1
0
func (*GroupsController) preCheckUser(ctx *gin.Context, paramJSON *paramUserJSON) (models.Group, error) {
	usernameExists := models.IsUsernameExists(paramJSON.Username)
	group := models.Group{}
	if !usernameExists {
		e := errors.New("username " + paramJSON.Username + " does not exist")
		ctx.AbortWithError(http.StatusInternalServerError, e)
		return group, e
	}
	errfinding := group.FindByName(paramJSON.Groupname)
	if errfinding != nil {
		ctx.AbortWithError(http.StatusInternalServerError, errfinding)
		return group, errfinding
	}

	if utils.IsTatAdmin(ctx) { // if Tat admin, ok
		return group, nil
	}

	user, err := PreCheckUser(ctx)
	if err != nil {
		return models.Group{}, err
	}

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

	return group, nil
}
Exemplo n.º 2
0
// Create creates a new group
func (*GroupsController) Create(ctx *gin.Context) {
	var groupJSON groupJSON
	ctx.Bind(&groupJSON)

	var groupIn models.Group
	groupIn.Name = groupJSON.Name
	groupIn.Description = groupJSON.Description

	err := groupIn.Insert()
	if err != nil {
		log.Errorf("Error while InsertGroup %s", err)
		ctx.AbortWithError(http.StatusInternalServerError, err)
		return
	}
	ctx.JSON(http.StatusCreated, groupIn)
}