Example #1
0
func getBackendConfFromBody(r *http.Request) (bkconf.BackendConf, error) {
	bc := bkconf.BackendConf{}
	err := util.DecodeJsonBody(r.Body, &bc)
	if err != nil {
		return bc, err
	}
	bc.HostName = strings.ToLower(strings.TrimSpace(bc.HostName))
	if bc.HostName == "" {
		return bc, errors.New("[hostname] must not empty.")
	}
	upstreams := []string{}
	for _, upstream := range bc.UpStreams {
		upstream = strings.TrimSpace(upstream)
		if upstream != "" {
			upstreams = append(upstreams, upstream)
		}
	}
	if len(upstreams) > 0 {
		bc.UpStreams = upstreams
	} else {
		return bc, errors.New("[upstreams] must not empty.")
	}
	bc.CreatedTime = time.Now()
	bc.UpdatedTime = bc.CreatedTime
	return bc, nil
}
Example #2
0
func getRoleFromBody(r *http.Request) (*auth.Role, error) {
	role := &auth.Role{}
	err := util.DecodeJsonBody(r.Body, &role)
	if err != nil {
		return role, err
	}

	role.Name = strings.TrimSpace(role.Name)
	if len(role.Name) < 1 {
		return role, errors.New("[Name] must not empty.")
	}

	allow := []string{}
	for _, a := range role.Allow {
		a = strings.TrimSpace(a)
		if a != "" {
			allow = append(allow, a)
		}
	}
	if len(allow) > 0 {
		role.Allow = allow
	}
	deny := []string{}
	for _, d := range role.Deny {
		d = strings.TrimSpace(d)
		if d != "" {
			deny = append(deny, d)
		}
	}
	if len(deny) > 0 {
		role.Deny = deny
	}
	if len(deny) < 1 && len(allow) < 1 {
		return role, errors.New("[Deny] and [Allow] can't all be empty.")
	}

	role.CreatedTime = time.Now()
	role.UpdatedTime = role.CreatedTime
	return role, nil
}
Example #3
0
func getAuthorityFromBody(r *http.Request) (*auth.Authority, error) {
	authority := &auth.Authority{}
	err := util.DecodeJsonBody(r.Body, &authority)
	if err != nil {
		return authority, err
	}

	authority.Email = strings.TrimSpace(authority.Email)
	if len(authority.Email) < 1 {
		return authority, errors.New("[Email] must not empty.")
	}

	allow := []string{}
	for _, a := range authority.Allow {
		a = strings.TrimSpace(a)
		if a != "" {
			allow = append(allow, a)
		}
	}
	if len(allow) > 0 {
		authority.Allow = allow
	}
	deny := []string{}
	for _, d := range authority.Deny {
		d = strings.TrimSpace(d)
		if d != "" {
			deny = append(deny, d)
		}
	}
	if len(deny) > 0 {
		authority.Deny = deny
	}
	if len(deny) < 1 && len(allow) < 1 && len(authority.Roles) < 1 {
		return authority, errors.New("[Deny], [Allow] and [Roles] can't all be empty.")
	}

	authority.CreatedTime = time.Now()
	authority.UpdatedTime = authority.CreatedTime
	return authority, nil
}
Example #4
0
func getUserFromBody(r *http.Request, needPwd bool) (*auth.User, error) {
	fuser := FormUser{}
	user := &auth.User{}
	err := util.DecodeJsonBody(r.Body, &fuser)
	if err != nil {
		return user, err
	}
	user.Id = fuser.Id
	user.Email = fuser.Email
	user.Pwd = fuser.Pwd

	user.Email = strings.TrimSpace(user.Email)
	if len(user.Email) < 1 {
		return user, errors.New("[Email] must not empty.")
	}
	user.Pwd = strings.TrimSpace(user.Pwd)
	if needPwd && len(user.Pwd) < 1 {
		return user, errors.New("[Password] must not empty.")
	}

	user.CreatedTime = time.Now()
	user.UpdatedTime = user.CreatedTime
	return user, nil
}