public ActionResult Index()
        {
            if (null != HttpContext)
            {
                if (!string.IsNullOrEmpty(Request["error"]) && Request["error"] == "ImportNotAuthorized")
                {
                    ViewBag.import_error = "Unable to sign in with those credentials";
                }
                ViewBag.UserName = HttpContext.User.Identity.Name;
                var u = database.Users.Include("Projects")
                        .Include("Projects.MessageRecipients")
                        .Include("Projects.TextMessageRecipients")
                        .Include("Projects.ProjectNotifications")
                        .Include("Projects.ProjectNotifications.Build")
                        .Include("Projects.ProjectNotifications.Build.commit")
                        .Include("AuthenticationAccounts")
                        .FirstOrDefault(usr => usr.UserName == HttpContext.User.Identity.Name);

                if (null != u)
                {
                    if (string.IsNullOrEmpty(u.UniqueId))
                    {
                        //TODO: Make this much more secure
                        u.UpdateUniqueId();
                        database.SaveChanges();
                    }
                    return(View(u));
                }
                else
                {
                    return(RedirectToAction("LogIn"));
                }
            }
            return(new EmptyResult());
        }
Exemple #2
0
 public ActionResult Edit(string Id, User user)
 {
     database.Users.Attach(user);
     (database as System.Data.Entity.DbContext).Entry(user).State = System.Data.EntityState.Modified;
     database.SaveChanges();
     return(View(user));
 }
        public ActionResult AssignToProject(int Id, string Type, int twitteraccount)
        {
            var project = database.Projects.FirstOrDefault(p => p.ProjectId == Id && p.User.UserName == User.Identity.Name);

            if (null != project)
            {
                var uac = database.UserAuthenticationAccounts.FirstOrDefault(uc => uc.UserAuthenticationAccountId == twitteraccount && uc.User.UserName == User.Identity.Name);
                if (null != uac)
                {
                    project.TwitterAccounts.Clear();
                    project.TwitterAccounts.Add(uac);
                    database.SaveChanges();
                    return(RedirectToAction("Index", "Account"));
                }
                else
                {
                    throw new Exception("User Authentication Account not found");
                }
            }
            else
            {
                //TODO: handle this in a more friendly way
                throw new Exception("Project not found");
            }
        }
 public ActionResult Delete(int Id)
 {
     database.UserAuthenticationAccounts.Remove(
         database.UserAuthenticationAccounts.FirstOrDefault(u => u.UserAuthenticationAccountId == Id)
         );
     database.SaveChanges();
     return(RedirectToAction("Index"));
 }
Exemple #5
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);
        }
Exemple #6
0
        public JsonResult ProjectNotificationToggle(string Id, string TweetType, bool Value)
        {
            if (null != HttpContext)
            {
                var prj = database.Projects.FirstOrDefault(p => p.User.UserName == HttpContext.User.Identity.Name && p.ProjectName == Id);

                if (null != prj)
                {
                    switch (TweetType)
                    {
                    case "SendPublicTweetOnSuccess":
                        prj.SendPublicTweetOnSuccess = Value;
                        break;

                    case "SendPrivateTweetOnSuccess":
                        prj.SendPrivateTweetOnSuccess = Value;
                        break;

                    case "SendPublicTweetOnFailure":
                        prj.SendPublicTweetOnFailure = Value;
                        break;

                    case "SendPrivateTweetOnFailure":
                        prj.SendPrivateTweetOnFailure = Value;
                        break;

                    case "SendTextOnSuccess":
                        prj.SendTextOnSuccess = Value;
                        break;

                    case "SendTextOnFailure":
                        prj.SendTextOnFailure = Value;
                        break;
                    }
                    database.SaveChanges();
                    return(Json(new { Success = true }));
                }
                else
                {
                    return(Json(new { Error = "Project Not Found", Success = false }));
                }
            }
            return(Json(new { Error = "Something", Success = false }));
        }
 private void SendSmsMessages(Project project, string update)
 {
     if (null != project.TextMessageRecipients && project.TextMessageRecipients.Count > 0)
     {
         foreach (var r in project.TextMessageRecipients)
         {
             try
             {
                 OutboundNotification n = new OutboundNotification()
                 {
                     DateCreated      = DateTime.Now,
                     Message          = update,
                     NotificationType = "SMS",
                     Recipient        = r.PhoneNumber,
                     SentSuccessfully = false,
                     DateSent         = DateTime.MaxValue
                 };
                 project.OutboundNotifications.Add(n);
                 database.SaveChanges();
                 try
                 {
                     textMessageService.SendText(r.PhoneNumber, update);
                     n.SentSuccessfully = true;
                     n.DateSent         = DateTime.Now;
                 }
                 catch (Exception sx)
                 {
                     n.SentSuccessfully = false;
                 }
                 database.SaveChanges();
             }
             catch (Exception e)
             {
                 //TODO: Log this
                 throw e;
             }
         }
     }
 }
Exemple #8
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);
        }