init: 1.0.0

This commit is contained in:
Chang lue Tsen
2025-04-25 12:08:29 +09:00
commit 8addcc584b
1031 changed files with 76472 additions and 0 deletions
+272
View File
@@ -0,0 +1,272 @@
package auth
import (
"encoding/json"
"time"
)
type Auth struct {
Id int64 `gorm:"primaryKey"`
Method string `gorm:"unique;type:varchar(255);not null;default:'';comment:platform"`
Config string `gorm:"type:text;not null;comment:Auth Configuration"`
Enabled *bool `gorm:"type:tinyint(1);not null;default:false;comment:Is Enabled"`
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
UpdatedAt time.Time `gorm:"comment:Update Time"`
}
func (Auth) TableName() string {
return "auth_method"
}
type AppleAuthConfig struct {
TeamID string `json:"team_id"`
KeyID string `json:"key_id"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectURL string `json:"redirect_url"`
}
func (l *AppleAuthConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(AppleAuthConfig))
}
return string(bytes)
}
func (l *AppleAuthConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), &l)
}
type GoogleAuthConfig struct {
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectURL string `json:"redirect_url"`
}
func (l *GoogleAuthConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(GoogleAuthConfig))
}
return string(bytes)
}
func (l *GoogleAuthConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), &l)
}
type GithubAuthConfig struct {
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectURL string `json:"redirect_url"`
}
func (l *GithubAuthConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(GithubAuthConfig))
}
return string(bytes)
}
func (l *GithubAuthConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), &l)
}
type FacebookAuthConfig struct {
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectURL string `json:"redirect_url"`
}
func (l *FacebookAuthConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(FacebookAuthConfig))
}
return string(bytes)
}
func (l *FacebookAuthConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), &l)
}
type TelegramAuthConfig struct {
BotToken string `json:"bot_token"`
EnableNotify bool `json:"enable_notify"`
WebHookDomain string `json:"webhook_domain"`
}
func (l *TelegramAuthConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(TelegramAuthConfig))
}
return string(bytes)
}
func (l *TelegramAuthConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), &l)
}
type EmailAuthConfig struct {
Platform string `json:"platform"`
PlatformConfig interface{} `json:"platform_config"`
EnableVerify bool `json:"enable_verify"`
EnableNotify bool `json:"enable_notify"`
EnableDomainSuffix bool `json:"enable_domain_suffix"`
DomainSuffixList string `json:"domain_suffix_list"`
VerifyEmailTemplate string `json:"verify_email_template"`
ExpirationEmailTemplate string `json:"expiration_email_template"`
MaintenanceEmailTemplate string `json:"maintenance_email_template"`
TrafficExceedEmailTemplate string `json:"traffic_exceed_email_template"`
}
func (l *EmailAuthConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(EmailAuthConfig))
}
return string(bytes)
}
func (l *EmailAuthConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), &l)
}
// SMTPConfig Email SMTP configuration
type SMTPConfig struct {
Host string `json:"host"`
Port int `json:"port"`
User string `json:"user"`
Pass string `json:"pass"`
From string `json:"from"`
SSL bool `json:"ssl"`
}
func (l *SMTPConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(SMTPConfig))
}
return string(bytes)
}
func (l *SMTPConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), &l)
}
type MobileAuthConfig struct {
Platform string `json:"platform"`
PlatformConfig interface{} `json:"platform_config"`
EnableWhitelist bool `json:"enable_whitelist"`
Whitelist []string `json:"whitelist"`
}
func (l *MobileAuthConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(MobileAuthConfig))
}
return string(bytes)
}
func (l *MobileAuthConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), &l)
}
type AlibabaCloudConfig struct {
Access string `json:"access"`
Secret string `json:"secret"`
SignName string `json:"sign_name"`
Endpoint string `json:"endpoint"`
TemplateCode string `json:"template_code"`
}
func (l *AlibabaCloudConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(AlibabaCloudConfig))
}
return string(bytes)
}
func (l *AlibabaCloudConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), l)
}
type SmsbaoConfig struct {
Access string `json:"access"`
Secret string `json:"secret"`
Template string `json:"template"`
}
func (l *SmsbaoConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(SmsbaoConfig))
}
return string(bytes)
}
func (l *SmsbaoConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), l)
}
type AbosendConfig struct {
ApiDomain string `json:"api_domain"`
Access string `json:"access"`
Secret string `json:"secret"`
Template string `json:"template"`
}
func (l *AbosendConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(AbosendConfig))
}
return string(bytes)
}
func (l *AbosendConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), l)
}
type TwilioConfig struct {
Access string `json:"access"`
Secret string `json:"secret"`
PhoneNumber string `json:"phone_number"`
Template string `json:"template"`
}
func (l *TwilioConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(TwilioConfig))
}
return string(bytes)
}
func (l *TwilioConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), l)
}
type DeviceConfig struct {
ShowAds bool `json:"show_ads"`
OnlyRealDevice bool `json:"only_real_device"`
EnableSecurity bool `json:"enable_security"`
SecuritySecret string `json:"security_secret"`
}
func (l *DeviceConfig) Marshal() string {
bytes, err := json.Marshal(l)
if err != nil {
bytes, _ = json.Marshal(new(DeviceConfig))
}
return string(bytes)
}
func (l *DeviceConfig) Unmarshal(data string) error {
return json.Unmarshal([]byte(data), l)
}
+30
View File
@@ -0,0 +1,30 @@
package auth
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAlibabaCloudConfig_Marshal(t *testing.T) {
v := new(AlibabaCloudConfig)
t.Log(v.Marshal())
}
func TestAlibabaCloudConfig_Unmarshal(t *testing.T) {
cfg := AlibabaCloudConfig{
Access: "AccessKeyId",
Secret: "AccessKeySecret",
SignName: "SignName",
Endpoint: "Endpoint",
TemplateCode: "VerifyTemplateCode",
}
data := cfg.Marshal()
v := new(AlibabaCloudConfig)
err := v.Unmarshal(data)
if err != nil {
t.Fatal(err.Error())
}
assert.Equal(t, "AccessKeyId", v.Access)
}
+120
View File
@@ -0,0 +1,120 @@
package auth
import (
"context"
"errors"
"fmt"
"github.com/perfect-panel/ppanel-server/pkg/cache"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
var _ Model = (*customAuthModel)(nil)
var (
cacheAuthIdPrefix = "cache:auth:id:"
cacheAuthMethodPrefix = "cache:auth:method:"
)
type (
Model interface {
authModel
customAuthLogicModel
}
authModel interface {
Insert(ctx context.Context, data *Auth) error
FindOne(ctx context.Context, id int64) (*Auth, error)
Update(ctx context.Context, data *Auth) error
Delete(ctx context.Context, id int64) error
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
}
customAuthModel struct {
*defaultAuthModel
}
defaultAuthModel struct {
cache.CachedConn
table string
}
)
func newAuthModel(db *gorm.DB, c *redis.Client) *defaultAuthModel {
return &defaultAuthModel{
CachedConn: cache.NewConn(db, c),
table: "`auth_config`",
}
}
//nolint:unused
func (m *defaultAuthModel) batchGetCacheKeys(Auths ...*Auth) []string {
var keys []string
for _, auth := range Auths {
keys = append(keys, m.getCacheKeys(auth)...)
}
return keys
}
func (m *defaultAuthModel) getCacheKeys(data *Auth) []string {
if data == nil {
return []string{}
}
authIdKey := fmt.Sprintf("%s%v", cacheAuthIdPrefix, data.Id)
platformKey := fmt.Sprintf("%s%s", cacheAuthMethodPrefix, data.Method)
cacheKeys := []string{
authIdKey,
platformKey,
}
return cacheKeys
}
func (m *defaultAuthModel) Insert(ctx context.Context, data *Auth) error {
err := m.ExecCtx(ctx, func(conn *gorm.DB) error {
return conn.Create(&data).Error
}, m.getCacheKeys(data)...)
return err
}
func (m *defaultAuthModel) FindOne(ctx context.Context, id int64) (*Auth, error) {
AuthIdKey := fmt.Sprintf("%s%v", cacheAuthIdPrefix, id)
var resp Auth
err := m.QueryCtx(ctx, &resp, AuthIdKey, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Auth{}).Where("`id` = ?", id).First(&resp).Error
})
switch {
case err == nil:
return &resp, nil
default:
return nil, err
}
}
func (m *defaultAuthModel) Update(ctx context.Context, data *Auth) error {
old, err := m.FindOne(ctx, data.Id)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
db := conn
return db.Save(data).Error
}, m.getCacheKeys(old)...)
return err
}
func (m *defaultAuthModel) Delete(ctx context.Context, id int64) error {
data, err := m.FindOne(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return err
}
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
db := conn
return db.Delete(&Auth{}, id).Error
}, m.getCacheKeys(data)...)
return err
}
func (m *defaultAuthModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
return m.TransactCtx(ctx, fn)
}
+60
View File
@@ -0,0 +1,60 @@
package auth
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
type customAuthLogicModel interface {
GetAuthListByPage(ctx context.Context) ([]*Auth, error)
FindOneByMethod(ctx context.Context, platform string) (*Auth, error)
FindAll(ctx context.Context) ([]*Auth, error)
}
// NewModel returns a model for the database table.
func NewModel(conn *gorm.DB, c *redis.Client) Model {
return &customAuthModel{
defaultAuthModel: newAuthModel(conn, c),
}
}
type Filter struct {
Show *bool
Pinned *bool
Popup *bool
Search string
}
// GetAuthListByPage get auth list by page
func (m *customAuthModel) GetAuthListByPage(ctx context.Context) ([]*Auth, error) {
var list []*Auth
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
conn = conn.Model(&Auth{})
return conn.Find(v).Error
})
return list, err
}
// FindOneByMethod find one by method
func (m *customAuthModel) FindOneByMethod(ctx context.Context, method string) (*Auth, error) {
key := fmt.Sprintf("%s%s", cacheAuthMethodPrefix, method)
var data Auth
err := m.QueryCtx(ctx, &data, key, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Auth{}).Where("method = ?", method).First(v).Error
})
return &data, err
}
// FindAll find all
func (m *customAuthModel) FindAll(ctx context.Context) ([]*Auth, error) {
var list []*Auth
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
conn = conn.Model(&Auth{})
return conn.Find(v).Error
})
return list, err
}