Exemple #1
0
        /// <summary>
        /// Creates a new page.
        /// </summary>
        /// <param name="tenantId">The website that created page will belong to.</param>
        /// <param name="parentPageId">Parent page (can be null if page has no parent - i.e. is home page, or if can be determined).</param>
        /// <param name="masterpageId">The master page that newly created page is based on.</param>
        /// <param name="pageInfo">If specified, used to override master page settings.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        /// <returns>Newly allocated page identifier.</returns>
        public long Create(long tenantId, long?parentPageId, long masterPageId, PageInfo pageInfo, IUnitOfWork unitOfWork = null)
        {
            // Multiple actions are performed during page creation, so we need a unit of work to perform rollback if any failures occur
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            try
            {
                // Get master page that will determine how new page is created
                MasterPage masterPage = _masterPageRepository.Read(tenantId, masterPageId, unitOfWork ?? localUnitOfWork);

                // Get valid parent pages
                List <Page> parentPages = null;
                if (masterPage.AncestorPageId.HasValue && masterPage.AncestorPageLevel.HasValue)
                {
                    parentPages = ListMasterPageParentPages(masterPage, unitOfWork ?? localUnitOfWork);
                }

                // Validate the page create request (including checking that all or none of the image upload properties are specified)
                _pageValidator.ValidateCreate(tenantId, parentPageId, pageInfo, parentPages);

                // Set parent page identifier?
                if (parentPageId == null && masterPage.AncestorPageId.HasValue && masterPage.AncestorPageLevel.HasValue)
                {
                    parentPageId = parentPages[0].PageId;
                }

                // Construct new page based on master page definition
                DateTime now  = DateTime.UtcNow;
                Page     page = new Page
                {
                    Created                = now,
                    Description            = pageInfo == null ? masterPage.PageDescription : pageInfo.Description,
                    MasterPageId           = masterPageId,
                    Name                   = pageInfo == null ? masterPage.PageName : pageInfo.Name,
                    Occurred               = masterPage.HasOccurred ? DateTime.UtcNow.Date : (DateTime?)null,
                    ParentPageId           = parentPageId,
                    Updated                = now,
                    TenantId               = tenantId,
                    PageZones              = new List <PageZone>(),
                    Tags                   = pageInfo == null ? new List <Tag>() : pageInfo.Tags,
                    ImageTenantId          = pageInfo == null ? null : pageInfo.ImageTenantId,
                    ThumbnailImageUploadId = pageInfo == null ? null : pageInfo.ThumbnailImageUploadId,
                    PreviewImageUploadId   = pageInfo == null ? null : pageInfo.PreviewImageUploadId,
                    ImageUploadId          = pageInfo == null ? null : pageInfo.ImageUploadId
                };

                // Construct page zones
                foreach (MasterPageZone masterPageZone in masterPage.MasterPageZones)
                {
                    if (masterPageZone.AdminType != MasterPageZoneAdminType.Static)
                    {
                        PageZone pageZone = new PageZone
                        {
                            TenantId         = tenantId,
                            MasterPageId     = masterPageId,
                            MasterPageZoneId = masterPageZone.MasterPageZoneId,
                            PageZoneElements = new List <PageZoneElement>()
                        };
                        foreach (MasterPageZoneElement masterPageZoneElement in masterPageZone.MasterPageZoneElements)
                        {
                            long            elementId       = _elementService.Copy(tenantId, masterPageZoneElement.ElementId, tenantId, masterPageZoneElement.Element.ElementTypeId, unitOfWork ?? localUnitOfWork);
                            PageZoneElement pageZoneElement = new PageZoneElement
                            {
                                TenantId      = tenantId,
                                ElementTypeId = masterPageZoneElement.Element.ElementTypeId,
                                ElementId     = elementId,
                                Parent        = pageZone
                            };
                            if (masterPageZone.AdminType == MasterPageZoneAdminType.Configurable)
                            {
                                pageZoneElement.SortOrder = masterPageZoneElement.SortOrder;
                            }
                            if (masterPageZone.AdminType == MasterPageZoneAdminType.Editable)
                            {
                                pageZoneElement.MasterPageId            = masterPageId;
                                pageZoneElement.MasterPageZoneId        = masterPageZone.MasterPageZoneId;
                                pageZoneElement.MasterPageZoneElementId = masterPageZoneElement.MasterPageZoneElementId;
                            }
                            pageZone.PageZoneElements.Add(pageZoneElement);
                        }
                        page.PageZones.Add(pageZone);
                    }
                }

                // Commit page images?
                if (page.ImageUploadId.HasValue)
                {
                    _uploadService.Commit(page.ImageTenantId.Value, page.ThumbnailImageUploadId.Value, GetPageImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                    _uploadService.Commit(page.ImageTenantId.Value, page.PreviewImageUploadId.Value, GetPageImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                    _uploadService.Commit(page.ImageTenantId.Value, page.ImageUploadId.Value, GetPageImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                }

                // Create page and return newly allocated page identifier
                page.PageId = _pageRepository.Create(page, unitOfWork ?? localUnitOfWork);

                // Update page tags?
                if (masterPage.Taggable)
                {
                    _pageRepository.UpdateTags(page, unitOfWork ?? localUnitOfWork);
                }

                // Commit work if local unit of work in place, then return newly allocated page identifier
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
                return(page.PageId);
            }
            catch (ValidationErrorException)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            catch (Exception ex)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }
Exemple #2
0
        public bool UpdateUser(UpdateUserModel model, IUnitOfWork unitOfWork = null)
        {
            // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            // Do the user update
            try
            {
                // Validate supplied details (including checking that all or none of the image upload properties are specified)
                _authenticationValidator.ValidateUpdateUser(model);

                // Get existing user details
                User   user         = _userRepository.ReadUser(model.TenantId, model.UserId);
                string currentAlias = user.Alias;
                string currentEmail = user.Email;

                // Get new details
                string newAlias = model.Alias.Trim();
                string newEmail = model.Email.Trim().ToLower();
                user.Alias = newAlias;
                user.Email = newEmail;

                // Has email address changed?
                bool reconfirm = (currentEmail != newEmail);
                if (reconfirm)
                {
                    // Get confirm verification token
                    TimeSpan expiryTimeSpan = _authenticationConfigurationService.GetUpdateUserExpiryTimeSpan(model.TenantId);
                    Token    confirmToken   = _securityService.CreateToken(expiryTimeSpan);

                    // Unconfirm account
                    user.Confirmed          = false;
                    user.ConfirmTokenValue  = confirmToken.Value.ToString();
                    user.ConfirmTokenExpiry = confirmToken.Expiry;

                    // Commit user images?
                    if (model.ImageUploadId.HasValue && user.ImageUploadId != model.ImageUploadId)
                    {
                        _uploadService.Commit(model.ImageTenantId.Value, model.ThumbnailImageUploadId.Value, GetUserImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                        _uploadService.Commit(model.ImageTenantId.Value, model.PreviewImageUploadId.Value, GetUserImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                        _uploadService.Commit(model.ImageTenantId.Value, model.ImageUploadId.Value, GetUserImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);

                        user.ImageTenantId          = model.ImageTenantId;
                        user.ThumbnailImageUploadId = model.ThumbnailImageUploadId;
                        user.PreviewImageUploadId   = model.PreviewImageUploadId;
                        user.ImageUploadId          = model.ImageUploadId;
                    }

                    // Update user details
                    _userRepository.UpdateUser(user, unitOfWork ?? localUnitOfWork);

                    // Get details of email that will be sent to user who must re-confirm their account
                    Email email = _authenticationConfigurationService.GetUpdateUserEmail(Web, Domain, newEmail, newAlias, confirmToken);

                    // Send email to newly created user
                    _emailService.SendEmail(email);
                }

                // If email address not changed
                if (!reconfirm)
                {
                    // Commit user images?
                    if (model.ImageUploadId.HasValue && user.ImageUploadId != model.ImageUploadId)
                    {
                        _uploadService.Commit(model.ImageTenantId.Value, model.ThumbnailImageUploadId.Value, GetUserImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                        _uploadService.Commit(model.ImageTenantId.Value, model.PreviewImageUploadId.Value, GetUserImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);
                        _uploadService.Commit(model.ImageTenantId.Value, model.ImageUploadId.Value, GetUserImageStorageHierarchy(), unitOfWork ?? localUnitOfWork);

                        user.ImageTenantId          = model.ImageTenantId;
                        user.ThumbnailImageUploadId = model.ThumbnailImageUploadId;
                        user.PreviewImageUploadId   = model.PreviewImageUploadId;
                        user.ImageUploadId          = model.ImageUploadId;
                    }

                    // Update user details
                    _userRepository.UpdateUser(user, unitOfWork ?? localUnitOfWork);

                    // If email address not changed, update authenticated user details
                    AuthenticatedUserInfo userInfo = GetCurrentUser();
                    userInfo.User.Alias = user.Alias;
                    _authenticationProviderService.LogonAuthenticatedUser(userInfo);
                }

                // Commit work if local unit of work in place
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }

                // Return whether or not user must reconfirm address
                return(reconfirm);
            }
            catch (ValidationErrorException)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            catch (Exception ex)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
        }
Exemple #3
0
        public void Update(IElementSettings settings, IUnitOfWork unitOfWork = null)
        {
            // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            // Begin work
            try
            {
                // Validate slides
                AlbumSettings albumSettings = (AlbumSettings)settings;
                foreach (AlbumPhoto photo in albumSettings.Photos)
                {
                    _albumValidator.ValidatePhoto(photo);
                }

                // Get current album settings
                AlbumSettings currentAlbumSettings = (AlbumSettings)New(settings.TenantId);
                currentAlbumSettings.ElementId = settings.ElementId;
                _albumRepository.Read(currentAlbumSettings, unitOfWork ?? localUnitOfWork);

                // Get photos to delete (i.e. photos that were in current settings, but not in the new settings)
                // Get photos with updated images
                List <AlbumPhoto>             photosToDelete                 = new List <AlbumPhoto>();
                List <AlbumPhoto>             photosWithUpdatedImages        = new List <AlbumPhoto>();
                List <AlbumPhoto>             currentPhotosWithUpdatedImages = new List <AlbumPhoto>();
                Dictionary <long, AlbumPhoto> photosById = albumSettings.Photos.Where(p => p.AlbumPhotoId != 0).GroupBy(s => s.AlbumPhotoId).ToDictionary(u => u.Key, u => u.First());
                foreach (AlbumPhoto currentPhoto in currentAlbumSettings.Photos)
                {
                    if (!photosById.ContainsKey(currentPhoto.AlbumPhotoId))
                    {
                        photosToDelete.Add(currentPhoto);
                    }
                    else
                    {
                        AlbumPhoto photo = photosById[currentPhoto.AlbumPhotoId];
                        if (photo.ImageUploadId != currentPhoto.ImageUploadId)
                        {
                            photosWithUpdatedImages.Add(photo);
                            currentPhotosWithUpdatedImages.Add(currentPhoto);
                        }
                    }
                }

                // Get new photos
                List <AlbumPhoto> photosToCreate = albumSettings.Photos.Where(s => s.AlbumPhotoId == 0).ToList();

                // Commit new images
                photosToCreate.AddRange(photosWithUpdatedImages);
                foreach (AlbumPhoto photo in photosToCreate)
                {
                    _uploadService.Commit(photo.ImageTenantId, photo.ThumbnailImageUploadId, GetAlbumPhotoStorageHierarchy(photo.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Commit(photo.ImageTenantId, photo.PreviewImageUploadId, GetAlbumPhotoStorageHierarchy(photo.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Commit(photo.ImageTenantId, photo.ImageUploadId, GetAlbumPhotoStorageHierarchy(photo.ElementId), unitOfWork ?? localUnitOfWork);
                }

                // Update database
                _albumRepository.Update((AlbumSettings)settings, unitOfWork ?? localUnitOfWork);

                // Delete uploads that are no longer required
                photosToDelete.AddRange(currentPhotosWithUpdatedImages);
                foreach (AlbumPhoto currentPhoto in photosToDelete)
                {
                    _uploadService.Delete(currentPhoto.ImageTenantId, currentPhoto.ThumbnailImageUploadId, GetAlbumPhotoStorageHierarchy(currentPhoto.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Delete(currentPhoto.ImageTenantId, currentPhoto.PreviewImageUploadId, GetAlbumPhotoStorageHierarchy(currentPhoto.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Delete(currentPhoto.ImageTenantId, currentPhoto.ImageUploadId, GetAlbumPhotoStorageHierarchy(currentPhoto.ElementId), unitOfWork ?? localUnitOfWork);
                }

                // Commit work if local unit of work in place and return result
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
            }
            catch (ValidationErrorException)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            catch (Exception ex)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }
        public void Update(IElementSettings settings, IUnitOfWork unitOfWork = null)
        {
            // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            // Begin work
            try
            {
                // Validate slides
                CarouselSettings carouselSettings = (CarouselSettings)settings;
                foreach (CarouselSlide slide in carouselSettings.Slides)
                {
                    _carouselValidator.ValidateSlide(slide);
                }

                // Get current carousel settings
                CarouselSettings currentCarouselSettings = (CarouselSettings)New(settings.TenantId);
                currentCarouselSettings.ElementId = settings.ElementId;
                _carouselRepository.Read(currentCarouselSettings, unitOfWork ?? localUnitOfWork);

                // Get slides to delete (i.e. slides that were in current settings, but not in the new settings)
                // Get slides with updated images
                List <CarouselSlide>             slidesToDelete                 = new List <CarouselSlide>();
                List <CarouselSlide>             slidesWithUpdatedImages        = new List <CarouselSlide>();
                List <CarouselSlide>             currentSlidesWithUpdatedImages = new List <CarouselSlide>();
                Dictionary <long, CarouselSlide> slidesById = carouselSettings.Slides.Where(s => s.CarouselSlideId != 0).GroupBy(s => s.CarouselSlideId).ToDictionary(u => u.Key, u => u.First());
                foreach (CarouselSlide currentSlide in currentCarouselSettings.Slides)
                {
                    if (!slidesById.ContainsKey(currentSlide.CarouselSlideId))
                    {
                        slidesToDelete.Add(currentSlide);
                    }
                    else
                    {
                        CarouselSlide slide = slidesById[currentSlide.CarouselSlideId];
                        if (slide.ImageUploadId != currentSlide.ImageUploadId)
                        {
                            slidesWithUpdatedImages.Add(slide);
                            currentSlidesWithUpdatedImages.Add(currentSlide);
                        }
                    }
                }

                // Get new slides
                List <CarouselSlide> slidesToCreate = carouselSettings.Slides.Where(s => s.CarouselSlideId == 0).ToList();

                // Commit new images
                slidesToCreate.AddRange(slidesWithUpdatedImages);
                foreach (CarouselSlide slide in slidesToCreate)
                {
                    _uploadService.Commit(slide.ImageTenantId, slide.ThumbnailImageUploadId, GetCarouselSlideStorageHierarchy(slide.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Commit(slide.ImageTenantId, slide.PreviewImageUploadId, GetCarouselSlideStorageHierarchy(slide.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Commit(slide.ImageTenantId, slide.ImageUploadId, GetCarouselSlideStorageHierarchy(slide.ElementId), unitOfWork ?? localUnitOfWork);
                }

                // Update database
                _carouselRepository.Update((CarouselSettings)settings, unitOfWork ?? localUnitOfWork);

                // Delete uploads that are no longer required
                slidesToDelete.AddRange(currentSlidesWithUpdatedImages);
                foreach (CarouselSlide currentSlide in slidesToDelete)
                {
                    _uploadService.Delete(currentSlide.ImageTenantId, currentSlide.ThumbnailImageUploadId, GetCarouselSlideStorageHierarchy(currentSlide.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Delete(currentSlide.ImageTenantId, currentSlide.PreviewImageUploadId, GetCarouselSlideStorageHierarchy(currentSlide.ElementId), unitOfWork ?? localUnitOfWork);
                    _uploadService.Delete(currentSlide.ImageTenantId, currentSlide.ImageUploadId, GetCarouselSlideStorageHierarchy(currentSlide.ElementId), unitOfWork ?? localUnitOfWork);
                }

                // Commit work if local unit of work in place and return result
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
            }
            catch (ValidationErrorException)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            catch (Exception ex)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }
        public long PrepareImages(long tenantId, long elementId, CreateUploadModel model, IUnitOfWork unitOfWork = null)
        {
            // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            // Begin work
            try
            {
                // Check that uploaded content is valid image
                System.Drawing.Size imageSize = _htmlValidator.ValidatePrepareImages(tenantId, elementId, model);

                // Get HTML settings (does not have to be within unit of work)
                HtmlSettings htmlSettings = (HtmlSettings)New(tenantId);
                htmlSettings.ElementId = elementId;
                Read(htmlSettings);

                // Create thumbnail model
                byte[] thumbnailContent = model.Content;
                if (imageSize.Width > htmlSettings.ThumbnailImageWidth || imageSize.Height > htmlSettings.ThumbnailImageHeight)
                {
                    ResizeInfo thumbnailResizeInfo = new ResizeInfo
                    {
                        Width      = htmlSettings.ThumbnailImageWidth,
                        Height     = htmlSettings.ThumbnailImageHeight,
                        ResizeMode = htmlSettings.ThumbnailImageResizeMode
                    };
                    thumbnailContent = _imageAnalysisService.ResizeImage(model.Content, thumbnailResizeInfo);
                }
                CreateUploadModel thumbnailModel = new CreateUploadModel
                {
                    Content     = thumbnailContent,
                    ContentType = model.ContentType,
                    Name        = model.Name,
                    TenantId    = model.TenantId
                };

                // Create preview model
                byte[] previewContent = model.Content;
                if (imageSize.Width > htmlSettings.PreviewImageWidth || imageSize.Height > htmlSettings.PreviewImageHeight)
                {
                    ResizeInfo previewResizeInfo = new ResizeInfo
                    {
                        Width      = htmlSettings.PreviewImageWidth,
                        Height     = htmlSettings.PreviewImageHeight,
                        ResizeMode = htmlSettings.PreviewImageResizeMode
                    };
                    previewContent = _imageAnalysisService.ResizeImage(model.Content, previewResizeInfo);
                }
                CreateUploadModel previewModel = new CreateUploadModel
                {
                    Content     = previewContent,
                    ContentType = model.ContentType,
                    Name        = model.Name,
                    TenantId    = model.TenantId
                };

                // Create uncommitted uploads for thumbnail, preview and original image
                long thumbnailImageUploadId = _uploadService.Create(thumbnailModel, unitOfWork ?? localUnitOfWork);
                long previewImageUploadId   = _uploadService.Create(previewModel, unitOfWork ?? localUnitOfWork);
                long imageUploadId          = _uploadService.Create(model, unitOfWork ?? localUnitOfWork);

                // Commit uploads
                _uploadService.Commit(tenantId, thumbnailImageUploadId, GetHtmlUploadStorageHierarchy(elementId), unitOfWork ?? localUnitOfWork);
                _uploadService.Commit(tenantId, previewImageUploadId, GetHtmlUploadStorageHierarchy(elementId), unitOfWork ?? localUnitOfWork);
                _uploadService.Commit(tenantId, imageUploadId, GetHtmlUploadStorageHierarchy(elementId), unitOfWork ?? localUnitOfWork);

                // Create HTML image, recording upload IDs of newly created images
                HtmlUpload upload = new HtmlUpload
                {
                    TenantId               = tenantId,
                    ElementId              = elementId,
                    ImageTenantId          = tenantId,
                    ThumbnailImageUploadId = thumbnailImageUploadId,
                    PreviewImageUploadId   = previewImageUploadId,
                    ImageUploadId          = imageUploadId
                };
                long htmlImageId = _htmlRepository.CreateUpload(upload, unitOfWork ?? localUnitOfWork);

                // Commit work if local unit of work in place and return result
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
                return(htmlImageId);
            }
            catch (ValidationErrorException)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            catch (Exception ex)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }