Documentation
¶
Index ¶
- Constants
- Variables
- func AddRoleForUser(db *gorm.DB, userId uint, roleId uint) error
- func CORSEnabled() gin.HandlerFunc
- func CheckGroupInUse(db *gorm.DB, groupID uint) (bool, error)
- func CheckPassword(user *User, password string) bool
- func CheckPermissionInUse(db *gorm.DB, permissionCode string) (bool, error)
- func CheckRoleInUse(db *gorm.DB, roleID uint) (bool, error)
- func CheckRolePermission(db *gorm.DB, roleID uint, code string, policies ...string) (bool, error)
- func CheckUserPermission(db *gorm.DB, userID uint, code string, policies ...string) (bool, error)
- func CheckValue(db *gorm.DB, key, defaultValue string)
- func CreateDatabaseInstance(driver, dsn string, cfg *gorm.Config) (*gorm.DB, error)
- func CurrentTimezone(c *gin.Context) *time.Location
- func Debug(v ...any)
- func DeletePermissionForRole(db *gorm.DB, roleID uint, permissionCode string) error
- func EncodeHashToken(user *User, timestamp int64, useLastlogin bool) (hash string)
- func Error(v ...any)
- func Get[T any](db *gorm.DB, val *T, where ...any) (*T, error)
- func GetBoolValue(db *gorm.DB, key string) bool
- func GetByID[T any, E ~uint | ~int | ~string](db *gorm.DB, id E, where ...any) (*T, error)
- func GetEnv(key string) string
- func GetIntValue(db *gorm.DB, key string, defaultValue int) int
- func GetValue(db *gorm.DB, key string) string
- func HashPassword(password string) string
- func InTimezone(c *gin.Context, timezone string)
- func Info(v ...any)
- func InitAuthHandler(prefix string, db *gorm.DB, r *gin.Engine)
- func InitDatabase(driver, dsn string, logWrite io.Writer) *gorm.DB
- func InitMigrate(db *gorm.DB) error
- func InitRabbit(db *gorm.DB, r *gin.Engine)
- func IsExistsByEmail(db *gorm.DB, email string) bool
- func Login(c *gin.Context, user *User)
- func Logout(c *gin.Context, user *User)
- func LookupEnv(key string) (string, bool)
- func MakeMigrates(db *gorm.DB, insts ...any) error
- func RandNumberText(n int) string
- func RandText(n int) string
- func SetLastLogin(db *gorm.DB, user *User, lastIp string) error
- func SetLogLevel(level int)
- func SetPassword(db *gorm.DB, user *User, password string) (err error)
- func SetValue(db *gorm.DB, key, value string)
- func StructAsMap(form any, fields []string) (vals map[string]any)
- func SwitchGroup(c *gin.Context, group *Group)
- func UpdateUserFields(db *gorm.DB, user *User, vals map[string]any) error
- func Warning(v ...any)
- func WithCookieSession(secret string) gin.HandlerFunc
- func WithGormDB(db *gorm.DB) gin.HandlerFunc
- func WithMemSession(secret string) gin.HandlerFunc
- type ChangePasswordForm
- type Config
- type Group
- func CreateGroupByUser(db *gorm.DB, userID uint, name string) (*Group, error)
- func CurrentGroup(c *gin.Context) *Group
- func GetFirstGroupByUser(db *gorm.DB, userID uint) (*Group, error)
- func GetGroupByID(db *gorm.DB, groupID uint) (*Group, error)
- func GetGroupByName(db *gorm.DB, name string) (*Group, error)
- func GetGroupsByUser(db *gorm.DB, userID uint) ([]*Group, error)
- type GroupMember
- type LoginForm
- type Permission
- func AddPermissionForRole(db *gorm.DB, roleID uint, name, code string, policies ...string) (*Permission, error)
- func GetPermissionByCode(db *gorm.DB, code string) (*Permission, error)
- func GetPermissionByID(db *gorm.DB, permissionID uint) (*Permission, error)
- func GetPermissionsByRole(db *gorm.DB, roleID uint) ([]*Permission, error)
- type Profile
- type RegisterUserForm
- type Role
- type RolePermission
- type SigHandler
- type Signals
- type TestClient
- func (c *TestClient) Call(method, path string, form any, result any) error
- func (c *TestClient) CallDelete(path string, form, result any) error
- func (c *TestClient) CallGet(path string, form, result any) error
- func (c *TestClient) CallPatch(path string, form, result any) error
- func (c *TestClient) CallPost(path string, form any, result any) error
- func (c *TestClient) CallPut(path string, form, result any) error
- func (c *TestClient) Get(path string) *httptest.ResponseRecorder
- func (c *TestClient) Post(method, path string, body []byte) *httptest.ResponseRecorder
- func (c *TestClient) SendReq(path string, req *http.Request) *httptest.ResponseRecorder
- type User
- func CreateUser(db *gorm.DB, email, password string) (*User, error)
- func CurrentUser(c *gin.Context) *User
- func DecodeHashToken(db *gorm.DB, hash string, useLastLogin bool) (user *User, err error)
- func GetUserByEmail(db *gorm.DB, email string) (user *User, err error)
- func GetUserByUID(db *gorm.DB, userID uint) (*User, error)
- func GetUsersByGroup(db *gorm.DB, groupID uint) ([]*User, error)
- func GetUsersByRole(db *gorm.DB, roleID uint) ([]*User, error)
- type UserRole
Constants ¶
View Source
const ( UserField = "_rabbit_uid" GroupField = "_rabbit_gid" DbField = "_rabbit_db" TzField = "_rabbit_tz" )
View Source
const ( LevelDebug = iota LevelInfo LevelWarning LevelError )
View Source
const ( // SigUserLogin: user *User, c *gin.Context SigUserLogin = "user.login" // SigUserLogout: user *User, c *gin.Context SigUserLogout = "user.logout" //SigUserCreate: user *User, c *gin.Context SigUserCreate = "user.create" )
View Source
const CORS_ALLOW_ALL = "*"
View Source
const CORS_ALLOW_CREDENTIALS = "true"
View Source
const CORS_ALLOW_HEADERS = "" /* 137-byte string literal not displayed */
View Source
const CORS_ALLOW_METHODS = "POST, OPTIONS, GET, PUT, PATCH, DELETE"
View Source
const ENV_AUTH_PREFIX = "AUTH_PREFIX"
View Source
const ENV_DB_DRIVER = "DB_DRIVER"
DB
View Source
const ENV_DSN = "DSN"
View Source
const ENV_SALT = "PASSWORD_SALT"
User Password salt
View Source
const ENV_SESSION_SECRET = "SESSION_SECRET"
View Source
const KEY_USER_ACTIVATED = "USER_ACTIVATED"
User need to activate
View Source
const SessionField = "rabbit"
Gin session field
View Source
const XAuthTokenHeader = "X-Auth-Token"
Variables ¶
View Source
var EnabledConsoleColor = false
View Source
var LogLevel = LevelDebug
Functions ¶
func CORSEnabled ¶
func CORSEnabled() gin.HandlerFunc
func CheckPassword ¶
func CheckPermissionInUse ¶
func CheckRolePermission ¶
check
func CheckUserPermission ¶
TODO: optimize
func CheckValue ¶
CheckValue check if key exists, if not, set defaultValue
func CreateDatabaseInstance ¶
func DeletePermissionForRole ¶
func EncodeHashToken ¶
ts-uid-token - base64(email$timestamp) + "-" + sha256(salt + logintimestamp + password + email$timestamp)
func HashPassword ¶
func InTimezone ¶
func InitMigrate ¶
func InitRabbit ¶
InitRabbit start with default middleware and auth handler 1. migrate models 2. gin middleware 3. auth handler
func RandNumberText ¶
func SetLogLevel ¶
func SetLogLevel(level int)
func SwitchGroup ¶
func WithCookieSession ¶
func WithCookieSession(secret string) gin.HandlerFunc
func WithGormDB ¶
func WithGormDB(db *gorm.DB) gin.HandlerFunc
func WithMemSession ¶
func WithMemSession(secret string) gin.HandlerFunc
Types ¶
type ChangePasswordForm ¶
type ChangePasswordForm struct {
Password string `json:"password" binding:"required"`
}
type Group ¶
type Group struct {
ID uint `json:"-" gorm:"primarykey"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Name string `json:"name" gorm:"size:200;uniqueIndex"`
Extra string `json:"extra"`
Users []*User `json:"users" gorm:"many2many:group_members;"`
}
func CreateGroupByUser ¶
type GroupMember ¶
type Permission ¶
type Permission struct {
ID uint `json:"-" gorm:"primarykey"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Name string `json:"name" gorm:"uniqueIndex"`
Code string `json:"code" gorm:"size:200;uniqueIndex"`
P1 string `json:"p1" gorm:"size:200"`
P2 string `json:"p2" gorm:"size:200"`
P3 string `json:"p3" gorm:"size:200"`
Groups []*Group `json:"groups" gorm:"many2many:group_permissions;"`
Roles []*Role `json:"roles" gorm:"many2many:role_permissions;"`
}
func AddPermissionForRole ¶
func GetPermissionByCode ¶
func GetPermissionByCode(db *gorm.DB, code string) (*Permission, error)
func GetPermissionByID ¶
func GetPermissionByID(db *gorm.DB, permissionID uint) (*Permission, error)
permission
func GetPermissionsByRole ¶
func GetPermissionsByRole(db *gorm.DB, roleID uint) ([]*Permission, error)
type Profile ¶
type RegisterUserForm ¶
type RegisterUserForm struct {
Email string `json:"email" binding:"required"`
Password string `json:"password" binding:"required"`
DisplayName string `json:"displayName"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Locale string `json:"locale"`
Timezone string `json:"timezone"`
Source string `json:"source"`
}
type Role ¶
type Role struct {
ID uint `json:"-" gorm:"primarykey"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Name string `json:"name" gorm:"size:50;uniqueIndex"`
Label string `json:"label" gorm:"size:200;uniqueIndex"`
Users []*User `json:"users" gorm:"many2many:user_roles;"`
Permissions []*Permission `json:"permissions" gorm:"many2many:role_permissions;"`
}
type RolePermission ¶
type RolePermission struct {
RoleID uint `json:"-" gorm:"primarykey"`
PermissionID uint `json:"-" gorm:"primarykey"`
Role Role `json:"role"`
Permission Permission `json:"permission"`
}
type SigHandler ¶
type Signals ¶
type Signals struct {
// contains filtered or unexported fields
}
func NewSignals ¶
func NewSignals() *Signals
func (*Signals) Connect ¶
func (s *Signals) Connect(event string, handler SigHandler)
func (*Signals) DisConnect ¶
type TestClient ¶
type TestClient struct {
CookieJar http.CookieJar
Scheme string
Host string
// contains filtered or unexported fields
}
func NewTestClient ¶
func NewTestClient(r http.Handler) (c *TestClient)
func (*TestClient) Call ¶
func (c *TestClient) Call(method, path string, form any, result any) error
func (*TestClient) CallDelete ¶
func (c *TestClient) CallDelete(path string, form, result any) error
func (*TestClient) CallPost ¶
func (c *TestClient) CallPost(path string, form any, result any) error
func (*TestClient) Get ¶
func (c *TestClient) Get(path string) *httptest.ResponseRecorder
Get return *httptest.ResponseRecoder
func (*TestClient) Post ¶
func (c *TestClient) Post(method, path string, body []byte) *httptest.ResponseRecorder
Post return *httptest.ResponseRecoder
func (*TestClient) SendReq ¶
func (c *TestClient) SendReq(path string, req *http.Request) *httptest.ResponseRecorder
type User ¶
type User struct {
ID uint `json:"-" gorm:"primarykey"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
Email string `json:"email" gorm:"size:128;uniqueIndex"`
Password string `json:"-" gorm:"size:128"`
Phone string `json:"phone,omitempty" gorm:"size:64;index"`
FirstName string `json:"firstName,omitempty" gorm:"size:128"`
LastName string `json:"lastName,omitempty" gorm:"size:128"`
DisplayName string `json:"displayName,omitempty" gorm:"size:128"`
IsSuperUser bool `json:"-"`
IsStaff bool `json:"-"`
Enabled bool `json:"-"`
Activated bool `json:"-"`
LastLogin *time.Time `json:"lastLogin,omitempty"`
LastLoginIP string `json:"-" gorm:"size:128"`
Source string `json:"-" gorm:"size:64;index"`
Locale string `json:"locale,omitempty" gorm:"size:20"`
Timezone string `json:"timezone,omitempty" gorm:"size:200"`
Profile *Profile `json:"profile,omitempty"`
AuthToken string `json:"token,omitempty" gorm:"-"`
Groups []*Group `json:"groups" gorm:"many2many:group_members;"`
Roles []*Role `json:"roles" gorm:"many2many:user_roles;"`
}
func CurrentUser ¶
func DecodeHashToken ¶
func (*User) GetProfile ¶
func (*User) GetVisibleName ¶
Source Files
¶
Click to show internal directories.
Click to hide internal directories.