Commit 5379acd1 authored by Julien Schröter's avatar Julien Schröter Committed by Julien Schröter

Create repository method to fetch userdata incl an hash of the password

parent 53d71c6d
......@@ -31,6 +31,7 @@ type UserQuery interface {
Insert(user *schemas.FullUserSchema, password string) (id uint32, err error)
Select(id uint32) ([]schemas.FullUserSchema, error)
SelectByUsername(username string) ([]schemas.InfoUserSchema, error)
SelectCredentialsByField(field UserField, value interface{}) (*schemas.CredentialsSchema, error)
Authenticate(name string, password string) (uint32, error)
Update(userID uint32, params PatchUserRequest, titleRepository title.TitleQuery) error
UpdatePassword(userID uint32, password string) error
......@@ -38,6 +39,12 @@ type UserQuery interface {
VerifyUser(uint32, string) error
}
type UserField string
const FieldID UserField = "iduser"
const FieldUsername UserField = "username"
const FieldEmail UserField = "email"
type MySQLUserQuery struct{}
/*
......@@ -236,6 +243,23 @@ func (MySQLUserQuery) Select(id uint32) ([]schemas.FullUserSchema, error) {
return users, nil
}
func (MySQLUserQuery) SelectCredentialsByField(field UserField, value interface{}) (*schemas.CredentialsSchema, error) {
db, err := dbhandler.GetDBConnection()
if err != nil {
return nil, err
}
row := db.QueryRow(fmt.Sprintf("SELECT iduser, username, email, SHA2(password, 512) FROM `user` WHERE %s=?", field), value)
var result schemas.CredentialsSchema
if err := row.Scan(&result.ID, &result.Username, &result.Email, &result.PasswordCipher); err != nil {
return nil, err
}
return &result, nil
}
func (MySQLUserQuery) SelectByUsername(username string) ([]schemas.InfoUserSchema, error) {
db, err := dbhandler.GetDBConnection()
if err != nil {
......
......@@ -23,6 +23,13 @@ type FullUserSchema struct {
Verified bool `json:"verified,omitempty"`
}
type CredentialsSchema struct {
ID uint32
Username string
Email string
PasswordCipher string
}
// InfoUserFromFullUser creates and returns an InfoUserSchema struct from a given FullUserSchema
func InfoUserFromFullUser(user FullUserSchema) InfoUserSchema {
return InfoUserSchema{
......
......@@ -107,6 +107,10 @@ var errBadRequest = user.ErrBadRequest
type mockUser struct {
}
func (mockUser) SelectCredentialsByField(field user.UserField, value interface{}) (*schemas.CredentialsSchema, error) {
return nil, errors.New("method SelectCredentialsByField not implemented")
}
func (mockUser) Insert(user *schemas.FullUserSchema, password string) (id uint32, err error) {
if _, exists := getMockUserByName(user.Username); exists {
return 0, errors.New("User already exists")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment