Example #1
0
        public async Task <bool> AuthenticateComplete(string userID, string code, ApplicationDbContext db)
        {
            var User = db.Users.FirstOrDefault(u => u.Id == userID);

            if (User != null && User.Id == userID && !string.IsNullOrEmpty(code))
            {
                var t = new Tracker()
                {
                    AuthToken = code, Type = Tracker.TrackerType.Fitbit
                };
                User.Trackers.Add(t);
                await GetAccessCode(db, t);

                Messaging.Add(Message.LevelEnum.alert_success, "You have successfully linked your account to Fitbit",
                              Message.TypeEnum.StickyAlert, User);
                db.SaveChanges();
                return(true);
            }


            //if we get here, something went wrong :(
            Messaging.Add(Message.LevelEnum.alert_danger,
                          "Oops! something went wrong trying to link your account to Fitbit", Message.TypeEnum.TemporaryAlert,
                          User);
            db.SaveChanges();
            return(false);
        }
Example #2
0
        public ActionResult Edit(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            Pledge pledge = db.OpenPledges.Include("Activity_Types").FirstOrDefault(p => p.PledgeID.ToString() == id);

            if (pledge == null)
            {
                Messaging.Add(Message.LevelEnum.alert_danger, "OOps! something went wrong trying to find that pledge.", Message.TypeEnum.TemporaryAlert, CurrentUser());
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            if (pledge.Originator.SinnerID != CurrentUser().Id)
            {
                Messaging.Add(Message.LevelEnum.alert_danger, "You are not the creator of that pledge", Message.TypeEnum.TemporaryAlert, CurrentUser());
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(new EditPledgeVM {
                Pledge = pledge, PledgeID = pledge.PledgeID.ToString()
            }));
        }
Example #3
0
        public ActionResult Edit(EditTeamVM TeamVM)
        {
            var user = CurrentUser();
            var team = db.Teams.FirstOrDefault(t => t.ID == TeamVM.TeamID);

            if (!ModelState.IsValid)
            {
                TeamVM.Team = team;
                return(View(TeamVM));
            }

            if (team == null)
            {
                Messaging.Add(Message.LevelEnum.alert_danger, "something went wrong trying to update the Team.", Message.TypeEnum.TemporaryAlert, user);
            }
            else if (user.IsTeamAdmin && user.TeamID == TeamVM.TeamID)
            {
                if (!string.IsNullOrEmpty(TeamVM.Description))
                {
                    team.Description = TeamVM.Description;
                }

                if (TeamVM.TeamImageID > 0)
                {
                    team.ImageID = TeamVM.TeamImageID;
                }

                if (!string.IsNullOrEmpty(TeamVM.Name))
                {
                    team.Name = TeamVM.Name;
                }

                team.Availability = TeamVM.Availability;

                Messaging.Add(Message.LevelEnum.alert_success, "Team details updated", Message.TypeEnum.StickyAlert, user);
                db.SaveChanges();
                return(RedirectToAction("Details", new { TeamName = team.Name.Replace(" ", "") }));
            }
            else
            {
                Messaging.Add(Message.LevelEnum.alert_danger, "You don't have permission to edit this Team.", Message.TypeEnum.TemporaryAlert, user);
            }

            db.SaveChanges();
            //if we get here something went wrong so send back the edit view.
            if (team != null)
            {
                TeamVM.Team         = team;
                TeamVM.Availability = team.Availability;
            }
            return(View(TeamVM));
        }
Example #4
0
        public async Task <ActionResult> ApproveJoinRequests(List <string> IDs, int thisTeamID)
        {
            var team = db.Teams.FirstOrDefault(t => t.ID == thisTeamID);

            if (team == null)
            {
                Messaging.Add(Message.LevelEnum.alert_danger, "something went wrong trying to approve users. please try again.", Message.TypeEnum.TemporaryAlert, CurrentUser());
                db.SaveChanges();
                return(RedirectToAction("Index", "Home"));
            }

            if (IDs == null)
            {
                Messaging.Add(Message.LevelEnum.alert_danger, "something went wrong trying to approve users. please try again.", Message.TypeEnum.TemporaryAlert, CurrentUser());
                db.SaveChanges();
                return(RedirectToAction("Details", new { teamname = team.Name.Replace(" ", "") }));
            }

            foreach (string ID in IDs)
            {
                var user = db.Users.FirstOrDefault(u => u.Id == ID);
                if (user != null)
                {
                    user.TeamID = thisTeamID;
                }
                var TJR = db.TeamJoinRequests.FirstOrDefault(JR => JR.UserID == ID && JR.TeamID == thisTeamID);
                if (TJR != null)
                {
                    db.TeamJoinRequests.Remove(TJR);
                }
            }

            var EmailVM = new GenericEmailViewModel {
                RootURL = GetRootURL()
            };

            var TeamURL = Url.Action("Details", "Teams", new { teamname = team.Name.Replace(" ", "") }, Request.Url.Scheme);

            foreach (string ID in IDs)
            {
                var user = db.Users.FirstOrDefault(u => u.Id == ID);
                if (user != null)
                {
                    await UserLogic.JoinTeamRequestApproved(user, team, EmailVM, TeamURL);
                }
            }

            Messaging.Add(Message.LevelEnum.alert_success, string.Format("{0} new members successfully approved", IDs.Count()), Message.TypeEnum.StickyAlert, CurrentUser());
            db.SaveChanges();
            return(RedirectToAction("Details", new { teamname = team.Name.Replace(" ", "") }));
        }
Example #5
0
        public bool AuthenticateComplete(string userID, string code, ApplicationDbContext db)
        {
            var User = db.Users.FirstOrDefault(u => u.Id == userID);


            if (User != null && User.Id == userID && !string.IsNullOrEmpty(code))
            {
                using (var wb = new WebClient())
                {
                    var data = new NameValueCollection();
                    data["grant_type"]    = "authorization_code";
                    data["code"]          = code;
                    data["client_id"]     = ConfigurationManager.AppSettings["RunKeeperClientID"];
                    data["client_secret"] = ConfigurationManager.AppSettings["RunKeeperClientSecret"];
                    data["redirect_uri"]  = ParentTracker.Url.Action("RunKeeperAuthComplete", "Trackers", new { userID = ParentTracker.User.Id }, ParentTracker.RequestScheme);

                    var     response     = wb.UploadValues(ConfigurationManager.AppSettings["RunKeeperAccessTokenURL"], "POST", data);
                    dynamic jsonresponse = System.Web.Helpers.Json.Decode(Encoding.UTF8.GetString(response));

                    if (!string.IsNullOrEmpty(jsonresponse?.access_token))
                    {
                        //success, we've got an access token
                        User.Trackers.Add(new Tracker()
                        {
                            AuthToken = jsonresponse.access_token, Type = Tracker.TrackerType.RunKeeper
                        });
                        Messaging.Add(Models.Message.LevelEnum.alert_success, "You have successfully linked your account to RunKeeper!", Message.TypeEnum.StickyAlert, User);
                        db.SaveChanges();

                        return(true);
                    }
                }
            }


            //if we get here, something went wrong :(
            Messaging.Add(Models.Message.LevelEnum.alert_danger, "Oops! something went wrong trying to link your account to RunKeeper", Message.TypeEnum.TemporaryAlert, User);

            db.SaveChanges();
            return(false);
        }
Example #6
0
        public ActionResult Create(Team team)
        {
            if (ModelState.IsValid)
            {
                var user = CurrentUser();
                if (user.TeamID.HasValue)
                {
                    ModelState.AddModelError("", "You can't create a team if you're already a member of another team.");
                    return(View(team));
                }

                db.Teams.Add(team);

                user.IsTeamAdmin = true;
                user.Team        = team;
                Messaging.Add(Message.LevelEnum.alert_success, $"Team '{team.Name}' successfully created. You have been added as the administrator of this team", Message.TypeEnum.StickyAlert, user);
                db.SaveChanges();
                return(RedirectToAction("Details", new { teamname = team.Name.Replace(" ", "") }));
            }

            return(View(team));
        }
Example #7
0
        public ActionResult Cancel(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }


            Pledge pledge = db.OpenPledges.Include("Activity_Types").FirstOrDefault(p => p.PledgeID.ToString() == id);

            if (pledge == null)
            {
                Messaging.Add(Message.LevelEnum.alert_warning, "OOps! something went wrong trying to find that pledge.", Message.TypeEnum.TemporaryAlert, CurrentUser());
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            var user = CurrentUser();

            if (pledge.Originator.SinnerID == user.Id & pledge.Contributors.Count == 1 && pledge.Offsets.Count == 0)
            {
                try
                {
                    pledge.Closed = true;
                    Messaging.Add(Message.LevelEnum.alert_success, "Pledge canceled.", Message.TypeEnum.StickyAlert, user);
                }
                catch
                {
                    Messaging.Add(Message.LevelEnum.alert_warning, "Sorry, something went wrong trying to cancel that pledge", Message.TypeEnum.TemporaryAlert, CurrentUser());
                }
            }
            else
            {
                Messaging.Add(Message.LevelEnum.alert_warning, "Sorry, you can't cancel that pledge", Message.TypeEnum.TemporaryAlert, CurrentUser());
            }

            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Example #8
0
        public ActionResult Create(CreatePledgeVM pledgeVM)
        {
            ModelState.Clear();
            TryValidateModel(pledgeVM.Pledge);


            if (ModelState.IsValid)
            {
                var ThisUser = CurrentUser();
                if (!PledgesLogic.getCompletePledgeFromCreatePledgeVM(pledgeVM, db, ThisUser))
                {
                    Messaging.Add(Message.LevelEnum.alert_danger, "Sorry something went wrong trying to create your pledge.", Message.TypeEnum.TemporaryAlert, ThisUser);
                    db.SaveChanges();
                    return(RedirectToAction("Create"));
                }
                db.Pledges.Add(pledgeVM.Pledge);
                Messaging.Add(Message.LevelEnum.alert_success, "Thank you! your new pledge has been created.", Message.TypeEnum.StickyAlert, ThisUser);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(RedirectToAction("Create"));
        }
Example #9
0
        public async Task <string> GetAccessCode(ApplicationDbContext db, Tracker t)
        {
            if (t == null)
            {
                return("No Authorization Code");
            }



            if (!string.IsNullOrEmpty(t.AccessToken) && (t.AccessTokenExpiry > DateTime.Now))
            {
                return(t.AccessToken);
            }

            if (!string.IsNullOrEmpty(t.RefreshToken))
            {
                var Headers = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("Authorization", "Basic " + GetAuthorizationCode())
                };

                var Data = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("refresh_token", t.RefreshToken),
                    new KeyValuePair <string, string>("grant_type", "refresh_token")
                };

                var result =
                    await GenericLogic.HttpPost(Headers, Data, ConfigurationManager.AppSettings["FitbitTokenURL"]);

                dynamic jsonresponse = System.Web.Helpers.Json.Decode(result);

                string accesstoken = jsonresponse?.access_token;
                if (!string.IsNullOrEmpty(accesstoken))
                {
                    t.AccessToken       = accesstoken;
                    t.RefreshToken      = jsonresponse?.refresh_token;
                    t.AccessTokenExpiry = DateTime.Now.AddSeconds((jsonresponse?.expires_in * 0.9));
                    t.ThirdPartyUserID  = jsonresponse?.user_id;
                    db.SaveChanges();
                    return(t.AccessToken);
                }

                Messaging.Add(Message.LevelEnum.alert_danger,
                              "Oops! something went wrong trying to request information from Fitbit",
                              Message.TypeEnum.TemporaryAlert, ParentTracker.User);
                db.SaveChanges();
                return(string.Empty);
            }
            else
            {
                var Headers = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("Authorization", "Basic " + GetAuthorizationCode())
                };

                var Data = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("code", t.AuthToken),
                    new KeyValuePair <string, string>("grant_type", "authorization_code"),
                    new KeyValuePair <string, string>("client_id", ConfigurationManager.AppSettings["FitbitClientID"]),
                    new KeyValuePair <string, string>("redirect_uri",
                                                      ParentTracker.Url.Action("FitbitAuthCompleted", "Trackers", null,
                                                                               ParentTracker.RequestScheme))
                };

                var result =
                    await GenericLogic.HttpPost(Headers, Data, ConfigurationManager.AppSettings["FitbitTokenURL"])
                ;

                dynamic jsonresponse = System.Web.Helpers.Json.Decode(result);

                string accesstoken = jsonresponse?.access_token;

                if (!string.IsNullOrEmpty(accesstoken))
                {
                    t.AccessToken       = accesstoken;
                    t.RefreshToken      = jsonresponse?.refresh_token;
                    t.AccessTokenExpiry = DateTime.Now.AddSeconds((jsonresponse?.expires_in * 0.9));
                    t.ThirdPartyUserID  = jsonresponse?.user_id;
                    db.SaveChanges();
                    return(t.AccessToken);
                }
                else
                {
                    //there was a problem.
                    Messaging.Add(Message.LevelEnum.alert_danger,
                                  "Oops! something went wrong trying to request information from Fitbit",
                                  Message.TypeEnum.TemporaryAlert, ParentTracker.User);
                    db.SaveChanges();
                    return(string.Empty);
                }
            }
        }
Example #10
0
        public ActionResult PaymentComplete(string ID)
        {
            try
            {
                var IDInt = GenericLogic.GetInt(ID);

                if (!IDInt.HasValue)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }

                var contrib = db.PledgeContributors.FirstOrDefault(c => c.ID == IDInt);

                if (contrib == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }


                string url = ConfigurationManager.AppSettings["JustGivingAPIURL"] + ConfigurationManager.AppSettings["JustGivingAppId"] + "/v1/donation/ref/" + contrib.ID;

                //need to check this...contrib.ThirdPartyRef
                var i = new Uri(url);

                var request = WebRequest.CreateDefault(i);
                request.Method      = "GET";
                request.ContentType = "application/json";

                var          response      = request.GetResponse();
                StreamReader reader        = new StreamReader(response.GetResponseStream());
                var          requestedText = reader.ReadToEnd();


                dynamic data = System.Web.Helpers.Json.Decode(requestedText);


                // var amount = data?.donations[0]?.amount;
                var thirdPartyReference = data?.donations[0]?.thirdPartyReference;
                var status = data?.donations[0]?.status;//"Accepted"

                if (thirdPartyReference != contrib.ID.ToString())
                {
                    throw new Exception();
                }

                if (status == "Accepted")
                {
                    contrib.Status = PledgeContributors.PledgeContribuionStatus.Completed;
                    db.SaveChanges();
                }
                else
                {
                    Messaging.Add(Message.LevelEnum.alert_warning, "Looks like the payment wasn't made. Try making payment again.", Message.TypeEnum.TemporaryAlert, contrib.Sinner);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }

                Messaging.Add(Message.LevelEnum.alert_success, "Thank You. Your payment has now been processed.", Message.TypeEnum.StickyAlert, contrib.Sinner);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            catch {
                if (CurrentUser() != null)
                {
                    Messaging.Add(Message.LevelEnum.alert_warning, "OOps! that didn't work. try making payment again.", Message.TypeEnum.TemporaryAlert, CurrentUser());
                    db.SaveChanges();
                }
                return(RedirectToAction("Index"));
            }
        }