Exemple #1
0
        Models.User IOAuthSignInClient.OAuthCallback(HttpRequestBase Request)
        {
            if (string.IsNullOrEmpty(Request["denied"]))
            {
                var requestToken = new OAuthRequestToken {
                    Token = Request["oauth_token"]
                };

                // Step 3 - Exchange the Request Token for an Access Token
                OAuthAccessToken accessToken = twitterService.GetAccessToken(requestToken, Request["oauth_verifier"]);
                // Step 4 - User authenticates using the Access Token
                twitterService.AuthenticateWith(accessToken.Token, accessToken.TokenSecret);

                TwitterUser user = twitterService.VerifyCredentials();
                if (null != user)
                {
                    // We are adding an account to this user
                    User masterUser = null;
                    if (!string.IsNullOrEmpty(Request["Id"]))
                    {
                        masterUser = database.Users.FirstOrDefault(u => u.UniqueId == Request["Id"]);
                    }
                    var appUser = TwitterCreateOrUpdateAccountIfNeeded(accessToken, user, masterUser);
                    return(appUser);
                }
                else
                {
                    //TODO: we should probably throw some kind of authentication error here
                    return(null);
                }
            }
            else
            {
                //TODO: Should we really return null here? This request was denied.
                return(null);
            }
        }
        public JsonResult New(string Id, string token, Notification notification)
        {
            // Get the User based on supplied Id
            // * Token must match *
            var user = database.Users
                       .Include("Projects")
                       .Include("Projects.TwitterAccounts")
                       .Include("Projects.MessageRecipients")
                       .Include("Projects.TextMessageRecipients")
                       .FirstOrDefault(usr => usr.UserName == Id && usr.UniqueId == token);

            // If Id or Token is invalid, user will not be found
            // TODO: Allow users to reset the token
            if (null != user)
            {
                // Locate or create our project
                var project = CreateProjectIfNecessary(notification, user);
                if (null != project)
                {
                    SaveNotification(notification, project);

                    // If the message ends with a dash, we are NOT notifying anyone of the push
                    if (notification.build.commit.message.Trim().EndsWith("-") == false)
                    {
                        // start our connection to twitter
                        var twitterAccount = project.TwitterAccounts != null?project.TwitterAccounts.FirstOrDefault() : null;

                        if (null == twitterAccount)
                        {
                            twitterAccount = user.AuthenticationAccounts.FirstOrDefault(a => a.AccountProvider.ToLower() == "twitter");
                        }
                        //TODO: Ensure we HAVE a twitter account
                        twitter.AuthenticateWith(twitterAccount.OAuthToken, twitterAccount.OAuthTokenSecret);

                        // Format and send appropriate messages
                        if (notification.build.status == "succeeded")
                        {
                            // Get the Success Template (or default)
                            var strSuccessUpdate = string.IsNullOrEmpty(project.SuccessTemplate) ?
                                                   Properties.Settings.Default.DefaultSuccessTemplate : project.SuccessTemplate;

                            // Replace tokens
                            strSuccessUpdate = DeTokenizeString(strSuccessUpdate, project, notification);

                            if (strSuccessUpdate.Length > 140)
                            {
                                strSuccessUpdate = strSuccessUpdate.Substring(0, 136) + "...";
                            }

                            // ensure we're 'authorized' to send the tweet
                            if (project.SendPrivateTweetOnSuccess && user.SendPrivateTweet)
                            {
                                SendDirectMessages(project, strSuccessUpdate);
                            }
                            if (project.SendPublicTweetOnSuccess && user.SendPublicTweet)
                            {
                                TwitterStatus pubRes = twitter.SendTweet(strSuccessUpdate);
                            }
                            if (project.SendTextOnSuccess && user.SendSMS)
                            {
                                SendSmsMessages(project, strSuccessUpdate);
                            }
                        }
                        else
                        {
                            // Get the Failure Template (or default)
                            var strFailureUpdate = string.IsNullOrEmpty(project.FailureTemplate) ?
                                                   Properties.Settings.Default.DefaultFailureTemplate : project.FailureTemplate;

                            // Replace tokens & clean up
                            strFailureUpdate = DeTokenizeString(strFailureUpdate, project, notification);

                            if (strFailureUpdate.Length > 140)
                            {
                                strFailureUpdate = strFailureUpdate.Substring(0, 136) + "...";
                            }

                            // ensure we're 'authorized' to send the tweet
                            if (project.SendPrivateTweetOnFailure && user.SendPrivateTweet)
                            {
                                SendDirectMessages(project, strFailureUpdate);
                            }
                            if (project.SendPublicTweetOnFailure && user.SendPublicTweet)
                            {
                                var pubRes = twitter.SendTweet(strFailureUpdate);
                            }
                            if (project.SendTextOnFailure && user.SendSMS)
                            {
                                SendSmsMessages(project, strFailureUpdate);
                            }
                        }
                    }

                    return(Json(new JsonResultModel()
                    {
                        Success = true
                    }));
                }
                else
                {
                    return(Json(new JsonResultModel()
                    {
                        Success = false, Error = "Unable to locate or create project"
                    }));
                }
            }
            else
            {
                return(Json(new JsonResultModel()
                {
                    Success = false, Error = "NotAuthorized"
                }));
            }
        }