auth

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthRequired

func AuthRequired() gin.HandlerFunc

AuthRequired middleware проверяет аутентификацию пользователя

func Migrate added in v0.2.0

func Migrate(db *gorm.DB, modes ...RoleMigrationMode) error

Migrate records the role source once. For an old single-role schema, Auto transfers RoleID. For an existing many-to-many schema, Auto preserves current assignments, even without a marker from an earlier development version. Explicit ImportLegacy is needed if AutoMigrate created user_roles before the initial transfer. Run that explicit migration before RegisterRoutes.

func ModelPermissions added in v0.2.0

func ModelPermissions(model string) go4rest.ViewSetOption

ModelPermissions registers a model's standard permissions and enables model and object authorization. Call Migrate or RegisterRoutes before constructing a protected ViewSet. The model name must be app.model (e.g. catalog.product).

func RegisterRoutes

func RegisterRoutes(r *gin.Engine, db *gorm.DB, signingKey ...[]byte)

RegisterRoutes accepts an optional persistent signing key (at least 32 bytes). Without a key, sessions are valid only for the lifetime of this process.

Types

type AuthHandler

type AuthHandler struct {
	DB *gorm.DB
}

func NewAuthHandler

func NewAuthHandler(db *gorm.DB) *AuthHandler

func (*AuthHandler) Login

func (h *AuthHandler) Login(c *gin.Context)

Login обрабатывает вход пользователя

func (*AuthHandler) Logout

func (h *AuthHandler) Logout(c *gin.Context)

Logout обрабатывает выход пользователя

func (*AuthHandler) ObjectPermissions added in v0.2.0

func (h *AuthHandler) ObjectPermissions(ctx *gin.Context)

func (*AuthHandler) Register

func (h *AuthHandler) Register(c *gin.Context)

Register обрабатывает регистрацию пользователя

func (*AuthHandler) SetPermission added in v0.2.0

func (h *AuthHandler) SetPermission(ctx *gin.Context)

func (*AuthHandler) SetRole added in v0.2.0

func (h *AuthHandler) SetRole(ctx *gin.Context)

Assignment management requires model-wide auth.change_permission, rather than change_user/change_role, so ordinary account editors cannot elevate privileges.

func (*AuthHandler) WhoI added in v0.1.5

func (h *AuthHandler) WhoI(c *gin.Context)

WhoI возвращает информацию о текущем пользователе

type ObjectPermission added in v0.2.0

type ObjectPermission struct {
	ID           uint   `json:"id"`
	PermissionID uint   `gorm:"uniqueIndex:object_grant;not null" json:"permission_id"`
	ObjectID     string `gorm:"uniqueIndex:object_grant;not null" json:"object_id"`
	UserID       int    `gorm:"uniqueIndex:object_grant;not null;default:0" json:"user_id"`
	RoleID       uint   `gorm:"uniqueIndex:object_grant;not null;default:0" json:"role_id"`
}

ObjectPermission has exactly one recipient: a user or a role.

type Permission added in v0.2.0

type Permission struct {
	ID     uint   `json:"id"`
	Model  string `gorm:"uniqueIndex:permission_key;not null" json:"model"`
	Action string `gorm:"uniqueIndex:permission_key;not null" json:"action"`
}

Permission identifies an action on an explicitly named model (e.g. catalog.product).

type PermissionService added in v0.2.0

type PermissionService struct{ DB *gorm.DB }

func NewPermissionService added in v0.2.0

func NewPermissionService(db *gorm.DB) *PermissionService

func (*PermissionService) Authorize added in v0.2.0

func (s *PermissionService) Authorize(ctx *gin.Context, model, action string) error

func (*PermissionService) CurrentUser added in v0.2.0

func (s *PermissionService) CurrentUser(ctx *gin.Context) (*User, error)

func (*PermissionService) DeleteObjectPermissions added in v0.2.0

func (s *PermissionService) DeleteObjectPermissions(tx *gorm.DB, model, objectID string) error

DeleteObjectPermissions participates in the caller's deletion transaction. It also removes grants held by a permanently deleted user or role.

func (*PermissionService) HasObjectPermission added in v0.2.0

func (s *PermissionService) HasObjectPermission(user *User, model, action, objectID string) (bool, error)

func (*PermissionService) HasPermission added in v0.2.0

func (s *PermissionService) HasPermission(user *User, model, action string) (bool, error)

func (*PermissionService) RegisterModel added in v0.2.0

func (s *PermissionService) RegisterModel(model string) error

func (*PermissionService) Scope added in v0.2.0

func (s *PermissionService) Scope(ctx *gin.Context, query *gorm.DB, model, action string) (*gorm.DB, error)

func (*PermissionService) SetPermission added in v0.2.0

func (s *PermissionService) SetPermission(userID int, roleID, permissionID uint, objectID string, grant bool) error

func (*PermissionService) SetRole added in v0.2.0

func (s *PermissionService) SetRole(userID int, roleID uint, grant bool) error

SetRole and SetPermission validate both ends, and are idempotent.

type Role added in v0.1.5

type Role struct {
	gorm.Model
	Name        string       `gorm:"unique;not null"`
	Permissions []Permission `gorm:"many2many:role_permissions;constraint:OnDelete:CASCADE;"`
}

type RoleController added in v0.1.5

type RoleController struct {
	*go4rest.ViewSet[Role]
	// contains filtered or unexported fields
}

func NewRoleController added in v0.1.5

func NewRoleController(db *gorm.DB) *RoleController

func (*RoleController) Create added in v0.2.0

func (c *RoleController) Create(ctx *gin.Context)

func (*RoleController) Update added in v0.2.0

func (c *RoleController) Update(ctx *gin.Context)

type RoleMigrationMode added in v0.2.0

type RoleMigrationMode string

RoleMigrationMode chooses the authoritative source on the first migration. Auto imports RoleID only when user_roles does not yet exist; an existing relation may contain deliberate revocations and is preserved by default.

const (
	RoleMigrationAuto         RoleMigrationMode = "auto"
	RoleMigrationPreserve     RoleMigrationMode = "preserve"
	RoleMigrationImportLegacy RoleMigrationMode = "import_legacy"
)

type User

type User struct {
	ID          int          `json:"id"`
	Username    string       `gorm:"unique;not null"`
	Password    string       `gorm:"not null" json:"-"`
	RoleID      uint         `gorm:"default:null"` // Deprecated: authorization uses Roles, not RoleID.
	Role        Role         `gorm:"foreignKey:RoleID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	Roles       []Role       `gorm:"many2many:user_roles;constraint:OnDelete:CASCADE;"`
	Permissions []Permission `gorm:"many2many:user_permissions;constraint:OnDelete:CASCADE;"`
	IsSuperuser bool         `json:"is_superuser"`
}

func (*User) CheckPassword

func (u *User) CheckPassword(password string) bool

CheckPassword проверяет соответствие пароля хешу

func (*User) HashPassword

func (u *User) HashPassword() error

HashPassword хеширует пароль перед сохранением

type UserController added in v0.1.5

type UserController struct {
	*go4rest.ViewSet[User]
	// contains filtered or unexported fields
}

func NewUserController added in v0.1.5

func NewUserController(db *gorm.DB) *UserController

func (*UserController) Create added in v0.2.0

func (c *UserController) Create(ctx *gin.Context)

User and role writes intentionally accept only editable scalar fields. Role memberships, grants and superuser status cannot be mass-assigned.

func (*UserController) Update added in v0.2.0

func (c *UserController) Update(ctx *gin.Context)

Jump to

Keyboard shortcuts

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