func create(c appengine.Context, pass string, pers *person.Person, userID string) ( pf *profile.Profile, err error) { var id string if userID == "" { u := user.New() u.SetKey(c) if err = u.Put(c); err != nil { return } id = u.Key.StringID() } else { id = userID } pf = profile.New("Password", "") pf.ID = id pf.UserID = id pf.Auth, _ = GenerateFromPassword([]byte(pass)) pf.Person = pers return }
// 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 }