Skip to content

Commit 8fad270

Browse files
committed
refactor: simplify context getter functions
1 parent 32595e3 commit 8fad270

1 file changed

Lines changed: 24 additions & 69 deletions

File tree

internal/model/context.go

Lines changed: 24 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -165,100 +165,55 @@ func (c *UserContext) NewFromSession(session *repository.Session) (*UserContext,
165165
return c, nil
166166
}
167167

168-
func (c *UserContext) GetUsername() string {
168+
func (c *UserContext) getBaseContext() *BaseContext {
169169
switch c.Provider {
170-
case ProviderLocal:
170+
case ProviderLocal, ProviderBasicAuth:
171171
if c.Local == nil {
172-
return ""
172+
return nil
173173
}
174-
return c.Local.Username
174+
return &c.Local.BaseContext
175175
case ProviderLDAP:
176176
if c.LDAP == nil {
177-
return ""
178-
}
179-
return c.LDAP.Username
180-
case ProviderBasicAuth:
181-
if c.Local == nil {
182-
return ""
177+
return nil
183178
}
184-
return c.Local.Username
179+
return &c.LDAP.BaseContext
185180
case ProviderOAuth:
186181
if c.OAuth == nil {
187-
return ""
182+
return nil
188183
}
189-
return c.OAuth.Username
184+
return &c.OAuth.BaseContext
190185
case ProviderTailscale:
191186
if c.Tailscale == nil {
192-
return ""
187+
return nil
193188
}
194-
return c.Tailscale.Username
189+
return &c.Tailscale.BaseContext
195190
default:
191+
return nil
192+
}
193+
}
194+
195+
func (c *UserContext) GetUsername() string {
196+
base := c.getBaseContext()
197+
if base == nil {
196198
return ""
197199
}
200+
return base.Username
198201
}
199202

200203
func (c *UserContext) GetEmail() string {
201-
switch c.Provider {
202-
case ProviderLocal:
203-
if c.Local == nil {
204-
return ""
205-
}
206-
return c.Local.Email
207-
case ProviderLDAP:
208-
if c.LDAP == nil {
209-
return ""
210-
}
211-
return c.LDAP.Email
212-
case ProviderBasicAuth:
213-
if c.Local == nil {
214-
return ""
215-
}
216-
return c.Local.Email
217-
case ProviderOAuth:
218-
if c.OAuth == nil {
219-
return ""
220-
}
221-
return c.OAuth.Email
222-
case ProviderTailscale:
223-
if c.Tailscale == nil {
224-
return ""
225-
}
226-
return c.Tailscale.Email
227-
default:
204+
base := c.getBaseContext()
205+
if base == nil {
228206
return ""
229207
}
208+
return base.Email
230209
}
231210

232211
func (c *UserContext) GetName() string {
233-
switch c.Provider {
234-
case ProviderLocal:
235-
if c.Local == nil {
236-
return ""
237-
}
238-
return c.Local.Name
239-
case ProviderLDAP:
240-
if c.LDAP == nil {
241-
return ""
242-
}
243-
return c.LDAP.Name
244-
case ProviderBasicAuth:
245-
if c.Local == nil {
246-
return ""
247-
}
248-
return c.Local.Name
249-
case ProviderOAuth:
250-
if c.OAuth == nil {
251-
return ""
252-
}
253-
return c.OAuth.Name
254-
case ProviderTailscale:
255-
if c.Tailscale == nil {
256-
return ""
257-
}
258-
return c.Tailscale.Name
259-
default:
212+
base := c.getBaseContext()
213+
if base == nil {
260214
return ""
261215
}
216+
return base.Name
262217
}
263218

264219
func (c *UserContext) GetProviderID() string {

0 commit comments

Comments
 (0)