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 }
func (t *TopicsController) preCheckUserAdminOnTopic(ctx *gin.Context, topicName string) (models.Topic, error) { topic := models.Topic{} errfinding := topic.FindByTopic(topicName, true) if errfinding != nil { e := errors.New(errfinding.Error()) ctx.AbortWithError(http.StatusInternalServerError, e) return topic, e } if utils.IsTatAdmin(ctx) { // if Tat admin, ok return topic, nil } user, err := PreCheckUser(ctx) if err != nil { return models.Topic{}, err } if !topic.IsUserAdmin(&user) { e := fmt.Errorf("user %s is not admin on topic %s", user.Username, topic.Topic) ctx.AbortWithError(http.StatusForbidden, e) return models.Topic{}, e } return topic, nil }
// CheckAdmin is a middleware, abort request if user is not admin func CheckAdmin() gin.HandlerFunc { return func(ctx *gin.Context) { if !utils.IsTatAdmin(ctx) { ctx.AbortWithError(http.StatusForbidden, errors.New("user is not admin")) } } }
// List list all users matching Criteria func (u *UsersController) List(ctx *gin.Context) { criteria := u.buildCriteria(ctx) count, users, err := models.ListUsers(criteria, utils.IsTatAdmin(ctx)) if err != nil { ctx.AbortWithError(http.StatusInternalServerError, err) return } out := &usersJSON{ Count: count, Users: users, } ctx.JSON(http.StatusOK, out) }
// List list groups with given criterias func (g *GroupsController) List(ctx *gin.Context) { var criteria models.GroupCriteria ctx.Bind(&criteria) count, groups, err := models.ListGroups(g.buildCriteria(ctx), utils.IsTatAdmin(ctx)) if err != nil { ctx.AbortWithError(http.StatusInternalServerError, err) return } out := &groupsJSON{ Count: count, Groups: groups, } ctx.JSON(http.StatusOK, out) }