mirror of
https://github.com/pushbits/server.git
synced 2025-05-28 08:16:35 +02:00
Restructure project layout
This commit is contained in:
parent
a49db216d5
commit
9a4a096526
32 changed files with 35 additions and 35 deletions
86
internal/model/user.go
Normal file
86
internal/model/user.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/pushbits/server/internal/authentication/credentials"
|
||||
)
|
||||
|
||||
// User holds information like the name, the secret, and the applications of a user.
|
||||
type User struct {
|
||||
ID uint `gorm:"AUTO_INCREMENT;primary_key"`
|
||||
Name string `gorm:"type:string;size:128;unique"`
|
||||
PasswordHash []byte
|
||||
IsAdmin bool
|
||||
MatrixID string `gorm:"type:string"`
|
||||
Applications []Application
|
||||
}
|
||||
|
||||
// ExternalUser represents a user for external purposes.
|
||||
type ExternalUser struct {
|
||||
ID uint `json:"id"`
|
||||
Name string `json:"name" form:"name" query:"name" binding:"required"`
|
||||
IsAdmin bool `json:"is_admin" form:"is_admin" query:"is_admin"`
|
||||
MatrixID string `json:"matrix_id" form:"matrix_id" query:"matrix_id" binding:"required"`
|
||||
}
|
||||
|
||||
// UserCredentials holds information for authenticating a user.
|
||||
type UserCredentials struct {
|
||||
Password string `json:"password,omitempty" form:"password" query:"password" binding:"required"`
|
||||
}
|
||||
|
||||
// CreateUser is used to process queries for creating users.
|
||||
type CreateUser struct {
|
||||
ExternalUser
|
||||
UserCredentials
|
||||
}
|
||||
|
||||
// NewUser creates a new user.
|
||||
func NewUser(cm *credentials.Manager, name, password string, isAdmin bool, matrixID string) (*User, error) {
|
||||
log.Printf("Creating user %s.\n", name)
|
||||
|
||||
passwordHash, err := cm.CreatePasswordHash(password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &User{
|
||||
Name: name,
|
||||
PasswordHash: passwordHash,
|
||||
IsAdmin: isAdmin,
|
||||
MatrixID: matrixID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// IntoInternalUser converts a CreateUser into a User.
|
||||
func (u *CreateUser) IntoInternalUser(cm *credentials.Manager) (*User, error) {
|
||||
passwordHash, err := cm.CreatePasswordHash(u.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &User{
|
||||
Name: u.Name,
|
||||
PasswordHash: passwordHash,
|
||||
IsAdmin: u.IsAdmin,
|
||||
MatrixID: u.MatrixID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// IntoExternalUser converts a User into a ExternalUser.
|
||||
func (u *User) IntoExternalUser() *ExternalUser {
|
||||
return &ExternalUser{
|
||||
ID: u.ID,
|
||||
Name: u.Name,
|
||||
IsAdmin: u.IsAdmin,
|
||||
MatrixID: u.MatrixID,
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateUser is used to process queries for updating users.
|
||||
type UpdateUser struct {
|
||||
Name *string `json:"name"`
|
||||
Password *string `json:"password"`
|
||||
IsAdmin *bool `json:"is_admin"`
|
||||
MatrixID *string `json:"matrix_id"`
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue