casdoor / casdoor-go-sdk

Go client SDK for Casdoor
https://github.com/casdoor/casdoor
Apache License 2.0
88 stars 74 forks source link

How do I dump init_data.json? #120

Closed hungrymonkey closed 2 months ago

hungrymonkey commented 2 months ago

This example is incomplete

https://github.com/casdoor/casdoor/blob/master/object/init_data_dump_test.go

The code below is incomplete too

https://github.com/casdoor/casdoor/issues/2461

Can someone tell me how to use the SDK to dump the casdoor init_data.json?

Example project

casdoor.zip

$docker-compose -f docker-compose-casdoor.yml up
$go build dumpcasdoor.go
$./dumpcasdoor
0
Writing out init_data.json

The example project has a known issue unrelated to the bug report. You will need to run docker volume prune -a and delete the saved volume. You cannot start the same volume twice.

postgres-casdoor-1  | 2024-08-04 17:48:24.494 UTC [58] ERROR:  duplicate key value violates unique constraint "model_pkey"
postgres-casdoor-1  | 2024-08-04 17:48:24.494 UTC [58] DETAIL:  Key (owner, name)=(, ) already exists.
postgres-casdoor-1  | 2024-08-04 17:48:24.494 UTC [58] STATEMENT:  INSERT INTO "model" ("owner","name","created_time","display_name","description","model_text") VALUES ($1,$2,$3,$4,$5,$6)
casdoor-1           | panic: pq: duplicate key value violates unique constraint "model_pkey"
casdoor-1           | 
casdoor-1           | goroutine 1 [running]:
casdoor-1           | github.com/casdoor/casdoor/object.initDefinedModel(0xc0003aae00)
casdoor-1           |   /go/src/casdoor/object/init_data.go:415 +0x107
casdoor-1           | github.com/casdoor/casdoor/object.InitFromFile()
casdoor-1           |   /go/src/casdoor/object/init_data.go:82 +0x252
casdoor-1           | main.main()
casdoor-1           |   /go/src/casdoor/main.go:44 +0x9d
postgres-casdoor-1  | 2024-08-04 17:48:28.633 UTC [71] ERROR:  duplicate key value violates unique constraint "model_pkey"
postgres-casdoor-1  | 2024-08-04 17:48:28.633 UTC [71] DETAIL:  Key (owner, name)=(, ) already exists.
postgres-casdoor-1  | 2024-08-04 17:48:28.633 UTC [71] STATEMENT:  INSERT INTO "model" 
cat dumpcasdoor.go 
package main
import (
      "fmt"

      "github.com/casdoor/casdoor-go-sdk/casdoorsdk"
//    "github.com/casvisor/casvisor-go-sdk/casvisorsdk"
      "github.com/casdoor/casdoor/util"
)

type AuthClient struct {
      AuthClient *casdoorsdk.Client
}
type InitData struct {
      Organizations []*casdoorsdk.Organization       `json:"organizations"`
      Applications  []*casdoorsdk.Application        `json:"applications"`
      Users         []*casdoorsdk.User               `json:"users"`
      Certs         []*casdoorsdk.Cert               `json:"certs"`
      Providers     []*casdoorsdk.Provider           `json:"providers"`
//    Ldaps         []*casdoorsdk.Ldap               `json:"ldaps"`
      Models        []*casdoorsdk.Model              `json:"models"`
      Permissions   []*casdoorsdk.Permission         `json:"permissions"`
      Payments      []*casdoorsdk.Payment            `json:"payments"`
      Products      []*casdoorsdk.Product            `json:"products"`
      Resources     []*casdoorsdk.Resource           `json:"resources"`
      Roles         []*casdoorsdk.Role               `json:"roles"`
      Syncers       []*casdoorsdk.Syncer             `json:"syncers"`
      Tokens        []*casdoorsdk.Token              `json:"tokens"`
      Webhooks      []*casdoorsdk.Webhook            `json:"webhooks"`
      Groups        []*casdoorsdk.Group              `json:"groups"`
      Adapters      []*casdoorsdk.Adapter            `json:"adapters"`
      Enforcers     []*casdoorsdk.Enforcer           `json:"enforcers"`
      Plans         []*casdoorsdk.Plan               `json:"plans"`
      Pricings      []*casdoorsdk.Pricing            `json:"pricings"`
//    Invitations   []*casdoorsdk.Invitation         `json:"invitations"`
      Records       []*casdoorsdk.Record            `json:"records"`
      Sessions      []*casdoorsdk.Session            `json:"sessions"`
      Subscriptions []*casdoorsdk.Subscription       `json:"subscriptions"`
      Transactions  []*casdoorsdk.Transaction        `json:"transactions"`
}

func dumpData() error {
      authClient := AuthClient{
        AuthClient: casdoorsdk.NewClient(
         "http://localhost:8080",
         "65b62e48d22c42521e90",
         "71d6a24c781e6aa5a5cee6d59d92344815fbf6ca",
         `-----BEGIN CERTIFICATE-----
MIIE3TCCAsWgAwIBAgIDAeJAMA0GCSqGSIb3DQEBCwUAMCgxDjAMBgNVBAoTB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/34eXjPXPmyCI9uTF8xmqYk4FnWiBtj/
NXn8kYTDrbXZyRtX2pzW3//CRROOlHxZpCJF2zD+++GcWp4uR+QnVhuR3XBy/jz0
ZhTfXMQCT/lj6FFP8W4Vo/mMbPTh7NUwCFj1/chZyvG/jHHZ/eLdjs1zzdNXv0tp
S5LQgJUo9+Y2ll3yiUn+G+ZSSKfjaiPFLWl/UQ9tKIczmayug+QL42AsUSk/x22k
2lyHljkUB9qm9lz+UZZfFc9uX463PgqeYpU4zFkIioQwiQLYtDIvBl95YUoBZqZS
wsN/eo2fyOJYdCbh1Mfp++AmDdOeIuOMkgiJlzrwhIA2WkyWXAswuS16+E9WXCKT
tyhSns5XAg/KWlrdeyAFRH6QkO98epxb4OIcFKAGC9cnMAwtD4YnW3ac6JoXbxto
VS63dfxu1kx3EFRDvZuMsUi2sVXcXWQvMy2EnKbQZPxld/pQAoFU6aP+AqXFLq+X
34oLytKRDfW1m1YhVtsngYqrJReUIQ7rVBc87KaIU3FizVYUY/4AOLA9Jq4Da3tM
cQ==
-----END CERTIFICATE-----`,
         `Sound-Prediction`,
         "glance",
        ),
      }
      users, _ := authClient.AuthClient.GetUsers()
      for u := range users {
        fmt.Println(u)
      }
      authClient.DumpToFile("init_data.json")
      return nil
}

func (ac *AuthClient) DumpToFile(filePath string) error {
      fmt.Printf("Writing out %v\n", filePath)
      return ac.writeInitDataToFile(filePath)
}

func (ac *AuthClient) writeInitDataToFile(filePath string) error {
      organizations, err := ac.AuthClient.GetOrganizations()
      if err != nil {
        fmt.Println(err)
        return err
      }

      applications, err := ac.AuthClient.GetApplications()
      if err != nil {
        fmt.Println(err)
        return err
      }

      users, err := ac.AuthClient.GetGlobalUsers()
      if err != nil {
        fmt.Println(err)
        return err
      }

      certs, err := ac.AuthClient.GetCerts()
      if err != nil {
        fmt.Println(err)
        return err
      }

      providers, err := ac.AuthClient.GetProviders()
      if err != nil {
        return err
      }

      models, err := ac.AuthClient.GetModels()
      if err != nil {
        return err
      }

      permissions, err := ac.AuthClient.GetPermissions()
      if err != nil {
        return err
      }

      payments, err := ac.AuthClient.GetPayments()
      if err != nil {
        return err
      }

      products, err := ac.AuthClient.GetProducts()
      if err != nil {
        return err
      }

      resources, err := ac.AuthClient.GetResources("", "", "", "", "", "")
      if err != nil {
        return err
      }

      roles, err := ac.AuthClient.GetRoles()
      if err != nil {
        return err
      }

      syncers, err := ac.AuthClient.GetSyncers()
      if err != nil {
        return err
      }

      tokens, err := ac.AuthClient.GetTokens()
                if err != nil {
        return err
      }

      webhooks, err := ac.AuthClient.GetWebhooks()
      if err != nil {
        return err
      }

      groups, err := ac.AuthClient.GetGroups()
      if err != nil {
        return err
      }

      adapters, err := ac.AuthClient.GetAdapters()
      if err != nil {
        return err
      }

      enforcers, err := ac.AuthClient.GetEnforcers()
      if err != nil {
        return err
      }

      plans, err := ac.AuthClient.GetPlans()
      if err != nil {
        return err
      }

      pricings, err := ac.AuthClient.GetPricings()
      if err != nil {
        return err
      }

      records, err := ac.AuthClient.GetRecords()
      if err != nil {
        return err
      }

      sessions, err := ac.AuthClient.GetSessions()
      if err != nil {
        return err
      }

      subscriptions, err := ac.AuthClient.GetSubscriptions()
      if err != nil {
        return err
      }

      transactions, err := ac.AuthClient.GetTransactions()
      if err != nil {
        return err
      }

      data := &InitData{
        Organizations: organizations,
        Applications:  applications,
        Users:         users,
        Certs:         certs,
        Providers:     providers,
//      Ldaps:         ldaps,
        Models:        models,
        Permissions:   permissions,
        Payments:      payments,
        Products:      products,
        Resources:     resources,
        Roles:         roles,
        Syncers:       syncers,
        Tokens:        tokens,
        Webhooks:      webhooks,
        Groups:        groups,
        Adapters:      adapters,
        Enforcers:     enforcers,
        Plans:         plans,
        Pricings:      pricings,
//       Invitations:   invitations,
         Records:       records,
        Sessions:      sessions,
        Subscriptions: subscriptions,
        Transactions:  transactions,
      }

      text := util.StructToJsonFormatted(data)
      fmt.Println(text)
      util.WriteStringToPath(text, filePath)

      return nil
}

func main(){
      dumpData()
}
 cat go.mod 
module object

go 1.21

require (
     dario.cat/mergo v1.0.0 // indirect
     github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
     github.com/Microsoft/go-winio v0.6.1 // indirect
     github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
     github.com/beego/beego v1.12.12 // indirect
     github.com/casbin/casbin/v2 v2.77.2 // indirect
     github.com/casdoor/casdoor v1.659.0 // indirect
     github.com/casdoor/casdoor-go-sdk v0.47.0 // indirect
     github.com/casdoor/xorm-adapter/v3 v3.1.0 // indirect
     github.com/cloudflare/circl v1.3.3 // indirect
     github.com/cyphar/filepath-securejoin v0.2.4 // indirect
     github.com/emirpasic/gods v1.18.1 // indirect
     github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
     github.com/go-git/go-billy/v5 v5.5.0 // indirect
     github.com/go-git/go-git/v5 v5.11.0 // indirect
     github.com/go-ole/go-ole v1.2.6 // indirect
     github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
     github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
     github.com/golang/protobuf v1.5.3 // indirect
     github.com/golang/snappy v0.0.4 // indirect
     github.com/google/uuid v1.6.0 // indirect
     github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
     github.com/json-iterator/go v1.1.12 // indirect
     github.com/kevinburke/ssh_config v1.2.0 // indirect
     github.com/lib/pq v1.10.9 // indirect
     github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
     github.com/modern-go/reflect2 v1.0.2 // indirect
     github.com/nyaruka/phonenumbers v1.1.5 // indirect
     github.com/pjbgf/sha1cd v0.3.0 // indirect
     github.com/sergi/go-diff v1.1.0 // indirect
     github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
     github.com/shirou/gopsutil v3.21.11+incompatible // indirect
     github.com/skeema/knownhosts v1.2.1 // indirect
     github.com/syndtr/goleveldb v1.0.0 // indirect
     github.com/thanhpk/randstr v1.0.4 // indirect
     github.com/tklauser/go-sysconf v0.3.10 // indirect
     github.com/tklauser/numcpus v0.4.0 // indirect
     github.com/xanzy/ssh-agent v0.3.3 // indirect
     github.com/xorm-io/builder v0.3.13 // indirect
     github.com/xorm-io/xorm v1.1.6 // indirect
     github.com/yusufpapurcu/wmi v1.2.2 // indirect
     golang.org/x/crypto v0.21.0 // indirect
     golang.org/x/mod v0.12.0 // indirect
     golang.org/x/net v0.21.0 // indirect
     golang.org/x/oauth2 v0.17.0 // indirect
     golang.org/x/sys v0.18.0 // indirect
     golang.org/x/text v0.14.0 // indirect
     golang.org/x/tools v0.13.0 // indirect
     google.golang.org/appengine v1.6.8 // indirect
     google.golang.org/protobuf v1.32.0 // indirect
     gopkg.in/warnings.v0 v0.1.2 // indirect
     gopkg.in/yaml.v2 v2.4.0 // indirect
)
$ docker images

REPOSITORY                       TAG       IMAGE ID       CREATED         SIZE
casbin/casdoor                   latest    59ed500d802d   2 days ago      149MB
postgres                         latest    07a4ee949b9e   2 months ago    432MB
sosedoff/pgweb                   latest    58d8137a6dd1   4 months ago    174MB
casbin-bot commented 2 months ago

@tangyang9464 @JalinWang @imp2002

hsluoyz commented 2 months ago

@hungrymonkey this is not Go SDK related. And your error seems to be a primary key in SQL error. Your database is bad

hungrymonkey commented 2 months ago

Thanks. GetRecords was failing. I didn't have error messages.