// IsUserAdmin return true if user is Tat admin or is admin on this topic // Check personal access to topic, and group access func (topic *Topic) IsUserAdmin(user *User) bool { if user.IsAdmin { return true } if utils.ArrayContains(topic.AdminUsers, user.Username) { return true } userGroups, err := user.GetGroups() if err != nil { log.Errorf("Error while fetching user groups") return false } var groups []string for _, g := range userGroups { groups = append(groups, g.Name) } if utils.ItemInBothArrays(topic.AdminGroups, groups) { return true } // user is "Admin" on his /Private/usrname topics if strings.HasPrefix(topic.Topic, "/Private/"+user.Username) { return true } return false }
func initTree(messages []Message, criteria *MessageCriteria) ([]Message, error) { var ids []string idMessages := "" for i := 0; i <= len(messages)-1; i++ { if utils.ArrayContains(ids, messages[i].ID) { continue } ids = append(ids, messages[i].ID) idMessages += messages[i].ID + "," } if idMessages == "" { return messages, nil } c := &MessageCriteria{ AllIDMessage: idMessages[:len(idMessages)-1], NotLabel: criteria.NotLabel, NotTag: criteria.NotTag, } var msgs []Message err := Store().clMessages.Find(buildMessageCriteria(c)).Sort("-dateCreation").All(&msgs) if err != nil { log.Errorf("Error while Find Messages in getTree %s", err) return messages, err } return msgs, nil }
// List returns the list of topics that can be viewed by user func (t *TopicsController) List(ctx *gin.Context) { criteria := t.buildCriteria(ctx) var user = &models.User{} err := user.FindByUsername(utils.GetCtxUsername(ctx)) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching user."}) return } count, topics, err := models.ListTopics(criteria, user) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching topics."}) return } out := &topicsJSON{Topics: topics, Count: count} if criteria.GetNbMsgUnread == "true" { c := &models.PresenceCriteria{ Username: user.Username, } count, presences, err := models.ListPresencesAllFields(c) if err != nil { ctx.AbortWithError(http.StatusInternalServerError, err) return } unread := make(map[string]int) knownPresence := false for _, topic := range topics { if utils.ArrayContains(user.OffNotificationsTopics, topic.Topic) { continue } knownPresence = false for _, presence := range presences { if topic.Topic != presence.Topic { continue } knownPresence = true nb, err := models.CountMsgSinceDate(presence.Topic, presence.DatePresence) if err != nil { ctx.JSON(http.StatusInternalServerError, err) return } unread[presence.Topic] = nb } if !knownPresence { unread[topic.Topic] = -1 } } out.TopicsMsgUnread = unread out.CountTopicsMsgUnread = count } ctx.JSON(http.StatusOK, out) }
// IsUserReadAccess return true if user has read access to topic func (topic *Topic) IsUserReadAccess(user User) bool { currentTopic := topic if topic.IsROPublic { return true } // if user not admin, reload topic with admin rights if !user.IsAdmin { currentTopic = &Topic{} e := currentTopic.FindByID(topic.ID, true) if e != nil { return false } } if utils.ArrayContains(currentTopic.ROUsers, user.Username) || utils.ArrayContains(currentTopic.RWUsers, user.Username) || utils.ArrayContains(currentTopic.AdminUsers, user.Username) { return true } userGroups, err := user.GetGroups() if err != nil { log.Errorf("Error while fetching user groups for user %s", user.Username) return false } var groups []string for _, g := range userGroups { groups = append(groups, g.Name) } if utils.ItemInBothArrays(currentTopic.RWGroups, groups) || utils.ItemInBothArrays(currentTopic.ROGroups, groups) || utils.ItemInBothArrays(currentTopic.AdminGroups, groups) { return true } return false }
// IsUserRW return true if user can write on a this topic // Check personal access to topic, and group access func (topic *Topic) IsUserRW(user *User) bool { if utils.ArrayContains(topic.RWUsers, user.Username) || utils.ArrayContains(topic.AdminUsers, user.Username) { return true } userGroups, err := user.GetGroups() if err != nil { log.Errorf("Error while fetching user groups") return false } var groups []string for _, g := range userGroups { groups = append(groups, g.Name) } if utils.ItemInBothArrays(topic.RWGroups, groups) { return true } return false }
// Unlike removes a like from one message func (message *Message) Unlike(user User) error { if !utils.ArrayContains(message.Likers, user.Username) { return fmt.Errorf("Unlike not possible, %s is not a liker of this message", user.Username) } err := Store().clMessages.Update( bson.M{"_id": message.ID}, bson.M{"$set": bson.M{"dateUpdate": time.Now().Unix()}, "$inc": bson.M{"nbLikes": -1}, "$pull": bson.M{"likers": user.Username}}) if err != nil { return err } return nil }
func fullTreeMessages(messages []Message, nloop int, criteria *MessageCriteria) ([]Message, error) { var tree []Message if nloop > 10 { e := "Infinite loop detected in fullTreeMessages" log.Errorf(e) return tree, errors.New(e) } replies := make(map[string][]Message) var alreadyDone []string for i := 0; i <= len(messages)-1; i++ { if utils.ArrayContains(alreadyDone, messages[i].ID) { continue } var replyAdded = false for _, msgReply := range replies[messages[i].ID] { messages[i].Replies = append(messages[i].Replies, msgReply) delete(replies, messages[i].ID) replyAdded = true } if messages[i].InReplyOfIDRoot == "" { if replyAdded || nloop > 1 { tree = append(tree, messages[i]) } else if nloop == 1 && !replyAdded { replies[messages[i].ID] = append(replies[messages[i].ID], messages[i]) } continue } replies[messages[i].InReplyOfID] = append(replies[messages[i].InReplyOfID], messages[i]) alreadyDone = append(alreadyDone, messages[i].ID) } if len(replies) == 0 { return tree, nil } t, err := getTree(replies, criteria) if err != nil { return tree, err } ft, err := fullTreeMessages(t, nloop+1, criteria) return append(ft, tree...), err }
// IsUserAdmin return true if user is admin on this group func (group *Group) IsUserAdmin(user *User) bool { if utils.ArrayContains(group.AdminUsers, user.Username) { return true } return false }