options.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package app
  15. import (
  16. "context"
  17. "net/http"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/util/httputils"
  20. "yunion.io/x/pkg/util/rbacscope"
  21. "yunion.io/x/onecloud/pkg/appsrv"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/policy"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/mcclient/auth"
  26. "yunion.io/x/onecloud/pkg/util/rbacutils"
  27. )
  28. func ExportOptionsHandler(app *appsrv.Application, options interface{}) {
  29. ExportOptionsHandlerWithPrefix(app, "", options)
  30. }
  31. func ExportOptionsHandlerWithPrefix(app *appsrv.Application, prefix string, options interface{}) {
  32. hf := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
  33. userCred := auth.FetchUserCredential(ctx, policy.FilterPolicyCredential)
  34. result := policy.PolicyManager.Allow(rbacscope.ScopeSystem, userCred, consts.GetServiceType(), "app-options", "list")
  35. if result.Result == rbacutils.Deny {
  36. httperrors.ForbiddenError(ctx, w, "Not allow to access")
  37. return
  38. }
  39. appsrv.SendJSON(w, jsonutils.Marshal(options))
  40. }
  41. ahf := auth.Authenticate(hf)
  42. name := "get_app_options"
  43. pref := httputils.JoinPath(prefix, "app-options")
  44. app.AddHandler2("GET", pref, ahf, nil, name, nil)
  45. }