router_alert_rule_group.go 3.6 KB
Newer Older
Q
qinyening 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
package http

import (
	"net/http"
	"strconv"
	"strings"
	"time"

	"github.com/gin-gonic/gin"

	"github.com/didi/nightingale/v5/cache"
	"github.com/didi/nightingale/v5/models"
)

func alertRuleGroupGets(c *gin.Context) {
	limit := queryInt(c, "limit", defaultLimit)
	query := queryStr(c, "query", "")

	total, err := models.AlertRuleGroupTotal(query)
	dangerous(err)

	list, err := models.AlertRuleGroupGets(query, limit, offset(c, limit))
	dangerous(err)

	renderData(c, gin.H{
		"list":  list,
		"total": total,
	}, nil)
}

func alertRuleGroupFavoriteGet(c *gin.Context) {
	lst, err := loginUser(c).FavoriteAlertRuleGroups()
	renderData(c, lst, err)
}

type alertRuleGroupForm struct {
	Name         string `json:"name"`
	UserGroupIds string `json:"user_group_ids"`
}

func alertRuleGroupAdd(c *gin.Context) {
	var f alertRuleGroupForm
	bind(c, &f)

	me := loginUser(c).MustPerm("alert_rule_group_create")

	arg := models.AlertRuleGroup{
		Name:         f.Name,
		UserGroupIds: f.UserGroupIds,
		CreateBy:     me.Username,
		UpdateBy:     me.Username,
	}

	err := arg.Add()
	if err == nil {
		// 我创建的,顺便设置为我关注的
		models.AlertRuleGroupFavoriteAdd(arg.Id, me.Id)
	}

	renderMessage(c, err)
}

func alertRuleGroupGet(c *gin.Context) {
	alertRuleGroup := AlertRuleGroup(urlParamInt64(c, "id"))
	alertRuleGroup.FillUserGroups()
	renderData(c, alertRuleGroup, nil)
}

func alertRuleOfGroupGet(c *gin.Context) {
	ars, err := models.AlertRulesOfGroup(urlParamInt64(c, "id"))
	renderData(c, ars, err)
}

func alertRuleOfGroupDel(c *gin.Context) {
	var f idsForm
	bind(c, &f)
	f.Validate()
	loginUser(c).MustPerm("alert_rule_delete")
	renderMessage(c, models.AlertRulesDel(f.Ids))
}

func alertRuleGroupPut(c *gin.Context) {
	var f alertRuleGroupForm
	bind(c, &f)

	me := loginUser(c).MustPerm("alert_rule_group_modify")
	arg := AlertRuleGroup(urlParamInt64(c, "id"))
	alertRuleWritePermCheck(arg, me)

	if arg.Name != f.Name {
		num, err := models.AlertRuleGroupCount("name=? and id<>?", f.Name, arg.Id)
		dangerous(err)

		if num > 0 {
			bomb(200, "AlertRuleGroup %s already exists", f.Name)
		}
	}

	arg.Name = f.Name
	arg.UserGroupIds = f.UserGroupIds
	arg.UpdateBy = me.Username
	arg.UpdateAt = time.Now().Unix()

	renderMessage(c, arg.Update("name", "update_by", "update_at", "user_group_ids"))
}

func alertRuleGroupDel(c *gin.Context) {
	me := loginUser(c).MustPerm("alert_rule_group_delete")
	arg := AlertRuleGroup(urlParamInt64(c, "id"))
	alertRuleWritePermCheck(arg, me)

	renderMessage(c, arg.Del())
}

func alertRuleGroupFavoriteAdd(c *gin.Context) {
	me := loginUser(c)
	arg := AlertRuleGroup(urlParamInt64(c, "id"))
	renderMessage(c, models.AlertRuleGroupFavoriteAdd(arg.Id, me.Id))
}

func alertRuleGroupFavoriteDel(c *gin.Context) {
	me := loginUser(c)
	arg := AlertRuleGroup(urlParamInt64(c, "id"))
	renderMessage(c, models.AlertRuleGroupFavoriteDel(arg.Id, me.Id))
}

func alertRuleWritePermCheck(alertRuleGroup *models.AlertRuleGroup, user *models.User) {
	if user.Role == "Admin" {
		return
	}

	gids := IdsInt64(alertRuleGroup.UserGroupIds)
	if len(gids) == 0 {
		// 压根没有配置管理团队,表示对所有Standard角色放开,那就不校验了
		return
	}

	for _, gid := range gids {
		if cache.UserGroupMember.Exists(gid, user.Id) {
			return
		}
	}

	bomb(http.StatusForbidden, "no permission")
}

func IdsInt64(ids string) []int64 {
	if ids == "" {
		return []int64{}
	}

	arr := strings.Fields(ids)
	count := len(arr)
	ret := make([]int64, 0, count)
	for i := 0; i < count; i++ {
		if arr[i] != "" {
			id, err := strconv.ParseInt(arr[i], 10, 64)
			if err == nil {
				ret = append(ret, id)
			}
		}
	}

	return ret
}