Exemplo n.º 1
0
        private User AppHarborCreateOrUpdateAccountIfNeeded(string AccessToken, AppHarbor.Client.User user, User returnUser)
        {
            //TODO: must have some kind of AppHb unique id-- username, etc --see twitter approach (screenname) (for now we used emailaddress)
            if (null == returnUser)
            {
                returnUser = (from u in database.Users
                              where u.AuthenticationAccounts.FirstOrDefault(ac => ac.AccountProvider == "appharbor" && ac.UserName == user.UserName) != null
                              select u).FirstOrDefault();
            }
            if (null == returnUser) // CREATE
            {
                var existingUser = (from u in database.Users where u.UserName.ToLower() == user.UserName.ToLower() select u).FirstOrDefault();
                returnUser              = new User();
                returnUser.UserName     = null == existingUser ? user.UserName : "";
                returnUser.EmailAddress = user.EmailAddress;
                returnUser.UpdateUniqueId();
                database.Users.Add(returnUser);
            }

            var newAppHarborAccount = returnUser.AuthenticationAccounts.FirstOrDefault(ac => ac.AccountProvider == "appharbor" &&
                                                                                       ac.UserName == user.UserName);

            if (newAppHarborAccount == null)
            {
                newAppHarborAccount = new UserAuthenticationAccount();
                newAppHarborAccount.AccountProvider = "appharbor";
                newAppHarborAccount.UserName        = user.UserName;
                newAppHarborAccount.UserName        = user.UserName;
                newAppHarborAccount.ProfilePicUrl   = "<not implemented>";
                if (null == returnUser.AuthenticationAccounts)
                {
                    returnUser.AuthenticationAccounts = new Collection <UserAuthenticationAccount>();
                }
                returnUser.AuthenticationAccounts.Add(newAppHarborAccount);
            }

            //returnUser.UserProfilePicUrl = user.ProfileImageUrl;
            var appharborAccount = returnUser.AuthenticationAccounts.First(t => t.AccountProvider == "appharbor");

            appharborAccount.OAuthToken = AccessToken;
            //appharborAccount.OAuthTokenSecret = accessToken.TokenSecret;
            //appharborAccount.ProfilePicUrl = user.ProfileImageUrl;

            try
            {
                database.SaveChanges();
            }
            catch (Exception e)
            {
                Trace.WriteLine("Exception: " + e.Message);
                throw e;
            }
            return(returnUser);
        }
Exemplo n.º 2
0
        private User TwitterCreateOrUpdateAccountIfNeeded(OAuthAccessToken accessToken, TwitterUser user, User returnUser)
        {
            // If not passed a user, let's query to find out if
            // we already have a master user for this twitter user
            if (null == returnUser)
            {
                returnUser = (from u in database.Users
                              where u.AuthenticationAccounts.FirstOrDefault(ac => ac.AccountProvider == "twitter" && ac.UserName == user.ScreenName) != null
                              select u).FirstOrDefault();
            }
            else
            {
                var otherUser = (from u in database.Users
                                 where u.AuthenticationAccounts.FirstOrDefault(ac => ac.AccountProvider == "twitter" && ac.UserName == user.ScreenName) != null &&
                                 u.UserId != returnUser.UserId
                                 select u).FirstOrDefault();

                if (null != otherUser)
                {
                    // This twitter account is owned by another user
                    // we need to merge the data
                    MergeUsers(returnUser, otherUser, user);
                }
            }

            // If we're still short a user account, we will create one here
            if (null == returnUser) // CREATE
            {
                returnUser              = new User();
                returnUser.UserName     = user.ScreenName;
                returnUser.EmailAddress = "";
                returnUser.UpdateUniqueId();
                database.Users.Add(returnUser);
            }

            // Now we will pull our actual twitter account, it if exists
            // If it doesn't, we will create it
            UserAuthenticationAccount twitterAccount = returnUser.
                                                       AuthenticationAccounts.FirstOrDefault(a => a.AccountProvider == "twitter" && a.UserName == user.ScreenName);

            if (twitterAccount == null)
            {
                twitterAccount = new UserAuthenticationAccount();
                twitterAccount.AccountProvider = "twitter";

                twitterAccount.UserName      = user.ScreenName;
                twitterAccount.ProfilePicUrl = user.ProfileImageUrl;
                if (null == returnUser.AuthenticationAccounts)
                {
                    returnUser.AuthenticationAccounts = new Collection <UserAuthenticationAccount>();
                }
                returnUser.AuthenticationAccounts.Add(twitterAccount);
            }

            // We'll update some information here
            if (string.IsNullOrEmpty(returnUser.UserProfilePicUrl))
            {
                returnUser.UserProfilePicUrl = user.ProfileImageUrl;
            }
            twitterAccount.OAuthToken       = accessToken.Token;
            twitterAccount.OAuthTokenSecret = accessToken.TokenSecret;
            twitterAccount.ProfilePicUrl    = user.ProfileImageUrl;

            try
            {
                database.SaveChanges();
            }
            catch (Exception e)
            {
                Trace.WriteLine("Exception: " + e.Message);
                throw e;
            }
            return(returnUser);
        }