mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
feat: support more objects in init_data JSON (#2776)
This commit is contained in:
parent
6a7ac35e65
commit
92b6fda0f6
@ -17,29 +17,35 @@ package object
|
|||||||
import (
|
import (
|
||||||
"github.com/casdoor/casdoor/conf"
|
"github.com/casdoor/casdoor/conf"
|
||||||
"github.com/casdoor/casdoor/util"
|
"github.com/casdoor/casdoor/util"
|
||||||
|
"github.com/casvisor/casvisor-go-sdk/casvisorsdk"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InitData struct {
|
type InitData struct {
|
||||||
Organizations []*Organization `json:"organizations"`
|
Organizations []*Organization `json:"organizations"`
|
||||||
Applications []*Application `json:"applications"`
|
Applications []*Application `json:"applications"`
|
||||||
Users []*User `json:"users"`
|
Users []*User `json:"users"`
|
||||||
Certs []*Cert `json:"certs"`
|
Certs []*Cert `json:"certs"`
|
||||||
Providers []*Provider `json:"providers"`
|
Providers []*Provider `json:"providers"`
|
||||||
Ldaps []*Ldap `json:"ldaps"`
|
Ldaps []*Ldap `json:"ldaps"`
|
||||||
Models []*Model `json:"models"`
|
Models []*Model `json:"models"`
|
||||||
Permissions []*Permission `json:"permissions"`
|
Permissions []*Permission `json:"permissions"`
|
||||||
Payments []*Payment `json:"payments"`
|
Payments []*Payment `json:"payments"`
|
||||||
Products []*Product `json:"products"`
|
Products []*Product `json:"products"`
|
||||||
Resources []*Resource `json:"resources"`
|
Resources []*Resource `json:"resources"`
|
||||||
Roles []*Role `json:"roles"`
|
Roles []*Role `json:"roles"`
|
||||||
Syncers []*Syncer `json:"syncers"`
|
Syncers []*Syncer `json:"syncers"`
|
||||||
Tokens []*Token `json:"tokens"`
|
Tokens []*Token `json:"tokens"`
|
||||||
Webhooks []*Webhook `json:"webhooks"`
|
Webhooks []*Webhook `json:"webhooks"`
|
||||||
Groups []*Group `json:"groups"`
|
Groups []*Group `json:"groups"`
|
||||||
Adapters []*Adapter `json:"adapters"`
|
Adapters []*Adapter `json:"adapters"`
|
||||||
Enforcers []*Enforcer `json:"enforcers"`
|
Enforcers []*Enforcer `json:"enforcers"`
|
||||||
Plans []*Plan `json:"plans"`
|
Plans []*Plan `json:"plans"`
|
||||||
Pricings []*Pricing `json:"pricings"`
|
Pricings []*Pricing `json:"pricings"`
|
||||||
|
Invitations []*Invitation `json:"invitations"`
|
||||||
|
Records []*casvisorsdk.Record `json:"records"`
|
||||||
|
Sessions []*Session `json:"sessions"`
|
||||||
|
Subscriptions []*Subscription `json:"subscriptions"`
|
||||||
|
Transactions []*Transaction `json:"transactions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitFromFile() {
|
func InitFromFile() {
|
||||||
@ -114,6 +120,21 @@ func InitFromFile() {
|
|||||||
for _, pricing := range initData.Pricings {
|
for _, pricing := range initData.Pricings {
|
||||||
initDefinedPricing(pricing)
|
initDefinedPricing(pricing)
|
||||||
}
|
}
|
||||||
|
for _, invitation := range initData.Invitations {
|
||||||
|
initDefinedInvitation(invitation)
|
||||||
|
}
|
||||||
|
for _, record := range initData.Records {
|
||||||
|
initDefinedRecord(record)
|
||||||
|
}
|
||||||
|
for _, session := range initData.Sessions {
|
||||||
|
initDefinedSession(session)
|
||||||
|
}
|
||||||
|
for _, subscription := range initData.Subscriptions {
|
||||||
|
initDefinedSubscription(subscription)
|
||||||
|
}
|
||||||
|
for _, transaction := range initData.Transactions {
|
||||||
|
initDefinedTransaction(transaction)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,6 +166,11 @@ func readInitDataFromFile(filePath string) (*InitData, error) {
|
|||||||
Enforcers: []*Enforcer{},
|
Enforcers: []*Enforcer{},
|
||||||
Plans: []*Plan{},
|
Plans: []*Plan{},
|
||||||
Pricings: []*Pricing{},
|
Pricings: []*Pricing{},
|
||||||
|
Invitations: []*Invitation{},
|
||||||
|
Records: []*casvisorsdk.Record{},
|
||||||
|
Sessions: []*Session{},
|
||||||
|
Subscriptions: []*Subscription{},
|
||||||
|
Transactions: []*Transaction{},
|
||||||
}
|
}
|
||||||
err := util.JsonToStruct(s, data)
|
err := util.JsonToStruct(s, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -225,6 +251,11 @@ func readInitDataFromFile(filePath string) (*InitData, error) {
|
|||||||
pricing.Plans = []string{}
|
pricing.Plans = []string{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, session := range data.Sessions {
|
||||||
|
if session.SessionId == nil {
|
||||||
|
session.SessionId = []string{}
|
||||||
|
}
|
||||||
|
}
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -543,3 +574,61 @@ func initDefinedPricing(pricing *Pricing) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initDefinedInvitation(invitation *Invitation) {
|
||||||
|
existed, err := getInvitation(invitation.Owner, invitation.Name)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if existed != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
invitation.CreatedTime = util.GetCurrentTime()
|
||||||
|
_, err = AddInvitation(invitation, "en")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func initDefinedRecord(record *casvisorsdk.Record) {
|
||||||
|
record.CreatedTime = util.GetCurrentTime()
|
||||||
|
_ = AddRecord(record)
|
||||||
|
}
|
||||||
|
|
||||||
|
func initDefinedSession(session *Session) {
|
||||||
|
session.CreatedTime = util.GetCurrentTime()
|
||||||
|
_, err := AddSession(session)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func initDefinedSubscription(subscription *Subscription) {
|
||||||
|
existed, err := getSubscription(subscription.Owner, subscription.Name)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if existed != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
subscription.CreatedTime = util.GetCurrentTime()
|
||||||
|
_, err = AddSubscription(subscription)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func initDefinedTransaction(transaction *Transaction) {
|
||||||
|
existed, err := getTransaction(transaction.Owner, transaction.Name)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if existed != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
transaction.CreatedTime = util.GetCurrentTime()
|
||||||
|
_, err = AddTransaction(transaction)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -121,6 +121,31 @@ func writeInitDataToFile(filePath string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
invitations, err := GetInvitations("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
records, err := GetRecords()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sessions, err := GetSessions("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
subscriptions, err := GetSubscriptions("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
transactions, err := GetTransactions("")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
data := &InitData{
|
data := &InitData{
|
||||||
Organizations: organizations,
|
Organizations: organizations,
|
||||||
Applications: applications,
|
Applications: applications,
|
||||||
@ -142,6 +167,11 @@ func writeInitDataToFile(filePath string) error {
|
|||||||
Enforcers: enforcers,
|
Enforcers: enforcers,
|
||||||
Plans: plans,
|
Plans: plans,
|
||||||
Pricings: pricings,
|
Pricings: pricings,
|
||||||
|
Invitations: invitations,
|
||||||
|
Records: records,
|
||||||
|
Sessions: sessions,
|
||||||
|
Subscriptions: subscriptions,
|
||||||
|
Transactions: transactions,
|
||||||
}
|
}
|
||||||
|
|
||||||
text := util.StructToJsonFormatted(data)
|
text := util.StructToJsonFormatted(data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user