-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress.go
175 lines (142 loc) · 5.14 KB
/
wordpress.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
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
package directadmin
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/spf13/cast"
)
type (
WordPressInstall struct {
AdminEmail string `json:"adminEmail" yaml:"adminEmail"`
AdminName string `json:"adminName" yaml:"adminName"`
AdminPass string `json:"adminPass" yaml:"adminPass"`
DbName string `json:"dbName" yaml:"dbName"`
DbPass string `json:"dbPass" yaml:"dbPass"`
DbPrefix string `json:"dbPrefix" yaml:"dbPrefix"`
DbUser string `json:"dbUser" yaml:"dbUser"`
FilePath string `json:"filePath" yaml:"filePath"`
Title string `json:"title" yaml:"title"`
}
WordPressInstallQuick struct {
AdminEmail string `json:"adminEmail" yaml:"adminEmail"`
AdminName string `json:"adminName" yaml:"adminName"`
AdminPass string `json:"adminPass" yaml:"adminPass"`
FilePath string `json:"filePath" yaml:"filePath"`
Title string `json:"title" yaml:"title"`
}
WordPressLocation struct {
FilePath string `json:"filePath"`
Host string `json:"host"`
Id string `json:"id"`
WebPath string `json:"webPath"`
Wordpress struct {
AutoUpdateMajor bool `json:"autoUpdateMajor"`
AutoUpdateMinor bool `json:"autoUpdateMinor"`
Error string `json:"error"`
SiteURL string `json:"siteURL"`
Template string `json:"template"`
Title string `json:"title"`
Version string `json:"version"`
} `json:"wordpress"`
}
WordPressUser struct {
Id int `json:"id"`
DisplayName string `json:"displayName"`
Email string `json:"email"`
Login string `json:"login"`
Registered time.Time `json:"registered"`
Roles []string `json:"roles"`
}
)
// ChangeWordPressUserPassword (user) changes the password of the given wordpress user.
func (c *UserContext) ChangeWordPressUserPassword(locationId string, userId int, password string) error {
var passwordObject struct {
Password string `json:"password"`
}
if password == "" {
return fmt.Errorf("password cannot be empty")
}
passwordObject.Password = password
if _, err := c.makeRequestNew(http.MethodPost, "wordpress/locations/"+locationId+"/users/"+cast.ToString(userId)+"/change-password", passwordObject, nil); err != nil {
return fmt.Errorf("failed to change wordpress user password: %v", err)
}
return nil
}
func (c *UserContext) CreateWordPressInstall(install WordPressInstall, createDatabase bool) error {
if createDatabase {
if err := c.CreateDatabaseWithUser(&DatabaseWithUser{
Database: Database{
Name: install.DbName,
},
Password: install.DbPass,
User: install.DbUser,
}); err != nil {
return fmt.Errorf("failed to create database: %v", err)
}
}
contextUsername := c.GetMyUsername()
dbPrefix := contextUsername + "_"
if !strings.Contains(install.DbName, dbPrefix) {
install.DbName = dbPrefix + install.DbName
}
if !strings.Contains(install.DbUser, dbPrefix) {
install.DbUser = dbPrefix + install.DbUser
}
if !strings.Contains(install.DbPrefix, "_") {
install.DbPrefix = install.DbPrefix + "_"
}
// remove / from the beginning of FilePath if it's there
if install.FilePath[0] == '/' {
install.FilePath = install.FilePath[1:]
}
if _, err := c.makeRequestNew(http.MethodPost, "wordpress/install", install, nil); err != nil {
if createDatabase {
if dbErr := c.DeleteDatabase(install.DbName); dbErr != nil {
err = fmt.Errorf("%v: %v", dbErr, err)
}
}
return err
}
return nil
}
// CreateWordPressInstallQuick (user) creates a new wordpress install and automatically creates a database
func (c *UserContext) CreateWordPressInstallQuick(install WordPressInstallQuick) error {
// remove / from the beginning of FilePath if it's there
if install.FilePath[0] == '/' {
install.FilePath = install.FilePath[1:]
}
if _, err := c.makeRequestNew(http.MethodPost, "wordpress/install-quick", install, nil); err != nil {
return err
}
return nil
}
func (c *UserContext) DeleteWordPressInstall(id string) error {
if _, err := c.makeRequestNew(http.MethodDelete, "wordpress/locations/"+id, nil, nil); err != nil {
return err
}
return nil
}
func (c *UserContext) GetWordPressInstalls() ([]*WordPressLocation, error) {
var wordpressInstalls []*WordPressLocation
if _, err := c.makeRequestNew(http.MethodGet, "wordpress/locations", nil, &wordpressInstalls); err != nil {
return nil, fmt.Errorf("failed to get wordpress installs: %v", err)
}
return wordpressInstalls, nil
}
func (c *UserContext) GetWordPressSSOLink(locationId string, userId int) (string, error) {
var ssoObject struct {
URL string `json:"url"`
}
if _, err := c.makeRequestNew(http.MethodPost, "wordpress/locations/"+locationId+"/users/"+cast.ToString(userId)+"/sso-login", nil, &ssoObject); err != nil {
return "", fmt.Errorf("failed to get wordpress installs: %v", err)
}
return ssoObject.URL, nil
}
func (c *UserContext) GetWordPressUsers(locationId string) ([]*WordPressUser, error) {
var wordpressUsers []*WordPressUser
if _, err := c.makeRequestNew(http.MethodGet, "wordpress/locations/"+locationId+"/users", nil, &wordpressUsers); err != nil {
return nil, fmt.Errorf("failed to get wordpress users: %v", err)
}
return wordpressUsers, nil
}