xauth

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: BSD-3-Clause-Clear Imports: 10 Imported by: 0

Documentation

Overview

权限表

此处采用RBAC0模型来构建权限体系,即用户有多角色、角色又有多权限

权限又拆分为菜单、页面、按钮、接口四种权限,然后权限后面又添加权限类型来区分前台/后台的权限 menu:xxx、page:xxx、button:xxx、api:xxx的形式来区分各个权限

超管权限:user_id=1为超级管理员权限,该用户会返回所有的权限、子用户列表

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckAuth

func CheckAuth(uid uint, auth string, typ uint) bool

检测用户是否存在某权限

uid		用户ID
auth	权限标识
typ		权限标识来源

func CreateAuth

func CreateAuth(c *Authority) error

创建权限操作

c	待保存的权限详情

func CreateRole

func CreateRole(c *Role) error

创建角色信息

c	待创建的角色信息

func CreateUserChild

func CreateUserChild(fid, uid, typ uint) error

创建/修改用户关联

fid	上级用户ID 0-删除此条目记录 1-忽略此条目(超管用户拥有所有用户ID) 其余值则表示判断是否存在上级,如果存在就修改,不存在就替换
uid	子用户ID 1-超管用户,此用户不可设置上下级关系,一旦传入此ID则直接返回nil
typ	关联权限来源

func GetUserAuth

func GetUserAuth(uid uint, typ uint) []string

获取用户的权限信息

uid	用户ID
typ	权限来源

func GetUserChild

func GetUserChild(uid uint, typ uint, sf bool) []uint

获取我的下级用户

uid	用户ID 1-超管用户,此用户会直接返回nil,所以需要外部判断如果uid==1就直接不查询此条件
typ	权限来源
sf	返回的下级ID中,是否需要包含自身 true-包含自身 false-不含自身

func GetUserChildOnly

func GetUserChildOnly(uid, typ uint) []uint

获取我的直属下级用户列表

uid	我的ID列表
typ	所属平台ID

func GetUserParent

func GetUserParent(uid, typ uint) uint

获取我的上级用户

uid	用户ID
typ	授权来源ID

func GetUserRole

func GetUserRole(uid uint) []uint

获取用户角色列表

uid	用户ID

func Regedit

func Regedit(c *Config)

配置项注入

func RemoveAuth added in v0.2.27

func RemoveAuth(id uint) error

移除权限标识

id	权限ID

func ReplaceRoleAuth

func ReplaceRoleAuth(id uint, auth []uint) error

替换角色的权限列表 此操作会删除掉该角色的之前的权限列表,使用新传入的权限列表进行替换

id		角色ID
auth	权限ID列表

func ReplaceUserRole

func ReplaceUserRole(uid uint, rid []uint) error

替换角色的权限列表 此操作会删除掉该角色的之前的权限列表,使用新传入的权限列表进行替换

uid	用户ID
rid	角色ID列表

func RoleRemove added in v0.2.59

func RoleRemove(id uint) error

删除角色信息

id	待删除的角色ID

Types

type Authority

type Authority struct {
	Id          uint   `gorm:"column:id;primaryKey;not null;autoIncrement" json:"id" form:"id"`                     // 权限ID
	Fid         uint   `gorm:"column:fid;comment:上级ID" json:"fid" form:"fid"`                                       // 上级权限ID
	Code        string `gorm:"column:code;type:VARCHAR(200);comment:权限标识;UNIQUEINDEX:code" json:"code" form:"code"` // 权限标识,使用menu:xxx、page:xxx、button:xxx、api:xxx的形式来区分各个权限
	Title       string `gorm:"column:title;type:VARCHAR(200);comment:权限标题" json:"title" form:"title"`               // 权限标题
	Type        uint   `gorm:"column:type;comment:权限来源;UNIQUEINDEX:code" json:"type" form:"type"`                   // 权限来源
	UserId      uint   `gorm:"column:user_id;comment:权限创建人" json:"user_id" form:"user_id"`                          // 创建人ID
	CreatedAt   string `gorm:"column:created_at;type:DATETIME;comment:创建时间" json:"created_at" form:"created_at"`    // 创建时间
	UpdatedAt   string `gorm:"column:updated_at;type:DATETIME;comment:最后修改时间" json:"-" form:"-"`                    // 更新时间
	HasChildren bool   `gorm:"-" json:"hasChildren" form:"-"`                                                       // 是否存在下级数据
}

权限明细表

func GetAuthorityList added in v0.1.5

func GetAuthorityList(fid uint, id []uint, offset, limit int) ([]Authority, error)

获取权限列表

fid		上级权限ID
id		筛选ID列表【一旦传入此条件则fid自动失效】
offset	跳过条数
limit	查询条数

func ListAuth added in v0.2.18

func ListAuth(typ, fid uint, offset, limit int) (int64, []Authority, error)

获取权限列表接口

typ		权限位置
fid		上级条目ID
offset	跳过条数
limit	查询条数

func (*Authority) TableName

func (c *Authority) TableName() string

获取表名

type Config

type Config struct {
	DB            *gorm.DB                    // 数据库连接
	AuthorityName string                      // 权限表
	RoleName      string                      // 角色表
	RoleAuthName  string                      // 角色权限表
	RoleUserName  string                      // 用户角色表
	UserChildName string                      // 用户关联表
	AuthMap       map[uint]string             // 权限类型字典
	ErrorFunc     func(msg string, err error) // 错误记录函数
}

配置项

type Role

type Role struct {
	Id        uint   `gorm:"column:id;primaryKey;not null;autoIncrement" json:"id" form:"id"`                  // 角色ID
	Title     string `gorm:"column:title;type:VARCHAR(200);comment:角色标题" json:"title" form:"title"`            // 角色标题
	CreatedAt string `gorm:"column:created_at;type:DATETIME;comment:创建时间" json:"created_at" form:"created_at"` // 创建时间
	UpdatedAt string `gorm:"column:updated_at;type:DATETIME;comment:最后修改时间" json:"-" form:"-"`                 // 更新时间
}

角色表

func GetRoleList added in v0.1.5

func GetRoleList(id []uint, offset, limit int) ([]Role, error)

获取角色列表

id		角色ID列表
offset	跳过条数
limit	查询条数

func ListRole added in v0.2.18

func ListRole(offset, limit int) (int64, []Role, error)

获取角色列表接口

offset	跳过条数
limit	查询条数

func (*Role) TableName

func (c *Role) TableName() string

获取表名

type RoleAuth

type RoleAuth struct {
	Id        uint   `gorm:"column:id;primaryKey;not null;autoIncrement" json:"id" form:"id"`                  // 角色ID
	RoleId    uint   `gorm:"column:role_id;comment:角色ID;UNIQUEINDEX:role_auth" json:"role_id" form:"role_id"`  // 角色ID
	AuthId    uint   `gorm:"column:auth_id;comment:权限ID;UNIQUEINDEX:role_auth" json:"auth_id" form:"auth_id"`  // 权限ID
	CreatedAt string `gorm:"column:created_at;type:DATETIME;comment:创建时间" json:"created_at" form:"created_at"` // 创建时间
}

角色权限表

func (*RoleAuth) TableName

func (c *RoleAuth) TableName() string

获取表名

type RoleUser

type RoleUser struct {
	Id        uint   `gorm:"column:id;primaryKey;not null;autoIncrement" json:"id" form:"id"`                  // 角色ID
	RoleId    uint   `gorm:"column:role_id;comment:角色ID;UNIQUEINDEX:role_user" json:"role_id" form:"role_id"`  // 角色ID
	UserId    uint   `gorm:"column:user_id;comment:用户ID;UNIQUEINDEX:role_user" json:"user_id" form:"user_id"`  // 用户ID
	CreatedAt string `gorm:"column:created_at;type:DATETIME;comment:创建时间" json:"created_at" form:"created_at"` // 创建时间
}

角色用户表

func (*RoleUser) TableName

func (c *RoleUser) TableName() string

获取表名

type UserChild

type UserChild struct {
	Id         uint   `gorm:"column:id;primaryKey;not null;autoIncrement" json:"id" form:"id"`                                // 角色ID
	SuperiorId uint   `gorm:"column:superior_id;comment:上级用户ID;UNIQUEINDEX:user_child" json:"superior_id" form:"superior_id"` // 上级用户ID
	UserId     uint   `gorm:"column:user_id;comment:用户ID;UNIQUEINDEX:user_child" json:"user_id" form:"user_id"`
	Type       uint   `gorm:"column:type;comment:权限来源;UNIQUEINDEX:user_child" json:"type" form:"type"`          // 权限来源            // 用户ID
	CreatedAt  string `gorm:"column:created_at;type:DATETIME;comment:创建时间" json:"created_at" form:"created_at"` // 创建时间
}

用户关联表

func (*UserChild) TableName

func (c *UserChild) TableName() string

获取表名

Jump to

Keyboard shortcuts

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