Added method to update a user
This commit is contained in:
parent
772ed316cb
commit
4b82af8ae8
|
@ -21,8 +21,8 @@ type User struct {
|
||||||
Password string `xorm:"varchar(250) not null" json:"password"`
|
Password string `xorm:"varchar(250) not null" json:"password"`
|
||||||
Email string `xorm:"varchar(250)" json:"email"`
|
Email string `xorm:"varchar(250)" json:"email"`
|
||||||
IsAdmin bool `xorm:"tinyint(1) not null" json:"isAdmin"`
|
IsAdmin bool `xorm:"tinyint(1) not null" json:"isAdmin"`
|
||||||
Created int64 `xorm:"created" json:"created"`
|
Created int64 `xorm:"created" json:"created"`
|
||||||
Updated int64 `xorm:"updated" json:"updated"`
|
Updated int64 `xorm:"updated" json:"updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserLog logs user actions
|
// UserLog logs user actions
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CreateUser creates a new user and inserts it into the database
|
||||||
|
func CreateUser(user User) (newUser User, err error) {
|
||||||
|
|
||||||
|
newUser = user
|
||||||
|
|
||||||
|
// Check if we have all needed informations
|
||||||
|
if newUser.Password == "" || newUser.Username == "" {
|
||||||
|
return User{}, fmt.Errorf("you need to specify at least a username and a password")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the user already existst
|
||||||
|
_, exists, err := GetUser(User{Name: newUser.Name})
|
||||||
|
if err != nil {
|
||||||
|
return User{}, err
|
||||||
|
}
|
||||||
|
if exists {
|
||||||
|
return User{}, fmt.Errorf("this username is already taken. Please use another")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash the password
|
||||||
|
newUser.Password, err = hashPassword(user.Password)
|
||||||
|
if err != nil {
|
||||||
|
return User{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert it
|
||||||
|
_, err = x.Insert(newUser)
|
||||||
|
if err != nil {
|
||||||
|
return User{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return newUser, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// HashPassword hashes a password
|
||||||
|
func hashPassword(password string) (string, error) {
|
||||||
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
||||||
|
return string(bytes), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateUser updates a user
|
||||||
|
func UpdateUser(user User) (updatedUser User, err error) {
|
||||||
|
// Check if we have at least a username
|
||||||
|
if user.Username == "" {
|
||||||
|
return User{}, fmt.Errorf("you need to specify at least a username and a password")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it exists
|
||||||
|
theUser, exists, err := GetUserByID(user.ID)
|
||||||
|
if exists {
|
||||||
|
|
||||||
|
user.Password = theUser.Password // set the password to the one in the database to not accedently resetting it
|
||||||
|
|
||||||
|
// Update it
|
||||||
|
_, err = x.Id(user.ID).Update(user)
|
||||||
|
if err != nil {
|
||||||
|
return User{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return User{}, fmt.Errorf("This user does not exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateUserPassword updates the password of a user
|
||||||
|
func UpdateUserPassword(userID int64, newPassword string) (err error) {
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -19,14 +19,14 @@ func TestCreateUser(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete every preexisting user to have a fresh start
|
// Delete every preexisting user to have a fresh start
|
||||||
/* allusers, err := ListUsers("")
|
/* allusers, err := ListUsers("")
|
||||||
for _, user := range allusers {
|
for _, user := range allusers {
|
||||||
// Delete it
|
// Delete it
|
||||||
assert.Equal(t, dummyuser.Name, user.Name)
|
assert.Equal(t, dummyuser.Name, user.Name)
|
||||||
assert.Equal(t, dummyuser.Username, user.Username)
|
assert.Equal(t, dummyuser.Username, user.Username)
|
||||||
assert.Equal(t, dummyuser.Email, user.Email)
|
assert.Equal(t, dummyuser.Email, user.Email)
|
||||||
assert.Equal(t, dummyuser.IsAdmin, user.IsAdmin)
|
assert.Equal(t, dummyuser.IsAdmin, user.IsAdmin)
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
// Create a new user
|
// Create a new user
|
||||||
createdUser, err := CreateUser(dummyuser)
|
createdUser, err := CreateUser(dummyuser)
|
||||||
|
|
|
@ -3,8 +3,8 @@ package v1
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
|
||||||
"git.mowie.cc/konrad/Library/models"
|
"git.mowie.cc/konrad/Library/models"
|
||||||
|
"github.com/labstack/echo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func UsersList(c echo.Context) error {
|
func UsersList(c echo.Context) error {
|
||||||
|
@ -24,4 +24,4 @@ func UsersList(c echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, list)
|
return c.JSON(http.StatusOK, list)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue