Files
GoETAAPI/app/libs/database/schemas/rbac/rbac.go

125 lines
4.7 KiB
Go

/**
* @file schema.rbac.go
* @brief RBAC schema models for GORM
*
* This file defines the base database models used by the RBAC
* (Role-Based Access Control) system. Models map to the following
* tables: user_types, users, auth_identities, auth_credentials,
* roles, permissions, role_permissions and user_roles.
*
* The structs include GORM tags for column names and relationships:
* - UserType: types of users.
* - User: main user record; links to UserType, AuthIdentity and UserRole.
* - AuthIdentity: external identity providers; links to AuthCredential.
* - AuthCredential: stored credentials for an identity.
* - Role: role definitions and their permissions and assigned users.
* - Permission: permission definitions.
* - RolePermission: join table between Role and Permission.
* - UserRole: join table between User and Role with optional expiration.
*
* These models are intended for use with GORM to perform ORM operations
* against the RBAC schema.
*/
package rbac
import (
"time"
)
type UserType struct {
ID uint `gorm:"primaryKey;column:id"`
Name string `gorm:"type:text;column:name"`
Description *string `gorm:"type:text;column:description"`
}
func (UserType) TableName() string { return "user_types" }
type User struct {
ID uint `gorm:"primaryKey;column:id"`
UserTypeID uint `gorm:"column:user_type"`
Name string `gorm:"type:text;column:name"`
LastName string `gorm:"type:text;column:last_name"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
UserType UserType `gorm:"foreignKey:UserTypeID;references:ID"`
AuthIdentities []AuthIdentity `gorm:"foreignKey:UserID;references:ID"`
UserRoles []UserRole `gorm:"foreignKey:UserID;references:ID"`
}
func (User) TableName() string { return "users" }
type AuthIdentity struct {
ID uint `gorm:"primaryKey;column:id"`
UserID uint `gorm:"column:user_id"`
Provider string `gorm:"type:text;column:provider"`
Identifier string `gorm:"type:text;column:identifier"`
IsPrimary bool `gorm:"column:is_primary"`
IsVerified bool `gorm:"column:is_verified"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
User User `gorm:"foreignKey:UserID;references:ID"`
Credentials []AuthCredential `gorm:"foreignKey:IdentityID;references:ID"`
}
func (AuthIdentity) TableName() string { return "auth_identities" }
type AuthCredential struct {
ID uint `gorm:"primaryKey;column:id"`
IdentityID uint `gorm:"column:identity_id"`
Password string `gorm:"type:text;column:password"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
Identity AuthIdentity `gorm:"foreignKey:IdentityID;references:ID"`
}
func (AuthCredential) TableName() string { return "auth_credentials" }
type Role struct {
ID uint `gorm:"primaryKey;column:id"`
Name string `gorm:"type:text;column:name"`
Description *string `gorm:"type:text;column:description"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
RolePermissions []RolePermission `gorm:"foreignKey:RoleID;references:ID"`
UserRoles []UserRole `gorm:"foreignKey:RoleID;references:ID"`
}
func (Role) TableName() string { return "roles" }
type Permission struct {
ID uint `gorm:"primaryKey;column:id"`
Name string `gorm:"type:text;column:name"`
Description *string `gorm:"type:text;column:description"`
RolePermissions []RolePermission `gorm:"foreignKey:PermissionID;references:ID"`
}
func (Permission) TableName() string { return "permissions" }
type RolePermission struct {
ID uint `gorm:"primaryKey;column:id"`
RoleID uint `gorm:"column:role_id"`
PermissionID uint `gorm:"column:permission_id"`
Role Role `gorm:"foreignKey:RoleID;references:ID"`
Permission Permission `gorm:"foreignKey:PermissionID;references:ID"`
}
func (RolePermission) TableName() string { return "role_permissions" }
type UserRole struct {
ID uint `gorm:"primaryKey;column:id"`
UserID uint `gorm:"column:user_id"`
RoleID uint `gorm:"column:role_id"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
ExpiresAt *time.Time `gorm:"column:expires_at"`
User User `gorm:"foreignKey:UserID;references:ID"`
Role Role `gorm:"foreignKey:RoleID;references:ID"`
}
func (UserRole) TableName() string { return "user_roles" }