Skip to content
Snippets Groups Projects
user.go 2.58 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	"encoding/json"
    
    
    	"code.fbi.h-da.de/danet/gosdn/controller/interfaces/rbac"
    
    	"github.com/google/uuid"
    
    )
    
    // User represents the data of a user for access control and is stored in a storage.
    type User struct {
    
    	UserID   uuid.UUID         `json:"_id"`
    
    	UserName string            `json:"username"`
    
    	Roles    map[string]string `json:"roles,omitempty"`
    
    	Password string            `json:"password"`
    
    // NewUser creates a new user.
    func NewUser(id uuid.UUID,
    	name string,
    	roles map[string]string,
    	pw string,
    	token string) rbac.User {
    
    Andre Sterba's avatar
    Andre Sterba 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
    }
    
    
    // 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"`
    
    		UserName string            `json:"username"`
    
    		Roles    map[string]string `json:"roles,omitempty"`
    
    		Password string            `json:"password"`
    
    		Token    string            `json:"token,omitempty"`
    	}{
    		UserID:   u.ID(),
    		UserName: u.Name(),
    		Roles:    u.Roles,
    		Password: u.Password,
    		Token:    u.Token,
    	})
    }
    
    
    Fabian Seidl's avatar
    Fabian Seidl committed
    // MarshalBSON implments the MarshalBSON interface to store a user as BSON
    
    func (u *User) MarshalBSON() ([]byte, error) {
    	return bson.Marshal(&struct {
    
    		UserID   string            `bson:"_id"`
    
    		UserName string            `bson:"username"`
    
    		Roles    map[string]string `bson:"roles,omitempty"`
    
    		Password string            `bson:"password"`
    
    		Token    string            `bson:"token,omitempty"`
    	}{
    		UserID:   u.ID().String(),
    		UserName: u.Name(),
    		Roles:    u.Roles,
    		Password: u.Password,
    		Token:    u.Token,
    	})
    }