casdoor/object/init.go

262 lines
8.3 KiB
Go
Raw Normal View History

2022-02-13 23:39:27 +08:00
// Copyright 2021 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.
2021-02-15 10:05:14 +08:00
package object
2021-12-31 09:36:48 +08:00
import (
"encoding/gob"
"fmt"
2022-08-09 16:50:49 +08:00
"os"
2021-12-31 09:36:48 +08:00
"github.com/astaxie/beego"
2022-01-20 14:11:46 +08:00
"github.com/casdoor/casdoor/util"
"github.com/duo-labs/webauthn/webauthn"
2021-12-31 09:36:48 +08:00
)
2021-02-15 10:05:14 +08:00
func InitDb() {
2022-03-31 12:28:45 +08:00
existed := initBuiltInOrganization()
if !existed {
initBuiltInPermission()
initBuiltInProvider()
2022-03-31 12:28:45 +08:00
initBuiltInUser()
initBuiltInApplication()
initBuiltInCert()
initBuiltInLdap()
}
initWebAuthn()
2021-02-15 10:05:14 +08:00
}
var staticBaseUrl = beego.AppConfig.String("staticBaseUrl")
2022-03-31 12:28:45 +08:00
func initBuiltInOrganization() bool {
2021-02-15 10:05:14 +08:00
organization := getOrganization("admin", "built-in")
if organization != nil {
2022-03-31 12:28:45 +08:00
return true
2021-02-15 10:05:14 +08:00
}
organization = &Organization{
Owner: "admin",
Name: "built-in",
CreatedTime: util.GetCurrentTime(),
DisplayName: "Built-in Organization",
WebsiteUrl: "https://example.com",
Favicon: fmt.Sprintf("%s/img/casbin/favicon.ico", staticBaseUrl),
2022-03-13 00:30:18 +08:00
PasswordType: "plain",
PhonePrefix: "86",
DefaultAvatar: fmt.Sprintf("%s/img/casbin.svg", staticBaseUrl),
2022-03-13 00:30:18 +08:00
Tags: []string{},
2022-06-18 01:41:21 +08:00
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: "Roles", Visible: true, ViewRule: "Public", ModifyRule: "Immutable"},
{Name: "Permissions", Visible: true, ViewRule: "Public", ModifyRule: "Immutable"},
2022-06-18 01:41:21 +08:00
{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"},
{Name: "WebAuthn credentials", Visible: true, ViewRule: "Self", ModifyRule: "Self"},
2022-06-18 01:41:21 +08:00
},
2021-02-15 10:05:14 +08:00
}
AddOrganization(organization)
2022-03-31 12:28:45 +08:00
return false
2021-02-15 10:05:14 +08:00
}
func initBuiltInUser() {
user := getUser("built-in", "admin")
if user != nil {
return
}
user = &User{
2021-12-23 21:28:40 +08:00
Owner: "built-in",
Name: "admin",
CreatedTime: util.GetCurrentTime(),
Id: util.GenerateId(),
Type: "normal-user",
Password: "123",
DisplayName: "Admin",
Avatar: fmt.Sprintf("%s/img/casbin.svg", staticBaseUrl),
2021-12-23 21:28:40 +08:00
Email: "admin@example.com",
Phone: "12345678910",
Address: []string{},
Affiliation: "Example Inc.",
Tag: "staff",
Score: 2000,
Ranking: 1,
IsAdmin: true,
IsGlobalAdmin: true,
IsForbidden: false,
IsDeleted: false,
SignupApplication: "app-built-in",
2021-12-23 21:28:40 +08:00
CreatedIp: "127.0.0.1",
Properties: make(map[string]string),
2021-02-15 10:05:14 +08:00
}
AddUser(user)
}
2021-02-15 10:32:14 +08:00
func initBuiltInApplication() {
application := getApplication("admin", "app-built-in")
if application != nil {
return
}
application = &Application{
Owner: "admin",
Name: "app-built-in",
CreatedTime: util.GetCurrentTime(),
DisplayName: "Casdoor",
Logo: fmt.Sprintf("%s/img/casdoor-logo_1185x256.png", staticBaseUrl),
2021-05-28 22:38:12 +08:00
HomepageUrl: "https://casdoor.org",
2021-02-15 10:32:14 +08:00
Organization: "built-in",
2021-12-31 09:36:48 +08:00
Cert: "cert-built-in",
2021-02-15 10:32:14 +08:00
EnablePassword: true,
2021-05-28 22:38:12 +08:00
EnableSignUp: true,
Providers: []*ProviderItem{
{Name: "provider_captcha_default", CanSignUp: false, CanSignIn: false, CanUnlink: false, Prompted: false, AlertType: "None", Provider: nil},
},
2021-09-20 22:17:52 +08:00
SignupItems: []*SignupItem{
{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: "Normal"},
2021-09-20 22:17:52 +08:00
{Name: "Phone", Visible: true, Required: true, Prompted: false, Rule: "None"},
{Name: "Agreement", Visible: true, Required: true, Prompted: false, Rule: "None"},
},
RedirectUris: []string{},
ExpireInHours: 168,
2021-02-15 10:32:14 +08:00
}
AddApplication(application)
}
func readTokenFromFile() (string, string) {
pemPath := "./object/token_jwt_key.pem"
keyPath := "./object/token_jwt_key.key"
2022-08-09 16:50:49 +08:00
pem, err := os.ReadFile(pemPath)
if err != nil {
return "", ""
}
2022-08-09 16:50:49 +08:00
key, err := os.ReadFile(keyPath)
if err != nil {
return "", ""
}
return string(pem), string(key)
}
2021-12-31 09:36:48 +08:00
func initBuiltInCert() {
tokenJwtCertificate, tokenJwtPrivateKey := readTokenFromFile()
2021-12-31 09:36:48 +08:00
cert := getCert("admin", "cert-built-in")
if cert != nil {
return
}
cert = &Cert{
Owner: "admin",
Name: "cert-built-in",
CreatedTime: util.GetCurrentTime(),
DisplayName: "Built-in Cert",
Scope: "JWT",
Type: "x509",
CryptoAlgorithm: "RS256",
2021-12-31 09:36:48 +08:00
BitSize: 4096,
ExpireInYears: 20,
Certificate: tokenJwtCertificate,
2021-12-31 09:36:48 +08:00
PrivateKey: tokenJwtPrivateKey,
}
AddCert(cert)
}
func initBuiltInLdap() {
ldap := GetLdap("ldap-built-in")
if ldap != nil {
return
}
ldap = &Ldap{
Id: "ldap-built-in",
Owner: "built-in",
ServerName: "BuildIn LDAP Server",
Host: "example.com",
Port: 389,
Admin: "cn=buildin,dc=example,dc=com",
Passwd: "123",
BaseDn: "ou=BuildIn,dc=example,dc=com",
AutoSync: 0,
LastSync: "",
}
AddLdap(ldap)
}
func initBuiltInProvider() {
provider := GetProvider("admin/provider_captcha_default")
if provider != nil {
return
}
provider = &Provider{
Owner: "admin",
Name: "provider_captcha_default",
CreatedTime: util.GetCurrentTime(),
DisplayName: "Captcha Default",
Category: "Captcha",
Type: "Default",
}
AddProvider(provider)
}
func initWebAuthn() {
gob.Register(webauthn.SessionData{})
}
func initBuiltInPermission() {
permission := GetPermission("built-in/permission-built-in")
if permission != nil {
return
}
permission = &Permission{
Owner: "built-in",
Name: "permission-built-in",
CreatedTime: util.GetCurrentTime(),
DisplayName: "Built-in Permission",
Users: []string{"built-in/admin"},
Roles: []string{},
ResourceType: "Application",
Resources: []string{"app-built-in"},
Actions: []string{"Read", "Write", "Admin"},
Effect: "Allow",
IsEnabled: true,
}
AddPermission(permission)
}