Exemplo n.º 1
0
        public async Task <IActionResult> Invate(int?id, int?familyId)
        {
            if (id == null || familyId == null)
            {
                return(View());
            }
            User user = await userManager.FindByNameAsync(User.Identity.Name);

            Person person = db.People.FirstOrDefault(x => x.Id == id && x.FamilyId == familyId);

            if (person == null)
            {
                return(NotFound());
            }
            int familId = FamlyMethods.GetFamilyId(db, user);

            if (familId != familyId)
            {
                return(NotFound());
            }
            InvateEmail invate = new InvateEmail {
                FamilyId = (int)familyId, PersonId = (int)id
            };

            return(View(invate));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Confirminvate(ConfirmInvateVM vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }
            PersonToken personToken = db.PersonTokens.FirstOrDefault(x => x.Code == vm.Token);

            if (personToken == null)
            {
                return(NotFound());
            }
            if (personToken.Date.AddDays(1) < DateTime.Now)
            {
                return(NotFound());
            }
            User user = await userManager.FindByIdAsync(personToken.UserId);

            if (user == null)
            {
                return(NotFound());
            }

            User invateUser = await userManager.FindByEmailAsync(personToken.Email);

            if (invateUser != null)
            {
                return(NotFound());
            }
            Person person = db.People.Include(x => x.UserToPerson).FirstOrDefault(x => x.Id == vm.Id && x.FamilyId == vm.FamilyId);

            if (person == null)
            {
                return(NotFound());
            }
            if (person.UserToPerson != null)
            {
                return(NotFound());
            }
            int familId = FamlyMethods.GetFamilyId(db, user);

            if (familId != vm.FamilyId)
            {
                return(NotFound());
            }

            User newUser = new User
            {
                Firstname      = person.Firstname,
                Lastname       = person.LastName,
                Email          = personToken.Email,
                BirthDate      = person.Birthdate,
                UserName       = person.Firstname.Trim() + person.LastName.Trim() + Guid.NewGuid().ToString(),
                EmailConfirmed = true,
                Avatar         = person.Photo
            };

            switch (person.GenderId)
            {
            case 1:
                newUser.GenderId = 1;
                newUser.Avatar   = "default1.jpg";
                break;

            case 2:
                newUser.GenderId = 2;
                newUser.Avatar   = "default2.jpg";
                break;
            }
            IdentityResult identityResult = await userManager.CreateAsync(newUser, vm.Password);

            if (!identityResult.Succeeded)
            {
                foreach (var er in identityResult.Errors)
                {
                    ModelState.AddModelError("", er.Description);
                }

                return(View(vm));
            }
            await userManager.AddToRoleAsync(newUser, Utilities.SD.MemberRole);

            await userManager.UpdateAsync(newUser);

            await signInManager.SignInAsync(newUser, true);

            FamilyToUser familyToUser = new FamilyToUser {
                FamilyId = vm.FamilyId, UserId = newUser.Id
            };
            await db.FamilyToUsers.AddAsync(familyToUser);

            db.SaveChanges();
            UserToPerson userToPerson = new UserToPerson {
                PersonId = person.Id, UserId = newUser.Id
            };

            db.UserToPeople.Add(userToPerson);
            db.PersonTokens.Remove(personToken);
            db.SaveChanges();
            return(RedirectToAction("Index", "Home"));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Invate(InvateEmail invateEmail)
        {
            if (!ModelState.IsValid)
            {
                return(View(invateEmail));
            }
            User user = await userManager.FindByNameAsync(User.Identity.Name);

            User invateUser = await userManager.FindByEmailAsync(invateEmail.Email);

            if (invateUser != null)
            {
                ViewBag.Error = "User already exists";
                return(View());
            }
            Person person = db.People.Include(x => x.UserToPerson).FirstOrDefault(x => x.Id == invateEmail.PersonId && x.FamilyId == invateEmail.FamilyId);

            if (person == null)
            {
                return(NotFound());
            }
            if (person.UserToPerson != null)
            {
                ViewBag.Error = "User already exists";
                return(View());
            }
            int familId = FamlyMethods.GetFamilyId(db, user);

            if (familId != invateEmail.FamilyId)
            {
                return(NotFound());
            }
            try
            {
                PersonToken token = new PersonToken
                {
                    Date     = DateTime.Now,
                    PersonId = person.Id,
                    UserId   = user.Id,
                    Code     = Guid.NewGuid().ToString(),
                    Email    = invateEmail.Email
                };
                #region Sending Email Invate Message
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                client.UseDefaultCredentials = false;
                client.EnableSsl             = true;
                client.Credentials           = new NetworkCredential(configuration["ConnectionStrings:SmtpClientCredentialEmail"], configuration["ConnectionStrings:SmtpClientCredentialPassword"]);

                MailMessage message = new MailMessage(configuration["ConnectionStrings:SmtpClientCredentialEmail"], invateEmail.Email);
                message.IsBodyHtml = true;
                message.Subject    = "Confirm invate";
                message.Body       = $"<table style='width:100%;background-color:#fbfbfb;padding:50px'><thead style ='width:100%;display:flex;justify-content:center;'><tr style ='width:100%;display:flex;justify-content:center;'><th style ='width:100%;color:#7e0f9a;font-family:Roboto, sans-serif;font-weight:400;font-size:50px'>Family Tree</th></tr><thead><tbody><tr><td style ='padding:30px 0px;color:#353535;font-family:Roboto Condensed, sans-serif;font-size:20px;'> Dear user, a friend invited you to his family. Click the 'Verify İnvate' button below to verify your invate.</td></tr><tr><td style ='font-family:Roboto Condensed, sans-serif;text-align:center;'><a href='https://localhost:44341/account/confirminvate?id={person.Id}&token={token.Code}&familyId={familId}' style ='text-decoration:none;padding:10px 30px;border-radius:3px;background-color:#8d11ff;color:black;font-weight:lighter;font-size:20px;cursor:pointer;'>Confirm account</a></td></tr></tbody></table>";

                client.Send(message);

                db.PersonTokens.Add(token);
                db.SaveChanges();
                #endregion
            }
            catch
            {
                ViewBag.Error = "An error occurred";
                return(View());
            }
            TempData["invate"] = true;
            return(View());
        }