github.com/decred/politeia@v1.4.0/politeiawww/user/user.go (about)

     1  // Copyright (c) 2022 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package user
     6  
     7  import (
     8  	"database/sql"
     9  	"errors"
    10  
    11  	"github.com/google/uuid"
    12  )
    13  
    14  var (
    15  	// ErrNotFound is returned when a user is not found in the database.
    16  	ErrNotFound = errors.New("user not found")
    17  )
    18  
    19  // User represents a politeia user.
    20  //
    21  // Plugins are only provided with and can only edit the plugin data that they
    22  // own. The user object does not necessarily contain all plugin data for the
    23  // user. Plugins have two ways to save user data:
    24  //
    25  //  1. Update the user object that is provided to them by the backend during the
    26  //     execution of plugin commands. These updates are saved to the database by
    27  //     the backend. Plugins have the option of saving data to the user object as
    28  //     either clear text or encrypted.
    29  //
    30  //  2. Plugins can create and manage a database table themselves to store plugin
    31  //     user data in. This option should be reserved for data that would cause
    32  //     performance issues if saved to this global user object.
    33  type User struct {
    34  	ID      uuid.UUID             // Unique ID
    35  	Plugins map[string]PluginData // [pluginID]PluginData
    36  	Updated bool
    37  }
    38  
    39  // PluginData contains the user data for a specific plugin.
    40  type PluginData struct {
    41  	ClearText []byte
    42  	Encrypted []byte
    43  }
    44  
    45  // DB represents the user database API.
    46  type DB interface {
    47  	// TxInsert inserts a user into the database using a transaction.
    48  	TxInsert(*sql.Tx, User) error
    49  
    50  	// TxUpdate updates a user in the database using a transaction.
    51  	TxUpdate(*sql.Tx, User) error
    52  
    53  	// TxGet gets a user from the database using a transactions.
    54  	//
    55  	// An ErrNotFound error is returned if a user is not found for the provided
    56  	// user ID.
    57  	TxGet(tx *sql.Tx, userID string) (*User, error)
    58  
    59  	// Get gets a user from the database.
    60  	//
    61  	// An ErrNotFound error is returned if a user is not found for the provided
    62  	// user ID.
    63  	Get(userID string) (*User, error)
    64  }