diff --git a/object/init_data_dump.go b/object/init_data_dump.go new file mode 100644 index 00000000..4ad8d194 --- /dev/null +++ b/object/init_data_dump.go @@ -0,0 +1,121 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package object + +import "github.com/casdoor/casdoor/util" + +func DumpToFile(filePath string) error { + return writeInitDataToFile(filePath) +} + +func writeInitDataToFile(filePath string) error { + organizations, err := GetOrganizations("admin") + if err != nil { + return err + } + + applications, err := GetApplications("admin") + if err != nil { + return err + } + + users, err := GetGlobalUsers() + if err != nil { + return err + } + + certs, err := GetCerts("") + if err != nil { + return err + } + + providers, err := GetGlobalProviders() + if err != nil { + return err + } + + ldaps, err := GetLdaps("") + if err != nil { + return err + } + + models, err := GetModels("") + if err != nil { + return err + } + + permissions, err := GetPermissions("") + if err != nil { + return err + } + + payments, err := GetPayments("") + if err != nil { + return err + } + + products, err := GetProducts("") + if err != nil { + return err + } + + resources, err := GetResources("", "") + if err != nil { + return err + } + + roles, err := GetRoles("") + if err != nil { + return err + } + + syncers, err := GetSyncers("") + if err != nil { + return err + } + + tokens, err := GetTokens("", "") + if err != nil { + return err + } + + webhooks, err := GetWebhooks("", "") + 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, + } + + text := util.StructToJsonFormatted(data) + util.WriteStringToPath(text, filePath) + + return nil +} diff --git a/object/init_data_dump_test.go b/object/init_data_dump_test.go new file mode 100644 index 00000000..384e0503 --- /dev/null +++ b/object/init_data_dump_test.go @@ -0,0 +1,29 @@ +// Copyright 2023 The Casdoor Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !skipCi +// +build !skipCi + +package object + +import "testing" + +func TestDumpToFile(t *testing.T) { + InitConfig() + + err := DumpToFile("./init_data_dump.json") + if err != nil { + panic(err) + } +}