Check user in security context before getting the ID

This commit fix a potential nil pointer issue when trying to read User
ID from security context

Signed-off-by: Daniel Jiang <jiangd@vmware.com>
This commit is contained in:
Daniel Jiang 2021-05-18 14:54:58 +08:00
parent 0a8ff4c1f9
commit ebe5733f27

View File

@ -113,7 +113,7 @@ func (c *controller) Get(ctx context.Context, id int, opt *Option) (*models.User
return nil, fmt.Errorf("can't find security context") return nil, fmt.Errorf("can't find security context")
} }
lsc, ok := sctx.(*local.SecurityContext) lsc, ok := sctx.(*local.SecurityContext)
if ok && lsc.User().UserID == id { if ok && lsc.User() != nil && lsc.User().UserID == id {
u.AdminRoleInAuth = lsc.User().AdminRoleInAuth u.AdminRoleInAuth = lsc.User().AdminRoleInAuth
} }
if opt != nil && opt.WithOIDCInfo { if opt != nil && opt.WithOIDCInfo {