Пример #1
0
        public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByNameAsync(model.Email);

                if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
                {
                    ModelState.AddModelError("", "The user either does not exist or is not confirmed.");
                    return(View());
                }

                //generate callback url
                string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

                var callbackUrl = Url.Action("ResetPassword", "Account",
                                             new { userId = user.Id, code = code }, protocol: GetURLScheme());

                //send notification
                bool notifySuccess =
                    new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                        "USER_ACCOUNT_RESET_PASSWORD",
                        model.Email,
                        "Account:" + user.Id.ToString(),
                        new Dictionary <string, string>
                {
                    { "ResetPasswordUrl", callbackUrl }
                });

                return(RedirectToAction("ForgotPasswordConfirmation", "Account"));
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Пример #2
0
        public async Task <ActionResult> ExecuteResetPasswordAsAdmin(ResetPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByNameAsync(model.Email);

                if (user == null)
                {
                    ModelState.AddModelError("", "No user found.");
                    return(View());
                }
                IdentityResult result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);

                if (result.Succeeded)
                {
                    //send notification
                    bool notifySuccess =
                        new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                            "USER_ACCOUNT_CHANGE_PASSWORD_BY_ADMIN",
                            UserContact.Email,
                            "User:"******"UserEmail", model.Email }
                    });

                    ViewBag.userId = user.Id;
                    return(RedirectToAction("Details", "Users", new { id = user.Id }));
                }
                else
                {
                    AddErrors(result);
                    return(View());
                }
            }

            return(View());
        }
Пример #3
0
        public async Task <ActionResult> ResetPassword(ResetPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByNameAsync(model.Email);

                if (user == null)
                {
                    ModelState.AddModelError("", "No user found.");
                    return(View());
                }
                IdentityResult result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);

                if (result.Succeeded)
                {
                    //send notification
                    bool notifySuccess =
                        new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                            "USER_ACCOUNT_CHANGE_PASSWORD",
                            user.Email,
                            "User:"******"Email", user.Email }
                    });

                    return(RedirectToAction("ResetPasswordConfirmation", "Account"));
                }
                else
                {
                    AddErrors(result);
                    return(View());
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Пример #4
0
        private void SendOrgAccountRegCompleteNotification(AspNetUser user)
        {
            try
            {
                if (!string.IsNullOrEmpty(user.DefaultOrganizationId))
                {
                    Guid      orgGuid    = Guid.Parse(user.DefaultOrganizationId);
                    GNAccount orgAccount = this.db.GNAccounts.Where(a => a.Organization.Id == orgGuid).FirstOrDefault();

                    if (orgAccount != null)
                    {
                        GNContact mainOrgContact = orgAccount.Organization.OrgMainContact;

                        if (mainOrgContact.AspNetUserId == user.Id)
                        {
                            //send notification
                            bool notifySuccess =
                                new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                                    "ORG_ACCOUNT_REGISTRATION_COMPLETE",
                                    mainOrgContact.Email,
                                    "Account:" + mainOrgContact.Id.ToString(),
                                    new Dictionary <string, string>
                            {
                                { "OrganizationName", orgAccount.Organization.Name },
                                { "FirstName", mainOrgContact.FirstName },
                                { "LastName", mainOrgContact.LastName }
                            });
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogUtil.Error(logger, "Unable to send org account registration complete notification after Email Confirmation: " + e.Message, e);
            }
        }
        //////////////////////////////////////////////////////////////////////////////
        // Common Methods
        //////////////////////////////////////////////////////////////////////////////

        private async Task <AspNetUser> AuthCreateUser(string email, string password, bool isExistingUser, bool verifyEmail, bool signUpForNews, string organizationId)
        {
            AspNetUser aspNetUser = base.identityDB.AspNetUsers.Where(u => u.Email == email).FirstOrDefault();

            //auth User, if existing
            if (isExistingUser)
            {
                if (aspNetUser != null)
                {
                    ApplicationUser user = new ApplicationUser()
                    {
                        UserName = email, Email = email
                    };
                    user = await base.UserManager.FindAsync(user.UserName, password);

                    if (user == null)
                    {
                        aspNetUser = null;
                        ModelState.AddModelError("Password", "User Email and/or Password is not valid");
                    }
                }
                else
                {
                    ModelState.AddModelError("IsExistingUser", "User does not exist");
                }
            }
            //else create User, send out verification email
            else
            {
                if (aspNetUser == null)
                {
                    ApplicationUser user = new ApplicationUser()
                    {
                        UserName = email, Email = email
                    };
                    IdentityResult result = await UserManager.CreateAsync(user, password);

                    if (result.Succeeded)
                    {
                        aspNetUser = identityDB.AspNetUsers.SingleOrDefault(u => u.Email == email);
                        user       = await UserManager.FindByEmailAsync(user.Email);


                        if (organizationId != null && aspNetUser.DefaultOrganizationId == null)
                        {
                            aspNetUser.DefaultOrganizationId = organizationId;
                            db.SaveChanges();
                        }

                        //send email verification notification to user
                        if (verifyEmail)
                        {
                            //generate callback url
                            string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                            var callbackUrl = Url.Action("ConfirmEmail", "Account",
                                                         new { userId = user.Id, code = code }, protocol: GetURLScheme());

                            //send notification
                            bool notifySuccess =
                                new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                                    "USER_ACCOUNT_CONFIRM_EMAIL",
                                    user.Email,
                                    "User:"******"InvitationUrl", callbackUrl },
                                { "Email", user.Email }
                            });
                        }
                        //set email as already confirmed
                        else
                        {
                            string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                            result = await UserManager.ConfirmEmailAsync(user.Id, token : code);
                        }

                        //subscribe user to news
                        if (signUpForNews)
                        {
                            //Subscribe the user for news
                            GNNotificationTopic NewsTopic   = db.GNNotificationTopics.Where(a => a.Code.Equals("GENOMENEXT_NEWS_AND_PRODUCTS") && a.Status == "ACTIVE").FirstOrDefault();
                            GNContact           UserContact = db.GNContacts.Where(a => a.Email.Equals(aspNetUser.Email)).FirstOrDefault();

                            if (UserContact != null)
                            {
                                UserContact.IsSubscribedForNewsletters = true;
                            }

                            db.SaveChanges();

                            if (NewsTopic != null && UserContact != null)
                            {
                                UserContact.GNNotificationTopicSubscribers.Add(new GNNotificationTopicSubscriber
                                {
                                    AddresseeType         = "TO",
                                    GNContactId           = UserContact.Id,
                                    GNNotificationTopicId = NewsTopic.Id,
                                    IsSubscribed          = "Y",
                                    CreateDateTime        = DateTime.Now,
                                    CreatedBy             = UserContact.Id
                                });
                            }
                        }
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
                else
                {
                    ModelState.AddModelError("Email", "User with this Email Address already exists");
                }
            }
            return(aspNetUser);
        }
        public async Task <ActionResult> AccountSubmit(RegisterAccountViewModel model)
        {
            if (ModelState.IsValid)
            {
                GNInviteCode inviteCodeRedemption = null;

                try
                {
                    //redeem invite code
                    inviteCodeRedemption = inviteCodeService.RedeemInviteCode(model.InviteCode);

                    if (inviteCodeRedemption != null)
                    {
                        //Auth or Create User
                        AspNetUser aspNetUser = await AuthCreateUser(model.Email, model.Password, model.IsExistingUser,
                                                                     verifyEmail : true, signUpForNews : model.SignUpForNewsAndProducts, organizationId : null);

                        if (ModelState.IsValid && aspNetUser != null)
                        {
                            //Insert New Org
                            GNOrganization org = await InsertNewOrganization(model);

                            if (org != null)
                            {
                                //Insert Main Org Contact
                                GNContact mainOrgContact = await InsertMainOrgContact(model, aspNetUser, org);

                                if (mainOrgContact != null)
                                {
                                    double maxBalanceAllowed             = 0.0;
                                    bool   validBillingAgreementRequired = false;

                                    //set free credit allowance based on invite code redemption
                                    if (inviteCodeRedemption.FreeCreditAllowance > 0.0)
                                    {
                                        maxBalanceAllowed = inviteCodeRedemption.FreeCreditAllowance;
                                    }

                                    GNAccount orgAccount =
                                        await InsertNewOrgAccount(org, mainOrgContact, maxBalanceAllowed, validBillingAgreementRequired);



                                    if (inviteCodeRedemption.InviteCode.Equals("GENandGEN2015"))
                                    {
                                        org.GNSettingsTemplate = db.GNSettingsTemplates.Where(a => a.Name == "GN_GENOMICS2015").FirstOrDefault();
                                        db.SaveChanges();
                                    }



                                    if (orgAccount != null)
                                    {
                                        if (model.IsExistingUser)
                                        {
                                            //send notification
                                            bool notifySuccess =
                                                new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                                                    "ORG_ACCOUNT_REGISTRATION_COMPLETE",
                                                    mainOrgContact.Email,
                                                    "Account:" + mainOrgContact.Id.ToString(),
                                                    new Dictionary <string, string>
                                            {
                                                { "OrganizationName", orgAccount.Organization.Name },
                                                { "FirstName", mainOrgContact.FirstName },
                                                { "LastName", mainOrgContact.LastName }
                                            });
                                        }

                                        return(RedirectToAction("AccountComplete", new { id = org.Id }));
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("InviteCode", "Unable to redeem Invite Code.");
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.Error(logger, "Unable to perform Account Registration", ex);
                    ModelState.AddModelError("", "Unable to perform Account Registration");
                }

                if (inviteCodeRedemption != null)
                {
                    inviteCodeService.UnRedeemInviteCode(model.InviteCode);
                }
            }

            return(View(model));
        }
        public async Task <ActionResult> Invite(GNContact contact)
        {
            auditResult = audit.LogEvent(UserContact, contact.Id, this.ENTITY, this.Request.UserHostAddress, "INVITE");

            contact = CreateOnSubmit(contact);

            if (ModelState.IsValid)
            {
                try
                {
                    bool sendInviteEmail = false;

                    GNContact existingContact = entityService.db.GNContacts
                                                .Where(c => (c.Email == contact.Email && c.GNOrganizationId == contact.GNOrganizationId))
                                                .FirstOrDefault();

                    if (existingContact != null)
                    {
                        if (existingContact.IsInviteAccepted.HasValue &&
                            existingContact.IsInviteAccepted.Value == true)
                        {
                            ModelState.AddModelError("Email", "A user with this email has already accepted an invitation to this Organization.");
                        }
                        else
                        {
                            ModelState.AddModelError("Email", "A user with this email has already been invited to this Organization. The email invitation has been re-sent.");

                            //update contact
                            existingContact.IsInviteAccepted = false;
                            int result = this.entityService.db.SaveChanges();
                            contact         = existingContact;
                            sendInviteEmail = true;
                        }
                    }
                    else
                    {
                        //insert contact
                        contact.AspNetUserId               = Guid.Empty.ToString();
                        contact.IsInviteAccepted           = false;
                        contact.IsSubscribedForNewsletters = false;
                        contact = await((ContactService)entityService).Insert(UserContact, contact);

                        if (contact != null)
                        {
                            sendInviteEmail = true;
                        }
                    }

                    if (sendInviteEmail)
                    {
                        //fetch org
                        var org = entityService.db.GNOrganizations.Find(contact.GNOrganizationId);

                        try
                        {
                            //generate callback url
                            var callbackUrl = Url.Action("Contact", "Register",
                                                         new { organizationId = org.Id, contactId = contact.Id }, GetURLScheme());

                            //send notification
                            bool notifySuccess =
                                new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                                    "USER_ACCOUNT_SEND_INVITATION",
                                    contact.Email,
                                    "Contact:" + contact.Id.ToString(),
                                    new Dictionary <string, string>
                            {
                                { "OrganizationName", org.Name.Trim() },
                                { "InvitedByName", UserContact.FullName },
                                { "InvitationUrl", callbackUrl },
                                { "FirstName", contact.FirstName }
                            });
                        }
                        catch (Exception e)
                        {
                            LogUtil.Error(logger, "Unable to send Invite Contact notification", e);
                            throw;
                        }

                        if (ModelState.IsValid)
                        {
                            return(CreateOnSuccess(contact));
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.AddModelStateErrorsFromException(db, ModelState, ex);
                }
            }

            contact = CreateOnModelStateInValid(contact);

            return(View(contact));
        }
Пример #8
0
        public async Task <ActionResult> MyCredentials(ManageUserViewModel model)
        {
            auditResult = audit.LogEvent(UserContact, Guid.Empty, this.ENTITY, this.Request.UserHostAddress, "LOAD_MY_CREDENTIALS_UI_POST_METHOD");

            bool hasPassword = HasPassword();

            ViewBag.HasLocalPassword = hasPassword;
            ViewBag.ReturnUrl        = Url.Action("MyCredentials");
            if (hasPassword)
            {
                if (ModelState.IsValid)
                {
                    IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);

                    if (result.Succeeded)
                    {
                        var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

                        //send notification
                        bool notifySuccess =
                            new GenomeNext.App.NotificationCloudMessageService().NotifyGNContact(
                                "USER_ACCOUNT_CHANGE_PASSWORD",
                                user.Email,
                                "User:"******"Email", user.Email }
                        });

                        await SignInAsync(user, isPersistent : false);

                        return(RedirectToAction("MyCredentials", new { Message = ManageMessageId.ChangePasswordSuccess }));
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }
            else
            {
                // User does not have a password so remove any validation errors caused by a missing OldPassword field
                ModelState state = ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("MyCredentials", new { Message = ManageMessageId.SetPasswordSuccess }));
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }