-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
137 lines (122 loc) · 4.09 KB
/
account.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
package account
import (
"strconv"
"github.com/go-chi/chi"
"github.com/qor/admin"
"github.com/qor/qor"
"github.com/qor/qor-example/config/application"
"github.com/qor/qor-example/config/auth"
"github.com/qor/qor-example/models/users"
"github.com/qor/qor-example/utils/funcmapmaker"
"github.com/qor/qor/resource"
qorutils "github.com/qor/qor/utils"
"github.com/qor/render"
"github.com/qor/validations"
"golang.org/x/crypto/bcrypt"
)
// New new home app
func New(config *Config) *App {
return &App{Config: config}
}
// App home app
type App struct {
Config *Config
}
// Config home config struct
type Config struct {
}
// ConfigureApplication configure application
func (app App) ConfigureApplication(application *application.Application) {
controller := &Controller{View: render.New(&render.Config{AssetFileSystem: application.AssetFS.NameSpace("account")}, "app/account/views")}
funcmapmaker.AddFuncMapMaker(controller.View)
app.ConfigureAdmin(application.Admin)
application.Router.Mount("/auth/", auth.Auth.NewServeMux())
application.Router.With(auth.Authority.Authorize()).Route("/account", func(r chi.Router) {
r.Get("/", controller.Orders)
r.With(auth.Authority.Authorize("logged_in_half_hour")).Post("/add_user_credit", controller.AddCredit)
r.Get("/profile", controller.Profile)
r.Post("/profile", controller.Update)
})
}
// ConfigureAdmin configure admin interface
func (App) ConfigureAdmin(Admin *admin.Admin) {
Admin.AddMenu(&admin.Menu{Name: "User Management", Priority: 3})
user := Admin.AddResource(&users.User{}, &admin.Config{Menu: []string{"User Management"}})
user.Meta(&admin.Meta{Name: "Gender", Config: &admin.SelectOneConfig{Collection: []string{"Male", "Female", "Unknown"}}})
user.Meta(&admin.Meta{Name: "Birthday", Type: "date"})
user.Meta(&admin.Meta{Name: "Role", Config: &admin.SelectOneConfig{Collection: []string{"Admin", "Maintainer", "Member"}}})
user.Meta(&admin.Meta{Name: "Password",
Type: "password",
Valuer: func(interface{}, *qor.Context) interface{} { return "" },
Setter: func(resource interface{}, metaValue *resource.MetaValue, context *qor.Context) {
if newPassword := qorutils.ToString(metaValue.Value); newPassword != "" {
bcryptPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
if err != nil {
context.DB.AddError(validations.NewError(user, "Password", "Can't encrpt password"))
return
}
u := resource.(*users.User)
u.Password = string(bcryptPassword)
}
},
})
user.Meta(&admin.Meta{Name: "Confirmed", Valuer: func(user interface{}, ctx *qor.Context) interface{} {
if user.(*users.User).ID == 0 {
return true
}
return user.(*users.User).Confirmed
}})
user.Meta(&admin.Meta{Name: "DefaultBillingAddress", Config: &admin.SelectOneConfig{Collection: userAddressesCollection}})
user.Meta(&admin.Meta{Name: "DefaultShippingAddress", Config: &admin.SelectOneConfig{Collection: userAddressesCollection}})
user.Filter(&admin.Filter{
Name: "Role",
Config: &admin.SelectOneConfig{
Collection: []string{"Admin", "Maintainer", "Member"},
},
})
user.IndexAttrs("ID", "Email", "Name", "Gender", "Role", "Balance")
user.ShowAttrs(
&admin.Section{
Title: "Basic Information",
Rows: [][]string{
{"Name"},
{"Email", "Password"},
{"Avatar"},
{"Gender", "Role", "Birthday"},
{"Confirmed"},
},
},
&admin.Section{
Title: "Credit Information",
Rows: [][]string{
{"Balance"},
},
},
&admin.Section{
Title: "Accepts",
Rows: [][]string{
{"AcceptPrivate", "AcceptLicense", "AcceptNews"},
},
},
&admin.Section{
Title: "Default Addresses",
Rows: [][]string{
{"DefaultBillingAddress"},
{"DefaultShippingAddress"},
},
},
"Addresses",
)
user.EditAttrs(user.ShowAttrs())
}
func userAddressesCollection(resource interface{}, context *qor.Context) (results [][]string) {
var (
user users.User
DB = context.DB
)
DB.Preload("Addresses").Where(context.ResourceID).First(&user)
for _, address := range user.Addresses {
results = append(results, []string{strconv.Itoa(int(address.ID)), address.Stringify()})
}
return
}