Added method to update a user

This commit is contained in:
konrad 2018-01-23 12:59:48 +01:00 committed by kolaente
parent 772ed316cb
commit 4b82af8ae8
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 86 additions and 12 deletions

View File

@ -21,8 +21,8 @@ type User struct {
Password string `xorm:"varchar(250) not null" json:"password"`
Email string `xorm:"varchar(250)" json:"email"`
IsAdmin bool `xorm:"tinyint(1) not null" json:"isAdmin"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
}
// UserLog logs user actions

74
models/user_add_update.go Normal file
View File

@ -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
}

View File

@ -19,14 +19,14 @@ func TestCreateUser(t *testing.T) {
}
// Delete every preexisting user to have a fresh start
/* allusers, err := ListUsers("")
for _, user := range allusers {
// Delete it
assert.Equal(t, dummyuser.Name, user.Name)
assert.Equal(t, dummyuser.Username, user.Username)
assert.Equal(t, dummyuser.Email, user.Email)
assert.Equal(t, dummyuser.IsAdmin, user.IsAdmin)
}*/
/* allusers, err := ListUsers("")
for _, user := range allusers {
// Delete it
assert.Equal(t, dummyuser.Name, user.Name)
assert.Equal(t, dummyuser.Username, user.Username)
assert.Equal(t, dummyuser.Email, user.Email)
assert.Equal(t, dummyuser.IsAdmin, user.IsAdmin)
}*/
// Create a new user
createdUser, err := CreateUser(dummyuser)

View File

@ -3,8 +3,8 @@ package v1
import (
"net/http"
"github.com/labstack/echo"
"git.mowie.cc/konrad/Library/models"
"github.com/labstack/echo"
)
func UsersList(c echo.Context) error {
@ -24,4 +24,4 @@ func UsersList(c echo.Context) error {
}
return c.JSON(http.StatusOK, list)
}
}