Example #1
0
// Current returns the current users password object minus the password
func (s *Service) Current(w http.ResponseWriter, r *http.Request,
	args *Args, reply *Args) (err error) {

	c := context.NewContext(r)
	var isSet bool
	userID, _ := user.CurrentUserID(r)
	_, err = profile.Get(c, profile.GenAuthID("Password", userID))
	if err == nil {
		isSet = true
	}
	reply.Password = &Password{IsSet: isSet}
	return nil
}
Example #2
0
// UpdateUser does the following:
//  - Search for an existing user - session -> Profile -> email address
//  - Creates a User or appends the AuthID to the Requesting user's account
//  - Adds the admin role to the User if they are a GAE Admin.
func (p *Profile) UpdateUser(w http.ResponseWriter, r *http.Request) (u *user.User, err error) {

	c := context.NewContext(r)
	if p.Key == nil {
		if p.ProviderName == "" && p.ID == "" {
			return nil, errors.New("auth: key not set")
		}
		p.SetKey(c)
	}
	var saveUser bool // flag indicating that the user needs to be saved.

	// Find the UserID
	// if the AuthProfile doesn't have a UserID look it up. And populate the
	// UserID from the saved profile.
	if p.UserID == "" {
		if p2, err := Get(c, p.Key.StringID()); err == nil {
			p.UserID = p2.UserID
		}
	}
	// look up the UserID in the session
	currentUserID, _ := user.CurrentUserID(r)
	if currentUserID != "" {
		if p.UserID == "" {
			p.UserID = currentUserID
		} else {
			// TODO: User merge
		}
	}

	// If we still don't have a UserID create a new user
	if p.UserID == "" {
		// Create User
		u = user.New()
		// Allocation an new ID
		if err = u.SetKey(c); err != nil {
			return nil, err
		}
		saveUser = true
	} else {
		if u, err = user.Get(c, p.UserID); err != nil {
			// if user is not found we have some type of syncing problem.
			c.Criticalf(`auth: userID: %v was saved to Profile / Session, but was not found in the datastore`, p.UserID)
			return nil, err
		}
	}
	// Add AuthID
	if err = u.AddAuthID(p.Key.StringID()); err == nil {
		saveUser = true
	}
	if p.Person.Email != "" {
		if _, err := u.AddEmail(c, p.Person.Email, 0); err == nil {
			saveUser = true
		}
	}
	// If current user is an admin in GAE add role to User
	if aeuser.IsAdmin(c) {
		// Save the roll to the session
		_ = user.CurrentUserSetRole(w, r, "admin", true)
		// Add the role to the user's roles.
		if err = u.AddRole("admin"); err == nil {
			saveUser = true
		}
	}
	if saveUser {
		if err = u.Put(c); err != nil {
			return nil, err
		}
	}
	p.UserID = u.Key.StringID()
	return u, nil
}