From ec296215471b1e2e35add3826f07ec5bd8165970 Mon Sep 17 00:00:00 2001 From: Bingchang Chen <19990626.love@163.com> Date: Tue, 5 Jul 2022 00:36:22 +0800 Subject: [PATCH] feat: init from configuration file (#858) * feat: init from configuration file * Update init_data.json.template * Update init_data.go Co-authored-by: Yang Luo --- init_data.json.template | 158 ++++++++++++++++++++++++++++++++++++++++ main.go | 1 + object/init_data.go | 146 +++++++++++++++++++++++++++++++++++++ 3 files changed, 305 insertions(+) create mode 100644 init_data.json.template create mode 100644 object/init_data.go diff --git a/init_data.json.template b/init_data.json.template new file mode 100644 index 00000000..b18d181e --- /dev/null +++ b/init_data.json.template @@ -0,0 +1,158 @@ +{ + "organizations": [ + { + "owner": "", + "name": "", + "displayName": "", + "websiteUrl": "", + "favicon": "", + "passwordType": "", + "phonePrefix": "", + "defaultAvatar": "", + "tags": [""] + } + ], + "applications": [ + { + "owner": "", + "name": "", + "displayName": "", + "logo": "", + "homepageUrl": "", + "organization": "", + "cert": "", + "enablePassword": true, + "enableSignUp": true, + "providers": [ + { + "name": "", + "canSignUp": true, + "canSignIn": true, + "canUnlink": false, + "prompted": false, + "alertType": "None" + } + ], + "signupItems": [ + { + "name": "ID", + "visible": false, + "required": true, + "prompted": false, + "rule": "Random" + }, + { + "name": "Username", + "visible": true, + "required": true, + "prompted": false, + "rule": "None" + }, + { + "name": "Display name", + "visible": true, + "required": true, + "prompted": false, + "rule": "None" + }, + { + "name": "Password", + "visible": true, + "required": true, + "prompted": false, + "rule": "None" + }, + { + "name": "Confirm password", + "visible": true, + "required": true, + "prompted": false, + "rule": "None" + }, + { + "name": "Email", + "visible": true, + "required": true, + "prompted": false, + "rule": "None" + }, + { + "name": "Phone", + "visible": true, + "required": true, + "prompted": false, + "rule": "None" + }, + { + "name": "Agreement", + "visible": true, + "required": true, + "prompted": false, + "rule": "None" + } + ], + "redirectUris": [""], + "expireInHours": 168 + } + ], + "users": [ + { + "owner": "", + "name": "", + "type": "normal-user", + "password": "", + "displayName": "", + "avatar": "", + "email": "", + "phone": "", + "address": [], + "affiliation": "", + "tag": "", + "score": 2000, + "ranking": 1, + "isAdmin": true, + "isGlobalAdmin": true, + "isForbidden": false, + "isDeleted": false, + "signupApplication": "", + "createdIp": "" + } + ], + "providers": [ + { + "owner": "", + "name": "", + "displayName": "", + "category": "", + "type": "" + } + ], + "certs": [ + { + "owner": "", + "name": "", + "displayName": "", + "scope": "JWT", + "type": "x509", + "cryptoAlgorithm": "RS256", + "bitSize": 4096, + "expireInYears": 20, + "publicKey": "", + "privateKey": "" + } + ], + "ldaps": [ + { + "id": "", + "owner": "", + "serverName": "", + "host": "", + "port": 389, + "admin": "", + "passwd": "", + "baseDn": "", + "autoSync": 0, + "lastSync": "" + } + ] +} diff --git a/main.go b/main.go index 275e223a..43028198 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,7 @@ func main() { object.InitAdapter(*createDatabase) object.InitDb() + object.InitFromFile() object.InitDefaultStorageProvider() object.InitLdapAutoSynchronizer() proxy.InitHttpClient() diff --git a/object/init_data.go b/object/init_data.go new file mode 100644 index 00000000..e16630fb --- /dev/null +++ b/object/init_data.go @@ -0,0 +1,146 @@ +// Copyright 2022 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" + +type InitData struct { + Organizations []*Organization `json:"organizations"` + Applications []*Application `json:"applications"` + Users []*User `json:"users"` + Certs []*Cert `json:"certs"` + Providers []*Provider `json:"providers"` + Ldaps []*Ldap `json:"ldaps"` +} + +func InitFromFile() { + initData := readInitDataFromFile("./init_data.json") + if initData != nil { + for _, organization := range initData.Organizations { + initDefinedOrganization(organization) + } + for _, provider := range initData.Providers { + initDefinedProvider(provider) + } + for _, user := range initData.Users { + initDefinedUser(user) + } + for _, application := range initData.Applications { + initDefinedApplication(application) + } + for _, cert := range initData.Certs { + initDefinedCert(cert) + } + for _, ldap := range initData.Ldaps { + initDefinedLdap(ldap) + } + } +} + +func readInitDataFromFile(filePath string) *InitData { + if !util.FileExist(filePath) { + return nil + } + + s := util.ReadStringFromPath(filePath) + + data := &InitData{} + err := util.JsonToStruct(s, data) + if err != nil { + panic(err) + } + + return data +} + +func initDefinedOrganization(organization *Organization) { + existed := getOrganization(organization.Owner, organization.Name) + if existed != nil { + return + } + organization.CreatedTime = util.GetCurrentTime() + organization.AccountItems = []*AccountItem{ + {Name: "Organization", Visible: true, ViewRule: "Public", ModifyRule: "Admin"}, + {Name: "ID", Visible: true, ViewRule: "Public", ModifyRule: "Immutable"}, + {Name: "Name", Visible: true, ViewRule: "Public", ModifyRule: "Admin"}, + {Name: "Display name", Visible: true, ViewRule: "Public", ModifyRule: "Self"}, + {Name: "Avatar", Visible: true, ViewRule: "Public", ModifyRule: "Self"}, + {Name: "User type", Visible: true, ViewRule: "Public", ModifyRule: "Admin"}, + {Name: "Password", Visible: true, ViewRule: "Self", ModifyRule: "Self"}, + {Name: "Email", Visible: true, ViewRule: "Public", ModifyRule: "Self"}, + {Name: "Phone", Visible: true, ViewRule: "Public", ModifyRule: "Self"}, + {Name: "Country/Region", Visible: true, ViewRule: "Public", ModifyRule: "Self"}, + {Name: "Location", Visible: true, ViewRule: "Public", ModifyRule: "Self"}, + {Name: "Affiliation", Visible: true, ViewRule: "Public", ModifyRule: "Self"}, + {Name: "Title", Visible: true, ViewRule: "Public", ModifyRule: "Self"}, + {Name: "Homepage", Visible: true, ViewRule: "Public", ModifyRule: "Self"}, + {Name: "Bio", Visible: true, ViewRule: "Public", ModifyRule: "Self"}, + {Name: "Tag", Visible: true, ViewRule: "Public", ModifyRule: "Admin"}, + {Name: "Signup application", Visible: true, ViewRule: "Public", ModifyRule: "Admin"}, + {Name: "3rd-party logins", Visible: true, ViewRule: "Self", ModifyRule: "Self"}, + {Name: "Properties", Visible: false, ViewRule: "Admin", ModifyRule: "Admin"}, + {Name: "Is admin", Visible: true, ViewRule: "Admin", ModifyRule: "Admin"}, + {Name: "Is global admin", Visible: true, ViewRule: "Admin", ModifyRule: "Admin"}, + {Name: "Is forbidden", Visible: true, ViewRule: "Admin", ModifyRule: "Admin"}, + {Name: "Is deleted", Visible: true, ViewRule: "Admin", ModifyRule: "Admin"}, + } + + AddOrganization(organization) +} + +func initDefinedApplication(application *Application) { + existed := getApplication(application.Owner, application.Name) + if existed != nil { + return + } + application.CreatedTime = util.GetCurrentTime() + AddApplication(application) +} + +func initDefinedUser(user *User) { + existed := getUser(user.Owner, user.Name) + if existed != nil { + return + } + user.CreatedTime = util.GetCurrentTime() + user.Id = util.GenerateId() + user.Properties = make(map[string]string) + AddUser(user) +} + +func initDefinedCert(cert *Cert) { + existed := getCert(cert.Owner, cert.Name) + if existed != nil { + return + } + cert.CreatedTime = util.GetCurrentTime() + AddCert(cert) +} + +func initDefinedLdap(ldap *Ldap) { + existed := GetLdap(ldap.Id) + if existed != nil { + return + } + AddLdap(ldap) +} + +func initDefinedProvider(provider *Provider) { + existed := GetProvider(provider.GetId()) + if existed != nil { + return + } + AddProvider(provider) +}