2020-12-04 23:41:26 +01:00
|
|
|
package db
|
2016-01-05 00:32:53 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-10-30 18:18:23 +01:00
|
|
|
// User is the model for an entity which has access to the API
|
2016-01-05 00:32:53 +01:00
|
|
|
type User struct {
|
2023-07-08 12:41:57 +02:00
|
|
|
ID int `db:"id" json:"id"`
|
|
|
|
Created time.Time `db:"created" json:"created"`
|
|
|
|
Username string `db:"username" json:"username" binding:"required"`
|
|
|
|
Name string `db:"name" json:"name" binding:"required"`
|
|
|
|
Email string `db:"email" json:"email" binding:"required"`
|
|
|
|
Password string `db:"password" json:"-"` // password hash
|
|
|
|
Admin bool `db:"admin" json:"admin"`
|
|
|
|
External bool `db:"external" json:"external"`
|
|
|
|
Alert bool `db:"alert" json:"alert"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserWithProjectRole struct {
|
|
|
|
Role ProjectUserRole `db:"role" json:"role"`
|
|
|
|
User
|
2016-01-05 00:32:53 +01:00
|
|
|
}
|
2021-03-12 22:13:39 +01:00
|
|
|
|
2021-08-28 13:44:41 +02:00
|
|
|
// UserWithPwd extends User structure with field for unhashed password received from JSON.
|
2021-03-12 22:13:39 +01:00
|
|
|
type UserWithPwd struct {
|
2021-12-18 14:16:34 +01:00
|
|
|
Pwd string `db:"-" json:"password"` // unhashed password from JSON
|
2021-03-12 22:13:39 +01:00
|
|
|
User
|
|
|
|
}
|
2021-10-12 13:37:51 +02:00
|
|
|
|
2021-12-18 14:16:34 +01:00
|
|
|
func ValidateUser(user User) error {
|
2022-10-30 18:18:23 +01:00
|
|
|
if user.Username == "" {
|
|
|
|
return &ValidationError{Message: "Username cannot be empty"}
|
|
|
|
}
|
|
|
|
if user.Email == "" {
|
|
|
|
return &ValidationError{Message: "Email cannot be empty"}
|
|
|
|
}
|
|
|
|
if user.Name == "" {
|
|
|
|
return &ValidationError{Message: "Name cannot be empty"}
|
|
|
|
}
|
2021-10-12 13:37:51 +02:00
|
|
|
return nil
|
|
|
|
}
|