Example #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));
        }
Example #2
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());
        }