auth

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package auth provides authentication and authorization functionalities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	gorm.Model

	AppID       string `gorm:"column:app_id" json:"app_id"`             // Application ID
	AppName     string `gorm:"column:app_name" json:"app_name"`         // Application Name
	AppSecret   string `gorm:"column:app_secret" json:"app_secret"`     // Application Secret Key
	RedirectUri string `gorm:"column:redirect_uri" json:"redirect_uri"` // Redirect URI after authorization
	Description string `gorm:"column:description" json:"description"`   // Description
	Status      int8   `gorm:"column:status" json:"status"`             // 1: Active; 2: Disabled
}

func (*App) BatchInsert added in v1.4.0

func (a *App) BatchInsert(ctx context.Context, db *gorm.DB, apps []App) error

BatchInsert inserts multiple apps into the database in a single batch operation.

Parameters:

  • ctx: context.Context for managing request-scoped values, cancellation signals, and deadlines.
  • db: *gorm.DB database connection.
  • apps: slice of App instances to be inserted.

Returns:

  • error: error if the batch insert operation fails, otherwise nil.

func (*App) Count added in v1.4.0

func (a *App) Count(ctx context.Context, db *gorm.DB) (int64, error)

Count counts the number of apps matching the criteria in the database.

Parameters:

  • ctx: context.Context for managing request-scoped values, cancellation signals, and deadlines.
  • db: *gorm.DB database connection.

Returns:

  • int64: count of matching apps.
  • error: error if the count operation fails, otherwise nil.

func (*App) CountByArgs added in v1.4.0

func (a *App) CountByArgs(ctx context.Context, db *gorm.DB, query interface{}, args ...interface{}) (int64, error)

CountByArgs counts the number of apps matching the specified query and arguments in the database.

Parameters:

  • ctx: context.Context for managing request-scoped values, cancellation signals, and deadlines.
  • db: *gorm.DB database connection.
  • query: SQL query string.
  • args: variadic arguments for the SQL query.

Returns:

  • int64: count of matching apps.
  • error: error if the count operation fails, otherwise nil.

func (*App) Create

func (a *App) Create(ctx context.Context, db *gorm.DB) (uint, error)

Create inserts a new app into the database and returns the ID of the created App.

Parameters:

  • ctx: context.Context for managing request-scoped values, cancellation signals, and deadlines.
  • db: *gorm.DB database connection.

Returns:

  • uint: ID of the created app.
  • error: error if the insert operation fails, otherwise nil.

func (*App) Delete

func (a *App) Delete(ctx context.Context, db *gorm.DB) error

Delete removes the app from the database.

Parameters:

  • ctx: context.Context for managing request-scoped values, cancellation signals, and deadlines.
  • db: *gorm.DB database connection.

Returns:

  • error: error if the delete operation fails, otherwise nil.

func (*App) FindWithPagination added in v1.4.0

func (a *App) FindWithPagination(ctx context.Context, db *gorm.DB, page, size int) ([]App, error)

FindWithPagination retrieves apps matching the criteria from the database with pagination support.

Parameters:

  • ctx: context.Context for managing request-scoped values, cancellation signals, and deadlines.
  • db: *gorm.DB database connection.
  • page: page number for pagination (1-based).
  • size: number of apps per page.

Returns:

  • []App: slice of retrieved apps.
  • error: error if the query fails, otherwise nil.

func (*App) FindWithSort added in v1.4.0

func (a *App) FindWithSort(ctx context.Context, db *gorm.DB, sort string) ([]App, error)

FindWithSort retrieves apps matching the criteria from the database with sorting support.

Parameters:

  • ctx: context.Context for managing request-scoped values, cancellation signals, and deadlines.
  • db: *gorm.DB database connection.
  • sort: sorting criteria (e.g., "id desc").

Returns:

  • []App: slice of retrieved apps.
  • error: error if the query fails, otherwise nil.

func (*App) First

func (a *App) First(ctx context.Context, db *gorm.DB) (*App, error)

First retrieves the first app matching the criteria from the database.

Parameters:

  • ctx: context.Context for managing request-scoped values, cancellation signals, and deadlines.
  • db: *gorm.DB database connection.

Returns:

  • *App: pointer to the retrieved app, or nil if not found.
  • error: error if the query fails, otherwise nil.

func (*App) Last added in v1.4.0

func (a *App) Last(ctx context.Context, db *gorm.DB) (*App, error)

Last retrieves the last app matching the criteria from the database, ordered by ID in descending order.

Parameters:

  • ctx: context.Context for managing request-scoped values, cancellation signals, and deadlines.
  • db: *gorm.DB database connection.

Returns:

  • *App: pointer to the retrieved app, or nil if not found.
  • error: error if the query fails, otherwise nil.

func (*App) List added in v1.4.0

func (a *App) List(ctx context.Context, db *gorm.DB) ([]App, error)

List retrieves all apps matching the criteria from the database.

Parameters:

  • ctx: context.Context for managing request-scoped values, cancellation signals, and deadlines.
  • db: *gorm.DB database connection.

Returns:

  • []App: slice of retrieved apps.
  • error: error if the query fails, otherwise nil.

func (*App) ListByArgs added in v1.4.0

func (a *App) ListByArgs(ctx context.Context, db *gorm.DB, query interface{}, args ...interface{}) ([]App, error)

ListByArgs retrieves apps matching the specified query and arguments from the database, ordered by ID in descending order.

Parameters:

  • ctx: context.Context for managing request-scoped values, cancellation signals, and deadlines.
  • db: *gorm.DB database connection.
  • query: SQL query string.
  • args: variadic arguments for the SQL query.

Returns:

  • []App: slice of retrieved apps.
  • error: error if the query fails, otherwise nil.

func (*App) TableName

func (a *App) TableName() string

TableName specifies the table name for the App model.

func (*App) Updates

func (a *App) Updates(ctx context.Context, db *gorm.DB, updates map[string]interface{}) error

Updates applies the specified updates to the app in the database.

Parameters:

  • ctx: context.Context for managing request-scoped values, cancellation signals, and deadlines.
  • db: *gorm.DB database connection.
  • updates: map[string]interface{} containing the updates to apply.

Returns:

  • error: error if the update operation fails, otherwise nil.

type MgoApp added in v1.4.0

type MgoApp struct {
	ID          primitive.ObjectID `json:"id" bson:"_id,omitempty"`
	AppName     string             `bson:"app_name" json:"app_name"`
	AppID       string             `bson:"app_id" json:"app_id"`
	AppSecret   string             `bson:"app_secret" json:"app_secret"`
	RedirectUri string             `bson:"redirect_uri" json:"redirect_uri"`
	Description string             `bson:"description" json:"description"`
	Status      uint8              `bson:"status" json:"status"`
}

MgoApp represents an application in the authentication system.

func (*MgoApp) CollectionName added in v1.4.0

func (a *MgoApp) CollectionName() string

CollectionName returns the name of the MongoDB collection for MgoApp.

func (*MgoApp) Count added in v1.4.0

func (a *MgoApp) Count(ctx context.Context, db *qmgo.Database) (int64, error)

Count returns the number of MgoApp documents that match the query.

Parameters:

  • ctx: A context.Context for the database operation.
  • db: A pointer to the qmgo.Database to perform the operation on.

Returns:

  • int64: The count of matching documents.
  • error: An error if the operation fails, or nil on success.

Example:

app := &MgoApp{Status: 1}
count, err := app.Count(ctx, db)
if err != nil {
    log.Printf("Error counting apps: %v", err)
    return
}

func (*MgoApp) Create added in v1.4.0

func (a *MgoApp) Create(ctx context.Context, db *qmgo.Database) (string, error)

Create inserts a new MgoApp document into the database.

Parameters:

  • ctx: A context.Context for the database operation.
  • db: A pointer to the qmgo.Database to perform the operation on.

Returns:

  • string: The hexadecimal representation of the inserted document's ObjectID.
  • error: An error if the operation fails, or nil on success.

Example:

newApp := &MgoApp{
    AppName: "New App",
    AppID: "new_app_id",
    AppSecret: "secret",
    Status: 1,
}
id, err := newApp.Create(ctx, db)
if err != nil {
    log.Printf("Error creating app: %v", err)
    return
}
fmt.Printf("Created app with ID: %s\n", id)

func (*MgoApp) Creates added in v1.4.0

func (a *MgoApp) Creates(ctx context.Context, db *qmgo.Database, apps []MgoApp) ([]primitive.ObjectID, error)

Creates inserts multiple MgoApp documents into the database.

Parameters:

  • ctx: A context.Context for the database operation.
  • db: A pointer to the qmgo.Database to perform the operation on.
  • apps: A slice of MgoApp structs to be inserted.

Returns:

  • []primitive.ObjectID: A slice of ObjectIDs for the inserted documents.
  • error: An error if the operation fails, or nil on success.

Example:

newApps := []MgoApp{
    {AppName: "App 1", AppID: "app1", Status: 1},
    {AppName: "App 2", AppID: "app2", Status: 1},
}
ids, err := (&MgoApp{}).Creates(ctx, db, newApps)
if err != nil {
    log.Printf("Error creating apps: %v", err)
    return
}
for _, id := range ids {
    fmt.Printf("Created app with ID: %s\n", id.Hex())
}

func (*MgoApp) Delete added in v1.4.0

func (a *MgoApp) Delete(ctx context.Context, db *qmgo.Database) error

Delete removes the MgoApp document that matches the query from the database.

Parameters:

  • ctx: A context.Context for the database operation.
  • db: A pointer to the qmgo.Database to perform the operation on.

Returns:

  • error: An error if the operation fails, or nil on success.

Example:

app := &MgoApp{AppID: "app_to_delete"}
err := app.Delete(ctx, db)
if err != nil {
    log.Printf("Error deleting app: %v", err)
    return
}
fmt.Println("App deleted successfully")

func (*MgoApp) FindWithSort added in v1.4.0

func (a *MgoApp) FindWithSort(ctx context.Context, db *qmgo.Database, sort string) ([]MgoApp, error)

FindWithSort retrieves all MgoApp documents that match the query, sorted according to the provided sort string.

Parameters:

  • ctx: A context.Context for the database operation.
  • db: A pointer to the qmgo.Database to perform the operation on.
  • sort: A string specifying the sort order (e.g., "-created_at" for descending order by created_at field).

Returns:

  • []MgoApp: A slice of MgoApp structs containing the matching documents, sorted as specified.
  • error: An error if the operation fails, or nil on success.

Example:

app := &MgoApp{Status: 1}
results, err := app.FindWithSort(ctx, db, "-app_name") // Sort by app_name in descending order
if err != nil {
    log.Printf("Error finding sorted apps: %v", err)
    return
}
for _, result := range results {
    fmt.Printf("Found app: %+v\n", result)
}

func (*MgoApp) First added in v1.4.0

func (a *MgoApp) First(ctx context.Context, db *qmgo.Database) (*MgoApp, error)

First retrieves the first MgoApp document that matches the query.

Parameters:

  • ctx: A context.Context for the database operation.
  • db: A pointer to the qmgo.Database to perform the operation on.

Returns:

  • *MgoApp: A pointer to the retrieved MgoApp, or nil if not found.
  • error: An error if the operation fails, or nil on success.

Example:

app := &MgoApp{AppID: "example_id"}
result, err := app.First(ctx, db)
if err != nil {
    log.Printf("Error finding app: %v", err)
    return
}
fmt.Printf("Found app: %+v\n", result)

func (*MgoApp) Last added in v1.4.0

func (a *MgoApp) Last(ctx context.Context, db *qmgo.Database) (*MgoApp, error)

Last retrieves the last MgoApp document that matches the query, sorted by _id in descending order.

Parameters:

  • ctx: A context.Context for the database operation.
  • db: A pointer to the qmgo.Database to perform the operation on.

Returns:

  • *MgoApp: A pointer to the retrieved MgoApp, or nil if not found.
  • error: An error if the operation fails, or nil on success.

Example:

app := &MgoApp{Status: 1}
result, err := app.Last(ctx, db)
if err != nil {
    log.Printf("Error finding last app: %v", err)
    return
}
fmt.Printf("Found last app: %+v\n", result)

func (*MgoApp) List added in v1.4.0

func (a *MgoApp) List(ctx context.Context, db *qmgo.Database) ([]MgoApp, error)

List retrieves all MgoApp documents that match the query.

Parameters:

  • ctx: A context.Context for the database operation.
  • db: A pointer to the qmgo.Database to perform the operation on.

Returns:

  • []MgoApp: A slice of MgoApp structs containing the matching documents.
  • error: An error if the operation fails, or nil on success.

Example:

app := &MgoApp{Status: 1}
results, err := app.List(ctx, db)
if err != nil {
    log.Printf("Error listing apps: %v", err)
    return
}
for _, result := range results {
    fmt.Printf("Found app: %+v\n", result)
}

func (*MgoApp) Pagination added in v1.4.0

func (a *MgoApp) Pagination(ctx context.Context, db *qmgo.Database, page, size int) ([]MgoApp, error)

Pagination retrieves a paginated list of MgoApp documents that match the query.

Parameters:

  • ctx: A context.Context for the database operation.
  • db: A pointer to the qmgo.Database to perform the operation on.
  • page: The page number (1-based) to retrieve.
  • size: The number of documents per page.

Returns:

  • []MgoApp: A slice of MgoApp structs containing the matching documents for the specified page.
  • error: An error if the operation fails, or nil on success.

Example:

app := &MgoApp{Status: 1}
results, err := app.Pagination(ctx, db, 2, 10) // Get the second page with 10 items per page
if err != nil {
    log.Printf("Error retrieving paginated apps: %v", err)
    return
}
for _, result := range results {
    fmt.Printf("Found app: %+v\n", result)
}

func (*MgoApp) Updates added in v1.4.0

func (a *MgoApp) Updates(ctx context.Context, db *qmgo.Database, updates bson.M) error

Updates modifies the MgoApp document that matches the query with the provided updates.

Parameters:

  • ctx: A context.Context for the database operation.
  • db: A pointer to the qmgo.Database to perform the operation on.
  • updates: A bson.M containing the fields to update and their new values.

Returns:

  • error: An error if the operation fails, or nil on success.

Example:

app := &MgoApp{AppID: "app_to_update"}
updates := bson.M{"status": 2, "description": "Updated description"}
err := app.Updates(ctx, db, updates)
if err != nil {
    log.Printf("Error updating app: %v", err)
    return
}
fmt.Println("App updated successfully")

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL