コード例 #1
0
ファイル: person.go プロジェクト: gaego/user
// CreateFromPerson Creates a User from a Person object
func CreateFromPerson(c appengine.Context, p *person.Person) (u *User, err error) {

	if err = validatePerson(p); err != nil {
		return
	}

	// Transaction Action
	err = datastore.RunInTransaction(c, func(c appengine.Context) error {
		// Get the email
		e, err := email.Get(c, p.Email)
		// An error that is not an ErrNoSuchEntity indicates an an internal error
		// and it should be returned.
		if err != nil && err != dserrors.ErrNoSuchEntity {
			return err
		}
		// Lack of an error indicates that the email existing in the ds.
		if err == nil {
			return ErrEmailInUse
		}
		// Create a new User
		u = New()
		u.Person = p
		u.Email = p.Email
		if err = u.Put(c); err != nil {
			return err
		}
		// Update the Email with UserID.
		e.UserID = u.Key.StringID()
		return e.Put(c)
		// XG transation
	}, &datastore.TransactionOptions{XG: true})

	return u, err
}
コード例 #2
0
ファイル: session.go プロジェクト: gaego/user
// CurrentUserIDByEmail returns the userId of the requesting user. Or the userID
// associated with the provided email.
func CurrentUserIDByEmail(r *http.Request, emailAddress string) (string, error) {
	// TODO: User merge if the session UserID is different then the email UserID
	// search session
	sessID, _ := CurrentUserID(r)
	if sessID != "" {
		// TODO: maybe confirm that the UserID exists?
		// There are case where the session may have an incorrect UserID.
		return sessID, nil
	}
	// search by email
	c := context.NewContext(r)
	e, err := email.Get(c, emailAddress)
	if err != nil {
		return "", err
	}
	return e.UserID, nil
}
コード例 #3
0
ファイル: person_test.go プロジェクト: gaego/user
func TestCreateFromPerson(t *testing.T) {
	c := context.NewContext(nil)
	defer tearDown()

	var err error
	var u *User
	var u2 *User
	var e *email.Email

	// Round #1 New User with email & password
	// Save it.
	if u, err = CreateFromPerson(c, p1); err != nil {
		t.Errorf(`err: %v, want nil`, err)
	}

	// Check User
	// Get from ds to confirm save
	if u, err = Get(c, u.Key.StringID()); err != nil {
		t.Errorf(`err: %v, want nil`, err)
	}
	if u.Email != p1.Email {
		t.Errorf(`u.Email: %v, want %v`, u.Email, p1.Email)
	}
	if u.Person.ID != u.Key.StringID() {
		t.Errorf(`u.Person.ID: %v, want %v`, u.Person.ID, u.Key.StringID())
	}
	// Check Email
	if e, err = email.Get(c, u.Email); err != nil {
		t.Errorf(`err: %v, want nil`, err)
	}
	if e.UserID != u.Key.StringID() {
		t.Errorf(`u.UserID: %v, want %v`, e.UserID, u.Key.StringID())
	}

	// Round #2 Existing User with email & password
	// Get it
	if u2, err = UpdateFromPerson(c, u.Person); err != nil {
		t.Errorf(`err: %v, want nil`, err)
	}

	if u2.Key.StringID() != u.Key.StringID() {
		t.Errorf(`u2.Key.StringID: %v, want %v`, u2.Key.StringID(), u.Key.StringID())
	}
}