Some checks failed
CI / Build & Test (push) Failing after 32s
Add explicit foreignKey/references tags to Role.Permissions association so GORM uses only ID (not composite ID+TenantID) for the join table.
83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
package domain
|
|
|
|
import "time"
|
|
|
|
type Role struct {
|
|
ID uint `json:"id" gorm:"primaryKey"`
|
|
TenantID string `json:"tenant_id" gorm:"type:uuid;not null;primaryKey;index:idx_tenant_role_name,unique"`
|
|
Name string `json:"name" gorm:"size:64;not null;index:idx_tenant_role_name,unique"`
|
|
Description string `json:"description" gorm:"size:256"`
|
|
IsSystem bool `json:"is_system" gorm:"default:false"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
|
|
Permissions []Permission `json:"permissions,omitempty" gorm:"many2many:role_permissions;foreignKey:ID;references:ID;joinForeignKey:RoleID;joinReferences:PermissionID"`
|
|
}
|
|
|
|
func (Role) TableName() string {
|
|
return "roles"
|
|
}
|
|
|
|
type RolePermission struct {
|
|
RoleID uint `gorm:"primaryKey"`
|
|
PermissionID uint `gorm:"primaryKey"`
|
|
}
|
|
|
|
func (RolePermission) TableName() string {
|
|
return "role_permissions"
|
|
}
|
|
|
|
type UserRole struct {
|
|
UserID uint `json:"user_id" gorm:"primaryKey"`
|
|
TenantID string `json:"tenant_id" gorm:"type:uuid;not null;primaryKey"`
|
|
RoleID uint `json:"role_id" gorm:"not null"`
|
|
}
|
|
|
|
func (UserRole) TableName() string {
|
|
return "user_roles"
|
|
}
|
|
|
|
type RoleDTO struct {
|
|
ID uint `json:"id"`
|
|
TenantID string `json:"tenant_id"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
IsSystem bool `json:"is_system"`
|
|
Permissions []PermissionDTO `json:"permissions,omitempty"`
|
|
}
|
|
|
|
func ToRoleDTO(r *Role) RoleDTO {
|
|
dto := RoleDTO{
|
|
ID: r.ID,
|
|
TenantID: r.TenantID,
|
|
Name: r.Name,
|
|
Description: r.Description,
|
|
IsSystem: r.IsSystem,
|
|
}
|
|
if r.Permissions != nil {
|
|
dto.Permissions = make([]PermissionDTO, len(r.Permissions))
|
|
for i, p := range r.Permissions {
|
|
dto.Permissions[i] = ToPermissionDTO(&p)
|
|
}
|
|
}
|
|
return dto
|
|
}
|
|
|
|
type CreateRoleRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
type UpdateRoleRequest struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
type SetRolePermissionsRequest struct {
|
|
PermissionIDs []uint `json:"permission_ids" binding:"required"`
|
|
}
|
|
|
|
type AssignRoleRequest struct {
|
|
RoleID uint `json:"role_id" binding:"required"`
|
|
}
|