Пример #1
0
func changePassword(c *gin.Context) {
	request := struct {
		ID              int    `json:'id'`
		CurrentPassword string `json:"current_password"`
		NewPassword     string `json:"new_password"`
	}{}
	response := gin.H{}
	defer c.JSON(http.StatusOK, response)
	if err := c.BindJSON(&request); err != nil {
		response["code"] = http.StatusBadRequest
		response["message"] = err.Error()
		return
	}
	currentUser := GetUser(c)
	if !currentUser.IsAdmin() && currentUser.ID != request.ID {
		response["code"] = 403
		response["message"] = "You don't have permission to access."
		return
	}
	if request.CurrentPassword == request.NewPassword {
		response["code"] = http.StatusNotModified
		response["message"] = "new password equal current password"
		return
	}
	user := types.User{}
	mydb.Table("user").Where("id = ?", request.ID).Find(&user)
	if user.Password != utils.Md5(request.CurrentPassword) {
		response["code"] = http.StatusNotModified
		response["message"] = "current password is incorrect"
		return
	}
	mydb.Table("user").Where("id = ?", request.ID).Update("password", utils.Md5(request.NewPassword))
	response["code"] = http.StatusOK
	response["message"] = "change password success"
}
Пример #2
0
func userCreate(c *gin.Context) {
	user := User{}
	response := gin.H{}
	defer c.JSON(http.StatusOK, response)
	if err := c.BindJSON(&user); err != nil {
		response["code"] = http.StatusBadRequest
		response["message"] = err.Error()
		return
	}
	if user.Username == "" || user.Phone == "" {
		response["code"] = http.StatusBadRequest
		response["message"] = "The username and phone number are not allowed to be empty"
		return
	}
	cnt := 0
	mydb.Table("user").Where("username=?", user.Username).Count(&cnt)
	if cnt > 0 {
		response["code"] = http.StatusBadRequest
		response["message"] = "username already exists"
		return
	}
	user.Password = utils.Md5(user.Username)
	if err := mydb.Set("gorm:save_associations", false).Save(&user).Error; err != nil {
		response["code"] = http.StatusInternalServerError
		response["message"] = err.Error()
		return
	}
	mydb.Model(&user).Association("Groups").Replace(user.Groups).Find(&user.Groups)

	response["code"] = http.StatusOK
	response["message"] = "user create successful"
	response["user"] = user
}
Пример #3
0
func (this *Agent) ID() string {
	var str string
	a, _ := net.Interfaces()
	for _, i := range a {
		for _, prifex := range []string{"en", "eth", "em"} {
			if strings.HasPrefix(i.Name, prifex) && len(i.HardwareAddr.String()) > 0 {
				str += i.HardwareAddr.String()
				continue
			}
		}
	}
	return utils.Md5(str)[:16]
}
Пример #4
0
func InitIpRange() error {
	ipList := []string{}
	for _, ipRange := range GlobalConfig.IP_RANGE {
		var (
			ipStart string
			ipEnd   string
		)
		if len(ipRange) == 0 {
			continue
		}
		if strings.Contains(ipRange, "-") {
			field := strings.Split(ipRange, "-")
			if len(field) != 2 {
				continue
			}
			ipStart, ipEnd = field[0], field[1]
		} else {
			ipStart = ipRange
			ipEnd = ipRange
		}
		ips, err := utils.GetIPRange(ipStart, ipEnd)
		if err != nil {
			return err
		}
		ipList = append(ipList, ips...)
	}
	for _, ip := range ipList {
		s := &types.Switch{
			ID:              utils.Md5(ip)[:16],
			IP:              ip,
			Hostname:        "",
			LegalPrefix:     GlobalConfig.LEGAL_PREFIX,
			CollectInterval: GlobalConfig.COLLECT_INTERVAL,
			Snmp: types.SnmpConfig{
				Port:      GlobalConfig.SNMP_PORT,
				Version:   GlobalConfig.SNMP_VERSION,
				Community: GlobalConfig.SNMP_COMMUNITY,
			},
		}
		netCollect.switchs = append(netCollect.switchs, s)
		lg.Info("do %s, %#v", s.IP, s.Snmp)
		go s.Do(netCollect.tsdBuffer, netCollect.metricBuffer)
		go netCollect.SendConfig2CFC()
		go netCollect.SendMetri2CFC()
	}
	return nil
}
Пример #5
0
func InitMiddleware() {
	globalMiddleware = GlobalMiddleware()
	authMiddleware = &jwt.GinJWTMiddleware{
		Realm:      "TalkingData",
		Key:        []byte(GlobalConfig.SECRET_KEY),
		Timeout:    time.Hour * 24 * 7,
		MaxRefresh: time.Hour * 24 * 7,
		Authenticator: func(userId string, password string, c *gin.Context) (string, bool) {
			var user types.User

			if err := mydb.Table("user").Where("username = ?", userId).First(&user).Error; err != nil {
				c.Set("code", http.StatusNotFound)
				c.Set("message", "user not found")
				return userId, false
			}

			if user.Status != 1 {
				c.Set("code", http.StatusUnauthorized)
				c.Set("message", "user is disable")
				return userId, false
			}

			if userId == user.Username && utils.Md5(strings.TrimSpace(password)) == user.Password {
				return userId, true
			}
			return userId, false
		},
		Authorizator: func(userId string, c *gin.Context) bool {
			var user types.User

			if err := mydb.Table("user").Where("username = ?", userId).First(&user).Error; err != nil {
				c.Set("code", http.StatusNotFound)
				c.Set("message", "user not found")
				return false
			}

			if user.Role == types.USER {
				for _, handler := range AdminHandlers {
					if c.HandlerName() == handler {
						return false
					}
				}
			}
			return true
		},
		PayloadFunc: func(userId string) map[string]interface{} {
			user_info := make(map[string]interface{})
			user_info["userId"] = userId
			return user_info
		},
		Unauthorized: func(c *gin.Context, code int, message string) {
			if new_code, ok := c.Get("code"); ok {
				code = new_code.(int)
				new_message, _ := c.Get("message")
				message = new_message.(string)
				c.JSON(code, gin.H{
					"code":    code,
					"message": message,
				})
				return
			}

			c.JSON(code, gin.H{
				"code":    code,
				"message": message,
			})
		},
	}
}
Пример #6
0
func (this *TimeSeriesData) PK() string {
	return utils.Md5(fmt.Sprintf("%s.%s", this.Metric, this.Tags2String()))
}