Newer
Older
package rbac
import (
Fabian Seidl
committed
"code.fbi.h-da.de/danet/gosdn/controller/interfaces/rbac"
"github.com/google/uuid"
Fabian Seidl
committed
"go.mongodb.org/mongo-driver/bson"
)
// User represents the data of a user for access control and is stored in a storage.
type User struct {
UserID uuid.UUID `json:"_id"`
Fabian Seidl
committed
Roles map[string]string `json:"roles,omitempty"`
Fabian Seidl
committed
Token string `json:"token,omitempty"`
Fabian Seidl
committed
// NewUser creates a new user.
func NewUser(id uuid.UUID,
name string,
roles map[string]string,
pw string,
token string) rbac.User {
Fabian Seidl
committed
return &User{
UserID: id,
UserName: name,
Roles: roles,
Password: pw,
Token: token,
}
}
// ID returns a UUID of the user.
func (u *User) ID() uuid.UUID {
return u.UserID
}
// Name returns the name of the user.
func (u *User) Name() string {
return u.UserName
// IsCorrectPassword compares the provided with the stored password of a user.
func (u *User) IsCorrectPassword(pwd string) bool {
return pwd == u.Password
}
Fabian Seidl
committed
// GetPassword returns the password of the user.
func (u *User) GetPassword() string {
return u.Password
}
// GetRoles returns the roles of the user.
func (u *User) GetRoles() map[string]string {
return u.Roles
}
// GetToken returns the token of the user.
func (u *User) GetToken() string {
return u.Token
}
// SetName sets the name of the user
func (u *User) SetName(name string) {
u.UserName = name
}
// SetToken sets the token of the user
func (u *User) SetToken(token string) {
u.Token = token
}
// MarshalJSON implements the MarshalJSON interface to store a user as JSON
func (u *User) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
UserID uuid.UUID `json:"_id"`
Roles map[string]string `json:"roles,omitempty"`
Token string `json:"token,omitempty"`
}{
UserID: u.ID(),
UserName: u.Name(),
Roles: u.Roles,
Password: u.Password,
Token: u.Token,
})
}
// MarshalBSON implments the MarshalBSON interface to store a user as BSON
Fabian Seidl
committed
func (u *User) MarshalBSON() ([]byte, error) {
return bson.Marshal(&struct {
UserID string `bson:"_id"`
Fabian Seidl
committed
Roles map[string]string `bson:"roles,omitempty"`