Beispiel #1
0
        protected override void AddItem()
        {
            Invitation newInvitation = new Invitation()
            {
                UserId  = CurrentUser.Id,
                EventId = Events[0].Id
            };

            InvitationManager.CreateManagedObject(ref newInvitation);

            // GUI can only be updated by using Dispatcher
            App.Current.Dispatcher.Invoke(() =>
            {
                Items.Add(newInvitation);
                newInvitation.Event = GetInvitationEvent(newInvitation);
                newInvitation.User  = GetInvitationUser(newInvitation);
                newInvitation.Title = newInvitation.CreateTitle();
                SelectedItem        = newInvitation;
            });
        }
Beispiel #2
0
        public void DetermineCustomersToInvite_NoCustomersWithinDistance_InvitesNobody()
        {
            var venue      = new RadianCoordinate(Calculator.ConvertDegreesToRadians(53.339428), Calculator.ConvertDegreesToRadians(-6.257664));
            var calculator = new Calculator();
            var customers  = new List <CustomerContract>
            {
                new CustomerContract {
                    Name = "1", UserId = 1, Latitude = 53.2487055, Longitude = -6.5992854
                },
                new CustomerContract {
                    Name = "3", UserId = 3, Latitude = 53.5061796, Longitude = -6.4699798
                },
                new CustomerContract {
                    Name = "2", UserId = 2, Latitude = 53.1970187, Longitude = -6.1308245
                },
            };

            var invitationManager = new InvitationManager(calculator, venue);
            var customersToInvite = invitationManager.DetermineCustomersToInvite(customers, 10);

            Assert.AreEqual(0, customersToInvite.Count());
        }
Beispiel #3
0
        private async Task <ActionResult> RedirectOnPostAuthenticate(string returnUrl, string invitationCode)
        {
            var userId = AuthenticationManager.AuthenticationResponseGrant.Identity.GetUserId();
            var user   = await UserManager.FindByIdAsync(userId);

            if (user != null)
            {
                IdentityResult redeemResult;
                var            invitation = await FindInvitationByCodeAsync(invitationCode);

                if (invitation != null)
                {
                    // Redeem invitation for the existing/registered contact
                    redeemResult = await InvitationManager.RedeemAsync(invitation, user, Request.UserHostAddress);
                }
                else if (!string.IsNullOrWhiteSpace(invitationCode))
                {
                    redeemResult = IdentityResult.Failed(ViewBag.IdentityErrors.InvalidInvitationCode().Description);
                }
                else
                {
                    redeemResult = IdentityResult.Success;
                }


                if (!redeemResult.Succeeded)
                {
                    return(RedirectToAction("RedeemInvitation", new { ReturnUrl = returnUrl, invitation = invitationCode, invalid = true }));
                }

                if (!DisplayModeIsActive() && (user.HasProfileAlert || user.ProfileModifiedOn == null))
                {
                    return(RedirectToProfile(returnUrl));
                }
            }

            return(RedirectToLocal(returnUrl));
        }
Beispiel #4
0
        public void DetermineCustomersToInvite_AllCustomersWithinDistance_InvitesAllCustomers()
        {
            var venue      = new RadianCoordinate(Calculator.ConvertDegreesToRadians(53.339428), Calculator.ConvertDegreesToRadians(-6.257664));
            var calculator = new Calculator();
            var customers  = new List <CustomerContract>
            {
                new CustomerContract {
                    Name = "1", UserId = 1, Latitude = 53.3334849, Longitude = -6.2612991
                },
                new CustomerContract {
                    Name = "3", UserId = 3, Latitude = 53.3334593, Longitude = -6.2646894
                },
                new CustomerContract {
                    Name = "2", UserId = 2, Latitude = 53.3334593, Longitude = -6.2646894
                },
            };

            var invitationManager = new InvitationManager(calculator, venue);
            var customersToInvite = invitationManager.DetermineCustomersToInvite(customers, 10);

            Assert.AreEqual(customers.Count, customersToInvite.Count());
            Assert.IsTrue(customersToInvite.ElementAt(0).UserId < customersToInvite.ElementAt(1).UserId);
            Assert.IsTrue(customersToInvite.ElementAt(1).UserId < customersToInvite.ElementAt(2).UserId);
        }
Beispiel #5
0
 public void NewInvitationManager_NullVenue_ThrowsArgumentNullException()
 {
     var invitationManager = new InvitationManager(new Calculator(), null);
 }
Beispiel #6
0
 public void NewInvitationManager_NullCalculator_ThrowsArgumentNullException()
 {
     var invitationManager = new InvitationManager(null, new RadianCoordinate(1, 1));
 }
Beispiel #7
0
        protected virtual async Task <ActionResult> ExternalLoginConfirmation(string username, string email, string returnUrl, string invitationCode, ExternalLoginInfo loginInfo)
        {
            if (!ViewBag.Settings.RegistrationEnabled || (!ViewBag.Settings.OpenRegistrationEnabled && !ViewBag.Settings.InvitationEnabled))
            {
                AddErrors(ViewBag.IdentityErrors.InvalidLogin());

                // Registration is disabled
                return(View("Login", GetLoginViewModel(null, null, returnUrl, invitationCode)));
            }

            if (!ViewBag.Settings.OpenRegistrationEnabled && ViewBag.Settings.InvitationEnabled && string.IsNullOrWhiteSpace(invitationCode))
            {
                // Registration requires an invitation
                return(RedirectToAction("RedeemInvitation", new { ReturnUrl = returnUrl }));
            }

            if (User.Identity.IsAuthenticated)
            {
                return(Redirect(returnUrl ?? "~/"));
            }

            if (ModelState.IsValid)
            {
                var invitation = await FindInvitationByCodeAsync(invitationCode);

                var contactId = ToContactId(invitation);

                if (!string.IsNullOrWhiteSpace(invitationCode) && contactId == null)
                {
                    AddErrors(ViewBag.IdentityErrors.InvalidInvitationCode());
                }

                if (ModelState.IsValid)
                {
                    // Validate the username and email
                    var user = contactId != null
                        ? new ApplicationUser {
                        UserName = username, Email = email, Id = contactId.Id.ToString()
                    }
                        : new ApplicationUser {
                        UserName = username, Email = email
                    };

                    var validateResult = await UserManager.UserValidator.ValidateAsync(user);

                    if (validateResult.Succeeded)
                    {
                        // Get the information about the user from the external login provider
                        var info = loginInfo ?? await AuthenticationManager.GetExternalLoginInfoAsync();

                        if (info == null)
                        {
                            return(View("ExternalLoginFailure"));
                        }

                        IdentityResult result;

                        if (contactId == null)
                        {
                            if (!ViewBag.Settings.OpenRegistrationEnabled)
                            {
                                throw new InvalidOperationException("Open registration is not enabled.");
                            }

                            // Create a new user
                            result = await UserManager.CreateAsync(user);
                        }
                        else
                        {
                            // Update the existing invited user
                            user = await UserManager.FindByIdAsync(contactId.Id.ToString());

                            if (user != null)
                            {
                                result = await UserManager.InitializeUserAsync(user, username, null, email, ViewBag.Settings.TriggerLockoutOnFailedPassword);
                            }
                            else
                            {
                                // Contact does not exist or login is disabled
                                result = IdentityResult.Failed(ViewBag.IdentityErrors.InvalidInvitationCode().Description);
                            }

                            if (!result.Succeeded)
                            {
                                AddErrors(result);

                                ViewBag.ReturnUrl      = returnUrl;
                                ViewBag.InvitationCode = invitationCode;

                                return(View("RedeemInvitation", new RedeemInvitationViewModel {
                                    InvitationCode = invitationCode
                                }));
                            }
                        }

                        if (result.Succeeded)
                        {
                            var addResult = await UserManager.AddLoginAsync(user.Id, info.Login);

                            if (addResult.Succeeded)
                            {
                                if (invitation != null)
                                {
                                    var redeemResult = await InvitationManager.RedeemAsync(invitation, user, Request.UserHostAddress);

                                    if (redeemResult.Succeeded)
                                    {
                                        return(await SignInAsync(user, returnUrl));
                                    }
                                    else
                                    {
                                        AddErrors(redeemResult);
                                    }
                                }
                                else
                                {
                                    return(await SignInAsync(user, returnUrl));
                                }
                            }
                            else
                            {
                                AddErrors(addResult);
                            }
                        }
                        else
                        {
                            AddErrors(result);
                        }
                    }
                    else
                    {
                        AddErrors(validateResult);
                    }
                }
            }

            ViewBag.ReturnUrl      = returnUrl;
            ViewBag.InvitationCode = invitationCode;
            return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel {
                Email = email
            }));
        }
Beispiel #8
0
        public async Task <ActionResult> Register(RegisterViewModel model, string returnUrl, string invitationCode)
        {
            if (!ViewBag.Settings.RegistrationEnabled ||
                (!ViewBag.Settings.OpenRegistrationEnabled && !ViewBag.Settings.InvitationEnabled) ||
                (ViewBag.Settings.OpenRegistrationEnabled && !ViewBag.Settings.InvitationEnabled && !string.IsNullOrWhiteSpace(invitationCode)) ||
                (!ViewBag.Settings.OpenRegistrationEnabled && ViewBag.Settings.InvitationEnabled && string.IsNullOrWhiteSpace(invitationCode)))
            {
                return(HttpNotFound());
            }

            if ((ViewBag.Settings.LocalLoginByEmail || ViewBag.Settings.RequireUniqueEmail) && string.IsNullOrWhiteSpace(model.Email))
            {
                ModelState.AddModelError("Email", ViewBag.IdentityErrors.EmailRequired().Description);
            }

            if (!ViewBag.Settings.LocalLoginByEmail && string.IsNullOrWhiteSpace(model.Username))
            {
                ModelState.AddModelError("Username", ViewBag.IdentityErrors.UserNameRequired().Description);
            }

            if (!string.Equals(model.Password, model.ConfirmPassword))
            {
                ModelState.AddModelError("Password", ViewBag.IdentityErrors.PasswordConfirmationFailure().Description);
            }

            if (ModelState.IsValid)
            {
                var invitation = await FindInvitationByCodeAsync(invitationCode);

                var contactId = ToContactId(invitation);

                if (!string.IsNullOrWhiteSpace(invitationCode) && contactId == null)
                {
                    AddErrors(ViewBag.IdentityErrors.InvalidInvitationCode());
                }

                if (ModelState.IsValid)
                {
                    ApplicationUser user;
                    IdentityResult  result;

                    if (contactId == null)
                    {
                        // Create a new user
                        user = new ApplicationUser
                        {
                            UserName = ViewBag.Settings.LocalLoginByEmail ? model.Email : model.Username,
                            Email    = model.Email
                        };

                        result = await UserManager.CreateAsync(user, model.Password);
                    }
                    else
                    {
                        // Update the existing invited user
                        user = await UserManager.FindByIdAsync(contactId.Id.ToString());

                        if (user != null)
                        {
                            result = await UserManager.InitializeUserAsync(
                                user,
                                ViewBag.Settings.LocalLoginByEmail?model.Email : model.Username,
                                model.Password,
                                model.Email,
                                ViewBag.Settings.TriggerLockoutOnFailedPassword);
                        }
                        else
                        {
                            // Contact does not exist or login is disabled
                            result = IdentityResult.Failed(ViewBag.IdentityErrors.InvalidInvitationCode().Description);
                        }

                        if (!result.Succeeded)
                        {
                            AddErrors(result);

                            ViewBag.ReturnUrl      = returnUrl;
                            ViewBag.InvitationCode = invitationCode;

                            return(View("RedeemInvitation", new RedeemInvitationViewModel {
                                InvitationCode = invitationCode
                            }));
                        }
                    }

                    if (result.Succeeded)
                    {
                        if (invitation != null)
                        {
                            var redeemResult = await InvitationManager.RedeemAsync(invitation, user, Request.UserHostAddress);

                            if (redeemResult.Succeeded)
                            {
                                return(await SignInAsync(user, returnUrl));
                            }
                            else
                            {
                                AddErrors(redeemResult);
                            }
                        }
                        else
                        {
                            return(await SignInAsync(user, returnUrl));
                        }
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            ViewBag.ReturnUrl      = returnUrl;
            ViewBag.InvitationCode = invitationCode;
            return(View(GetRegisterViewModel(model, null)));
        }
Beispiel #9
0
 public InvitationsController(InvitationManager invitationManager)
 {
     InvitationManager = invitationManager ?? throw new System.ArgumentNullException(nameof(invitationManager));
 }
Beispiel #10
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Please specify the path to the input file in the command line arguments.");
                return;
            }

            if (string.IsNullOrWhiteSpace(args[0]) || !File.Exists(args[0]))
            {
                Console.WriteLine("Input file not found.");
                return;
            }

            try
            {
                var lines      = File.ReadAllLines(args[0]);
                var customers  = new List <CustomerContract>();
                var calculator = new Calculator();

                var dublinOfficeLocation = new RadianCoordinate(Calculator.ConvertDegreesToRadians(53.339428), Calculator.ConvertDegreesToRadians(-6.257664));

                foreach (var line in lines)
                {
                    try
                    {
                        var customer = CustomerContract.Deserialize(line);

                        if (customer.IsValid())
                        {
                            customers.Add(customer);
                        }
                    }
                    catch (JsonSerializationException e)
                    {
                        continue;
                    }
                    catch (ArgumentNullException e)
                    {
                        continue;
                    }
                    catch (ArgumentException e)
                    {
                        continue;
                    }
                }

                var invitationManager = new InvitationManager(calculator, dublinOfficeLocation);
                var customersToInvite = invitationManager.DetermineCustomersToInvite(customers, 100d);

                Console.WriteLine("The following customers should be invited:");

                foreach (var customer in customersToInvite)
                {
                    Console.WriteLine($"{customer.Name} ({customer.UserId})");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"An unexpected error occurred: {e.Message}");
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
 public UserController(UserManager userManager, InvitationManager invitationManager)
 {
     this.users             = userManager;
     this.invitationManager = invitationManager;
 }
Beispiel #12
0
 public void sendInvitation_Click(object sender, EventArgs e)
 {
     var addStatus = InvitationManager.AddInvitation(InvitedUsername.Value, Campaign.CampaignID, UserManager.GetUser(Session["username"].ToString()).UserID);
 }