mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-05-17 09:40:14 +00:00
refactor: simplify context getter functions
This commit is contained in:
+24
-69
@@ -165,100 +165,55 @@ func (c *UserContext) NewFromSession(session *repository.Session) (*UserContext,
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *UserContext) GetUsername() string {
|
func (c *UserContext) getBaseContext() *BaseContext {
|
||||||
switch c.Provider {
|
switch c.Provider {
|
||||||
case ProviderLocal:
|
case ProviderLocal, ProviderBasicAuth:
|
||||||
if c.Local == nil {
|
if c.Local == nil {
|
||||||
return ""
|
return nil
|
||||||
}
|
}
|
||||||
return c.Local.Username
|
return &c.Local.BaseContext
|
||||||
case ProviderLDAP:
|
case ProviderLDAP:
|
||||||
if c.LDAP == nil {
|
if c.LDAP == nil {
|
||||||
return ""
|
return nil
|
||||||
}
|
}
|
||||||
return c.LDAP.Username
|
return &c.LDAP.BaseContext
|
||||||
case ProviderBasicAuth:
|
|
||||||
if c.Local == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return c.Local.Username
|
|
||||||
case ProviderOAuth:
|
case ProviderOAuth:
|
||||||
if c.OAuth == nil {
|
if c.OAuth == nil {
|
||||||
return ""
|
return nil
|
||||||
}
|
}
|
||||||
return c.OAuth.Username
|
return &c.OAuth.BaseContext
|
||||||
case ProviderTailscale:
|
case ProviderTailscale:
|
||||||
if c.Tailscale == nil {
|
if c.Tailscale == nil {
|
||||||
return ""
|
return nil
|
||||||
}
|
}
|
||||||
return c.Tailscale.Username
|
return &c.Tailscale.BaseContext
|
||||||
default:
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *UserContext) GetUsername() string {
|
||||||
|
base := c.getBaseContext()
|
||||||
|
if base == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
return base.Username
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *UserContext) GetEmail() string {
|
func (c *UserContext) GetEmail() string {
|
||||||
switch c.Provider {
|
base := c.getBaseContext()
|
||||||
case ProviderLocal:
|
if base == nil {
|
||||||
if c.Local == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return c.Local.Email
|
|
||||||
case ProviderLDAP:
|
|
||||||
if c.LDAP == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return c.LDAP.Email
|
|
||||||
case ProviderBasicAuth:
|
|
||||||
if c.Local == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return c.Local.Email
|
|
||||||
case ProviderOAuth:
|
|
||||||
if c.OAuth == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return c.OAuth.Email
|
|
||||||
case ProviderTailscale:
|
|
||||||
if c.Tailscale == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return c.Tailscale.Email
|
|
||||||
default:
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
return base.Email
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *UserContext) GetName() string {
|
func (c *UserContext) GetName() string {
|
||||||
switch c.Provider {
|
base := c.getBaseContext()
|
||||||
case ProviderLocal:
|
if base == nil {
|
||||||
if c.Local == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return c.Local.Name
|
|
||||||
case ProviderLDAP:
|
|
||||||
if c.LDAP == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return c.LDAP.Name
|
|
||||||
case ProviderBasicAuth:
|
|
||||||
if c.Local == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return c.Local.Name
|
|
||||||
case ProviderOAuth:
|
|
||||||
if c.OAuth == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return c.OAuth.Name
|
|
||||||
case ProviderTailscale:
|
|
||||||
if c.Tailscale == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return c.Tailscale.Name
|
|
||||||
default:
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
return base.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *UserContext) GetProviderID() string {
|
func (c *UserContext) GetProviderID() string {
|
||||||
|
|||||||
Reference in New Issue
Block a user