Exemplo n.º 1
0
func _adminUserSetting(w http.ResponseWriter, r *http.Request) (result string, bSuccess bool) {
	action := r.FormValue("action")
	key := r.FormValue("user")
	if key == "" && action != "list" {
		result = "please spec user"
		bSuccess = false
		return
	}
	switch action {
	case "limit":
		n := r.FormValue("size")
		size, _ := strconv.Atoi(n)

		user, err := auth.GetUser(key)
		if err != nil {
			result = err.Error()
			bSuccess = false
			return
		} else {
			user.LimitDataSize = size
			err := auth.UpdateUser(key, user)
			if err == nil {
				bSuccess = true
			} else {
				result = err.Error()
				bSuccess = false
			}
			return
		}
	case "add":
		passwd := r.FormValue("passwd")
		if passwd == "" {
			result = "please spec passwd"
			bSuccess = false
			return
		}
		_usertype := auth.UserType_Normal
		usertype := r.FormValue("type")
		maxOnlineServerNum := auth.DefaultMaxOnlineServerNum
		maxSessionNum := auth.DefaultMaxSessionNum
		maxPipeNum := auth.DefaultMaxPipeNum
		maxSameIPServers := auth.DefaultMaxSameIPServers
		switch usertype {
		case "black":
			_usertype = auth.UserType_BlackList
		case "super":
			_usertype = auth.UserType_Super
			maxOnlineServerNum = 10
			maxSessionNum = 10
			maxPipeNum = 10
			maxSameIPServers = 10
		case "admin":
			_usertype = auth.UserType_Admin
		}

		user := &auth.User{UserName: key, Passwd: common.HashPasswd(common.Md5(passwd)), UserType: _usertype, LastLoginTime: 0, LastLogoutTime: 0, MaxOnlineServerNum: maxOnlineServerNum, MaxSessionNum: maxSessionNum, MaxPipeNum: maxPipeNum, MaxSameIPServers: maxSameIPServers, TodayCSModeData: 0, LimitDataSize: 0}
		key, err := auth.AddUser(key, user)
		if err != nil {
			result = err.Error()
			bSuccess = false
			return
		} else {
			result = key
			bSuccess = true
			return
		}
	case "list":
		limita := r.FormValue("limita")
		limitb := r.FormValue("limitb")
		if limita == "" || limitb == "" {
			result = "please limita and limitb"
			bSuccess = false
			return
		}
		arr := auth.GetUserNameList(limita, limitb)
		res, _ := json.Marshal(arr)
		result = string(res)
		bSuccess = true
		return
	case "get":
		user, err := auth.GetUser(key)
		if err != nil {
			result = err.Error()
			bSuccess = false
			return
		} else {
			if user == nil {
				result = "donnot have this user"
				bSuccess = false
				return
			}

			res, _ := json.Marshal(user)
			result = string(res)
			bSuccess = true
			return
		}
	case "del":
		bHave, err := auth.DelUser(key)
		if err != nil {
			result = err.Error()
			bSuccess = false
			return
		} else {
			if !bHave {
				result = "donnot have this user"
			}
			bSuccess = true
			return
		}
	case "key":
		_key := auth.GenUserKey(key)
		if _key == "" {
			result = "gen user key fail"
			bSuccess = false
			return
		}
		err := auth.UpdateUserKey(key, _key)
		if err == nil {
			result = _key
			bSuccess = true
		} else {
			result = err.Error()
			bSuccess = false
		}
		return
	case "set":
		_usertype := -1
		maxOnlineServerNum := -1
		maxSessionNum := -1
		maxPipeNum := -1
		maxSameIPServers := -1
		pass := ""
		passwd := r.FormValue("passwd")
		if passwd != "" {
			pass = common.HashPasswd(common.Md5(passwd))
		}
		usertype := r.FormValue("type")
		if usertype != "" {
			switch usertype {
			case "black":
				_usertype = auth.UserType_BlackList
			case "super":
				_usertype = auth.UserType_Super
				maxOnlineServerNum = 10
				maxSessionNum = 10
				maxPipeNum = 10
			case "normal":
				_usertype = auth.UserType_Normal
				maxOnlineServerNum = auth.DefaultMaxOnlineServerNum
				maxSessionNum = auth.DefaultMaxSessionNum
				maxPipeNum = auth.DefaultMaxPipeNum
				maxSameIPServers = auth.DefaultMaxSameIPServers
			case "admin":
				_usertype = auth.UserType_Admin
			}
		}
		servern := r.FormValue("serven")
		if servern != "" {
			maxOnlineServerNum, _ = strconv.Atoi(servern)
		}
		sessionn := r.FormValue("sessionn")
		if sessionn != "" {
			maxSessionNum, _ = strconv.Atoi(sessionn)
		}
		pipen := r.FormValue("pipen")
		if pipen != "" {
			maxPipeNum, _ = strconv.Atoi(pipen)
		}
		ipn := r.FormValue("sameip")
		if ipn != "" {
			maxSameIPServers, _ = strconv.Atoi(ipn)
		}
		user, err := auth.GetUser(key)
		if err != nil {
			result = err.Error()
			bSuccess = false
			return
		} else {
			if _usertype != -1 {
				user.UserType = _usertype
			}
			if maxOnlineServerNum != -1 {
				user.MaxOnlineServerNum = maxOnlineServerNum
			}
			if maxSessionNum != -1 {
				user.MaxSessionNum = maxSessionNum
			}
			if maxPipeNum != -1 {
				user.MaxPipeNum = maxPipeNum
			}
			if maxSameIPServers != -1 {
				user.MaxSameIPServers = maxSameIPServers
			}
			if pass != "" {
				user.Passwd = pass
			}
			err := auth.UpdateUser(key, user)
			if err == nil {
				bSuccess = true
			} else {
				result = err.Error()
				bSuccess = false
			}
			return
		}
	default:
		result = "invalid action"
		bSuccess = false
		return
	}
	result = ""
	bSuccess = true
	return
}
Exemplo n.º 2
0
func genUserKey(name string) string {
	authBaseId++
	return common.Md5(fmt.Sprintf("%d%.0f%s%s", authBaseId, time.Now().Unix(), name, staticKey))
}