This repository was archived by the owner on Apr 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdatabase.go
More file actions
255 lines (220 loc) · 7.01 KB
/
Copy pathdatabase.go
File metadata and controls
255 lines (220 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package beubo
import (
"fmt"
"github.com/uberswe/beubo/pkg/plugin"
"github.com/uberswe/beubo/pkg/structs"
"github.com/uberswe/beubo/pkg/utility"
"golang.org/x/crypto/bcrypt"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"io/ioutil"
"log"
"os"
"time"
)
var (
seedEmail = "seed@beubo.com"
seedPassword = "Beubo1234!"
// TODO change this to a config
shouldSeed = false
shouldRefreshDatabase = false
// DB is used to perform database queries globally. In the future this should probably
// be changed so that database.go declares methods that can be used to perform types of
// queries
DB *gorm.DB
)
func setupDB() *gorm.DB {
log.Println("Opening database")
dialector := getDialector(databaseUser, databasePassword, databaseHost, databasePort, databaseName, databaseDriver)
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Silent, // Log level
Colorful: true,
},
)
config := gorm.Config{
Logger: newLogger,
}
if databaseDriver == "sqlite3" {
config = gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
Logger: newLogger,
}
}
db, err := gorm.Open(dialector, &config)
utility.ErrorHandler(err, true)
return db
}
func getDialector(user string, pass string, host string, port string, name string, driver string) gorm.Dialector {
connectString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", user, pass, host, port, name)
dialector := mysql.Open(connectString)
if driver == "sqlite3" {
connectString = databaseName
dialector = sqlite.Open(connectString)
}
return dialector
}
func databaseInit() {
DB = setupDB()
if shouldRefreshDatabase {
type Result struct {
DropQuery string
}
var result []Result
log.Println("Dropping all database tables")
if databaseDriver == "sqlite3" {
DB.Raw("SELECT 'DROP TABLE IF EXISTS `' || name || '`;' as drop_query FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';").Scan(&result)
} else {
DB.Raw("SELECT concat('DROP TABLE IF EXISTS `', table_name, '`;') as drop_query FROM information_schema.tables WHERE table_schema = 'beubo';").Scan(&result)
}
for _, r := range result {
DB.Exec(r.DropQuery)
}
}
log.Println("Running database migrations")
err := DB.AutoMigrate(
&structs.User{},
&structs.UserActivation{},
&structs.Config{},
&structs.Page{},
&structs.Theme{},
&structs.Session{},
&structs.Site{},
&structs.Tag{},
&structs.Comment{},
&structs.Setting{},
&plugin.PluginSite{},
&structs.Role{},
&structs.Feature{})
utility.ErrorHandler(err, true)
}
func prepareSeed(email string, password string) {
shouldSeed = true
seedEmail = email
seedPassword = password
}
func databaseSeed() {
theme := addThemes()
// user registration is disabled by default
disableRegistration := structs.Setting{Key: "enable_user_registration", Value: "false"}
DB.Where("key = ?", disableRegistration.Key).First(&disableRegistration)
if disableRegistration.ID == 0 {
DB.Create(&disableRegistration)
}
// users who register should have a member role
newUserRole := structs.Setting{Key: "new_user_role", Value: "Member"}
DB.Where("key = ?", newUserRole.Key).First(&newUserRole)
if newUserRole.ID == 0 {
DB.Create(&newUserRole)
}
features := []*structs.Feature{
{Key: "manage_sites"},
{Key: "manage_pages"},
{Key: "manage_users"},
{Key: "manage_user_roles"},
{Key: "manage_plugins"},
{Key: "manage_settings"},
}
for _, feature := range features {
DB.Where("key = ?", feature.Key).First(&feature)
if feature.ID == 0 {
DB.Create(&feature)
}
}
// Add default roles if not exist
adminRole := structs.Role{}
DB.Where("name = ?", "Administrator").First(&adminRole)
if adminRole.ID == 0 {
adminRole = structs.Role{Name: "Administrator", Features: features}
DB.Create(&adminRole)
}
role := structs.Role{}
DB.Where("name = ?", "Member").First(&role)
if role.ID == 0 {
role = structs.Role{Name: "Member"}
DB.Create(&role)
}
// Add the specified default test user if the environment is also not set to production
if environment != "production" && testuser != "" && testpass != "" {
var err error
// ASVS 4.0 point 2.4.4 states cost should be at least 13 https://github.com/OWASP/ASVS/
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(testpass), 14)
utility.ErrorHandler(err, true)
user := structs.User{Email: testuser, Password: string(hashedPassword)}
DB.Where("email = ?", user.Email).First(&user)
if user.ID == 0 {
user.Roles = []*structs.Role{
&adminRole,
}
DB.Create(&user)
}
}
log.Println("should seed", shouldSeed)
// If seeding is enabled we perform the seed with default info
if shouldSeed {
seedData(theme, adminRole)
}
shouldSeed = false
seedEmail = ""
seedPassword = ""
}
func seedData(theme structs.Theme, adminRole structs.Role) {
log.Println("Seeding database")
var err error
// Create a site
site := structs.Site{
Title: "Beubo",
Domain: "beubo.localhost",
Theme: theme,
Type: 1,
}
DB.Create(&site)
// Create a page
content := `<p>Welcome to Beubo! Beubo is a free, simple, and minimal CMS with unlimited extensibility using plugins. This is the default page and can be changed in the admin area for this site.</p>`
content += `<p>Beubo is open source and the project can be found on <a href="https://github.com/uberswe/beubo">Github</a>. If you find any problems or have an idea on how Beubo can be improved, please feel free to <a href="https://github.com/uberswe/beubo/issues">open an issue here</a>.</p>`
content += `<p>Feel free to <a href="https://github.com/uberswe/beubo/pulls">open a pull request</a> if you would like to contribute your own changes.</p>`
content += `<p>For more information on how to use, customize and extend Beubo please see the <a href="https://github.com/uberswe/beubo/wiki">wiki</a></p>`
page := structs.Page{
Model: gorm.Model{},
Title: "Default page",
Content: content,
Slug: "/",
Template: "page",
SiteID: int(site.ID),
}
DB.Create(&page)
// ASVS 4.0 point 2.4.4 states cost should be at least 13 https://github.com/OWASP/ASVS/
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(seedPassword), 14)
utility.ErrorHandler(err, true)
user := structs.User{Email: seedEmail, Password: string(hashedPassword)}
DB.Where("email = ?", user.Email).First(&user)
if user.ID == 0 {
user.Roles = []*structs.Role{
&adminRole,
}
DB.Create(&user)
}
}
func addThemes() (theme structs.Theme) {
// Add initial themes
files, err := ioutil.ReadDir(rootDir)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
// Ignore the install directory, only used for installation
if file.IsDir() && file.Name() != "install" {
theme = structs.Theme{}
DB.Where("slug = ?", file.Name()).First(&theme)
if theme.ID == 0 {
theme = structs.Theme{Slug: file.Name(), Title: file.Name()}
DB.Create(&theme)
}
}
}
return theme
}