Exemplo n.º 1
0
        /// <summary>
        /// Sends the e-mail. If <paramref name="suppressException"/> is <c>true</c>, record any exception that occurs but do not
        /// let it propagate out of this function. When <c>false</c>, record the exception and re-throw it. The caller is
        /// responsible for disposing the <paramref name="mail"/> object.
        /// </summary>
        /// <param name="mail">The mail message to send.</param>
        /// <param name="suppressException">If <c>true</c>, record any exception that occurs but do not
        /// let it propagate out of this function. When <c>false</c>, record the exception and re-throw it.</param>
        private void SendEmail(MailMessage mail, bool suppressException)
        {
            try
            {
                if (mail == null)
                {
                    throw new ArgumentNullException("mail");
                }

                using (SmtpClient smtpClient = new SmtpClient())
                {
                    var appSettings = AppSetting.Instance;

                    smtpClient.EnableSsl = appSettings.SendEmailUsingSsl;

                    string smtpServer = appSettings.SmtpServer;
                    int    smtpServerPort;
                    if (!Int32.TryParse(appSettings.SmtpServerPort, out smtpServerPort))
                    {
                        smtpServerPort = Int32.MinValue;
                    }

                    // Specify SMTP server if it is specified in the gallery settings. The server might have been assigned via web.config,
                    // so only update this if we have a setting.
                    if (!String.IsNullOrEmpty(smtpServer))
                    {
                        smtpClient.Host = smtpServer;
                    }

                    // Specify port number if it is specified in the gallery settings and it's not the default value of 25. The port
                    // might have been assigned via web.config, so only update this if we have a setting.
                    if ((smtpServerPort > 0) && (smtpServerPort != 25))
                    {
                        smtpClient.Port = smtpServerPort;
                    }

                    if (String.IsNullOrEmpty(smtpClient.Host))
                    {
                        throw new WebException(@"Cannot send e-mail because a SMTP Server is not specified. This can be configured in any of the following places: (1) Site Admin - General page (preferred), or (2) web.config (Ex: configuration/system.net/mailSettings/smtp/network host='your SMTP server').");
                    }

                    smtpClient.Send(mail);
                }
            }
            catch (Exception ex)
            {
                AppEventController.LogError(ex);

                if (!suppressException)
                {
                    throw;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initialize the Gallery Server application. This method is designed to be run at application startup. The business layer
        /// is initialized with the current trust level and a few configuration settings. The business layer also initializes
        /// the data store, including verifying a minimal level of data integrity, such as at least one record for the root album.
        /// Initialization that requires an HttpContext is also performed. When this method completes, <see cref="IAppSetting.IsInitialized" />
        /// will be <c>true</c>, but <see cref="AppController.IsInitialized" /> will be <c>true</c> only when an HttpContext instance
        /// exists. If this function is initially called from a place where an HttpContext doesn't exist, it will automatically be called
        /// again later, eventually being called from a place where an HttpContext does exist, thus completing app initialization.
        /// </summary>
        public async Task InitializeGspApplication()
        {
            //try
            //{
            await _lock.WaitAsync().ConfigureAwait(false);

            try
            {
                if (IsInitialized)
                {
                    return;
                }

                InitializeApplication();

                await _gallerySettingController.AddMembershipDataToGallerySettings();

                _isInitialized = true;

                AppEventController.LogEvent("Application has started.");
            }
            finally
            {
                _lock.Release();
            }
            //}
            //catch (ThreadAbortException)
            //{
            //}
            //catch (CannotWriteToDirectoryException ex)
            //{
            //    // Let the error handler log it and try to redirect to a dedicated page for this error. The transfer will fail when the error occurs
            //    // during the app's init event, so when this happens don't re-throw (like we do in the generic catch below). This will allow the
            //    // initialize routine to run again from the GalleryPage constructor, and when the error happens again, this time the handler will be able to redirect.
            //    AppEventController.HandleGalleryException(ex);
            //    //throw; // Don't re-throw
            //}
            //catch (Exception ex)
            //{
            //    // Let the error handler deal with it. It will decide whether to transfer the user to a friendly error page.
            //    // If the function returns, that means it didn't redirect, so we should re-throw the exception.
            //    AppEventController.HandleGalleryException(ex);
            //    throw;
            //}
        }
        /// <summary>
        /// Render the treeview with the first two levels of albums that are viewable to the logged on user.
        /// </summary>
        /// <param name="tvOptions">The treeview options.</param>
        /// <returns>Task&lt;TreeView&gt;.</returns>
        private async Task <TreeView> Generate(TreeViewOptions tvOptions)
        {
            Options = tvOptions;
            Tree.EnableCheckBoxPlugin = Options.EnableCheckboxPlugin;

            foreach (IAlbum rootAlbum in await GetTopAlbums())
            {
                if (!Options.UserController.IsUserAuthorized(SecurityActions.ViewAlbumOrMediaObject, await Options.UserController.GetGalleryServerRolesForUser(), rootAlbum.Id, rootAlbum.GalleryId, rootAlbum.IsPrivate, rootAlbum.IsVirtualAlbum))
                {
                    continue;
                }

                // Add root node.
                TreeNode rootNode = new TreeNode();

                string albumTitle = GetTopAlbumTitle(rootAlbum);
                rootNode.Text     = albumTitle;
                rootNode.ToolTip  = albumTitle;
                rootNode.Id       = String.Concat("tv_", rootAlbum.Id.ToString(CultureInfo.InvariantCulture));
                rootNode.DataId   = rootAlbum.Id.ToString(CultureInfo.InvariantCulture);
                rootNode.Expanded = (Options.NumberOfLevels > 1);

                if (rootAlbum.Parent is NullGalleryObject)
                {
                    rootNode.AddCssClass("jstree-root-node");
                }

                if (!String.IsNullOrEmpty(Options.NavigateUrl))
                {
                    //var url = rootAlbum.IsVirtualAlbum ? Options.NavigateUrl : Utils.AddQueryStringParameter(Options.NavigateUrl, String.Concat("aid=", rootAlbum.Id.ToString(CultureInfo.InvariantCulture)));
                    var url = rootAlbum.IsVirtualAlbum ? Options.NavigateUrl : string.Concat(Options.NavigateUrl, "?aid=", rootAlbum.Id.ToString(CultureInfo.InvariantCulture));
                    rootNode.NavigateUrl = url;
                }

                // If it has a nav URL, it's always selectable & won't have a checkbox. If not, then it's selectable if the user has permission
                if (string.IsNullOrEmpty(rootNode.NavigateUrl))
                {
                    rootNode.Selectable = !rootAlbum.IsVirtualAlbum && Options.UserController.IsUserAuthorized(Options.RequiredSecurityPermissions, await GetRoles(), rootAlbum.Id, rootAlbum.GalleryId, rootAlbum.IsPrivate, SecurityActionsOption.RequireOne, rootAlbum.IsVirtualAlbum);

                    if (Options.EnableCheckboxPlugin)
                    {
                        rootNode.ShowCheckBox = rootNode.Selectable;
                    }
                }
                else
                {
                    rootNode.Selectable = true;
                }

                // Select and check this node if needed.
                if (Options.SelectedAlbumIds.Contains(rootAlbum.Id))
                {
                    rootNode.Selected = true;
                }

                Tree.Nodes.Add(rootNode);

                // Add the first level of albums below the root album.
                var childAlbums = rootAlbum.GetChildGalleryObjects(GalleryObjectType.Album, !Options.UserController.IsAuthenticated);

                if (Options.NumberOfLevels == 1)
                {
                    rootNode.HasChildren = childAlbums.Any();
                }
                else
                {
                    BindAlbumToTreeview(childAlbums.ToSortedList(), 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)
                //{
                //  Tree.Nodes.Remove(rootNode);
                //}
            }

            // Make sure all specified albums are visible and checked.
            try
            {
                foreach (var albumId in Options.SelectedAlbumIds.Where(id => id > int.MinValue))
                {
                    var album = Factory.LoadAlbumInstance(new AlbumLoadOptions(albumId));

                    if (Options.UserController.IsUserAuthorized(Options.RequiredSecurityPermissions, await GetRoles(), album.Id, album.GalleryId, album.IsPrivate, SecurityActionsOption.RequireOne, album.IsVirtualAlbum))
                    {
                        BindSpecificAlbumToTreeview(album);
                    }
                }
            }
            catch (InvalidAlbumException ex)
            {
                // One of the albums we want to select doesn't exist. Log the event but otherwise continue on gracefully.
                if (!ex.Data.Contains("Tree_SelectedAlbum_Info"))
                {
                    ex.Data.Add("Tree_SelectedAlbum_Info", $"Album {ex.AlbumId} was one of the SelectedAlbumIds of the TreeViewOptions object. It may have been deleted by another user just before this code ran.");
                }

                AppEventController.LogError(ex);
            }

            return(Tree);
        }