2021-09-13 11:16:25 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.ctrlz.es/mgdelacroix/craban/model"
|
|
|
|
"git.ctrlz.es/mgdelacroix/craban/utils"
|
|
|
|
)
|
|
|
|
|
2021-09-13 13:52:00 +01:00
|
|
|
func (a *App) CreateUser(username, password, name, mail string, admin bool) (*model.User, error) {
|
2021-09-13 11:16:25 +01:00
|
|
|
hashedPassword, err := utils.Encrypt(password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create user: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newUser := &model.User{
|
|
|
|
Username: username,
|
|
|
|
Password: hashedPassword,
|
|
|
|
Name: name,
|
|
|
|
Mail: mail,
|
2021-09-13 13:52:00 +01:00
|
|
|
Admin: admin,
|
2021-09-13 11:16:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := newUser.IsValid(); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid user for creation: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:52:00 +01:00
|
|
|
return a.Store.User().Create(newUser)
|
2021-09-13 11:16:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) ListUsers() ([]*model.User, error) {
|
2021-09-13 13:52:00 +01:00
|
|
|
return a.Store.User().List()
|
2021-09-13 11:16:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) DeleteUserByUsername(username string) error {
|
2021-09-13 13:52:00 +01:00
|
|
|
return a.Store.User().DeleteByUsername(username)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) GetUserByID(userID int) (*model.User, error) {
|
|
|
|
return a.Store.User().GetByID(userID)
|
2021-09-13 11:16:25 +01:00
|
|
|
}
|