answer/plugin/user_center.go

104 lines
3.1 KiB
Go
Raw Normal View History

2023-03-29 17:12:45 +08:00
package plugin
type UserCenter interface {
Base
// Description returns the description of the user center, including the name, icon, url, etc.
2023-03-29 17:12:45 +08:00
Description() UserCenterDesc
// ControlCenterItems returns the items that will be displayed in the control center
2023-03-29 17:12:45 +08:00
ControlCenterItems() []ControlCenter
// LoginCallback is called when the user center login callback is called
2023-03-29 17:12:45 +08:00
LoginCallback(ctx *GinContext) (userInfo *UserCenterBasicUserInfo, err error)
// SignUpCallback is called when the user center sign up callback is called
2023-03-29 17:12:45 +08:00
SignUpCallback(ctx *GinContext) (userInfo *UserCenterBasicUserInfo, err error)
// UserInfo returns the user information
2023-03-29 17:12:45 +08:00
UserInfo(externalID string) (userInfo *UserCenterBasicUserInfo, err error)
// UserList returns the user list information
2023-03-29 17:12:45 +08:00
UserList(externalIDs []string) (userInfo []*UserCenterBasicUserInfo, err error)
// UserSettings returns the user settings
2023-03-29 17:12:45 +08:00
UserSettings(externalID string) (userSettings *SettingInfo, err error)
// PersonalBranding returns the personal branding information
2023-03-29 17:12:45 +08:00
PersonalBranding(externalID string) (branding []*PersonalBranding)
// AfterLogin is called after the user logs in
AfterLogin(externalID, accessToken string)
2023-03-29 17:12:45 +08:00
}
type UserCenterDesc struct {
Name string `json:"name"`
Icon string `json:"icon"`
Url string `json:"url"`
LoginRedirectURL string `json:"login_redirect_url"`
SignUpRedirectURL string `json:"sign_up_redirect_url"`
RankAgentEnabled bool `json:"rank_agent_enabled"`
RoleAgentEnabled bool `json:"role_agent_enabled"`
MustAuthEmailEnabled bool `json:"must_auth_email_enabled"`
2023-03-29 17:12:45 +08:00
}
type UserStatus int
const (
UserStatusAvailable UserStatus = 1
UserStatusSuspended UserStatus = 9
UserStatusDeleted UserStatus = 10
)
2023-03-29 17:12:45 +08:00
type UserCenterBasicUserInfo struct {
ExternalID string `json:"external_id"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
Email string `json:"email"`
Rank int `json:"rank"`
Avatar string `json:"avatar"`
Mobile string `json:"mobile"`
Bio string `json:"bio"`
Status UserStatus `json:"status"`
2023-03-29 17:12:45 +08:00
}
type ControlCenter struct {
Name string `json:"name"`
Label string `json:"label"`
Url string `json:"url"`
}
type SettingInfo struct {
ProfileSettingRedirectURL string `json:"profile_setting_redirect_url"`
AccountSettingRedirectURL string `json:"account_setting_redirect_url"`
}
type PersonalBranding struct {
Icon string `json:"icon"`
Name string `json:"name"`
Label string `json:"label"`
Url string `json:"url"`
}
var (
// CallUserCenter is a function that calls all registered parsers
CallUserCenter,
registerUserCenter = MakePlugin[UserCenter](false)
)
func UserCenterEnabled() (enabled bool) {
_ = CallUserCenter(func(fn UserCenter) error {
enabled = true
return nil
})
return
}
func RankAgentEnabled() (enabled bool) {
_ = CallUserCenter(func(fn UserCenter) error {
enabled = fn.Description().RankAgentEnabled
return nil
})
return
}
func GetUserCenter() (uc UserCenter, ok bool) {
_ = CallUserCenter(func(fn UserCenter) error {
uc = fn
ok = true
return nil
})
return
}