Esempio n. 1
0
        public ActionResult Edit(MemberFrontEndEditViewModel userModel)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var loggedOnUserId = (LoggedOnReadOnlyUser != null ? LoggedOnReadOnlyUser.Id : Guid.Empty);
                var permissions = RoleService.GetPermissions(null, UsersRole);

                // Check is has permissions
                if (UserIsAdmin || loggedOnUserId == userModel.Id || permissions[AppConstants.PermissionEditMembers].IsTicked)
                {
                    // Get the user from DB
                    var user = MembershipService.GetUser(userModel.Id);

                    // Before we do anything - Check stop words
                    var stopWords = _bannedWordService.GetAll(true);
                    var bannedWords = _bannedWordService.GetAll().Select(x => x.Word).ToList();

                    // Check the fields for bad words
                    foreach (var stopWord in stopWords)
                    {
                        if ((userModel.Facebook != null && userModel.Facebook.IndexOf(stopWord.Word, StringComparison.CurrentCultureIgnoreCase) >= 0) ||
                            (userModel.Location != null && userModel.Location.IndexOf(stopWord.Word, StringComparison.CurrentCultureIgnoreCase) >= 0) ||
                            (userModel.Signature != null && userModel.Signature.IndexOf(stopWord.Word, StringComparison.CurrentCultureIgnoreCase) >= 0) ||
                            (userModel.Twitter != null && userModel.Twitter.IndexOf(stopWord.Word, StringComparison.CurrentCultureIgnoreCase) >= 0) ||
                            (userModel.Website != null && userModel.Website.IndexOf(stopWord.Word, StringComparison.CurrentCultureIgnoreCase) >= 0))
                        {

                            ShowMessage(new GenericMessageViewModel
                            {
                                Message = LocalizationService.GetResourceString("StopWord.Error"),
                                MessageType = GenericMessages.danger
                            });

                            // Ahhh found a stop word. Abandon operation captain.
                            return View(userModel);

                        }
                    }

                    // Sort image out first
                    if (userModel.Files != null)
                    {
                        // Before we save anything, check the user already has an upload folder and if not create one
                        var uploadFolderPath = HostingEnvironment.MapPath(string.Concat(SiteConstants.UploadFolderPath, LoggedOnReadOnlyUser.Id));
                        if (!Directory.Exists(uploadFolderPath))
                        {
                            Directory.CreateDirectory(uploadFolderPath);
                        }

                        // Loop through each file and get the file info and save to the users folder and Db
                        var file = userModel.Files[0];
                        if (file != null)
                        {
                            // If successful then upload the file
                            var uploadResult = AppHelpers.UploadFile(file, uploadFolderPath, LocalizationService, true);

                            if (!uploadResult.UploadSuccessful)
                            {
                                TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                                {
                                    Message = uploadResult.ErrorMessage,
                                    MessageType = GenericMessages.danger
                                };
                                return View(userModel);
                            }

                            // Save avatar to user
                            user.Avatar = uploadResult.UploadedFileName;
                        }
                    }

                    // Set the users Avatar for the confirmation page
                    userModel.Avatar = user.Avatar;

                    // Update other users properties
                    user.Age = userModel.Age;
                    user.Facebook = _bannedWordService.SanitiseBannedWords(userModel.Facebook, bannedWords);
                    user.Location = _bannedWordService.SanitiseBannedWords(userModel.Location, bannedWords);
                    user.Signature = _bannedWordService.SanitiseBannedWords(StringUtils.ScrubHtml(userModel.Signature, true), bannedWords);
                    user.Twitter = _bannedWordService.SanitiseBannedWords(userModel.Twitter, bannedWords);
                    user.Website = _bannedWordService.SanitiseBannedWords(userModel.Website, bannedWords);
                    user.DisableEmailNotifications = userModel.DisableEmailNotifications;

                    // User is trying to change username, need to check if a user already exists
                    // with the username they are trying to change to
                    var changedUsername = false;
                    var sanitisedUsername = _bannedWordService.SanitiseBannedWords(userModel.UserName, bannedWords);
                    if (sanitisedUsername != user.UserName)
                    {
                        if (MembershipService.GetUser(sanitisedUsername) != null)
                        {
                            unitOfWork.Rollback();
                            ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Members.Errors.DuplicateUserName"));
                            return View(userModel);
                        }

                        user.UserName = sanitisedUsername;
                        changedUsername = true;
                    }

                    // User is trying to update their email address, need to 
                    // check the email is not already in use
                    if (userModel.Email != user.Email)
                    {
                        // Add get by email address
                        if (MembershipService.GetUserByEmail(userModel.Email) != null)
                        {
                            unitOfWork.Rollback();
                            ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Members.Errors.DuplicateEmail"));
                            return View(userModel);
                        }
                        user.Email = userModel.Email;
                    }

                    MembershipService.ProfileUpdated(user);

                    ShowMessage(new GenericMessageViewModel
                    {
                        Message = LocalizationService.GetResourceString("Member.ProfileUpdated"),
                        MessageType = GenericMessages.success
                    });

                    try
                    {
                        unitOfWork.Commit();

                        if (changedUsername)
                        {
                            // User has changed their username so need to log them in
                            // as there new username of 
                            var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                            if (authCookie != null)
                            {
                                var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                                if (authTicket != null)
                                {
                                    var newFormsIdentity = new FormsIdentity(new FormsAuthenticationTicket(authTicket.Version,
                                                                                                           user.UserName,
                                                                                                           authTicket.IssueDate,
                                                                                                           authTicket.Expiration,
                                                                                                           authTicket.IsPersistent,
                                                                                                           authTicket.UserData));
                                    var roles = authTicket.UserData.Split("|".ToCharArray());
                                    var newGenericPrincipal = new GenericPrincipal(newFormsIdentity, roles);
                                    System.Web.HttpContext.Current.User = newGenericPrincipal;
                                }
                            }

                            // sign out current user
                            FormsAuthentication.SignOut();

                            // Abandon the session
                            Session.Abandon();

                            // Sign in new user
                            FormsAuthentication.SetAuthCookie(user.UserName, false);
                        }
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LoggingService.Error(ex);
                        ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Errors.GenericMessage"));
                    }

                    return View(userModel);
                }


                return ErrorToHomePage(LocalizationService.GetResourceString("Errors.NoPermission"));
            }
        }
Esempio n. 2
0
 private MemberFrontEndEditViewModel PopulateMemberViewModel(MembershipUser user)
 {
     var viewModel = new MemberFrontEndEditViewModel
     {
         Id = user.Id,
         UserName = user.UserName,
         Email = user.Email,
         Signature = user.Signature,
         Age = user.Age,
         Location = user.Location,
         Website = user.Website,
         Twitter = user.Twitter,
         Facebook = user.Facebook,
         DisableFileUploads = user.DisableFileUploads == true,
         Avatar = user.Avatar,
         DisableEmailNotifications = user.DisableEmailNotifications == true
     };
     return viewModel;
 }
Esempio n. 3
0
        public ActionResult Edit(Guid id)
        {
            using (UnitOfWorkManager.NewUnitOfWork())
            {

                var user = MembershipService.GetUser(id);
                var viewModel = new MemberFrontEndEditViewModel
                                    {
                                        Id = user.Id,
                                        UserName = user.UserName,
                                        Email = user.Email,
                                        Signature = user.Signature,
                                        Age = user.Age,
                                        Location = user.Location,
                                        Website = user.Website,
                                        Twitter = user.Twitter,
                                        Facebook = user.Facebook,
                                    };

                return View(viewModel);
            }
        }
Esempio n. 4
0
        public ActionResult Edit(MemberFrontEndEditViewModel userModel)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var user = MembershipService.GetUser(userModel.Id);

                user.Age = userModel.Age;
                user.Facebook = _bannedWordService.SanitiseBannedWords(userModel.Facebook);
                user.Location = _bannedWordService.SanitiseBannedWords(userModel.Location);
                user.Signature = _bannedWordService.SanitiseBannedWords(userModel.Signature);
                user.Twitter = _bannedWordService.SanitiseBannedWords(userModel.Twitter);
                user.Website = _bannedWordService.SanitiseBannedWords(userModel.Website);

                // If there is a location try and save the longitude and latitude
                if (!string.IsNullOrEmpty(user.Location))
                {
                    try
                    {
                        var longLat = LocalisationUtils.GeocodeGoogle(user.Location);
                        if (longLat != null && longLat[0] != "0")
                        {
                            // Got the long lat and save them to the user
                            user.Latitude = longLat[0];
                            user.Longitude = longLat[1];
                        }
                    }
                    catch
                    {
                        LoggingService.Error("Error getting longitude and latitude from location");
                    }
                }

                // User is trying to change username, need to check if a user already exists
                // with the username they are trying to change to
                var changedUsername = false;
                var sanitisedUsername = _bannedWordService.SanitiseBannedWords(userModel.UserName);
                if (sanitisedUsername != user.UserName)
                {
                    if (MembershipService.GetUser(sanitisedUsername) != null)
                    {
                        unitOfWork.Rollback();
                        ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Members.Errors.DuplicateUserName"));
                        return View(userModel);
                    }

                    user.UserName = sanitisedUsername;
                    changedUsername = true;
                }

                // User is trying to update their email address, need to
                // check the email is not already in use
                if (userModel.Email != user.Email)
                {
                    // Add get by email address
                    if (MembershipService.GetUserByEmail(userModel.Email) != null)
                    {
                        unitOfWork.Rollback();
                        ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Members.Errors.DuplicateEmail"));
                        return View(userModel);
                    }
                    user.Email = userModel.Email;
                }

                MembershipService.ProfileUpdated(user);

                ViewBag.Message = new GenericMessageViewModel
                {
                    Message = LocalizationService.GetResourceString("Member.ProfileUpdated"),
                    MessageType = GenericMessages.success
                };

                var viewModel = new MemberFrontEndEditViewModel
                {
                    Id = user.Id,
                    UserName = user.UserName,
                    Email = user.Email,
                    Signature = user.Signature,
                    Age = user.Age,
                    Location = user.Location,
                    Website = user.Website,
                    Twitter = user.Twitter,
                    Facebook = user.Facebook,
                };

                try
                {
                    unitOfWork.Commit();

                    if (changedUsername)
                    {
                        // User has changed their username so need to log them in
                        // as there new username of
                        var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                        if (authCookie != null)
                        {
                            var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                            if (authTicket != null)
                            {
                                var newFormsIdentity = new FormsIdentity(new FormsAuthenticationTicket(authTicket.Version,
                                                                                                       user.UserName,
                                                                                                       authTicket.IssueDate,
                                                                                                       authTicket.Expiration,
                                                                                                       authTicket.IsPersistent,
                                                                                                       authTicket.UserData));
                                var roles = authTicket.UserData.Split("|".ToCharArray());
                                var newGenericPrincipal = new GenericPrincipal(newFormsIdentity, roles);
                                System.Web.HttpContext.Current.User = newGenericPrincipal;
                            }
                        }

                        // sign out current user
                        FormsAuthentication.SignOut();

                        // Abandon the session
                        Session.Abandon();

                        // Sign in new user
                        FormsAuthentication.SetAuthCookie(user.UserName, false);
                    }
                }
                catch (Exception ex)
                {
                    unitOfWork.Rollback();
                    LoggingService.Error(ex);
                }

                return View(viewModel);
            }
        }
        public ActionResult Edit(MemberFrontEndEditViewModel userModel)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var user = MembershipService.GetUser(userModel.Id);

                // Sort image out first
                if (userModel.Files != null)
                {
                    // Before we save anything, check the user already has an upload folder and if not create one
                    var uploadFolderPath = Server.MapPath(string.Concat(AppConstants.UploadFolderPath, LoggedOnUser.Id));
                    if (!Directory.Exists(uploadFolderPath))
                    {
                        Directory.CreateDirectory(uploadFolderPath);
                    }

                    // Loop through each file and get the file info and save to the users folder and Db
                    var file = userModel.Files[0];
                    if (file != null)
                    {
                        // If successful then upload the file
                        var uploadResult = AppHelpers.UploadFile(file, uploadFolderPath, LocalizationService, true);                        

                        if (!uploadResult.UploadSuccessful)
                        {
                            TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                            {
                                Message = uploadResult.ErrorMessage,
                                MessageType = GenericMessages.error
                            };
                            return View(userModel);
                        }


                        // Save avatar to user
                        user.Avatar = uploadResult.UploadedFileName;   

                    }

                }

                user.Age = userModel.Age;
                user.Facebook = _bannedWordService.SanitiseBannedWords(userModel.Facebook);
                user.Location = _bannedWordService.SanitiseBannedWords(userModel.Location);
                user.Signature = _bannedWordService.SanitiseBannedWords(StringUtils.ScrubHtml(userModel.Signature));
                user.Twitter = _bannedWordService.SanitiseBannedWords(userModel.Twitter);
                user.Website = _bannedWordService.SanitiseBannedWords(userModel.Website);

                // If there is a location try and save the longitude and latitude
                if (!string.IsNullOrEmpty(user.Location))
                {
                    try
                    {
                        var longLat = LocalisationUtils.GeocodeGoogle(user.Location);
                        if (longLat != null && longLat[0] != "0")
                        {
                            // Got the long lat and save them to the user
                            user.Latitude = longLat[0];
                            user.Longitude = longLat[1];
                        }
                    }
                    catch
                    {
                        LoggingService.Error("Error getting longitude and latitude from location");
                    }
                }

                // User is trying to change username, need to check if a user already exists
                // with the username they are trying to change to
                var changedUsername = false;
                var sanitisedUsername = _bannedWordService.SanitiseBannedWords(userModel.UserName);
                if (sanitisedUsername != user.UserName)
                {
                    if (MembershipService.GetUser(sanitisedUsername) != null)
                    {
                        unitOfWork.Rollback();
                        ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Members.Errors.DuplicateUserName"));
                        return View(userModel);
                    }

                    user.UserName = sanitisedUsername;
                    changedUsername = true;
                }

                // User is trying to update their email address, need to 
                // check the email is not already in use
                if (userModel.Email != user.Email)
                {
                    // Add get by email address
                    if (MembershipService.GetUserByEmail(userModel.Email) != null)
                    {
                        unitOfWork.Rollback();
                        ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Members.Errors.DuplicateEmail"));
                        return View(userModel);
                    }
                    user.Email = userModel.Email;
                }

                MembershipService.ProfileUpdated(user);

                ViewBag.Message = new GenericMessageViewModel
                {
                    Message = LocalizationService.GetResourceString("Member.ProfileUpdated"),
                    MessageType = GenericMessages.success
                };

                var viewModel = new MemberFrontEndEditViewModel
                {
                    Id = user.Id,
                    UserName = user.UserName,
                    Email = user.Email,
                    Signature = user.Signature,
                    Age = user.Age,
                    Location = user.Location,
                    Website = user.Website,
                    Twitter = user.Twitter,
                    Facebook = user.Facebook,
                    Avatar = user.Avatar
                };

                try
                {
                    unitOfWork.Commit();

                    if (changedUsername)
                    {
                        // User has changed their username so need to log them in
                        // as there new username of 
                        var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                        if (authCookie != null)
                        {
                            var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                            if (authTicket != null)
                            {
                                var newFormsIdentity = new FormsIdentity(new FormsAuthenticationTicket(authTicket.Version,
                                                                                                       user.UserName,
                                                                                                       authTicket.IssueDate,
                                                                                                       authTicket.Expiration,
                                                                                                       authTicket.IsPersistent,
                                                                                                       authTicket.UserData));
                                var roles = authTicket.UserData.Split("|".ToCharArray());
                                var newGenericPrincipal = new GenericPrincipal(newFormsIdentity, roles);
                                System.Web.HttpContext.Current.User = newGenericPrincipal;
                            }
                        }

                        // sign out current user
                        FormsAuthentication.SignOut();

                        // Abandon the session
                        Session.Abandon();

                        // Sign in new user
                        FormsAuthentication.SetAuthCookie(user.UserName, false);
                    }
                }
                catch (Exception ex)
                {
                    unitOfWork.Rollback();
                    LoggingService.Error(ex);
                    ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Errors.GenericMessage"));
                }

                return View(viewModel);
            }
        }