Ejemplo n.º 1
0
Archivo: topic.go Proyecto: ovh/tat
// UpdateTopicTags updates tags on topic
func UpdateTopicTags(topic *tat.Topic, tags []string) {
	if !topic.IsAutoComputeTags || len(tags) == 0 {
		return
	}

	update := false
	newTags := topic.Tags
	for _, tag := range tags {
		if !tat.ArrayContains(topic.Tags, tag) {
			update = true
			newTags = append(newTags, tag)
		}
	}

	if update {
		err := store.Tat().CTopics.Update(
			bson.M{"_id": topic.ID},
			bson.M{"$set": bson.M{"tags": newTags}})

		if err != nil {
			log.Errorf("UpdateTopicTags> Error while updating tags on topic")
		} else {
			log.Debugf("UpdateTopicTags> Topic %s ", topic.Topic)
		}
		cache.CleanTopicByName(topic.Topic)
	}
}
Ejemplo n.º 2
0
Archivo: topic.go Proyecto: ovh/tat
// IsUserAdmin return true if user is Tat admin or is admin on this topic
// Check personal access to topic, and group access
func IsUserAdmin(topic *tat.Topic, user *tat.User) bool {

	if user.IsAdmin {
		return true
	}

	if tat.ArrayContains(topic.AdminUsers, user.Username) {
		return true
	}

	userGroups, err := group.GetGroups(user.Username)
	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 tat.ItemInBothArrays(topic.AdminGroups, groups) {
		return true
	}

	// user is "Admin" on his /Private/usrname topics
	return strings.HasPrefix(topic.Topic, "/Private/"+user.Username)
}
Ejemplo n.º 3
0
Archivo: topics.go Proyecto: ovh/tat
// List returns the list of topics that can be viewed by user
func (t *TopicsController) List(ctx *gin.Context) {
	var user = &tat.User{}
	found, err := userDB.FindByUsername(user, getCtxUsername(ctx))
	if !found {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "User unknown"})
		return
	} else if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching user."})
		return
	}
	criteria := t.buildCriteria(ctx, user)
	count, topics, err := topicDB.ListTopics(criteria, user, false, false, false)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching topics."})
		return
	}

	out := &tat.TopicsJSON{Topics: topics, Count: count}

	if criteria.GetNbMsgUnread == "true" {
		c := &tat.PresenceCriteria{
			Username: user.Username,
		}
		count, presences, err := presenceDB.ListPresencesAllFields(c)
		if err != nil {
			ctx.AbortWithError(http.StatusInternalServerError, err)
			return
		}

		unread := make(map[string]int)
		knownPresence := false
		for _, topic := range topics {
			if tat.ArrayContains(user.OffNotificationsTopics, topic.Topic) {
				continue
			}
			knownPresence = false
			for _, presence := range presences {
				if topic.Topic != presence.Topic {
					continue
				}
				knownPresence = true
				if topic.DateLastMessage > presence.DatePresence {
					unread[presence.Topic] = 1
				}
				break
			}
			if !knownPresence {
				unread[topic.Topic] = -1
			}
		}
		out.TopicsMsgUnread = unread
		out.CountTopicsMsgUnread = count
	}
	ctx.JSON(http.StatusOK, out)
}
Ejemplo n.º 4
0
Archivo: topic.go Proyecto: ovh/tat
// GetUserRights return isRW, isAdmin for user
// Check personal access to topic, and group access
func GetUserRights(topic *tat.Topic, user *tat.User) (bool, bool) {

	isUserAdmin := tat.ArrayContains(topic.AdminUsers, user.Username)
	if isUserAdmin {
		return true, true
	}

	userGroups, err := group.GetGroups(user.Username)
	if err != nil {
		log.Errorf("Error while fetching user groups")
		return false, false
	}

	var groups []string
	for _, g := range userGroups {
		groups = append(groups, g.Name)
	}

	isUserRW := tat.ArrayContains(topic.RWUsers, user.Username)
	isRW := isUserRW || tat.ItemInBothArrays(topic.RWGroups, groups)
	isAdmin := isUserAdmin || tat.ItemInBothArrays(topic.AdminUsers, groups)
	return isRW, isAdmin
}
Ejemplo n.º 5
0
Archivo: hook.go Proyecto: ovh/tat
func innerSendHookTopicParameters(hook *tat.HookJSON, topic tat.Topic) {
	for _, p := range topic.Parameters {
		if !tat.ArrayContains(tat.HooksType, p.Key) {
			continue
		}
		h := &tat.HookJSON{
			HookMessage: hook.HookMessage,
			Hook: tat.Hook{
				Action:      "all",
				Type:        p.Key,
				Destination: p.Value,
				Enabled:     true,
			},
		}
		runHook(h, nil, topic)
	}
}
Ejemplo n.º 6
0
Archivo: group.go Proyecto: ovh/tat
// IsUserAdmin return true if user is admin on this group
func IsUserAdmin(group *tat.Group, username string) bool {
	return tat.ArrayContains(group.AdminUsers, username)
}
Ejemplo n.º 7
0
func (ui *tatui) RunExec(hookIn *config.Hook, pathHook string, sendText string) {

	if ui.current != uiMessage && ui.current != uiMessages {
		return
	}

	if sendText == "" && pathHook == "" {
		return
	}
	if sendText != "" && hookIn == nil {
		return
	}
	var hook *config.Hook
	if pathHook != "" {
		for _, h := range ui.hooks {
			if "/sys/kbd/"+h.Shortcut == pathHook {
				hook = &h
				sendText = ""
				break
			}
		}
	} else {
		hook = hookIn
	}

	userExec := strings.Split(hook.Exec, " ")
	execCMD := strings.TrimSpace(userExec[0])
	if execCMD == "" {
		return
	}

	_, err := exec.LookPath(execCMD)
	if err != nil {
		internal.Exit("Invalid exec path for post-exec-run-action, err: %s", err.Error())
		return
	}

	msg := ui.currentListMessages[ui.selectedPaneMessages][ui.uilists[uiMessages][ui.selectedPaneMessages].position]

	if len(hook.Topics) > 0 && !tat.ArrayContains(hook.Topics, msg.Topic) {
		return
	}

	toExec := strings.Replace(hook.Exec, "$UI_SELECTED_MSG_ID", msg.ID, 1)
	toExec = strings.Replace(toExec, "$UI_SELECTED_MSG_TEXT", msg.Text, 1)
	toExec = strings.Replace(toExec, "$UI_SELECTED_MSG_TOPIC", msg.Topic, 1)
	toExec = strings.Replace(toExec, "$UI_SELECTED_MSG_AUTHOR_USERNAME", msg.Author.Username, 1)
	toExec = strings.Replace(toExec, "$UI_SELECTED_MSG_DATE_CREATION", fmt.Sprintf("%f", msg.DateCreation), 1)
	toExec = strings.Replace(toExec, "$UI_SELECTED_MSG_DATE_UPDATE", fmt.Sprintf("%f", msg.DateUpdate), 1)
	toExec = strings.Replace(toExec, "$UI_CURRENT_USERNAME", viper.GetString("username"), 1)

	if sendText != "" {
		sendTextWithoutCMD := strings.Replace(sendText, hook.Command+" ", "", 1)
		toExec = strings.Replace(toExec, "$UI_ACTION_TEXT", sendTextWithoutCMD, 1)
	}

	args := []string{}
	if len(toExec) > 1 {
		args = strings.Split(toExec, " ")
	}

	cmd := exec.Command(execCMD, args[1:]...)
	if e := cmd.Start(); e != nil {
		internal.Exit("Error with exec hook, err: %s", e.Error())
		return
	}

	ui.msg.Text = "Waiting " + hook.Exec + " for command to finish..."
	err = cmd.Wait()
	if err != nil {
		ui.msg.Text = fmt.Sprintf("Error:%s cmd:%s", err.Error(), strings.Join(cmd.Args, " "))
	} else {
		ui.msg.Text = fmt.Sprintf("Success: %s", strings.Join(cmd.Args, " "))
	}

}