Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding email to users #14

Merged
merged 1 commit into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/admin/handlers-post.go
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ func usersPOSTHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("DebugService: %s %v", responseMessage, err)
}
} else {
newUser, err := adminUsers.New(u.Username, u.Password, u.Fullname, u.Admin)
newUser, err := adminUsers.New(u.Username, u.Password, u.Email, u.Fullname, u.Admin)
if err != nil {
responseMessage = "error with new user"
responseCode = http.StatusInternalServerError
Expand Down
5 changes: 4 additions & 1 deletion cmd/admin/static/js/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function addUser() {
$("#user_username").val('');
$("#user_email").val('');
$("#user_fullname").val('');
$("#user_password").val('');
$("#addUserModal").modal();
Expand All @@ -11,16 +12,18 @@ function confirmAddUser() {
var _url = window.location.pathname;

var _username = $("#user_username").val();
var _email = $("#user_email").val();
var _fullname = $("#user_fullname").val();
var _password = $("#user_password").val();

var data = {
csrftoken: _csrftoken,
action: 'add',
username: _username,
email: _email,
fullname: _fullname,
password: _password,
admin: true
admin: false
};
sendPostRequest(data, _url, _url, false);
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/admin/templates/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
<thead>
<tr>
<th width="10%">Username</th>
<th width="20%">Fullname</th>
<th width="20%">Last IP</th>
<th width="15%">Email</th>
<th width="15%">Fullname</th>
<th width="10%">Last IP</th>
<th width="30%">Last UserAgent</th>
<th width="5%">Admin</th>
<th width="10%">Last Session</th>
Expand All @@ -53,6 +54,7 @@
{{range $i, $e := $.CurrentUsers}}
<tr>
<td>{{ $e.Username }}</td>
<td>{{ $e.Email }}</td>
<td>{{ $e.Fullname }}</td>
<td>{{ $e.LastIPAddress }}</td>
<td>{{ $e.LastUserAgent }}</td>
Expand Down Expand Up @@ -97,6 +99,10 @@ <h4 class="modal-title">Add new user</h4>

</div>
<div class="form-group row">
<label class="col-md-2 col-form-label" for="user_email">Email: </label>
<div class="col-md-4">
<input class="form-control" name="user_email" id="user_email" type="text" autocomplete="off">
</div>
<label class="col-md-2 col-form-label" for="user_fullname">Fullname: </label>
<div class="col-md-4">
<input class="form-control" name="user_fullname" id="user_fullname" type="text" autocomplete="off">
Expand Down
1 change: 1 addition & 0 deletions cmd/admin/types-requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type UsersRequest struct {
CSRFToken string `json:"csrftoken"`
Action string `json:"action"`
Username string `json:"username"`
Email string `json:"email"`
Fullname string `json:"fullname"`
Password string `json:"password"`
Admin bool `json:"admin"`
Expand Down
8 changes: 8 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func init() {
Hidden: false,
Usage: "Make this user an admin",
},
cli.StringFlag{
Name: "email, e",
Usage: "Email for the new user",
},
cli.StringFlag{
Name: "fullname, n",
Usage: "Full name for the new user",
Expand All @@ -103,6 +107,10 @@ func init() {
Name: "password, p",
Usage: "New password to be used",
},
cli.StringFlag{
Name: "email, e",
Usage: "Email to be used",
},
cli.StringFlag{
Name: "fullname, n",
Usage: "Full name to be used",
Expand Down
9 changes: 8 additions & 1 deletion cmd/cli/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ func addUser(c *cli.Context) error {
os.Exit(1)
}
password := c.String("password")
email := c.String("email")
fullname := c.String("fullname")
admin := c.Bool("admin")
user, err := adminUsers.New(username, password, fullname, admin)
user, err := adminUsers.New(username, password, email, fullname, admin)
if err != nil {
return err
}
Expand All @@ -46,6 +47,12 @@ func editUser(c *cli.Context) error {
return err
}
}
email := c.String("email")
if email != "" {
if err := adminUsers.ChangeEmail(username, email); err != nil {
return err
}
}
fullname := c.String("fullname")
if fullname != "" {
if err := adminUsers.ChangeFullname(username, fullname); err != nil {
Expand Down
26 changes: 13 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ require (
github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.1.3
github.com/jinzhu/gorm v1.9.10
github.com/jmpsec/osctrl/pkg/carves v0.1.7
github.com/jmpsec/osctrl/pkg/environments v0.1.7
github.com/jmpsec/osctrl/pkg/metrics v0.1.7
github.com/jmpsec/osctrl/pkg/nodes v0.1.7
github.com/jmpsec/osctrl/pkg/queries v0.1.7
github.com/jmpsec/osctrl/pkg/settings v0.1.7
github.com/jmpsec/osctrl/pkg/types v0.1.7
github.com/jmpsec/osctrl/pkg/users v0.1.7
github.com/jmpsec/osctrl/pkg/utils v0.1.7
github.com/jmpsec/osctrl/plugins/db_logging v0.1.7 // indirect
github.com/jmpsec/osctrl/plugins/graylog_logging v0.1.7 // indirect
github.com/jmpsec/osctrl/plugins/logging_dispatcher v0.1.7 // indirect
github.com/jmpsec/osctrl/plugins/splunk_logging v0.1.7 // indirect
github.com/jmpsec/osctrl/pkg/carves v0.1.8
github.com/jmpsec/osctrl/pkg/environments v0.1.8
github.com/jmpsec/osctrl/pkg/metrics v0.1.8
github.com/jmpsec/osctrl/pkg/nodes v0.1.8
github.com/jmpsec/osctrl/pkg/queries v0.1.8
github.com/jmpsec/osctrl/pkg/settings v0.1.8
github.com/jmpsec/osctrl/pkg/types v0.1.8
github.com/jmpsec/osctrl/pkg/users v0.1.8
github.com/jmpsec/osctrl/pkg/utils v0.1.8
github.com/jmpsec/osctrl/plugins/db_logging v0.1.8 // indirect
github.com/jmpsec/osctrl/plugins/graylog_logging v0.1.8 // indirect
github.com/jmpsec/osctrl/plugins/logging_dispatcher v0.1.8 // indirect
github.com/jmpsec/osctrl/plugins/splunk_logging v0.1.8 // indirect
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/olekukonko/tablewriter v0.0.1
github.com/russellhaering/goxmldsig v0.0.0-20180430223755-7acd5e4a6ef7 // indirect
Expand Down
2 changes: 1 addition & 1 deletion pkg/queries/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/jmpsec/osctrl/pkg/queries
go 1.12

require (
github.com/jmpsec/osctrl/pkg/nodes v0.1.7
github.com/jmpsec/osctrl/pkg/nodes v0.1.8
github.com/jinzhu/gorm v1.9.8
)
2 changes: 1 addition & 1 deletion pkg/types/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/jmpsec/osctrl/pkg/types

go 1.12

require github.com/jmpsec/osctrl/pkg/queries v0.1.7
require github.com/jmpsec/osctrl/pkg/queries v0.1.8
18 changes: 17 additions & 1 deletion pkg/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type AdminUser struct {
gorm.Model
Username string `gorm:"index"`
Email string
Fullname string
PassHash string
Admin bool
Expand Down Expand Up @@ -87,7 +88,7 @@ func (m *UserManager) Create(user AdminUser) error {
}

// New empty user
func (m *UserManager) New(username, password, fullname string, admin bool) (AdminUser, error) {
func (m *UserManager) New(username, password, email, fullname string, admin bool) (AdminUser, error) {
if !m.Exists(username) {
passhash, err := m.HashPasswordWithSalt(password)
if err != nil {
Expand All @@ -97,6 +98,7 @@ func (m *UserManager) New(username, password, fullname string, admin bool) (Admi
Username: username,
PassHash: passhash,
Admin: admin,
Email: email,
Fullname: fullname,
}, nil
}
Expand Down Expand Up @@ -170,6 +172,20 @@ func (m *UserManager) ChangePassword(username, password string) error {
return nil
}

// ChangeEmail for user by username
func (m *UserManager) ChangeEmail(username, email string) error {
user, err := m.Get(username)
if err != nil {
return fmt.Errorf("error getting user %v", err)
}
if email != user.Email {
if err := m.DB.Model(&user).Update("email", email).Error; err != nil {
return fmt.Errorf("Update %v", err)
}
}
return nil
}

// ChangeFullname for user by username
func (m *UserManager) ChangeFullname(username, fullname string) error {
user, err := m.Get(username)
Expand Down
2 changes: 1 addition & 1 deletion plugins/db_logging/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/jmpsec/osctrl/plugins/db_logging
go 1.12

require (
github.com/jmpsec/osctrl/pkg/types v0.1.7
github.com/jmpsec/osctrl/pkg/types v0.1.8
github.com/jinzhu/gorm v1.9.10
)
2 changes: 1 addition & 1 deletion plugins/graylog_logging/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/jmpsec/osctrl/plugins/graylog_logging

go 1.12

require github.com/jmpsec/osctrl/pkg/utils v0.1.7
require github.com/jmpsec/osctrl/pkg/utils v0.1.8
4 changes: 2 additions & 2 deletions plugins/logging_dispatcher/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/jmpsec/osctrl/plugins/logging_dispatcher
go 1.12

require (
github.com/jmpsec/osctrl/pkg/settings v0.1.7
github.com/jmpsec/osctrl/pkg/types v0.1.7
github.com/jmpsec/osctrl/pkg/settings v0.1.8
github.com/jmpsec/osctrl/pkg/types v0.1.8
github.com/jinzhu/gorm v1.9.8
github.com/spf13/viper v1.4.0
)
4 changes: 2 additions & 2 deletions plugins/splunk_logging/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/jmpsec/osctrl/plugins/splunk_logging
go 1.12

require (
github.com/jmpsec/osctrl/pkg/types v0.1.7
github.com/jmpsec/osctrl/pkg/utils v0.1.7
github.com/jmpsec/osctrl/pkg/types v0.1.8
github.com/jmpsec/osctrl/pkg/utils v0.1.8
)