// CheckDefaultGroup check default group and creates it if fixDefaultGroup is true func CheckDefaultGroup(user *tat.User, fixDefaultGroup bool) string { defaultGroupInfo := "" userGroups, err := group.GetUserGroupsOnlyName(user.Username) if err != nil { return "Error while fetching user groups" } find := false for _, g := range userGroups { if g == viper.GetString("default_group") { find = true defaultGroupInfo = fmt.Sprintf("user in %s OK", viper.GetString("default_group")) break } } if !find { if fixDefaultGroup { if err = AddDefaultGroup(user); err != nil { return err.Error() } defaultGroupInfo = fmt.Sprintf("user added in default group %s", viper.GetString("default_group")) } else { defaultGroupInfo = fmt.Sprintf("user in default group %s KO", viper.GetString("default_group")) } } return defaultGroupInfo }
// Me retrieves all information about me (exception information about Authentication) func (*UsersController) Me(ctx *gin.Context) { var user = tat.User{} found, err := userDB.FindByUsername(&user, getCtxUsername(ctx)) if !found { ctx.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) return } else if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching user"}) return } gs, errGetGroupsOnlyName := groupDB.GetUserGroupsOnlyName(user.Username) if errGetGroupsOnlyName != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while getting groups"}) return } user.Groups = gs out := &tat.UserJSON{User: user} ctx.JSON(http.StatusOK, out) }
// ListUsers returns users list selected by criteria func ListUsers(criteria *tat.UserCriteria, isAdmin bool) (int, []tat.User, error) { var users []tat.User cursor, errl := listUsersCursor(criteria, isAdmin) if errl != nil { return -1, users, errl } count, err := cursor.Count() if err != nil { return -1, users, fmt.Errorf("Error while count Users %s", err) } sortBy := criteria.SortBy if sortBy == "" { sortBy = "-dateCreation" } err = cursor.Select(getUserListField(isAdmin)). Sort(sortBy). Skip(criteria.Skip). Limit(criteria.Limit). All(&users) if err != nil { return -1, users, fmt.Errorf("Error while Find All Users %s", err) } // Admin could ask groups for all users. Not perf, but really rare if criteria.WithGroups && isAdmin { var usersWithGroups []tat.User for _, u := range users { gs, errGetGroupsOnlyName := group.GetUserGroupsOnlyName(u.Username) u.Groups = gs log.Infof("User %s, Groups%s", u.Username, u.Groups) if errGetGroupsOnlyName != nil { log.Errorf("Error while getting group for user %s, Error:%s", u.Username, errGetGroupsOnlyName) } usersWithGroups = append(usersWithGroups, u) } return count, usersWithGroups, nil } return count, users, err }
func buildTopicCriteria(criteria *tat.TopicCriteria, user *tat.User) (bson.M, error) { var query = []bson.M{} if criteria.IDTopic != "" { queryIDTopics := bson.M{} queryIDTopics["$or"] = []bson.M{} for _, val := range strings.Split(criteria.IDTopic, ",") { queryIDTopics["$or"] = append(queryIDTopics["$or"].([]bson.M), bson.M{"_id": val}) } query = append(query, queryIDTopics) } if criteria.Topic != "" || criteria.OnlyFavorites == tat.True { queryTopics := bson.M{} queryTopics["$or"] = []bson.M{} for _, val := range strings.Split(criteria.Topic, ",") { queryTopics["$or"] = append(queryTopics["$or"].([]bson.M), bson.M{"topic": val}) } query = append(query, queryTopics) } if criteria.TopicPath != "" { query = append(query, bson.M{"topic": bson.RegEx{Pattern: "^" + regexp.QuoteMeta(criteria.TopicPath) + ".*$", Options: "im"}}) } if criteria.Description != "" { queryDescriptions := bson.M{} queryDescriptions["$or"] = []bson.M{} for _, val := range strings.Split(criteria.Description, ",") { queryDescriptions["$or"] = append(queryDescriptions["$or"].([]bson.M), bson.M{"description": val}) } query = append(query, queryDescriptions) } if criteria.Group != "" { queryGroups := bson.M{} queryGroups["$or"] = []bson.M{} queryGroups["$or"] = append(queryGroups["$or"].([]bson.M), bson.M{"adminGroups": bson.M{"$in": strings.Split(criteria.Group, ",")}}) queryGroups["$or"] = append(queryGroups["$or"].([]bson.M), bson.M{"roGroups": bson.M{"$in": strings.Split(criteria.Group, ",")}}) queryGroups["$or"] = append(queryGroups["$or"].([]bson.M), bson.M{"rwGroups": bson.M{"$in": strings.Split(criteria.Group, ",")}}) query = append(query, queryGroups) } var bsonDate = bson.M{} if criteria.DateMinCreation != "" { i, err := strconv.ParseInt(criteria.DateMinCreation, 10, 64) if err != nil { return bson.M{}, fmt.Errorf("Error while parsing dateMinCreation %s", err) } tm := time.Unix(i, 0) bsonDate["$gte"] = tm.Unix() } if criteria.DateMaxCreation != "" { i, err := strconv.ParseInt(criteria.DateMaxCreation, 10, 64) if err != nil { return bson.M{}, fmt.Errorf("Error while parsing dateMaxCreation %s", err) } tm := time.Unix(i, 0) bsonDate["$lte"] = tm.Unix() } if len(bsonDate) > 0 { query = append(query, bson.M{"dateCreation": bsonDate}) } if user != nil { if criteria.GetForAllTasksTopics { query = append(query, bson.M{ "topic": bson.RegEx{Pattern: "^\\/Private\\/.*/Tasks", Options: "i"}, }) } else if criteria.GetForTatAdmin == tat.True && user.IsAdmin { // requester is tat Admin and wants all topics, except /Private/* topics query = append(query, bson.M{ "topic": bson.M{"$not": bson.RegEx{Pattern: "^\\/Private\\/.*", Options: "i"}}, }) } else if criteria.GetForTatAdmin == tat.True && !user.IsAdmin { log.Warnf("User %s (not a TatAdmin) try to list all topics as an admin", user.Username) } else { bsonUser := []bson.M{} bsonUser = append(bsonUser, bson.M{"roUsers": bson.M{"$in": [1]string{user.Username}}}) bsonUser = append(bsonUser, bson.M{"rwUsers": bson.M{"$in": [1]string{user.Username}}}) bsonUser = append(bsonUser, bson.M{"adminUsers": bson.M{"$in": [1]string{user.Username}}}) userGroups, err := group.GetUserGroupsOnlyName(user.Username) if err != nil { log.Errorf("Error with getting groups for user %s", err) } else { bsonUser = append(bsonUser, bson.M{"roGroups": bson.M{"$in": userGroups}}) bsonUser = append(bsonUser, bson.M{"rwGroups": bson.M{"$in": userGroups}}) bsonUser = append(bsonUser, bson.M{"adminGroups": bson.M{"$in": userGroups}}) } query = append(query, bson.M{"$or": bsonUser}) } } if len(query) > 0 { return bson.M{"$and": query}, nil } else if len(query) == 1 { return query[0], nil } return bson.M{}, nil }