Пример #1
0
        /// <summary>
        /// Configures the <paramref name="user" /> as a site administrator in the gallery. The user must already exist in
        /// Active Directory. A System Administrator role is created if it does not exist.
        /// </summary>
        /// <param name="user">The user to configure as a site administrator in the gallery. The only property that is
        /// references is <see cref="User.UserName" />.</param>
        /// <returns>Returns an <see cref="IUserAccount" /> representing the admin account, or null if <paramref name="user" />
        /// did not specify a username.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="user" /> is null.</exception>
        /// <exception cref="GalleryServerPro.Events.CustomExceptions.InvalidUserException">Thrown when the <paramref name="user" />
        /// does not exist in Active Directory.</exception>
        private static IUserAccount CreateActiveDirectoryAdministrator(User user)
        {
            if (user == null)
                throw new ArgumentNullException();

            if (UserController.MembershipGsp.GetType().ToString() != GlobalConstants.ActiveDirectoryMembershipProviderName)
            {
                throw new InvalidOperationException(String.Format("The function CreateActiveDirectoryAdministrator should be called only when using ActiveDirectoryMembershipProvider. Instead, {0} was detected.", UserController.MembershipGsp.GetType()));
            }

            var sysAdminRole = RoleController.ValidateSysAdminRole();

            IUserAccount userAccount = null;
            if (!String.IsNullOrEmpty(user.UserName))
            {
                userAccount = UserController.GetUser(user.UserName, false);

                if (userAccount == null)
                {
                    throw new InvalidUserException(string.Format("The Active Directory account {0} does not exist. Edit the text file at {1} to specify an existing AD account.", user.UserName, Utils.InstallFilePath));
                }

                if (!RoleController.IsUserInRole(user.UserName, sysAdminRole))
                {
                    RoleController.AddUserToRole(user.UserName, sysAdminRole);
                }
            }

            return userAccount;
        }
Пример #2
0
        /// <summary>
        /// Configures the <paramref name="user" /> as a site administrator in the gallery. The user is created if it doesn't
        /// exist. If the user exists, the user's password is updated with the specified password. A System Administrator role
        /// is created if it does not exist.
        /// </summary>
        /// <param name="user">The user to configure as a site administrator in the gallery. The <see cref="User.UserName" /> 
        /// and <see cref="User.Password" /> properties must both be specified. If both are null or empty, null is returned.</param>
        /// <returns>Returns an <see cref="IUserAccount" /> representing the admin account.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="user" /> is null.</exception>
        /// <exception cref="GalleryServerPro.Events.CustomExceptions.InvalidUserException">Thrown when <paramref name="user" />
        /// does not specify a username and password.</exception>
        private static IUserAccount CreateMembershipAdministrator(User user)
        {
            if (user == null)
                throw new ArgumentNullException();

            if (String.IsNullOrEmpty(user.UserName) && String.IsNullOrEmpty(user.Password))
                return null;

            if (!String.IsNullOrEmpty(user.UserName) && String.IsNullOrEmpty(user.Password))
            {
                throw new InvalidUserException(string.Format("No password was specified. Add a line to the text file at {0} that specifies a password. Example: Password=MyPassword", Utils.InstallFilePath));
            }

            var userAccount = UserController.GetUser(user.UserName, false);

            if (userAccount != null)
            {
                if (!UserController.MembershipGsp.ValidateUser(user.UserName, user.Password))
                {
                    // Password doesn't match. Try to update.
                    if (!UserController.EnablePasswordRetrieval)
                    {
                        throw new Exception(String.Format(CultureInfo.InvariantCulture, "Cannot change password because the membership's password retrieval setting is disabled. The password specified in {0} does not match the existing password for user {1}, so an attempt was made to change it. However, the membership provider does not allow it. Things you can try: (1) Specify a different username in the text file. (2) Enter the correct password for the user in the text file. (3) Edit web.config to allow password retrieval: Set enablePasswordRetrieval=\"true\" in the membership section.", Utils.InstallFilePath, user.UserName));
                    }

                    if (!UserController.ChangePassword(user.UserName, UserController.GetPassword(user.UserName), user.Password))
                    {
                        throw new Exception(String.Format(CultureInfo.InvariantCulture, "Cannot change password. The password specified in {0} does not match the existing password for user {1}, so an attempt was made to change it. However, the membership provider wouldn't allow it and did not specify a reason. Things you can try: (1) Specify a different username in the text file. (2) Enter a different password for the user in the text file, taking care to meet length and complexity requirements.", Utils.InstallFilePath, user.UserName));
                    }
                }

                RoleController.ValidateSysAdminRole();
                if (!RoleController.IsUserInRole(user.UserName, Resources.GalleryServerPro.Site_Sys_Admin_Role_Name))
                {
                    RoleController.AddUserToRole(user.UserName, Resources.GalleryServerPro.Site_Sys_Admin_Role_Name);
                }
            }
            else
            {
                // User account doesn't exist. Create it.
                user.Roles = new[] { RoleController.ValidateSysAdminRole() };

                userAccount = UserController.CreateUser(user);
            }

            return userAccount;
        }
Пример #3
0
        /// <summary>
        /// Gets a <see cref="User" /> instance having the properties specified in <see cref="Utils.InstallFilePath" />.
        /// Supports these properties: UserName, Password, Email. Returns null if none of these exist in the text file.
        /// </summary>
        /// <returns>An instance of <see cref="User" />, or null.</returns>
        public static User GetAdminUserFromInstallTextFile()
        {
            User user = null;

            try
            {
                using (var sr = new StreamReader(Utils.InstallFilePath))
                {
                    var lineText = sr.ReadLine();
                    while (lineText != null)
                    {
                        var kvp = lineText.Split(new[] { '=' });

                        if (kvp.Length == 2)
                        {
                            if (kvp[0] == "UserName")
                            {
                                if (user == null)
                                    user = new User();

                                user.UserName = kvp[1].Trim(); // Found username row
                            }

                            if (kvp[0] == "Password")
                            {
                                if (user == null)
                                    user = new User();

                                user.Password = kvp[1].Trim(); // Found password row
                            }

                            if (kvp[0] == "Email")
                            {
                                if (user == null)
                                    user = new User();

                                user.Email = kvp[1].Trim(); // Found email row
                            }
                        }

                        lineText = sr.ReadLine();
                    }
                }
            }
            catch (FileNotFoundException) { }

            return user;
        }