Esempio n. 1
0
        public User CreateOrUpdate(OAuthData oAuthData, string userName, string fullName, string email)
        {
            // Lets find an existing user for the provider OR the email address if the provider doesn't exist.
            User user = DocumentSession
                            .Query<User>()
                            .SingleOrDefault(x =>
                                             x.OAuthData.Any(y => y.Id == oAuthData.Id &&
                                                                  y.OAuthProvider == oAuthData.OAuthProvider)) ??
                        DocumentSession
                            .Query<User>()
                            .SingleOrDefault(x => x.Email == email);

            if (user != null)
            {
                // User exists, so lets update the OAuth data, for this user.
                if (user.OAuthData != null)
                {
                    OAuthData existingProvider =
                        user.OAuthData.SingleOrDefault(x => x.OAuthProvider == oAuthData.OAuthProvider);
                    if (existingProvider != null)
                    {
                        user.OAuthData.Remove(existingProvider);
                    }
                }
                else
                {
                    user.OAuthData = new List<OAuthData>();
                }

                user.OAuthData.Add(oAuthData);
            }
            else
            {
                // Ok. No user at all. We create one and store it.
                user = new User
                           {
                               DisplayName = userName,
                               Email = email,
                               Id = null,
                               FullName = fullName,
                               CreatedOn = DateTime.UtcNow,
                               IsActive = true,
                               OAuthData = new List<OAuthData>(),
                               FavoriteTags = new List<string> {"ravendb", "c#", "asp.net-mvc3"}
                           };
                user.OAuthData.Add(oAuthData);
            }

            DocumentSession.Store(user);

            return user;
        }
Esempio n. 2
0
        public User CreateOrUpdate(OAuthData oAuthData, string userName, string fullName, string email)
        {
            // First, lets see if we have a user with this id, for this provider.
            User existingUser = _documentSession.Query<User>()
                .Where(
                    x =>
                    x.OAuthData.Any(y => y.Id == oAuthData.Id && y.OAuthProvider == oAuthData.OAuthProvider))
                .SingleOrDefault();

            if (existingUser != null)
            {
                // User exist. All is good :)
                return existingUser;
            }

            // No user exists for the OAuth provider and Id. So lets try their email address.
            existingUser = _documentSession.Query<User>()
                .Where(x => x.Email == email)
                .SingleOrDefault();

            if (existingUser != null)
            {
                // User exist, but isn't associated to this OAuthProvider. So lets do that!
                if (existingUser.OAuthData == null)
                {
                    existingUser.OAuthData = new List<OAuthData>();
                }
                existingUser.OAuthData.Add(oAuthData);
                _documentSession.Store(existingUser);
                _documentSession.SaveChanges();
                return existingUser;
            }

            // Ok. No user at all. We create one and store it.
            var newUser = new User
                              {
                                  DisplayName = userName,
                                  Email = email,
                                  Id = null,
                                  FullName = fullName,
                                  OAuthData = new List<OAuthData>()
                              };
            newUser.OAuthData.Add(oAuthData);

            _documentSession.Store(newUser);
            _documentSession.SaveChanges();

            return newUser;
        }