mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-06 03:31:02 -06:00
38 lines
991 B
Go
38 lines
991 B
Go
package user
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/shroff/phylum/server/internal/core/db"
|
|
"github.com/shroff/phylum/server/internal/core/errors"
|
|
"github.com/shroff/phylum/server/internal/core/util/crypt"
|
|
)
|
|
|
|
var ErrUserExists = errors.NewError(http.StatusNotFound, "user_already_exists", "user already exists")
|
|
|
|
func (m manager) CreateUser(username, displayName, password string, root uuid.UUID) (User, error) {
|
|
if hash, err := crypt.GenerateArgon2EncodedHash(password); err != nil {
|
|
return User{}, err
|
|
} else if u, err := m.db.CreateUser(m.ctx, db.CreateUserParams{
|
|
Username: username,
|
|
DisplayName: displayName,
|
|
PasswordHash: hash,
|
|
Root: root,
|
|
Home: root,
|
|
}); err != nil {
|
|
if strings.Contains(err.Error(), "users_pkey") {
|
|
err = ErrUserExists
|
|
}
|
|
return User{}, err
|
|
} else {
|
|
return User{
|
|
Username: u.Username,
|
|
DisplayName: u.DisplayName,
|
|
Root: u.Root,
|
|
Home: u.Home,
|
|
}, nil
|
|
}
|
|
}
|