Exemple #1
0
func CurrentUser(c appengine.Context) *User {
	appengineUser := user.Current(c)
	if appengineUser == nil {
		return Anonymous
	}

	u, err := GetUserByUserId(c, appengineUser.ID)
	if err != nil {
		return Anonymous
	}

	if u == nil {
		u, err = CreateUserFromAppengine(c, appengineUser)
		if err != nil {
			return Anonymous
		}
	}

	if user.IsAdmin(c) && !u.IsAdmin {
		u.IsAdmin = true
		// ignore error
		entity.Put(c, u)
	}

	return u
}
Exemple #2
0
func CreateUserFromAppengine(c appengine.Context, appengineUser *user.User) (*User, error) {
	u := &User{
		UserId: appengineUser.ID,

		Name:       appengineUser.String(),
		Email:      appengineUser.Email,
		AuthDomain: appengineUser.AuthDomain,
		IsAdmin:    user.IsAdmin(c),

		FederatedIdentity: appengineUser.FederatedIdentity,
		FederatedProvider: appengineUser.FederatedProvider,
	}
	initUser(u)
	if err := entity.Put(c, u); err != nil {
		return nil, err
	}
	return u, nil
}