示例#1
0
        public BaseController(ILoggingService loggingService, IUnitOfWorkManager unitOfWorkManager, IMembershipService membershipService, IRoleService roleService, ISettingsService settingsService)
        {
            UnitOfWorkManager = unitOfWorkManager;
            MembershipService = membershipService;
            RoleService       = roleService;
            SettingsService   = settingsService;
            LoggingService    = loggingService;

            using (UnitOfWorkManager.NewUnitOfWork())
            {
                LoggedOnReadOnlyUser = UserIsAuthenticated ? MembershipService.GetUserByEmail(Email, true) : null;
                UsersRole            = LoggedOnReadOnlyUser == null?RoleService.GetRole(AppConstants.GuestRoleName, true) : LoggedOnReadOnlyUser.Roles.FirstOrDefault();
            }
        }
        private static MemberFrontEndEditViewModel PopulateMemberViewModel(MembershipUser user)
        {
            var viewModel = new MemberFrontEndEditViewModel
            {
                Id          = user.Id,
                Email       = user.Email,
                DisplayName = user.DisplayName,
                FirstName   = user.FirstName,
                LastName    = user.LastName,
                City        = user.City,
                State       = user.State,
                Avatar      = user.Avatar
            };

            return(viewModel);
        }
        private void UpdateUserRoles(MembershipUser user, IEnumerable <string> updatedRoles)
        {
            // ---------------------------------------------------------------------
            // IMPORTANT - If you call this it MUST be within a unit of work
            // ---------------------------------------------------------------------

            // Not done in automapper to avoid handling services in the mapper
            var updatedRolesSet = new List <MembershipRole>();

            foreach (var roleStr in updatedRoles)
            {
                var alreadyIsRoleForUser = false;
                foreach (var role in user.Roles)
                {
                    if (roleStr == role.RoleName)
                    {
                        // This role for this user is UNchanged
                        updatedRolesSet.Add(role);
                        alreadyIsRoleForUser = true;
                        break;
                    }
                }

                if (!alreadyIsRoleForUser)
                {
                    // This is a new role for this user
                    updatedRolesSet.Add(RoleService.GetRole(roleStr));
                }
            }

            // Replace the roles in the user's collection
            user.Roles.Clear();
            foreach (var role in updatedRolesSet)
            {
                user.Roles.Add(role);
            }
        }
 private void SetRegisterViewBagMessage(bool manuallyAuthoriseMembers, bool memberEmailAuthorisationNeeded, MembershipUser userToSave)
 {
     if (manuallyAuthoriseMembers)
     {
         TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
         {
             Message     = "An administrator will authorize you shortly",
             MessageType = GenericMessages.success
         };
     }
     else if (memberEmailAuthorisationNeeded)
     {
         TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
         {
             Message     = "Check your email for authorization",
             MessageType = GenericMessages.success
         };
     }
     else
     {
         // If not manually authorise then log the user in
         FormsAuthentication.SetAuthCookie(userToSave.Email, false);
         TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
         {
             Message     = "You are registered with Chamber's app",
             MessageType = GenericMessages.success
         };
     }
 }
        public ActionResult MemberRegisterLogic(MemberAddViewModel viewModel)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var settings     = SettingsService.GetSettings();
                var homeRedirect = false;

                var userToSave = new MembershipUser
                {
                    Email     = viewModel.Email,
                    Password  = viewModel.Password,
                    FirstName = viewModel.FirstName,
                    LastName  = viewModel.LastName,
                    City      = viewModel.City,
                    State     = viewModel.State
                };
                var createStatus = MembershipService.CreateUser(userToSave);
                if (createStatus != MembershipCreateStatus.Success)
                {
                    ModelState.AddModelError(string.Empty, MembershipService.ErrorCodeToString(createStatus));
                }
                else
                {
                    var uploadFolderPath = HostingEnvironment.MapPath(string.Concat(SiteConstants.Instance.UploadFolderPath, userToSave.Id));
                    if (uploadFolderPath != null && !Directory.Exists(uploadFolderPath))
                    {
                        Directory.CreateDirectory(uploadFolderPath);
                    }

                    //admin/email approval goes here
                    SetRegisterViewBagMessage(false, false, userToSave);
                    //if (!manuallyAuthoriseMembers && !memberEmailAuthorisationNeeded)
                    //{
                    homeRedirect = true;
                    //}
                    try
                    {
                        // Only send the email if the admin is not manually authorising emails or it's pointless
                        //SendEmailConfirmationEmail(userToSave);
                        unitOfWork.Commit();

                        if (homeRedirect)
                        {
                            if (Url.IsLocalUrl(viewModel.ReturnUrl) && viewModel.ReturnUrl.Length > 1 && viewModel.ReturnUrl.StartsWith("/") &&
                                !viewModel.ReturnUrl.StartsWith("//") && !viewModel.ReturnUrl.StartsWith("/\\"))
                            {
                                return(Redirect(viewModel.ReturnUrl));
                            }
                            return(RedirectToAction("Index", "Home", new { area = string.Empty }));
                        }
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LoggingService.Error(ex);
                        FormsAuthentication.SignOut();
                        ModelState.AddModelError(string.Empty, "Error registering");
                    }
                }
            }
            return(View("Register"));
        }
        public ActionResult Login(LoginViewModel viewModel)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var email    = viewModel.Email;
                var password = viewModel.Password;

                try
                {
                    if (ModelState.IsValid)
                    {
                        var e = new LoginEventArgs
                        {
                            Email      = viewModel.Email,
                            Password   = viewModel.Password,
                            RememberMe = viewModel.RememberMe,
                            ReturnUrl  = viewModel.ReturnUrl,
                            UnitOfWork = unitOfWork
                        };
                        EventManager.Instance.FireBeforeLogin(this, e);

                        if (!e.Cancel)
                        {
                            var message = new GenericMessageViewModel();
                            var user    = new MembershipUser();
                            if (MembershipService.ValidateUser(email, password, 0))
                            {
                                user = MembershipService.GetUserByEmail(email);
                                //if (user.IsApproved && !user.IsLockedOut && !user.IsBanned)
                                //{
                                FormsAuthentication.SetAuthCookie(email, viewModel.RememberMe);
                                user.LastLoginDate = DateTime.UtcNow;

                                if (Url.IsLocalUrl(viewModel.ReturnUrl) && viewModel.ReturnUrl.Length > 1 && viewModel.ReturnUrl.StartsWith("/") &&
                                    !viewModel.ReturnUrl.StartsWith("//") && !viewModel.ReturnUrl.StartsWith("/\\"))
                                {
                                    return(Redirect(viewModel.ReturnUrl));
                                }

                                message.Message     = "You have successfully logged into the Chamber app";
                                message.MessageType = GenericMessages.success;
                                TempData[AppConstants.MessageViewBagName] = message;

                                EventManager.Instance.FireAfterLogin(this, new LoginEventArgs
                                {
                                    Email      = viewModel.Email,
                                    Password   = viewModel.Password,
                                    RememberMe = viewModel.RememberMe,
                                    ReturnUrl  = viewModel.ReturnUrl,
                                    UnitOfWork = unitOfWork
                                });

                                return(RedirectToAction("Index", "Home", new { area = string.Empty }));
                            }
                            else
                            {
                                message.Message     = "Something happened and your login failed.  Try again.";
                                message.MessageType = GenericMessages.danger;
                                TempData[AppConstants.MessageViewBagName] = message;
                                return(View());
                            }
                        }
                    }
                }
                finally
                {
                    try
                    {
                        unitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LoggingService.Error(ex);
                    }
                }
                return(View(viewModel));
            }
        }