/// <summary> /// Creates an album, assigns the user name as the owner, saves it, and returns the newly created album. /// A profile entry is created containing the album ID. Returns null if the ID specified in the config file /// for the parent album does not represent an existing album. That is, returns null if the userAlbumParentAlbumId /// in galleryserverpro.config does not match an existing album. /// </summary> /// <param name="userName">The user name representing the user who is the owner of the album.</param> /// <returns>Returns the newly created user album. It has already been persisted to the database. /// Returns null if the ID specified in the config file for the parent album does not represent an existing album. /// That is, returns null if the userAlbumParentAlbumId in galleryserverpro.config does not match an existing album.</returns> public static IAlbum CreateUserAlbum(string userName) { Core core = Config.GetCore(); string albumNameTemplate = core.UserAlbumNameTemplate; IAlbum parentAlbum; try { parentAlbum = Factory.LoadAlbumInstance(core.UserAlbumParentAlbumId, false); } catch (ErrorHandler.CustomExceptions.InvalidAlbumException ex) { // The parent album does not exist. Record the error and return null. string msg = String.Format(CultureInfo.CurrentCulture, Resources.GalleryServerPro.Error_User_Album_Parent_Invalid_Ex_Msg, core.UserAlbumParentAlbumId); AppErrorController.LogError(new ErrorHandler.CustomExceptions.WebException(msg, ex)); return(null); } IAlbum album = Factory.CreateAlbumInstance(); album.Title = albumNameTemplate.Replace("{UserName}", userName); album.Summary = core.UserAlbumSummaryTemplate; album.OwnerUserName = userName; //newAlbum.ThumbnailMediaObjectId = 0; // not needed album.Parent = parentAlbum; album.IsPrivate = parentAlbum.IsPrivate; GalleryObjectController.SaveGalleryObject(album, userName); SaveAlbumIdToProfile(album.Id, userName); HelperFunctions.PurgeCache(); return(album); }
/// <summary> /// Creates an album, assigns the user name as the owner, saves it, and returns the newly created album. /// A profile entry is created containing the album ID. Returns null if the ID specified in the gallery settings /// for the parent album does not represent an existing album. That is, returns null if <see cref="IGallerySettings.UserAlbumParentAlbumId" /> /// does not match an existing album. /// </summary> /// <param name="userName">The user name representing the user who is the owner of the album.</param> /// <param name="galleryId">The gallery ID for the gallery in which the album is to be created.</param> /// <returns> /// Returns the newly created user album. It has already been persisted to the database. /// Returns null if the ID specified in the gallery settings for the parent album does not represent an existing album. /// That is, returns null if <see cref="IGallerySettings.UserAlbumParentAlbumId" /> /// does not match an existing album. /// </returns> public static IAlbum CreateUserAlbum(string userName, int galleryId) { IGallerySettings gallerySetting = Factory.LoadGallerySetting(galleryId); string albumNameTemplate = gallerySetting.UserAlbumNameTemplate; IAlbum parentAlbum; try { parentAlbum = AlbumController.LoadAlbumInstance(gallerySetting.UserAlbumParentAlbumId, false); } catch (InvalidAlbumException ex) { // The parent album does not exist. Record the error and return null. string galleryDescription = Utils.HtmlEncode(Factory.LoadGallery(gallerySetting.GalleryId).Description); string msg = String.Format(CultureInfo.CurrentCulture, Resources.GalleryServerPro.Error_User_Album_Parent_Invalid_Ex_Msg, galleryDescription, gallerySetting.UserAlbumParentAlbumId); AppErrorController.LogError(new WebException(msg, ex), galleryId); return(null); } IAlbum album = null; try { album = Factory.CreateEmptyAlbumInstance(parentAlbum.GalleryId); album.Title = albumNameTemplate.Replace("{UserName}", userName); album.Summary = gallerySetting.UserAlbumSummaryTemplate; album.OwnerUserName = userName; //newAlbum.ThumbnailMediaObjectId = 0; // not needed album.Parent = parentAlbum; album.IsPrivate = parentAlbum.IsPrivate; GalleryObjectController.SaveGalleryObject(album, userName); SaveAlbumIdToProfile(album.Id, userName, album.GalleryId); HelperFunctions.PurgeCache(); } catch { if (album != null) { album.Dispose(); } throw; } return(album); }
/// <summary> /// Validates the album owner. If an album is being removed from the <paramref name="roleName"/> and that album is /// using this role for album ownership, remove the ownership setting from the album. /// </summary> /// <param name="roleName">Name of the role that is being modified.</param> /// <param name="rootAlbumIdsOld">The list of album ID's that were previously assigned to the role. If an album ID exists /// in this object and not in <paramref name="rootAlbumIdsNew"/>, that means the album is being removed from the role.</param> /// <param name="rootAlbumIdsNew">The list of album ID's that are now assigned to the role. If an album ID exists /// in this object and not in <paramref name="rootAlbumIdsOld"/>, that means it is a newly added album.</param> private static void ValidateAlbumOwnerRoles(string roleName, IEnumerable <int> rootAlbumIdsOld, ICollection <int> rootAlbumIdsNew) { foreach (int albumId in rootAlbumIdsOld) { if (!rootAlbumIdsNew.Contains(albumId)) { // Album has been removed from role. Remove owner from the album if the album owner role matches the one we are dealing with. IAlbum album = Factory.LoadAlbumInstance(albumId, false); if (album.OwnerRoleName == roleName) { album.OwnerUserName = String.Empty; GalleryObjectController.SaveGalleryObject(album); } } } }
/// <summary> /// Set the IsPrivate property of all child albums and media objects of the specified album to have the same value /// as the specified album. /// </summary> /// <param name="album">The album whose child objects are to be updated to have the same IsPrivate value.</param> private static void SynchIsPrivatePropertyOnChildGalleryObjects(IAlbum album) { album.Inflate(true); foreach (IAlbum childAlbum in album.GetChildGalleryObjects(GalleryObjectType.Album)) { childAlbum.Inflate(true); // The above Inflate() does not inflate child albums, so we need to explicitly inflate it. childAlbum.IsPrivate = album.IsPrivate; GalleryObjectController.SaveGalleryObject(childAlbum); SynchIsPrivatePropertyOnChildGalleryObjects(childAlbum); } foreach (IGalleryObject childGalleryObject in album.GetChildGalleryObjects(GalleryObjectType.MediaObject)) { childGalleryObject.IsPrivate = album.IsPrivate; GalleryObjectController.SaveGalleryObject(childGalleryObject); } }
/// <summary> /// Inspects the specified <paramref name="album" /> to see if the OwnerUserName is in the list of user accounts. If not, the property /// is cleared out (which also clears out the OwnerRoleName property). This function acts recursively on any child albums of the /// <paramref name="album" />. /// </summary> /// <param name="album">The album to inspect.</param> /// <param name="userNames">A list of all usernames in the current membership.</param> private static void DeleteOrphanedAlbumOwner(IAlbum album, List <String> userNames) { if ((!String.IsNullOrEmpty(album.OwnerUserName)) && (!userNames.Contains(album.OwnerUserName))) { if (RoleController.GetUsersInRole(album.OwnerRoleName).Length == 0) { RoleController.DeleteGalleryServerProRole(album.OwnerRoleName); } album.OwnerUserName = String.Empty; // This will also clear out the OwnerRoleName property. GalleryObjectController.SaveGalleryObject(album); } foreach (IAlbum childAlbum in album.GetChildGalleryObjects(GalleryObjectType.Album)) { DeleteOrphanedAlbumOwner(childAlbum, userNames); } }
/// <summary> /// Remove the user from any roles. If a role is an ownership role, then delete it if the user is the only member. /// Remove the user from ownership of any albums. /// </summary> /// <param name="userName">Name of the user to be deleted.</param> /// <remarks>The user will be specified as an owner only for those albums that belong in ownership roles, so /// to find all albums the user owns, we need only to loop through the user's roles and inspect the ones /// where the names begin with the album owner role name prefix variable.</remarks> private static void UpdateRolesAndOwnershipForDeletedUser(string userName) { List <string> rolesToDelete = new List <string>(); string[] userRoles = RoleController.GetRolesForUser(userName); foreach (string roleName in userRoles) { if (RoleController.IsRoleAnAlbumOwnerRole(roleName)) { // This is a role that was automatically created to provide ownership permission to an album. Check each // album and empty out the OwnerUserName field if the listed owner matches our user. IGalleryServerRole role = Factory.LoadGalleryServerRole(roleName); foreach (int albumId in role.RootAlbumIds) { IAlbum album = Factory.LoadAlbumInstance(albumId, false); if (album.OwnerUserName == userName) { album.OwnerUserName = String.Empty; GalleryObjectController.SaveGalleryObject(album); } } if (RoleController.GetUsersInRole(roleName).Length <= 1) { // The user we are deleting is the only user in the owner role. Mark for deletion. rolesToDelete.Add(roleName); } } } if (userRoles.Length > 0) { foreach (string role in userRoles) { RoleController.RemoveUserFromRole(userName, role); } } foreach (string roleName in rolesToDelete) { RoleController.DeleteGalleryServerProRole(roleName); } }
/// <summary> /// For roles that provide album ownership functionality, remove users belonging to this role from the OwnedBy /// property of any albums this role is assigned to. Since we are deleting the role that provides the ownership /// functionality, it is necessary to clear the owner field of all affected albums. /// </summary> /// <param name="role">Name of the role to be deleted.</param> private static void UpdateAlbumOwnerBeforeRoleDelete(IGalleryServerRole role) { // Proceed only when dealing with an album ownership role. if (!IsRoleAnAlbumOwnerRole(role.RoleName)) { return; } // Loop through each album assigned to this role. If this role is assigned as the owner role, // clear the OwnerUserName property. foreach (int albumId in role.RootAlbumIds) { IAlbum album = Factory.LoadAlbumInstance(albumId, false); if (album.OwnerRoleName == role.RoleName) { album.OwnerUserName = String.Empty; GalleryObjectController.SaveGalleryObject(album); } } }
/// <summary> /// Inspects the specified <paramref name="album" /> to see if the <see cref="IAlbum.OwnerUserName" /> is an existing user. /// If not, the property is cleared out (which also clears out the <see cref="IAlbum.OwnerRoleName" /> property). /// </summary> /// <param name="album">The album to inspect.</param> private static void ValidateAlbumOwner(IAlbum album) { if ((!String.IsNullOrEmpty(album.OwnerUserName)) && (!UserController.GetAllUsers().Contains(album.OwnerUserName))) { if (RoleController.GetUsersInRole(album.OwnerRoleName).Length == 0) { RoleController.DeleteGalleryServerProRole(album.OwnerRoleName); } if (album.IsWritable) { album.OwnerUserName = String.Empty; // This will also clear out the OwnerRoleName property. GalleryObjectController.SaveGalleryObject(album); } else { // Load a writeable version and update the database, then do the same update to our in-memory instance. IAlbum albumWritable = Factory.LoadAlbumInstance(album.Id, false, true); albumWritable.OwnerUserName = String.Empty; // This will also clear out the OwnerRoleName property. GalleryObjectController.SaveGalleryObject(albumWritable); // Update our local in-memory object to match the one we just saved. album.OwnerUserName = String.Empty; album.OwnerRoleName = String.Empty; } // Remove this item from cache so that we don't have any old copies floating around. Dictionary <int, IAlbum> albumCache = (Dictionary <int, IAlbum>)HelperFunctions.GetCache(CacheItem.Albums); if (albumCache != null) { albumCache.Remove(album.Id); } } }
private static ActionResult CreateMediaObject(string filePath, IAlbum album, AddMediaObjectSettings options) { var result = new ActionResult() { Title = Path.GetFileName(filePath) }; try { using (IGalleryObject go = Factory.CreateMediaObjectInstance(filePath, album)) { SaveGalleryObject(go); if (options.DiscardOriginalFile) { go.DeleteOriginalFile(); GalleryObjectController.SaveGalleryObject(go); } result.Status = ActionResultStatus.Success; } } catch (UnsupportedMediaObjectTypeException ex) { try { File.Delete(filePath); } catch (UnauthorizedAccessException) { } // Ignore an error; the file will continue to exist in the destination album directory result.Status = ActionResultStatus.Error; result.Message = ex.Message; } return(result); }
/// <summary> /// Update the album with the specified properties in the albumEntity parameter. The title is validated before /// saving, and may be altered to conform to business rules, such as removing HTML tags. After the object is persisted /// to the data store, the albumEntity parameter is updated with the latest properties from the album object and returned. /// If the user is not authorized to edit the album, no action is taken. /// </summary> /// <param name="albumEntity">An AlbumWebEntity instance containing data to be persisted to the data store.</param> /// <returns>Returns an AlbumWebEntity instance containing the data as persisted to the data store. Some properties, /// such as the Title property, may be slightly altered to conform to validation rules.</returns> public static AlbumWebEntity UpdateAlbumInfo(AlbumWebEntity albumEntity) { if (albumEntity.Owner == Resources.GalleryServerPro.UC_Album_Header_Edit_Album_No_Owner_Text) { albumEntity.Owner = String.Empty; } IAlbum album = Factory.LoadAlbumInstance(albumEntity.Id, false); // Update remaining properties if user has edit album permission. if (GalleryPage.IsUserAuthorized(SecurityActions.EditAlbum, album.Id)) { if (album.Title != albumEntity.Title) { album.Title = Util.CleanHtmlTags(albumEntity.Title); if ((!album.IsRootAlbum) && (Config.GetCore().SynchAlbumTitleAndDirectoryName)) { // Root albums do not have a directory name that reflects the album's title, so only update this property for non-root albums. album.DirectoryName = HelperFunctions.ValidateDirectoryName(album.Parent.FullPhysicalPath, album.Title); } } album.Summary = Util.CleanHtmlTags(albumEntity.Summary); album.DateStart = albumEntity.DateStart.Date; album.DateEnd = albumEntity.DateEnd.Date; if (albumEntity.IsPrivate != album.IsPrivate) { if (!albumEntity.IsPrivate && album.Parent.IsPrivate) { throw new NotSupportedException("Cannot make album public: It is invalid to make an album public when it's parent album is private."); } album.IsPrivate = albumEntity.IsPrivate; SynchIsPrivatePropertyOnChildGalleryObjects(album); } // If the owner has changed, update it, but only if the user is administrator. if (albumEntity.Owner != album.OwnerUserName) { if (GalleryPage.IsUserAuthorized(SecurityActions.AdministerSite, album.Id)) { if (!String.IsNullOrEmpty(album.OwnerUserName)) { // Another user was previously assigned as owner. Delete role since this person will no longer be the owner. RoleController.DeleteGalleryServerProRole(album.OwnerRoleName); } // Util.SaveGalleryObject will make sure there is a role created for this user. album.OwnerUserName = albumEntity.Owner; } } GalleryObjectController.SaveGalleryObject(album); HelperFunctions.PurgeCache(); // Refresh the entity object with the data from the album object, in case something changed. For example, // some javascript or HTML may have been removed from the Title or Summary fields. albumEntity.Title = album.Title; albumEntity.Summary = album.Summary; albumEntity.DateStart = album.DateStart; albumEntity.DateEnd = album.DateEnd; albumEntity.IsPrivate = album.IsPrivate; albumEntity.Owner = album.OwnerUserName; } return(albumEntity); }