Exemplo n.º 1
0
        public ActionResult AddUser(AddUserVM vm)
        {
            string input = vm.UserName;

            var user = Context.Users.FirstOrDefault(g => g.UserName == vm.UserName || g.Mail == vm.UserName);

            //Return a list with possible users if the username is not found.
            if (user == null)
            {
                if (MailHelpers.CheckIfValidEmail(vm.UserName))
                {
                    string key     = Guid.NewGuid().ToString();
                    string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
                    string link    = baseUrl + "Account/Register?key=" + key;
                    Context.NewRegisters.Add(new NewRegister
                    {
                        Key     = key,
                        Email   = vm.UserName,
                        GroupId = vm.GroupId
                    });
                    Context.SaveChanges();
                    MailService ms = new MailService();
                    ms.SendRegisterMail(vm.UserName, link);

                    return(View(new AddUserVM {
                        GroupId = vm.GroupId, Message = "An email has been sent to the given email."
                    }));
                }

                var possibleUsers = Context.Users.Select(g => new Web.Models.Group.UserVM
                {
                    Id        = g.Id,
                    UserName  = g.UserName,
                    FirstName = g.FirstName,
                    LastName  = g.LastName
                }).Where(g => g.UserName.Contains(vm.UserName)).ToList();

                if (possibleUsers != null && possibleUsers.Count > 0)
                {
                    return(View(new AddUserVM {
                        GroupId = vm.GroupId, PossibleUsers = possibleUsers
                    }));
                }
                else
                {
                    return(RedirectToAction("Details", new { id = vm.Id, state = "No user" }));
                }
            }
            else
            {
                var group = Context.Groups.GetById(vm.Id);
                if (!group.Archived)
                {
                    if (group.Users == null)
                    {
                        group.Users = new List <User>();
                    }
                    group.Users.Add(user);

                    user.Notifications.Add(new Notification
                    {
                        Title   = "Added to group: " + group.GroupName,
                        Content = $"You've been added to the group '{group.GroupName}' " +
                                  $"and can now rate yourself for the associated skills through the following link: " +
                                  Url.Action("RateUser", "Group", new { userId = user.Id }, this.Request.Url.Scheme),
                        Date   = DateTime.Now,
                        IsRead = false
                    });

                    Context.SaveChanges();

                    return(RedirectToAction("Details", new { id = vm.Id, state = "success" }));
                }
                else
                {
                    var message = "Target group is archived";
                    return(RedirectToAction("Index", message));
                }
            }
        }
Exemplo n.º 2
0
        public ActionResult RegisterPost(RegisterVM vm)
        {
            var recaptchaHelper = this.GetRecaptchaVerificationHelper();

            if (string.IsNullOrEmpty(recaptchaHelper.Response))
            {
                //This means the captcha had a empty response.
                ModelState.AddModelError("CaptchaResponse", "Please complete the captcha.");
            }

            var recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse();

            if (recaptchaResult != RecaptchaVerificationResult.Success)
            {
                ModelState.AddModelError("CaptchaResponse", "Invalid captcha response.");
            }

            if (!string.Equals(vm.Password, vm.PasswordCheck))
            {
                ModelState.AddModelError("Password", "Passwords do not match ");
            }

            if (MailHelpers.CheckIfValidEmail(vm.MailAdress))
            {
                var userExists = Context.Users.Any(x => x.UserName == vm.UserName);
                var mailExists = Context.Users.Any(x => x.Mail == vm.MailAdress);
                if (!mailExists && !userExists)
                {
                    Context.Users.Add(new User
                    {
                        UserName         = vm.UserName,
                        Mail             = vm.MailAdress,
                        Password         = Hashing.Hash(vm.Password),
                        RegisterDate     = DateTime.Now,
                        FirstName        = vm.FirstName,
                        LastName         = vm.LastName,
                        LastLoginAttempt = DateTime.Now
                    });
                }
                else
                {
                    if (mailExists)
                    {
                        ModelState.AddModelError("MailAdress", "Email already in use.");
                    }

                    if (userExists)
                    {
                        ModelState.AddModelError("UserName", "Username already in use.");
                    }
                }

                if (ModelState.IsValid && (vm.Password == vm.PasswordCheck))
                {
                    Context.SaveChanges();

                    //Add the user to the specified group if the person was invited while a groupadmin was inviting people
                    var newRegister = Context.NewRegisters.GetByKey(vm.Key);
                    if (newRegister != null)
                    {
                        var user = Context.Users.GetByMail(vm.MailAdress);
                        if (user != null)
                        {
                            Context.Groups.GetById(newRegister.GroupId).Users.Add(user);
                            Context.SaveChanges();
                        }
                    }

                    //Send register mail that explains wolfpack a bit
                    var ms      = new MailService();
                    var message = "<h2>Welcome to Wolfpack!</h2>" +
                                  "<p>Hello " + vm.FirstName + ",<br>" +
                                  "First of all welcome to Wolfpack!<br><br>" +
                                  "Wolfpack is a platform for people that want to create the most efficient teams possible based on an individual's skills. " +
                                  "In wolfpack most users will join groups where they are then added to events that will make the best possible teams. " +
                                  "To take part in a group you will first have to give yourself ratings about a few skills. With these rating we can properly create teams. " +
                                  "You can also start a group yourself, add people to this group and start events.<br><br>" +
                                  "With Wolfpack you can see a history of groups, events and teams you have partaken in. " +
                                  "You can see how others rated you about your skills and other useful information.<br><br>" +
                                  "We hope you'll enjoy using Wolfpack.<br>" +
                                  " - Wolfpack Team</p>";
                    ms.SendMailCustom(vm.MailAdress, "Wolfpack - Welcome to the pack", message, true);
                }
                else
                {
                    return(View("Register", vm));
                }

                return(RedirectToAction("Login", "Account", new { message = "Your account has been successfully created!" }));
            }
            else
            {
                ModelState.AddModelError("MailAdress", "This email is not valid please try again.");
                return(View("Register", vm));
            }
        }
Exemplo n.º 3
0
        public void CheckIfValidMail_Mail_IsFalse()
        {
            var validMail = MailHelpers.CheckIfValidEmail("wolfpack.nhlstenden@gmail");

            Assert.IsFalse(validMail);
        }