/// <summary> /// Extract information from the query string and assign to our class level variables. Return false if something goes wrong /// and the variables cannot be set. This will happen when the query string is in an unexpected format. /// </summary> /// <param name="queryString">The query string for the current request. Can be populated with HttpContext.Request.Url.Query.</param> /// <returns>Returns true if all relevant variables were assigned from the query string; returns false if there was a problem.</returns> private bool ExtractQueryStringParms(string queryString) { if (String.IsNullOrEmpty(queryString)) { return(false); } if (queryString.StartsWith("?", StringComparison.Ordinal)) { queryString = queryString.Remove(0, 1); } //aid={0}&secaction={1} foreach (string nameValuePair in queryString.Split(new char[] { '&' })) { string[] nameOrValue = nameValuePair.Split(new char[] { '=' }); switch (nameOrValue[0]) { case "aid": { int aid; if (Int32.TryParse(nameOrValue[1], out aid)) { _albumId = aid; } else { return(false); } break; } case "secaction": { int secActionInt; if (Int32.TryParse(nameOrValue[1], out secActionInt)) { if (SecurityActionEnumHelper.IsValidSecurityAction((SecurityActions)secActionInt)) { _securityAction = (SecurityActions)secActionInt; break; } else { return(false); } } else { return(false); } } default: return(false); // Unexpected query string parm. Return false so execution is aborted. } } return(true); }
private TreeView GenerateTreeview() { // We'll use a TreeView instance to generate the appropriate XML structure ComponentArt.Web.UI.TreeView tv = new ComponentArt.Web.UI.TreeView(); string handlerPath = String.Concat(Utils.GalleryRoot, "/handler/gettreeviewxml.ashx"); IAlbum parentAlbum = AlbumController.LoadAlbumInstance(this._albumId, true); string securityActionParm = String.Empty; if (SecurityActionEnumHelper.IsValidSecurityAction(this._securityAction)) { securityActionParm = String.Format(CultureInfo.CurrentCulture, "&secaction={0}", (int)this._securityAction); } foreach (IAlbum childAlbum in parentAlbum.GetChildGalleryObjects(GalleryObjectType.Album, true, !Utils.IsAuthenticated)) { TreeViewNode node = new TreeViewNode(); node.Text = Utils.RemoveHtmlTags(childAlbum.Title); node.Value = childAlbum.Id.ToString(CultureInfo.InvariantCulture); node.ID = childAlbum.Id.ToString(CultureInfo.InvariantCulture); if (!String.IsNullOrEmpty(_navigateUrl)) { node.NavigateUrl = Utils.AddQueryStringParameter(_navigateUrl, String.Concat("aid=", childAlbum.Id.ToString(CultureInfo.InvariantCulture))); node.HoverCssClass = "tv0HoverTreeNodeLink"; } bool isUserAuthorized = true; if (SecurityActionEnumHelper.IsValidSecurityAction(this._securityAction)) { isUserAuthorized = Utils.IsUserAuthorized(_securityAction, RoleController.GetGalleryServerRolesForUser(), childAlbum.Id, childAlbum.GalleryId, childAlbum.IsPrivate); } node.ShowCheckBox = isUserAuthorized && _showCheckbox; node.Selectable = isUserAuthorized; if (!isUserAuthorized) { node.HoverCssClass = String.Empty; } if (childAlbum.GetChildGalleryObjects(GalleryObjectType.Album).Count > 0) { string handlerPathWithAlbumId = Utils.AddQueryStringParameter(handlerPath, String.Concat("aid=", childAlbum.Id.ToString(CultureInfo.InvariantCulture))); node.ContentCallbackUrl = String.Format(CultureInfo.CurrentCulture, "{0}{1}&sc={2}&nurl={3}", handlerPathWithAlbumId, securityActionParm, node.ShowCheckBox, Utils.UrlEncode(_navigateUrl)); } tv.Nodes.Add(node); } return(tv); }
private static bool IsAnonymousUserAuthorized(SecurityActions securityRequests, bool isPrivateAlbum, int galleryId, SecurityActionsOption secActionsOption) { // Anonymous user. Return true for viewing-related permission requests on PUBLIC albums; return false for all others. IGallerySettings gallerySettings = Factory.LoadGallerySetting(galleryId); if (SecurityActionEnumHelper.IsSingleSecurityAction(securityRequests)) { return(IsAnonymousUserAuthorizedForSingleSecurityAction(securityRequests, isPrivateAlbum, gallerySettings)); } else { return(IsAnonymousUserAuthorizedForMultipleSecurityActions(securityRequests, isPrivateAlbum, gallerySettings, secActionsOption)); } }
private TreeView GenerateTreeview() { // We'll use a TreeView instance to generate the appropriate XML structure ComponentArt.Web.UI.TreeView tv = new ComponentArt.Web.UI.TreeView(); string handlerPath = String.Concat(Util.GalleryRoot, "/handler/gettreeviewxml.ashx"); IAlbum parentAlbum = Factory.LoadAlbumInstance(this._albumId, true); string securityActionParm = String.Empty; if (SecurityActionEnumHelper.IsValidSecurityAction(this._securityAction)) { securityActionParm = String.Format(CultureInfo.CurrentCulture, "&secaction={0}", (int)this._securityAction); } foreach (IAlbum childAlbum in parentAlbum.GetChildGalleryObjects(GalleryObjectType.Album, true)) { TreeViewNode node = new TreeViewNode(); node.Text = Util.RemoveHtmlTags(childAlbum.Title); node.Value = childAlbum.Id.ToString(CultureInfo.InvariantCulture); node.ID = childAlbum.Id.ToString(CultureInfo.InvariantCulture); bool isUserAuthorized = true; if (SecurityActionEnumHelper.IsValidSecurityAction(this._securityAction)) { isUserAuthorized = Util.IsUserAuthorized(_securityAction, RoleController.GetGalleryServerRolesForUser(), childAlbum.Id, childAlbum.IsPrivate); } node.ShowCheckBox = isUserAuthorized; node.Selectable = isUserAuthorized; if (!isUserAuthorized) { node.HoverCssClass = String.Empty; } if (childAlbum.GetChildGalleryObjects(GalleryObjectType.Album).Count > 0) { node.ContentCallbackUrl = String.Format(CultureInfo.CurrentCulture, "{0}?aid={1}{2}", handlerPath, childAlbum.Id.ToString(CultureInfo.InvariantCulture), securityActionParm); } tv.Nodes.Add(node); } return(tv); }
///// <summary> ///// Render the treeview to two levels - the root album and its direct children. If the AlbumIdsToCheck property ///// has items in its collection, make sure every album in the collection is rendered, no matter how deep in the album heirarchy ///// they may be. If the albumToSelect parameter is specified, then make sure this album is rendered and ///// selected/checked, no matter how deep in the hierarchy it may be. ///// </summary> //public void BindTreeView() //{ // //BindTreeView(null); //} ///// <summary> ///// Render the treeview to two levels - the root album and its direct children. If the <see cref="CheckedAlbumIds" /> property ///// has items in its collection, make sure every album in the collection is rendered, no matter how deep in the album heirarchy ///// they may be. If the <paramref name="albumToSelect" /> parameter is specified, then make sure this album is rendered and ///// selected/checked, no matter how deep in the hierarchy it may be. ///// </summary> ///// <param name="albumToSelect">An album to be selected, checked, and made visible. The treeview is automatically expanded as ///// needed to ensure this album is visible.</param> //public void BindTreeView(IAlbum albumToSelect) //{ // //this._albumToSelect = albumToSelect; // //DataBindTreeView(); // //this._albumToSelect = null; // //this.AlbumIdsToCheck.Clear(); //} #endregion #region Private Methods /// <summary> /// Gets a JSON-formatted string of data that can be assigned to the data property of a /// jsTree jQuery instance. /// </summary> /// <returns>A string formatted as JSON.</returns> /// <exception cref="System.InvalidOperationException">Thrown when one or more business rules /// are violated.</exception> private string GetTreeData() { #region Validation if (!this.AllowMultiCheck && this.SelectedAlbumIds.Count > 1) { throw new InvalidOperationException("The property AllowMultiCheck must be false when multiple album IDs have been assigned to the property SelectedAlbumIds."); } if (!SecurityActionEnumHelper.IsValidSecurityAction(this.RequiredSecurityPermissions)) { throw new InvalidOperationException("The property GalleryServer.Web.Controls.albumtreeview.RequiredSecurityPermissions must be assigned before the TreeView can be rendered."); } #endregion TreeViewOptions tvOptions = new TreeViewOptions() { SelectedAlbumIds = SelectedAlbumIds, NumberOfLevels = NumberOfLevels, NavigateUrl = NavigateUrl, EnableCheckboxPlugin = EnableCheckboxPlugin, IncludeAlbum = IncludeAlbum, RequiredSecurityPermissions = RequiredSecurityPermissions, AlbumId = AlbumId, RootNodesPrefix = RootNodesPrefix, Galleries = Galleries }; Entity.TreeView tv = AlbumTreeViewBuilder.GetAlbumsAsTreeView(tvOptions); //if (_nodesToCheckIds == null) // _nodesToCheckIds = new List<string>(tv.NodesToCheckIdArray.Count); //_nodesToCheckIds.AddRange(tv.NodesToCheckIdArray); return(tv.ToJson()); }
private TreeView GenerateTreeview() { // We'll use a TreeView instance to generate the appropriate JSON structure TreeView tv = new TreeView(); IAlbum parentAlbum = AlbumController.LoadAlbumInstance(this._albumId, true); foreach (IAlbum childAlbum in parentAlbum.GetChildGalleryObjects(GalleryObjectType.Album, !Utils.IsAuthenticated).ToSortedList()) { TreeNode node = new TreeNode(); node.Id = String.Concat("tv_", childAlbum.Id.ToString(CultureInfo.InvariantCulture)); node.Text = Utils.RemoveHtmlTags(childAlbum.Title); node.DataId = childAlbum.Id.ToString(CultureInfo.InvariantCulture); if (!String.IsNullOrEmpty(_navigateUrl)) { node.NavigateUrl = Utils.AddQueryStringParameter(_navigateUrl, String.Concat("aid=", childAlbum.Id.ToString(CultureInfo.InvariantCulture))); } bool isUserAuthorized = true; if (SecurityActionEnumHelper.IsValidSecurityAction(this._securityAction)) { isUserAuthorized = Utils.IsUserAuthorized(_securityAction, RoleController.GetGalleryServerRolesForUser(), childAlbum.Id, childAlbum.GalleryId, childAlbum.IsPrivate, childAlbum.IsVirtualAlbum); } node.ShowCheckBox = isUserAuthorized && _showCheckbox; node.Selectable = isUserAuthorized; if (childAlbum.GetChildGalleryObjects(GalleryObjectType.Album, !Utils.IsAuthenticated).Any()) { node.HasChildren = true; } tv.Nodes.Add(node); } return(tv); }
private static bool IsAnonymousUserAuthorizedForMultipleSecurityActions(SecurityActions securityRequests, bool isPrivateAlbum, IGallerySettings gallerySettings, SecurityActionsOption secActionsOption) { // There are multiple security actions in securityAction enum. Iterate through each one and determine if the user // has permission for it. List <bool> authResults = new List <bool>(); foreach (SecurityActions securityAction in SecurityActionEnumHelper.ParseSecurityAction(securityRequests)) { authResults.Add(IsAnonymousUserAuthorizedForSingleSecurityAction(securityAction, isPrivateAlbum, gallerySettings)); } if (secActionsOption == SecurityActionsOption.RequireAll) { return(authResults.Count > 0 ? authResults.TrueForAll(delegate(bool value) { return value; }) : false); } else if (secActionsOption == SecurityActionsOption.RequireOne) { return(authResults.Contains(true)); } else { throw new InvalidEnumArgumentException("secActionsOption", (int)secActionsOption, typeof(SecurityActionsOption)); } }
/// <summary> /// Extract information from the query string and assign to our class level variables. Return false if something goes wrong /// and the variables cannot be set. This will happen when the query string is in an unexpected format. /// </summary> /// <param name="queryString">The query string for the current request. Can be populated with HttpContext.Request.Url.Query.</param> /// <returns>Returns true if all relevant variables were assigned from the query string; returns false if there was a problem.</returns> private bool ExtractQueryStringParms(string queryString) { if (String.IsNullOrEmpty(queryString)) { return(false); } if (queryString.StartsWith("?", StringComparison.Ordinal)) { queryString = queryString.Remove(0, 1); } //id={0}&secaction={1}&sc={2}&navurl={3} foreach (string nameValuePair in queryString.Split(new char[] { '&' })) { string[] nameOrValue = nameValuePair.Split(new char[] { '=' }); if (nameOrValue.Length < 2) { return(false); } switch (nameOrValue[0]) { case "id": { int aid; if (Int32.TryParse(nameOrValue[1], out aid)) { _albumId = aid; } else { return(false); } break; } case "secaction": { int secActionInt; if (Int32.TryParse(nameOrValue[1], out secActionInt)) { if (SecurityActionEnumHelper.IsValidSecurityAction((SecurityActions)secActionInt)) { _securityAction = (SecurityActions)secActionInt; break; } else { return(false); } } else { return(false); } } case "sc": { bool showCheckbox; if (Boolean.TryParse(nameOrValue[1], out showCheckbox)) { _showCheckbox = showCheckbox; } else { return(false); } break; } case "navurl": { _navigateUrl = Utils.UrlDecode(nameOrValue[1]).Trim(); break; } default: return(false); // Unexpected query string parm. Return false so execution is aborted. } } return(true); }
/// <summary> /// Render the treeview with the first two levels of albums that are viewable to the logged on user. /// </summary> private void DataBindTreeView() { #region Validation //if (!this.AllowMultiSelect && this.AlbumIdsToCheck.Count > 1) //{ // throw new InvalidOperationException("The property AllowMultiSelect must be false when multiple album IDs have been assigned to the property AlbumIdsToCheck."); //} if (!this.AllowMultiCheck && this.AlbumIdsToCheck.Count > 1) { throw new InvalidOperationException("The property AllowMultiCheck must be false when multiple album IDs have been assigned to the property AlbumIdsToCheck."); } if (!SecurityActionEnumHelper.IsValidSecurityAction(this.RequiredSecurityPermissions)) { throw new InvalidOperationException("The property GalleryServerPro.Web.Controls.albumtreeview.RequiredSecurityPermissions must be assigned before the TreeView can be rendered."); } #endregion tv.Nodes.Clear(); foreach (IAlbum rootAlbum in GetRootAlbums()) { // Add root node. TreeViewNode rootNode = new TreeViewNode(); string albumTitle = GetRootAlbumTitle(rootAlbum); rootNode.Text = albumTitle; rootNode.ToolTip = albumTitle; rootNode.Value = rootAlbum.Id.ToString(CultureInfo.InvariantCulture); rootNode.ID = rootAlbum.Id.ToString(CultureInfo.InvariantCulture); rootNode.Expanded = true; if (!String.IsNullOrEmpty(NavigateUrl)) { rootNode.NavigateUrl = String.Format("{0}?aid={1}", NavigateUrl, rootAlbum.Id); rootNode.HoverCssClass = "tv0HoverTreeNodeLink"; } bool isAlbumSelectable = !rootAlbum.IsVirtualAlbum; rootNode.ShowCheckBox = isAlbumSelectable && ShowCheckbox; rootNode.Selectable = isAlbumSelectable; if (!isAlbumSelectable) { rootNode.HoverCssClass = String.Empty; } // Select and check this node if needed. if (isAlbumSelectable && (this._albumToSelect != null) && (rootAlbum.Id == _albumToSelect.Id)) { tv.SelectedNode = rootNode; rootNode.Checked = true; } // Check this node if needed. if (this._albumIdsToCheck.Contains(rootAlbum.Id)) { rootNode.Checked = true; } tv.Nodes.Add(rootNode); // Add the first level of albums below the root album. BindAlbumToTreeview(rootAlbum.GetChildGalleryObjects(GalleryObjectType.Album, true, this.GalleryPage.IsAnonymousUser), rootNode, false); // Only display the root node if it is selectable or we added any children to it; otherwise, remove it. if (!rootNode.Selectable && rootNode.Nodes.Count == 0) { tv.Nodes.Remove(rootNode); } } if ((this._albumToSelect != null) && (tv.SelectedNode == null)) { // We have an album we are supposed to select, but we haven't encountered it in the first two levels, // so expand the treeview as needed to include this album. BindSpecificAlbumToTreeview(this._albumToSelect); } // Make sure all specified albums are visible and checked. foreach (int albumId in this._albumIdsToCheck) { IAlbum album = Factory.LoadAlbumInstance(albumId, false); if (this.GalleryPage.IsUserAuthorized(RequiredSecurityPermissions, album)) { BindSpecificAlbumToTreeview(album); } } }
/// <summary> /// Determine whether the user belonging to the specified <paramref name="roles" /> has permission to perform all of the specified security /// actions against the specified <paramref name="albumId" />. The user may be anonymous or logged on. /// When the the user is logged on (i.e. <paramref name="isAuthenticated" /> = true), this method determines whether the user is authorized by /// validating that at least one role has the requested permission to the specified album. When the user is anonymous, /// the <paramref name="roles" /> parameter is ignored and instead the <paramref name="isPrivateAlbum" /> parameter is used. /// Anonymous users do not have any access to private albums. When the the user is logged on (i.e. <paramref name="isAuthenticated" /> = true), /// the <paramref name="roles" /> parameter must contain the roles belonging to the user. /// </summary> /// <param name="securityRequests">Represents the permission or permissions being requested. Multiple actions can be specified by using /// a bitwise OR between them (example: <see cref="SecurityActions.AdministerSite" /> | <see cref="SecurityActions.AdministerGallery" />). /// If multiple actions are specified, use <paramref name="secActionsOption" /> to specify whether all of the actions must be satisfied /// to be successful or only one item must be satisfied.</param> /// <param name="roles">A collection of Gallery Server roles to which the currently logged-on user belongs. This parameter is ignored /// for anonymous users (i.e. <paramref name="isAuthenticated" />=false). The parameter may be null.</param> /// <param name="albumId">The album for which the requested permission applies. This parameter does not apply when the requested permission /// is <see cref="SecurityActions.AdministerSite" /> or <see cref="SecurityActions.AdministerGallery" />.</param> /// <param name="galleryId">The ID for the gallery the user is requesting permission in. The <paramref name="albumId" /> must exist in this /// gallery. This parameter is not required <paramref name="securityRequests" /> is SecurityActions.AdministerSite (you can specify /// <see cref="int.MinValue" />).</param> /// <param name="isAuthenticated">A value indicating whether the current user is logged on. If true, the /// <paramref name="roles" /> parameter should contain the names of the roles for the current user. If <paramref name="isAuthenticated" />=true /// and the <paramref name="roles" /> parameter is either null or an empty collection, this method returns false.</param> /// <param name="isPrivateAlbum">A value indicating whether the album is hidden from anonymous users. This parameter is ignored for /// logged-on users.</param> /// <param name="secActionsOption">Specifies whether the user must have permission for all items in <paramref name="securityRequests" /> /// to be successful or just one. This parameter defaults to SecurityActionsOption.RequireAll when not specified, and is applicable only /// when <paramref name="securityRequests" /> contains more than one item.</param> /// <param name="isVirtualAlbum">if set to <c>true</c> the album is a virtual album.</param> /// <returns> /// Returns true if the user has the requested permission; returns false if not. /// </returns> /// <exception cref="System.ArgumentOutOfRangeException"></exception> /// <exception cref="System.ComponentModel.InvalidEnumArgumentException"></exception> /// <remarks> /// This method handles both anonymous and logged on users. Note that when <paramref name="isAuthenticated" />=true, the /// <paramref name="isPrivateAlbum" /> parameter is ignored. When it is false, the <paramref name="roles" /> parameter is ignored. /// </remarks> public static bool IsUserAuthorized(SecurityActions securityRequests, IGalleryServerRoleCollection roles, int albumId, int galleryId, bool isAuthenticated, bool isPrivateAlbum, SecurityActionsOption secActionsOption, bool isVirtualAlbum) { #region Validation if (isAuthenticated && !isVirtualAlbum && ((roles == null) || (roles.Count == 0))) { return(false); } var userIsRequestingSysAdminPermission = (securityRequests & SecurityActions.AdministerSite) == SecurityActions.AdministerSite; var userIsRequestingGalleryAdminPermission = (securityRequests & SecurityActions.AdministerGallery) == SecurityActions.AdministerGallery; if (galleryId == int.MinValue) { var isMoreThanOnePermissionRequest = !SecurityActionEnumHelper.IsSingleSecurityAction(securityRequests); if (isMoreThanOnePermissionRequest || !userIsRequestingSysAdminPermission) { throw new ArgumentOutOfRangeException("galleryId", String.Format(CultureInfo.CurrentCulture, "A valid gallery ID must be specified. Instead, the value was {0}.", galleryId)); } } #endregion if (isVirtualAlbum && (!userIsRequestingSysAdminPermission && !userIsRequestingGalleryAdminPermission)) { return(true); // Virtual albums are always allowed, but only for non-admin requests. This feels hacky and non-intuitive; should try to improve someday } // Handle anonymous users. if (!isAuthenticated) { return(IsAnonymousUserAuthorized(securityRequests, isPrivateAlbum, galleryId, secActionsOption)); } // If we get here we are dealing with an authenticated (logged on) user. Authorization for authenticated users is // given if the user is a member of at least one role that provides permission. if (SecurityActionEnumHelper.IsSingleSecurityAction(securityRequests)) { // Iterate through each GalleryServerRole. If at least one allows the action, return true. Note that the // AdministerSite security action, if granted, applies to all albums and allows all actions (except HideWatermark). foreach (IGalleryServerRole role in roles) { if (IsAuthenticatedUserAuthorized(securityRequests, role, albumId, galleryId)) { return(true); } } return(false); } else { // There are multiple security actions in securityRequest enum. Iterate through each one and determine if the user // has permission for it. List <bool> authResults = new List <bool>(); foreach (SecurityActions securityAction in SecurityActionEnumHelper.ParseSecurityAction(securityRequests)) { // Iterate through each role. If at least one role allows the action, permission is granted. foreach (IGalleryServerRole role in roles) { bool authResult = IsAuthenticatedUserAuthorized(securityAction, role, albumId, galleryId); authResults.Add(authResult); if (authResult) { break; // We found a role that provides permission, so no need to check the other roles. Just move on to the next security request. } } } // Determine the return value based on what the calling method wanted. if (secActionsOption == SecurityActionsOption.RequireAll) { return(authResults.Count > 0 ? authResults.TrueForAll(delegate(bool value) { return value; }) : false); } else if (secActionsOption == SecurityActionsOption.RequireOne) { return(authResults.Contains(true)); } else { throw new InvalidEnumArgumentException("secActionsOption", (int)secActionsOption, typeof(SecurityActionsOption)); } } }
/// <summary> /// Extract information from the query string and assign to our class level variables. Return false if something goes wrong /// and the variables cannot be set. This will happen when the query string is in an unexpected format. /// </summary> /// <param name="queryString">The query string for the current request. Can be populated with HttpContext.Request.Url.Query.</param> /// <returns>Returns true if all relevant variables were assigned from the query string; returns false if there was a problem.</returns> private bool ExtractQueryStringParms(string queryString) { if (String.IsNullOrEmpty(queryString)) { return(false); } if (queryString.StartsWith("?", StringComparison.Ordinal)) { queryString = queryString.Remove(0, 1); } // id=0&gid=all&secaction=6&sc=false&navurl=&levels=2&includealbum=true&idtoselect%5B%5D=220&idtoselect%5B%5D=99 foreach (string nameValuePair in queryString.Split(new char[] { '&' })) { string[] nameOrValue = nameValuePair.Split(new char[] { '=' }); if (nameOrValue.Length < 2) { return(false); } switch (Utils.UrlDecode(nameOrValue[0])) { case "id": { int aid; if (Int32.TryParse(nameOrValue[1], out aid)) { _albumId = aid; } else { return(false); } break; } case "gid": { int gid; if (Int32.TryParse(nameOrValue[1], out gid)) { _galleries = new GalleryCollection() { Factory.LoadGallery(gid) }; } else if (Utils.UrlDecode(nameOrValue[1]).Trim() == "all") { _galleries = UserController.GetGalleriesCurrentUserCanAdminister(); } else { return(false); } break; } case "secaction": { int secActionInt; if (Int32.TryParse(nameOrValue[1], out secActionInt)) { if (SecurityActionEnumHelper.IsValidSecurityAction((SecurityActions)secActionInt)) { _securityAction = (SecurityActions)secActionInt; break; } else { return(false); } } else { return(false); } } case "sc": { bool showCheckbox; if (Boolean.TryParse(nameOrValue[1], out showCheckbox)) { _showCheckbox = showCheckbox; } else { return(false); } break; } case "navurl": { _navigateUrl = Utils.UrlDecode(nameOrValue[1]).Trim(); break; } case "levels": { int numLevels; if (Int32.TryParse(nameOrValue[1], out numLevels)) { _numberOfLevels = numLevels; } else { return(false); } break; } case "includealbum": { bool includeAlbum; if (Boolean.TryParse(nameOrValue[1], out includeAlbum)) { _includeAlbum = includeAlbum; } else { return(false); } break; } case "idtoselect": case "idtoselect[]": { int idToSelect; if (Int32.TryParse(nameOrValue[1], out idToSelect)) { _albumIdsToSelect.Add(idToSelect); } else { return(false); } break; } default: return(false); // Unexpected query string parm. Return false so execution is aborted. } } return(true); }
/// <summary> /// Render the treeview with the first two levels of albums that are viewable to the logged on user. /// </summary> private void DataBindTreeView() { #region Validation //if (!this.AllowMultiSelect && this.AlbumIdsToCheck.Count > 1) //{ // throw new InvalidOperationException("The property AllowMultiSelect must be false when multiple album IDs have been assigned to the property AlbumIdsToCheck."); //} if (!this.AllowMultiSelect && this.AlbumIdsToSelect.Count > 1) { throw new InvalidOperationException("The property AllowMultiSelect must be false when multiple album IDs have been assigned to the property TopLevelAlbumIdsToCheck."); } if (!SecurityActionEnumHelper.IsValidSecurityAction(this.RequiredSecurityPermissions)) { throw new InvalidOperationException("The property GalleryServerPro.Web.Controls.albumtreeview.RequiredSecurityPermissions must be assigned before the TreeView can be rendered."); } #endregion // Add root node. IAlbum rootAlbum = Factory.LoadRootAlbum(this.RequiredSecurityPermissions, this.GalleryPage.GetGalleryServerRolesForUser()); TreeViewNode rootNode = new TreeViewNode(); rootNode.Text = Util.RemoveHtmlTags(rootAlbum.Title); rootNode.Value = rootAlbum.Id.ToString(CultureInfo.InvariantCulture); rootNode.ID = rootAlbum.Id.ToString(CultureInfo.InvariantCulture); rootNode.Expanded = true; bool isAlbumSelectable = !rootAlbum.IsVirtualAlbum; rootNode.ShowCheckBox = isAlbumSelectable; rootNode.Selectable = isAlbumSelectable; if (!isAlbumSelectable) { rootNode.HoverCssClass = String.Empty; } // Select and check this node if needed. if (isAlbumSelectable && (this._albumToSelect != null) && (rootAlbum.Id == _albumToSelect.Id)) { tv.SelectedNode = rootNode; rootNode.Checked = true; } // Check this node if needed. if (this._albumIdsToSelect.Contains(rootAlbum.Id)) { rootNode.Checked = true; } tv.Nodes.Clear(); tv.Nodes.Add(rootNode); // Add the first level of albums below the root album. BindAlbumToTreeview(rootAlbum.GetChildGalleryObjects(GalleryObjectType.Album), rootNode, false); if ((this._albumToSelect != null) && (tv.SelectedNode == null)) { // We have an album we are supposed to select, but we haven't encountered it in the first two levels, // so expand the treeview as needed to include this album. BindSpecificAlbumToTreeview(this._albumToSelect); } // Make sure all specified albums are visible and checked. foreach (int albumId in this._albumIdsToSelect) { BindSpecificAlbumToTreeview(Factory.LoadAlbumInstance(albumId, false)); } }