-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommands.go
130 lines (103 loc) · 2.76 KB
/
commands.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
/*
Set of commands that can be run from command line.
*/
package core
import (
"errors"
"fmt"
"os/user"
"github.com/asaskevich/govalidator"
)
/*
Command interface that all commands use
*/
type Command interface {
// Run runs command
Run() error
}
/*
CreateAdminCommand creates new admin in database
*/
type CreateAdminCommand struct {
Config Config
}
/*
Run is method that runs the command
*/
func (c *CreateAdminCommand) Run() error {
u, err := user.Current()
if err != nil {
return err
}
user := User{}
user.Username = TerminalGetStringValue("Please enter username", u.Username)
if c.Config.Manager().User().ExistsUsername(user.Username) {
return fmt.Errorf("User with username %s already exists", user.Username)
}
user.Email = TerminalGetStringValue("Please enter email")
if user.Email != "" {
if !govalidator.IsEmail(user.Email) {
return fmt.Errorf("Invalid email %s", user.Email)
}
if c.Config.Manager().User().ExistsEmail(user.Email) {
return fmt.Errorf("User with email %s already exists", user.Email)
}
}
// Get password
password1 := TerminalGetPasswordValue("Please enter password")
if password1 == "" {
return errors.New("No password supplied")
}
password2 := TerminalGetPasswordValue("Please retype password")
if password1 != password2 {
return errors.New("Passwords don't match")
}
// set password
c.Config.Manager().User().SetPassword(&user, password1)
user.IsActive = true
user.IsAdmin = true
if errSave := c.Config.DB().Save(&user).Error; errSave != nil {
return errSave
}
println("Admin has been succesfully created.")
return nil
}
/*
ChangePasswordCommand command line command to change password
*/
type ChangePasswordCommand struct {
Config Config
}
/*
Run
*/
func (c *ChangePasswordCommand) Run() (err error) {
u, err := user.Current()
if err != nil {
return err
}
user := User{}
user.Username = TerminalGetStringValue("Please enter username", u.Username)
if !c.Config.Manager().User().ExistsUsername(user.Username) {
return fmt.Errorf("User with username %s doesn't exist.", user.Username)
}
if c.Config.Manager().User().Get(&user).RecordNotFound() {
return fmt.Errorf("User with username %s doesn't exist.", user.Username)
}
// Get password
password1 := TerminalGetPasswordValue("Please enter password")
if password1 == "" {
return errors.New("No password supplied")
}
password2 := TerminalGetPasswordValue("Please retype password")
if password1 != password2 {
return errors.New("Passwords don't match")
}
// set password
c.Config.Manager().User().SetPassword(&user, password1)
if err = c.Config.DB().Save(&user).Error; err != nil {
return fmt.Errorf("Error when saving user to database: %s", err.Error())
}
println("Password successfully changed for user", user.Username, ".")
return
}