-
Notifications
You must be signed in to change notification settings - Fork 10
/
actions.go
127 lines (116 loc) · 3.55 KB
/
actions.go
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
// apcore is a server framework for implementing an ActivityPub application.
// Copyright (C) 2020 Cory Slep
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package apcore
import (
"context"
"github.com/go-fed/apcore/app"
"github.com/go-fed/apcore/framework"
"github.com/go-fed/apcore/services"
"github.com/go-fed/apcore/util"
)
func doCreateTables(configFilePath string, a app.Application, debug bool, scheme string) error {
db, d, ms, cfg, err := newModels(configFilePath, a, debug, scheme)
if err != nil {
return err
}
defer db.Close()
tx, err := db.BeginTx(context.Background(), nil)
if err != nil {
return err
}
defer tx.Rollback()
for _, m := range ms {
if err := m.CreateTable(tx, d); err != nil {
return err
}
}
if err = tx.Commit(); err != nil {
return err
}
return a.CreateTables(context.Background(), &services.Any{db}, cfg, debug)
}
func doInitAdmin(configFilePath string, a app.Application, debug bool, scheme string) error {
db, users, c, err := newUserService(configFilePath, a, debug, scheme)
if err != nil {
return err
}
// Prompt for admin information
p := services.CreateUserParameters{
Scheme: scheme,
Host: c.ServerConfig.Host,
RSAKeySize: c.ServerConfig.RSAKeySize,
HashParams: services.HashPasswordParameters{
SaltSize: c.ServerConfig.SaltSize,
BCryptStrength: c.ServerConfig.BCryptStrength,
},
}
var password string
p.Username, p.Email, password, err = framework.PromptAdminUser()
// Create the user in the database
defer db.Close()
tx, err := db.BeginTx(context.Background(), nil)
if err != nil {
return err
}
defer tx.Rollback()
userID, err := users.CreateAdminUser(util.Context{context.Background()}, p, password)
if err != nil {
return err
}
if err = tx.Commit(); err != nil {
return err
}
return a.OnCreateAdminUser(context.Background(), userID, &services.Any{db}, c)
}
func doInitData(configFilePath string, a app.Application, debug bool, scheme string) error {
db, users, c, err := newUserService(configFilePath, a, debug, scheme)
if err != nil {
return err
}
// Create the server actor in the database
defer db.Close()
tx, err := db.BeginTx(context.Background(), nil)
if err != nil {
return err
}
defer tx.Rollback()
_, err = users.CreateInstanceActorSingleton(util.Context{context.Background()}, scheme, c.ServerConfig.Host, c.ServerConfig.RSAKeySize)
if err != nil {
return err
}
return tx.Commit()
}
func doInitServerProfile(configFilePath string, a app.Application, debug bool, scheme string) error {
db, users, c, err := newUserService(configFilePath, a, debug, scheme)
if err != nil {
return err
}
defer db.Close()
sp, err := framework.PromptServerProfile(scheme, c.ServerConfig.Host)
if err != nil {
return err
}
tx, err := db.BeginTx(context.Background(), nil)
if err != nil {
return err
}
defer tx.Rollback()
err = users.SetServerPreferences(util.Context{context.Background()}, sp)
if err != nil {
return err
}
return tx.Commit()
}