Ejemplo n.º 1
0
        public async Task <IActionResult> Index()
        {
            User user = await userManager.FindByNameAsync(User.Identity.Name);

            FamilyToUser familyToUser = db.FamilyToUsers.FirstOrDefault(x => x.UserId == user.Id);
            VM           vm           = new VM
            {
                User   = user,
                People = db.People.Include(x => x.Gender).Include(x => x.UserToPerson).Where(x => x.FamilyId == familyToUser.FamilyId)
            };

            return(View(vm));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> NewPerson(Person person)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Genders = db.Genders;
                return(View(person));
            }
            User user = await userManager.FindByNameAsync(User.Identity.Name);

            FamilyToUser familyToUser = db.FamilyToUsers.FirstOrDefault(x => x.UserId == user.Id);

            person.FamilyId = familyToUser.FamilyId;

            if (person.PhotoFile != null)
            {
                if (!person.PhotoFile.ContentType.Contains("image/"))
                {
                    ViewBag.Genders = db.Genders;
                    ModelState.AddModelError("PhotoFile", "Invalid photo type");
                    return(View(person));
                }
                string fileName   = Guid.NewGuid().ToString() + person.PhotoFile.FileName;
                string resultpath = Path.Combine(env.WebRootPath, "image", fileName);
                using (FileStream stream = new FileStream(resultpath, FileMode.Create))
                {
                    await person.PhotoFile.CopyToAsync(stream);
                }
                person.Photo = fileName;
            }
            else
            {
                person.Photo = person.GenderId == 1 ? "default1.jpg" : "default2.jpg";
            }
            Person newPerson = new Person
            {
                Birthdate = person.Birthdate,
                Age       = DateTime.Now.Year - person.Birthdate.Year,
                Firstname = person.Firstname,
                LastName  = person.LastName,
                FamilyId  = person.FamilyId,
                GenderId  = person.GenderId,
                Photo     = person.Photo,
            };

            db.People.Add(newPerson);
            db.SaveChanges();
            TempData["addPerson"] = true;
            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 3
0
        public static int GetFamilyId(DB db, User user)
        {
            FamilyToUser familyToUser = db.FamilyToUsers.FirstOrDefault(x => x.UserId == user.Id);

            return(familyToUser.FamilyId);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> SignUp(SignUpVM newUser)
        {
            if (!ModelState.IsValid)
            {
                return(View(newUser));
            }
            var checkEmail = new EmailAddressAttribute();

            if (!checkEmail.IsValid(newUser.Email))
            {
                ModelState.AddModelError("Email", "This email is invalid");
                return(View(newUser));
            }

            try
            {
                var checkEmail2 = new System.Net.Mail.MailAddress(newUser.Email);
                if (checkEmail2.Address != newUser.Email)
                {
                    ModelState.AddModelError("Email", "This email is invalid");
                    return(View(newUser));
                }
            }

            catch
            {
                ModelState.AddModelError("Email", "This email is invalid");
                return(View(newUser));
            }

            if (DateTime.Today.Year - newUser.BirthDate.Year <= 1)
            {
                ModelState.AddModelError("Birthday", "The user must be older than 1 year");
                return(View(newUser));
            }
            User user = new User
            {
                Firstname = newUser.Firtsname.Trim(),
                Lastname  = newUser.Lastname.Trim(),
                Email     = newUser.Email.Trim(),
                BirthDate = newUser.BirthDate,
                UserName  = newUser.Firtsname.Trim() + newUser.Lastname.Trim() + Guid.NewGuid().ToString()
            };

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

            case 2:
                user.GenderId = 2;
                user.Avatar   = "default2.jpg";
                break;

            default:
                ModelState.AddModelError("GenderId", "Select valid gender");
                return(View(newUser));
            }

            IdentityResult identityResult = await userManager.CreateAsync(user, newUser.Password);

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

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

            await userManager.UpdateAsync(user);

            string emailConfirmationToken = await userManager.GenerateEmailConfirmationTokenAsync(user);

            #region Sending Email Confirmation 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"], newUser.Email);
            message.IsBodyHtml = true;
            message.Subject    = "Confirm account";
            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, you have successfully signed up. Click the 'Verify Account' button below to verify your account.</td></tr><tr><td style ='font-family:Roboto Condensed, sans-serif;text-align:center;'><a href='https://localhost:44341/account/confirmemail?id={user.Id}&emailConfirmationToken={emailConfirmationToken}' 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);
            #endregion
            TempData["signup"] = true;
            await signInManager.SignInAsync(user, true);

            Family family = new Family {
                Name = $"{user.Firstname}'s family", Logo = "defaultfamily.png"
            };
            db.Families.Add(family);
            db.SaveChanges();
            FamilyToUser familyToUser = new FamilyToUser {
                FamilyId = family.Id, UserId = user.Id
            };
            await db.FamilyToUsers.AddAsync(familyToUser);

            db.SaveChanges();
            Person person = new Person
            {
                Age       = DateTime.Today.Year - user.BirthDate.Year,
                Birthdate = user.BirthDate,
                FamilyId  = family.Id,
                Firstname = user.Firstname,
                GenderId  = user.GenderId,
                LastName  = user.Lastname,
                Photo     = user.Avatar
            };
            db.People.Add(person);
            db.SaveChanges();
            UserToPerson userToPerson = new UserToPerson {
                PersonId = person.Id, UserId = user.Id
            };
            db.UserToPeople.Add(userToPerson);
            db.SaveChanges();
            Relationship relationship = new Relationship
            {
                IsMain        = true,
                FamilyId      = family.Id,
                RelatedUserId = person.Id,
                RelRoleId     = db.RelRoles.FirstOrDefault(x => x.Name.ToLower() == "norole").Id
            };
            db.Relationships.Add(relationship);
            db.SaveChanges();
            return(RedirectToAction("Index", "Home"));
        }
Ejemplo n.º 5
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"));
        }