Beispiel #1
0
        /// <summary>
        /// In certain cases, the web-based installer creates a text file in the App Data directory that is meant as a signal to this
        /// code that additional setup steps are required. If this file is found, carry out the additional actions. This file is
        /// created in the SetFlagForMembershipConfiguration() method of pages\install.ascx.cs.
        /// </summary>
        internal static void ProcessInstallerFile()
        {
            string filePath = Path.Combine(AppSetting.Instance.PhysicalApplicationPath, Path.Combine(GlobalConstants.AppDataDirectory, GlobalConstants.InstallerFileName));

            if (!File.Exists(filePath))
            {
                return;
            }

            string adminUserName;
            string adminPwd;
            string adminEmail;

            using (StreamReader sw = File.OpenText(filePath))
            {
                adminUserName = sw.ReadLine();
                adminPwd      = sw.ReadLine();
                adminEmail    = sw.ReadLine();
            }

            HelperFunctions.BeginTransaction();

            #region Create the Sys Admin role.

            // Create the Sys Admin role. If it already exists, make sure it has AllowAdministerSite permission.
            string sysAdminRoleName = Resources.GalleryServerPro.Installer_Sys_Admin_Role_Name;
            if (!RoleController.RoleExists(sysAdminRoleName))
            {
                RoleController.CreateRole(sysAdminRoleName);
            }

            IGalleryServerRole role = Factory.LoadGalleryServerRole(sysAdminRoleName);
            if (role == null)
            {
                role = Factory.CreateGalleryServerRoleInstance(sysAdminRoleName, true, true, true, true, true, true, true, true, true, true, false);
                role.RootAlbumIds.Add(Factory.LoadRootAlbumInstance().Id);
                role.Save();
            }
            else
            {
                // Role already exists. Make sure it has Sys Admin permission.
                if (!role.AllowAdministerSite)
                {
                    role.AllowAdministerSite = true;
                    role.Save();
                }
            }

            #endregion

            #region Create the Sys Admin user account.

            // Create the Sys Admin user account. Will throw an exception if the name is already in use.
            try
            {
                CreateUser(adminUserName, adminPwd, adminEmail);
            }
            catch (MembershipCreateUserException ex)
            {
                if (ex.StatusCode == MembershipCreateStatus.DuplicateUserName)
                {
                    // The user already exists. Update the password and email address to our values.
                    UserEntity user = GetUser(adminUserName, true);
                    ChangePassword(user.UserName, GetPassword(user.UserName), adminPwd);
                    user.Email = adminEmail;
                    UpdateUser(user);
                }
            }

            // Add the Sys Admin user to the Sys Admin role.
            if (!RoleController.IsUserInRole(adminUserName, sysAdminRoleName))
            {
                RoleController.AddUserToRole(adminUserName, sysAdminRoleName);
            }

            #endregion

            #region Create sample album and image

            if (!Config.GetCore().MediaObjectPathIsReadOnly)
            {
                DateTime currentTimestamp = DateTime.Now;
                IAlbum   sampleAlbum      = null;
                foreach (IAlbum album in Factory.LoadRootAlbumInstance().GetChildGalleryObjects(GalleryObjectType.Album))
                {
                    if (album.DirectoryName == "Samples")
                    {
                        sampleAlbum = album;
                        break;
                    }
                }
                if (sampleAlbum == null)
                {
                    // Create sample album.
                    sampleAlbum                        = Factory.CreateAlbumInstance();
                    sampleAlbum.Parent                 = Factory.LoadRootAlbumInstance();
                    sampleAlbum.Title                  = "Samples";
                    sampleAlbum.DirectoryName          = "Samples";
                    sampleAlbum.Summary                = "Welcome to Gallery Server Pro!";
                    sampleAlbum.CreatedByUserName      = adminUserName;
                    sampleAlbum.DateAdded              = currentTimestamp;
                    sampleAlbum.LastModifiedByUserName = adminUserName;
                    sampleAlbum.DateLastModified       = currentTimestamp;
                    sampleAlbum.Save();
                }

                // Look for sample image in sample album.
                IGalleryObject sampleImage = null;
                foreach (IGalleryObject image in sampleAlbum.GetChildGalleryObjects(GalleryObjectType.Image))
                {
                    if (image.Original.FileName == Constants.SAMPLE_IMAGE_FILENAME)
                    {
                        sampleImage = image;
                        break;
                    }
                }

                if (sampleImage == null)
                {
                    // Sample image not found. Pull image from assembly, save to disk, and create a media object from it.
                    string sampleDirPath       = Path.Combine(AppSetting.Instance.MediaObjectPhysicalPath, sampleAlbum.DirectoryName);
                    string sampleImageFilename = HelperFunctions.ValidateFileName(sampleDirPath, Constants.SAMPLE_IMAGE_FILENAME);
                    string sampleImageFilepath = Path.Combine(sampleDirPath, sampleImageFilename);

                    System.Reflection.Assembly asm = Assembly.GetExecutingAssembly();
                    using (Stream stream = asm.GetManifestResourceStream(String.Concat("GalleryServerPro.Web.gs.images.", Constants.SAMPLE_IMAGE_FILENAME)))
                    {
                        if (stream != null)
                        {
                            BinaryWriter bw     = new BinaryWriter(File.Create(sampleImageFilepath));
                            byte[]       buffer = new byte[stream.Length];
                            stream.Read(buffer, 0, (int)stream.Length);
                            bw.Write(buffer);
                            bw.Flush();
                            bw.Close();
                        }
                    }

                    if (File.Exists(sampleImageFilepath))
                    {
                        IGalleryObject image = Factory.CreateImageInstance(new FileInfo(sampleImageFilepath), sampleAlbum);
                        image.Title                  = "Margaret, Skyler and Roger Martin (December 2008)";
                        image.CreatedByUserName      = adminUserName;
                        image.DateAdded              = currentTimestamp;
                        image.LastModifiedByUserName = adminUserName;
                        image.DateLastModified       = currentTimestamp;
                        image.Save();
                    }
                }
            }

            #endregion

            HelperFunctions.CommitTransaction();
            HelperFunctions.PurgeCache();

            File.Delete(filePath);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new account in the membership system with the specified <paramref name="userName"/>, <paramref name="password"/>,
        /// <paramref name="email"/>, and belonging to the specified <paramref name="roles"/>. If required, it sends a verification
        /// e-mail to the user, sends an e-mail notification to admins, and creates a user album. The account will be disabled when
        /// <paramref name="isSelfRegistration"/> is <c>true</c> and either the system option RequireEmailValidationForSelfRegisteredUser
        /// or RequireApprovalForSelfRegisteredUser is enabled.
        /// </summary>
        /// <param name="userName">Account name of the user. Cannot be null or empty.</param>
        /// <param name="password">The password for the user. Cannot be null or empty.</param>
        /// <param name="email">The email associated with the user. Required when <paramref name="isSelfRegistration"/> is true
        /// and email verification is enabled.</param>
        /// <param name="roles">The names of the roles to assign to the user. The roles must already exist. If null or empty, no
        /// roles are assigned to the user.</param>
        /// <param name="isSelfRegistration">Indicates when the user is creating his or her own account. Set to false when an
        /// administrator creates an account.</param>
        /// <returns>Returns the newly created user.</returns>
        /// <exception cref="MembershipCreateUserException">Thrown when an error occurs during account creation. Check the StatusCode
        /// property for a MembershipCreateStatus value.</exception>
        public static UserEntity CreateUser(string userName, string password, string email, string[] roles, bool isSelfRegistration)
        {
            #region Validation

            if (String.IsNullOrEmpty(userName))
            {
                throw new ArgumentException("userName");
            }

            if (String.IsNullOrEmpty(password))
            {
                throw new ArgumentException("password");
            }

            if ((String.IsNullOrEmpty(email)) && (HelperFunctions.IsValidEmail(userName)))
            {
                // No email address was specified, but the user name happens to be in the form of an email address,
                // so let's set the email property to the user name.
                email = userName;
            }

            #endregion

            // Step 1: Create the user. Any number of exceptions may occur; we'll let the caller deal with them.
            UserEntity user = CreateUser(userName, password, email);

            // Step 2: If this is a self-registered account and email verification is enabled or admin approval is required,
            // disable it. It will be approved when the user validates the email or the admin gives approval.
            if (isSelfRegistration)
            {
                if (Config.GetCore().RequireEmailValidationForSelfRegisteredUser || Config.GetCore().RequireApprovalForSelfRegisteredUser)
                {
                    user.IsApproved = false;
                    UpdateUser(user);
                }
            }

            // Step 3: Add user to roles.
            if ((roles != null) && (roles.Length > 0))
            {
                foreach (string role in roles)
                {
                    RoleController.AddUserToRole(userName, role);
                }
            }

            // Step 4: Notify admins that an account was created.
            NotifyAdminsOfNewlyCreatedAccount(user, isSelfRegistration, false);

            // Step 5: Send user a welcome message or a verification link.
            if (HelperFunctions.IsValidEmail(user.Email))
            {
                NotifyUserOfNewlyCreatedAccount(user);
            }
            else if (isSelfRegistration && Config.GetCore().RequireEmailValidationForSelfRegisteredUser)
            {
                // Invalid email, but we need one to send the email verification. Throw error.
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidEmail);
            }

            HelperFunctions.PurgeCache();

            return(user);
        }