store

package
v0.0.0-...-e8ca371 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2025 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrWrongCredentials is returned when credentials are wrong.
	ErrWrongCredentials = errors.New("wrong credentials provided")

	// ErrAlreadyAssigned defines the error if relation is already assigned.
	ErrAlreadyAssigned = errors.New("user pack already exists")

	// ErrNotAssigned defines the error if relation is not assigned.
	ErrNotAssigned = errors.New("user pack is not defined")

	// ErrGroupNotFound is returned when a user was not found.
	ErrGroupNotFound = errors.New("group not found")

	// ErrUserNotFound is returned when a user was not found.
	ErrUserNotFound = errors.New("user not found")
)
View Source
var (
	// ErrUnknownDriver defines a named error for unknown store drivers.
	ErrUnknownDriver = fmt.Errorf("unknown database driver")
)
View Source
var (
	// Migrations define all database migrations.
	Migrations = []*gormigrate.Migration{
		{
			ID: "0001_create_users_table",
			Migrate: func(tx *gorm.DB) error {
				type User struct {
					ID        string `gorm:"primaryKey;length:20"`
					Scim      string `gorm:"length:255"`
					Username  string `gorm:"unique;length:255"`
					Hashword  string `gorm:"length:255"`
					Email     string `gorm:"length:255"`
					Fullname  string `gorm:"length:255"`
					Active    bool   `gorm:"default:false"`
					Admin     bool   `gorm:"default:false"`
					CreatedAt time.Time
					UpdatedAt time.Time
				}

				return tx.Migrator().CreateTable(&User{})
			},
			Rollback: func(tx *gorm.DB) error {
				return tx.Migrator().DropTable("users")
			},
		},
		{
			ID: "0002_create_user_auths_table",
			Migrate: func(tx *gorm.DB) error {
				type UserAuth struct {
					ID        string `gorm:"primaryKey;length:20"`
					UserID    string `gorm:"length:20"`
					Provider  string `gorm:"length:255"`
					Ref       string `gorm:"length:255"`
					CreatedAt time.Time
					UpdatedAt time.Time
				}

				return tx.Migrator().CreateTable(&UserAuth{})
			},
			Rollback: func(tx *gorm.DB) error {
				return tx.Migrator().DropTable("user_auths")
			},
		},
		{
			ID: "0003_create_user_auths_users_constraint",
			Migrate: func(tx *gorm.DB) error {
				type UserAuth struct {
					UserID string `gorm:"length:20"`
				}

				type User struct {
					ID    string      `gorm:"primaryKey"`
					Auths []*UserAuth `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
				}

				return tx.Migrator().CreateConstraint(&User{}, "Auths")
			},
			Rollback: func(tx *gorm.DB) error {
				type UserAuth struct {
					UserID string `gorm:"length:20"`
				}

				type User struct {
					ID    string      `gorm:"primaryKey"`
					Auths []*UserAuth `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
				}

				return tx.Migrator().DropConstraint(&User{}, "Auths")
			},
		},
		{
			ID: "0004_create_groups_table",
			Migrate: func(tx *gorm.DB) error {
				type Group struct {
					ID        string `gorm:"primaryKey;length:20"`
					Scim      string `gorm:"length:255"`
					Slug      string `gorm:"unique;length:255"`
					Name      string `gorm:"unique;length:255"`
					CreatedAt time.Time
					UpdatedAt time.Time
				}

				return tx.Migrator().CreateTable(&Group{})
			},
			Rollback: func(tx *gorm.DB) error {
				return tx.Migrator().DropTable("groups")
			},
		},
		{
			ID: "0005_create_user_groups_table",
			Migrate: func(tx *gorm.DB) error {
				type UserGroup struct {
					UserID    string `gorm:"primaryKey;autoIncrement:false;length:20"`
					GroupID   string `gorm:"primaryKey;autoIncrement:false;length:20"`
					Perm      string `gorm:"length:64"`
					CreatedAt time.Time
					UpdatedAt time.Time
				}

				return tx.Migrator().CreateTable(&UserGroup{})
			},
			Rollback: func(tx *gorm.DB) error {
				return tx.Migrator().DropTable("user_groups")
			},
		},
		{
			ID: "0006_create_user_groups_groups_constraint",
			Migrate: func(tx *gorm.DB) error {
				type UserGroup struct {
					UserID  string `gorm:"primaryKey;autoIncrement:false;length:20"`
					GroupID string `gorm:"primaryKey;autoIncrement:false;length:20"`
				}

				type Group struct {
					ID    string       `gorm:"primaryKey"`
					Users []*UserGroup `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
				}

				return tx.Migrator().CreateConstraint(&Group{}, "Users")
			},
			Rollback: func(tx *gorm.DB) error {
				type UserGroup struct {
					UserID  string `gorm:"primaryKey;autoIncrement:false;length:20"`
					GroupID string `gorm:"primaryKey;autoIncrement:false;length:20"`
				}

				type Group struct {
					ID    string       `gorm:"primaryKey"`
					Users []*UserGroup `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
				}

				return tx.Migrator().DropConstraint(&Group{}, "Users")
			},
		},
		{
			ID: "0007_create_user_groups_users_constraint",
			Migrate: func(tx *gorm.DB) error {
				type UserGroup struct {
					UserID  string `gorm:"primaryKey;autoIncrement:false;length:20"`
					GroupID string `gorm:"primaryKey;autoIncrement:false;length:20"`
				}

				type User struct {
					ID     string       `gorm:"primaryKey"`
					Groups []*UserGroup `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
				}

				return tx.Migrator().CreateConstraint(&User{}, "Groups")
			},
			Rollback: func(tx *gorm.DB) error {
				type UserGroup struct {
					UserID  string `gorm:"primaryKey;autoIncrement:false;length:20"`
					GroupID string `gorm:"primaryKey;autoIncrement:false;length:20"`
				}

				type User struct {
					ID     string       `gorm:"primaryKey"`
					Groups []*UserGroup `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
				}

				return tx.Migrator().DropConstraint(&User{}, "Groups")
			},
		},
	}
)

Functions

This section is empty.

Types

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

Auth provides all database operations related to auth.

func (*Auth) ByCreds

func (s *Auth) ByCreds(ctx context.Context, username, password string) (*model.User, error)

ByCreds tries to authenticate a user based on credentials.

func (*Auth) ByID

func (s *Auth) ByID(ctx context.Context, userID string) (*model.User, error)

ByID tries to authenticate a user based on identifier.

func (*Auth) External

func (s *Auth) External(ctx context.Context, provider, ref, username, email, fullname string, admin bool) (*model.User, error)

External creates and authenticates external users.

type Groups

type Groups struct {
	// contains filtered or unexported fields
}

Groups provides all database operations related to groups.

func (*Groups) AttachUser

func (s *Groups) AttachUser(ctx context.Context, params model.UserGroupParams) error

AttachUser implements the attachment of a group to an user.

func (*Groups) Create

func (s *Groups) Create(ctx context.Context, record *model.Group) error

Create implements the create of a new group.

func (*Groups) Delete

func (s *Groups) Delete(ctx context.Context, name string) error

Delete implements the deletion of a group.

func (*Groups) DropUser

func (s *Groups) DropUser(ctx context.Context, params model.UserGroupParams) error

DropUser implements the removal of a group from an user.

func (*Groups) List

func (s *Groups) List(ctx context.Context, params model.ListParams) ([]*model.Group, int64, error)

List implements the listing of all users.

func (*Groups) ListUsers

func (s *Groups) ListUsers(ctx context.Context, params model.UserGroupParams) ([]*model.UserGroup, int64, error)

ListUsers implements the listing of all users for a group.

func (*Groups) PermitUser

func (s *Groups) PermitUser(ctx context.Context, params model.UserGroupParams) error

PermitUser implements the permission update for a user on a group.

func (*Groups) Show

func (s *Groups) Show(ctx context.Context, name string) (*model.Group, error)

Show implements the details for a specific user.

func (*Groups) Update

func (s *Groups) Update(ctx context.Context, record *model.Group) error

Update implements the update of an existing group.

type Store

type Store struct {
	Auth   *Auth
	Groups *Groups
	Users  *Users
	// contains filtered or unexported fields
}

Store provides the general database abstraction layer.

func MustStore

func MustStore(cfg config.Database, scim config.Scim) *Store

MustStore simply calls NewStore and panics on an error.

func NewStore

func NewStore(cfg config.Database, scim config.Scim) (*Store, error)

NewStore initializes a new Bun

func (*Store) Admin

func (s *Store) Admin(username, password, email string) error

Admin creates an initial admin user within the database.

func (*Store) Close

func (s *Store) Close() (bool, error)

Close simply closes the database connection.

func (*Store) Handle

func (s *Store) Handle() *bun.DB

Handle returns a database handle.

func (*Store) Info

func (s *Store) Info() map[string]interface{}

Info returns some basic db informations.

func (*Store) Migrate

func (s *Store) Migrate(ctx context.Context) (*migrate.MigrationGroup, error)

Migrate handles a database migration.

func (*Store) Migrator

func (s *Store) Migrator(ctx context.Context) (*migrate.Migrator, error)

Migrator provides the migration client.

func (*Store) Open

func (s *Store) Open() (bool, error)

Open simply opens the database connection.

func (*Store) Ping

func (s *Store) Ping() (bool, error)

Ping just tests the database connection.

func (*Store) Prepare

func (s *Store) Prepare() error

Prepare is preparing some database behavior.

func (*Store) Rollback

func (s *Store) Rollback(ctx context.Context) (*migrate.MigrationGroup, error)

Rollback handles a database rollback.

func (*Store) SearchQuery

func (s *Store) SearchQuery(q *bun.SelectQuery, _ string) *bun.SelectQuery

SearchQuery builds a query for search terms.

func (*Store) WithPrincipal

func (s *Store) WithPrincipal(principal *model.User) *Store

WithPrincipal integrates the current user.

type Users

type Users struct {
	// contains filtered or unexported fields
}

Users provides all database operations related to users.

func (*Users) AttachGroup

func (s *Users) AttachGroup(ctx context.Context, params model.UserGroupParams) error

AttachGroup implements the attachment of an user to a group.

func (*Users) Create

func (s *Users) Create(ctx context.Context, record *model.User) error

Create implements the create of a new user.

func (*Users) Delete

func (s *Users) Delete(ctx context.Context, name string) error

Delete implements the deletion of an user.

func (*Users) DropGroup

func (s *Users) DropGroup(ctx context.Context, params model.UserGroupParams) error

DropGroup implements the removal of an user from a group.

func (*Users) List

func (s *Users) List(ctx context.Context, params model.ListParams) ([]*model.User, int64, error)

List implements the listing of all users.

func (*Users) ListGroups

func (s *Users) ListGroups(ctx context.Context, params model.UserGroupParams) ([]*model.UserGroup, int64, error)

ListGroups implements the listing of all groups for an user.

func (*Users) PermitGroup

func (s *Users) PermitGroup(ctx context.Context, params model.UserGroupParams) error

PermitGroup implements the permission update for a group on an user.

func (*Users) Show

func (s *Users) Show(ctx context.Context, name string) (*model.User, error)

Show implements the details for a specific user.

func (*Users) Update

func (s *Users) Update(ctx context.Context, record *model.User) error

Update implements the update of an existing user.

Jump to

Keyboard shortcuts

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