示例#1
0
        public async Task <bool> CheckUserEmail(string email)
        {
            var emailChecker = new ValidEmailChecker();

            return(!emailChecker.IsValidEmail(email) && await _userRepo.UniqueEmailCheck(email));
        }
示例#2
0
        public async Task <ActionResult> Create([Bind(Include = "Name, Invitees")] NeedHouseholdViewModel householdVM)
        {
            if (ModelState.IsValid)
            {
                string          currentUserId = User.Identity.GetUserId();
                ApplicationUser currentUser   = db.Users.Find(currentUserId);

                // Create new household
                Household household = new Household();
                household.Name    = householdVM.Name;
                household.OwnerId = currentUserId;
                db.Households.Add(household);
                db.SaveChanges();

                currentUser.HouseholdId = household.Id;
                db.Entry(currentUser).Property("HouseholdId").IsModified = true;
                db.SaveChanges();

                BudgetCategory budgetCategory = new BudgetCategory();
                budgetCategory.Name        = "General";
                budgetCategory.HouseholdId = household.Id;
                db.BudgetCategories.Add(budgetCategory);
                db.SaveChanges();

                Budget budget = new Budget();
                budget.Amount           = 500.00M;
                budget.HouseholdId      = household.Id;
                budget.BudgetCategoryId = budgetCategory.Id;
                db.Budgets.Add(budget);
                db.SaveChanges();

                if (householdVM.Invitees != null)
                {
                    // Send Invites
                    foreach (string invitee in householdVM.Invitees)
                    {
                        // good code for invite method also
                        bool userIsNotAlreadyHouseholdMember = household.Members.ToList().FirstOrDefault(m => m.Email == invitee || m.FullName == invitee) == null;

                        if (userIsNotAlreadyHouseholdMember)
                        {
                            // Taking this out because there will never be any invitations for this project as its just been created
                            // Use this code for the invite action and make sure its specific to household (I've already edited it)
                            // bool noExistingInvitation = !household.Invitations.Select(i => i.UserEmail).Contains(email) ;
                            //if (noExistingInvitation)
                            //{

                            bool userIsRegistered = db.Users.ToList().FirstOrDefault(u => u.FullName == invitee) != null;

                            //bool userIsRegistered = false;
                            //foreach (ApplicationUser user in db.Users.ToList())
                            //{
                            //    fullName = $"{user.FirstName} {user.LastName}";
                            //    if (fullName == invitee)
                            //    {
                            //        userIsRegistered = true;
                            //    }
                            //}
                            //don't need this because it's already covered by userIsNotAlreadyHouseholdMember (may be useful elsewhere)
                            //bool didNotEnterOwnName = currentUserId != db.Users.FirstOrDefault(u => u.FullName == invitee).Id;

                            if (userIsRegistered)
                            {
                                string     email      = db.Users.ToList().FirstOrDefault(u => u.FullName == invitee && u.Email != currentUser.Email).Email;
                                Invitation invitation = new Invitation();
                                invitation.HouseholdId = household.Id;
                                invitation.UserEmail   = email;
                                db.Invitations.Add(invitation);
                                db.SaveChanges();

                                //email notification
                                var callbackUrl = Url.Action("JoinAccept", "Households", new { householdId = household.Id }, protocol: Request.Url.Scheme);
                                try
                                {
                                    var from    = "ReFund Budgeter<*****@*****.**>";
                                    var to      = email;
                                    var message = new MailMessage(from, to)
                                    {
                                        Subject    = "You've Been Invited!",
                                        Body       = $"You were invited to join household \"<strong>{household.Name}</strong>\" by <strong>{currentUser.FirstName} {currentUser.LastName}</strong>. Click <a href='{callbackUrl}'>here</a> to join.",
                                        IsBodyHtml = true
                                    };
                                    var svc = new PersonalEmail();
                                    await svc.SendAsync(message);

                                    ViewBag.Message = "Email has been sent";
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e.Message);
                                    await Task.FromResult(0);
                                }
                            }
                            else
                            {
                                // If use is not registered then they should have provided an email
                                ValidEmailChecker VEC = new ValidEmailChecker();
                                bool validEmail       = VEC.IsValidEmail(invitee);

                                if (validEmail)
                                {
                                    Invitation invitation = new Invitation();
                                    invitation.HouseholdId = household.Id;
                                    invitation.UserEmail   = invitee;
                                    db.Invitations.Add(invitation);
                                    db.SaveChanges();

                                    bool emailBelongsToExistingUser = db.Users.Select(u => u.Email).Contains(invitee);
                                    if (emailBelongsToExistingUser)
                                    {
                                        //email notification
                                        var callbackUrl = Url.Action("JoinAccept", "Households", new { householdId = household.Id }, protocol: Request.Url.Scheme);
                                        try
                                        {
                                            var from    = "ReFund Budgeter<*****@*****.**>";
                                            var to      = invitee;
                                            var message = new MailMessage(from, to)
                                            {
                                                Subject    = "You've Been Invited!",
                                                Body       = $"You were invited to join household \"<strong>{household.Name}</strong>\" by <strong>{currentUser.FirstName} {currentUser.LastName}</strong>. Click <a href='{callbackUrl}'>here</a> to join.",
                                                IsBodyHtml = true
                                            };
                                            var svc = new PersonalEmail();
                                            await svc.SendAsync(message);

                                            ViewBag.Message = "Email has been sent";
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e.Message);
                                            await Task.FromResult(0);
                                        }
                                    }
                                    else
                                    {
                                        //email notification
                                        var callbackUrl = Url.Action("Register", "Account", null, protocol: Request.Url.Scheme);
                                        try
                                        {
                                            var from    = "ReFund Budgeter<*****@*****.**>";
                                            var to      = invitee;
                                            var message = new MailMessage(from, to)
                                            {
                                                Subject    = "You've Been Invited!",
                                                Body       = $"You were invited to join household \"<strong>{household.Name}</strong>\" by <strong>{currentUser.FirstName} {currentUser.LastName}</strong>. If you would like to join, you must first register for Refund Budgeter. After that, you can choose the join option to become a part of this household. Click <a href='{callbackUrl}'>here</a> to get started.",
                                                IsBodyHtml = true
                                            };
                                            var svc = new PersonalEmail();
                                            await svc.SendAsync(message);

                                            ViewBag.Message = "Email has been sent";
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e.Message);
                                            await Task.FromResult(0);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Refresh User's session so that they are associated with new household
                await ControllerContext.HttpContext.RefreshAuthentication(currentUser);

                return(RedirectToAction("Index"));
            }

            return(RedirectToAction("JoinAccept", "Home"));
        }
示例#3
0
        public async Task <Person> ProcessLtiUser(LtiRequest parsedRequest)
        {
            var user = await ctxManager.Context.People
                       .Include(s => s.Security)
                       .Include(s => s.Faculty)
                       .Include(s => s.Student)
                       .SingleOrDefaultAsync(person => person.BbUserId == parsedRequest.UserId);

            var emailChecker = new ValidEmailChecker();

            if (user != null)
            {
                if (user.Email.ToLower() != parsedRequest.LisPersonEmailPrimary.ToLower())
                {
                    if (!emailChecker.IsValidEmail(parsedRequest.LisPersonEmailPrimary))
                    {
                        throw new InvalidEmailException("The email address associated with your LMS account (" + parsedRequest.LisPersonEmailPrimary + ") is not a valid email address.");
                    }
                    if (!await UniqueEmailCheck(parsedRequest.LisPersonEmailPrimary))
                    {
                        throw new InvalidEmailException("The email address associated with your LMS account (" + parsedRequest.LisPersonEmailPrimary + ") is already being used in ECAT.");
                    }
                }
                user.ModifiedById = user.PersonId;
            }
            else
            {
                if (!emailChecker.IsValidEmail(parsedRequest.LisPersonEmailPrimary))
                {
                    throw new InvalidEmailException("The email address associated with your LMS account (" + parsedRequest.LisPersonEmailPrimary + ") is not a valid email address.");
                }
                if (!await UniqueEmailCheck(parsedRequest.LisPersonEmailPrimary))
                {
                    throw new InvalidEmailException("The email address associated with your LMS account (" + parsedRequest.LisPersonEmailPrimary + ") is already being used in ECAT.");
                }

                user = new Person
                {
                    IsActive             = true,
                    MpGender             = MpGender.Unk,
                    MpAffiliation        = MpAffiliation.Unk,
                    MpComponent          = MpComponent.Unk,
                    MpPaygrade           = MpPaygrade.Unk,
                    MpInstituteRole      = MpInstituteRoleId.Undefined,
                    RegistrationComplete = false
                };

                ctxManager.Context.People.Add(user);
            }

            //var userIsCourseAdmin = false;

            var roles = parsedRequest.Parameters["Roles"].ToLower().Split(',');

            switch (roles[0])
            {
            case "instructor":
                //userIsCourseAdmin = true;
                user.MpInstituteRole = MpInstituteRoleId.Faculty;
                break;

            case "teachingassistant":
                user.MpInstituteRole = MpInstituteRoleId.Faculty;
                break;

            case "contentdeveloper":
                user.MpInstituteRole = MpInstituteRoleId.Designer;
                break;

            default:
                user.MpInstituteRole = MpInstituteRoleId.Student;
                break;
            }

            switch (user.MpInstituteRole)
            {
            case MpInstituteRoleId.Faculty:
                user.Faculty = user.Faculty ?? new ProfileFaculty();
                //user.Faculty.IsCourseAdmin = userIsCourseAdmin;
                user.Faculty.AcademyId = parsedRequest.Parameters["custom_ecat_school"];
                break;

            case MpInstituteRoleId.Designer:
                //user.Designer = user.Designer ?? new ProfileDesigner();
                //user.Designer.AssociatedAcademyId = parsedRequest.Parameters["custom_ecat_school"];
                break;

            default:
                user.Student = user.Student ?? new ProfileStudent();
                break;
            }

            user.IsActive     = true;
            user.Email        = parsedRequest.LisPersonEmailPrimary.ToLower();
            user.LastName     = parsedRequest.LisPersonNameFamily;
            user.FirstName    = parsedRequest.LisPersonNameGiven;
            user.BbUserId     = parsedRequest.UserId;
            user.ModifiedDate = DateTime.Now;

            if (await ctxManager.Context.SaveChangesAsync() > 0)
            {
                return(user);
            }

            throw new UserUpdateException("Save User Changes did not succeed!");
        }
示例#4
0
        public async Task <ActionResult> Invite([Bind(Include = "Invitees")] HouseholdIndexViewModel vm)
        {
            Household       household     = db.Households.Find(Convert.ToInt32(User.Identity.GetHouseholdId()));
            string          currentUserId = User.Identity.GetUserId();
            ApplicationUser currentUser   = db.Users.Find(currentUserId);

            if (ModelState.IsValid)
            {
                if (vm.Invitees != null)
                {
                    // Send Invites
                    foreach (string invitee in vm.Invitees)
                    {
                        bool userIsNotAlreadyHouseholdMember = household.Members.ToList().FirstOrDefault(m => m.Email == invitee || m.FullName == invitee) == null; // this will do for now but need to figure out way of solving 2 same names, either make names be unique (not optimal) or use email as value always

                        if (userIsNotAlreadyHouseholdMember)
                        {
                            bool userIsRegistered = db.Users.ToList().FirstOrDefault(u => u.FullName == invitee) != null;

                            if (userIsRegistered)
                            {
                                string     email      = db.Users.ToList().FirstOrDefault(u => u.FullName == invitee && u.Email != currentUser.Email).Email;
                                Invitation invitation = new Invitation();
                                invitation.HouseholdId = household.Id;
                                invitation.UserEmail   = email;
                                db.Invitations.Add(invitation);
                                db.SaveChanges();

                                //email notification
                                var callbackUrl = Url.Action("JoinAccept", "Households", new { householdId = household.Id }, protocol: Request.Url.Scheme);
                                try
                                {
                                    var from    = "ReFund Budgeter<*****@*****.**>";
                                    var to      = email;
                                    var message = new MailMessage(from, to)
                                    {
                                        Subject    = "You've Been Invited!",
                                        Body       = $"You were invited to join household \"<strong>{household.Name}</strong>\" by <strong>{currentUser.FirstName} {currentUser.LastName}</strong>. Click <a href='{callbackUrl}'>here</a> to join.",
                                        IsBodyHtml = true
                                    };
                                    var svc = new PersonalEmail();
                                    await svc.SendAsync(message);

                                    ViewBag.Message = "Email has been sent";
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e.Message);
                                    await Task.FromResult(0);
                                }
                            }
                            else
                            {
                                // If use is not registered then they should have provided an email
                                ValidEmailChecker VEC = new ValidEmailChecker();
                                bool validEmail       = VEC.IsValidEmail(invitee);

                                if (validEmail)
                                {
                                    Invitation invitation = new Invitation();
                                    invitation.HouseholdId = household.Id;
                                    invitation.UserEmail   = invitee;
                                    db.Invitations.Add(invitation);
                                    db.SaveChanges();

                                    bool emailBelongsToExistingUser = db.Users.Select(u => u.Email).Contains(invitee);
                                    if (emailBelongsToExistingUser)
                                    {
                                        //email notification
                                        var callbackUrl = Url.Action("JoinAccept", "Households", new { householdId = household.Id }, protocol: Request.Url.Scheme);
                                        try
                                        {
                                            var from    = "ReFund Budgeter<*****@*****.**>";
                                            var to      = invitee;
                                            var message = new MailMessage(from, to)
                                            {
                                                Subject    = "You've Been Invited!",
                                                Body       = $"You were invited to join household \"<strong>{household.Name}</strong>\" by <strong>{currentUser.FirstName} {currentUser.LastName}</strong>. Click <a href='{callbackUrl}'>here</a> to join.",
                                                IsBodyHtml = true
                                            };
                                            var svc = new PersonalEmail();
                                            await svc.SendAsync(message);

                                            ViewBag.Message = "Email has been sent";
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e.Message);
                                            await Task.FromResult(0);
                                        }
                                    }
                                    else
                                    {
                                        //email notification
                                        var callbackUrl = Url.Action("Register", "Account", null, protocol: Request.Url.Scheme);
                                        try
                                        {
                                            var from    = "ReFund Budgeter<*****@*****.**>";
                                            var to      = invitee;
                                            var message = new MailMessage(from, to)
                                            {
                                                Subject    = "You've Been Invited!",
                                                Body       = $"You were invited to join household \"<strong>{household.Name}</strong>\" by <strong>{currentUser.FirstName} {currentUser.LastName}</strong>. If you would like to join, you must first register for Refund Budgeter. After that, you can choose the join option to become a part of this household. Click <a href='{callbackUrl}'>here</a> to get started.",
                                                IsBodyHtml = true
                                            };
                                            var svc = new PersonalEmail();
                                            await svc.SendAsync(message);

                                            ViewBag.Message = "Email has been sent";
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e.Message);
                                            await Task.FromResult(0);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(RedirectToAction("Index"));
        }