This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsuperadmin.go
132 lines (124 loc) · 3.51 KB
/
superadmin.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
package uadmin
import (
"errors"
"fmt"
"github.com/asaskevich/govalidator"
"github.com/jessevdk/go-flags"
"github.com/miquella/ask"
utils2 "github.com/sergeyglazyrindev/uadmin/blueprint/auth/utils"
userblueprint "github.com/sergeyglazyrindev/uadmin/blueprint/user"
"github.com/sergeyglazyrindev/uadmin/core"
"os"
)
type SuperadminCommand struct {
}
func (c SuperadminCommand) Proceed(subaction string, args []string) error {
var action string
var help string
var isCorrectActionPassed bool = false
commandRegistry := &core.CommandRegistry{
Actions: make(map[string]core.ICommand),
}
commandRegistry.AddAction("create", &CreateSuperadmin{})
if len(os.Args) > 2 {
action = os.Args[2]
isCorrectActionPassed = commandRegistry.IsRegisteredCommand(action)
}
if !isCorrectActionPassed {
helpText := commandRegistry.MakeHelpText()
help = fmt.Sprintf(`
Please provide what do you want to do ?
%s
`, helpText)
fmt.Print(help)
return nil
}
return commandRegistry.RunAction(subaction, "", args)
}
func (c SuperadminCommand) GetHelpText() string {
return "Manage your superusers"
}
type SuperadminCommandOptions struct {
Username string `short:"n" required:"true" description:"Username" valid:"username-uadmin,username-unique"`
Email string `short:"e" required:"true" description:"Email'" valid:"email,email-unique"`
FirstName string `short:"f" required:"false" description:"First name'"`
LastName string `short:"l" required:"false" description:"Last name'"`
}
type CreateSuperadmin struct {
}
func (command CreateSuperadmin) Proceed(subaction string, args []string) error {
var opts = &SuperadminCommandOptions{}
parser := flags.NewParser(opts, flags.Default)
var err error
_, err = parser.ParseArgs(args)
if len(args) == 0 {
var help string = `
Please provide flags -n and -e which are username and email of the user respectively
`
fmt.Printf(help)
return nil
}
if err != nil {
return err
}
_, err = govalidator.ValidateStruct(opts)
if err != nil {
return err
}
uadminDatabase := core.NewUadminDatabase()
defer uadminDatabase.Close()
db := uadminDatabase.Db
if opts.FirstName == "" {
opts.FirstName = "System"
}
if opts.LastName == "" {
opts.LastName = "Admin"
}
err = ask.Print("Warning! I am about to ask you for a password!\n")
if err != nil {
return err
}
var password string
for true {
password, err = ask.HiddenAsk("Password: ")
if err != nil {
return err
}
confirmpassword, err := ask.HiddenAsk("Confirm Password: ")
if err != nil {
return err
}
passwordValidationStruct := &userblueprint.PasswordValidationStruct{
Password: password,
ConfirmedPassword: confirmpassword,
}
_, err = govalidator.ValidateStruct(passwordValidationStruct)
if err != nil {
core.Trail(core.ERROR, errors.New("please try to to repeat password again"))
continue
}
break
}
salt := core.GenerateRandomString(appInstance.Config.D.Auth.SaltLength)
// hashedPassword, err := utils2.HashPass(password, salt)
hashedPassword, err := utils2.HashPass(password, salt)
if err != nil {
return err
}
admin := core.GenerateUserModel()
admin.SetFirstName(opts.FirstName)
admin.SetLastName(opts.LastName)
admin.SetUsername(opts.Username)
admin.SetEmail(opts.Email)
admin.SetPassword(hashedPassword)
admin.SetActive(true)
admin.SetIsSuperUser(true)
admin.SetSalt(salt)
admin.SetIsPasswordUsable(true)
db.Create(admin)
core.Trail(core.INFO, "Superuser created successfully")
return nil
}
func (command CreateSuperadmin) GetHelpText() string {
return "Create superadmin"
}