user_vm.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2014 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. // +build !appengine
  5. package user
  6. import (
  7. "golang.org/x/net/context"
  8. "google.golang.org/appengine/internal"
  9. )
  10. // Current returns the currently logged-in user,
  11. // or nil if the user is not signed in.
  12. func Current(c context.Context) *User {
  13. h := internal.IncomingHeaders(c)
  14. u := &User{
  15. Email: h.Get("X-AppEngine-User-Email"),
  16. AuthDomain: h.Get("X-AppEngine-Auth-Domain"),
  17. ID: h.Get("X-AppEngine-User-Id"),
  18. Admin: h.Get("X-AppEngine-User-Is-Admin") == "1",
  19. FederatedIdentity: h.Get("X-AppEngine-Federated-Identity"),
  20. FederatedProvider: h.Get("X-AppEngine-Federated-Provider"),
  21. }
  22. if u.Email == "" && u.FederatedIdentity == "" {
  23. return nil
  24. }
  25. return u
  26. }
  27. // IsAdmin returns true if the current user is signed in and
  28. // is currently registered as an administrator of the application.
  29. func IsAdmin(c context.Context) bool {
  30. h := internal.IncomingHeaders(c)
  31. return h.Get("X-AppEngine-User-Is-Admin") == "1"
  32. }