コード例 #1
0
        /// <summary>
        /// Loads a proper local instance of a user account.
        /// </summary>
        /// <param name="user">The user account.</param>
        /// <returns>The local instance, or <c>null</c>.</returns>
        private LocalUserInfo LoadLocalInstance(UserInfo user)
        {
            UserInfo[]       users = GetUsers();
            UsernameComparer comp  = new UsernameComparer();

            for (int i = 0; i < users.Length; i++)
            {
                if (comp.Compare(users[i], user) == 0)
                {
                    return(users[i] as LocalUserInfo);
                }
            }
            return(null);
        }
コード例 #2
0
        /// <summary>
        /// Searches for a User.
        /// </summary>
        /// <param name="user">The User to search for.</param>
        /// <returns>True if the User already exists.</returns>
        private bool UserExists(UserInfo user)
        {
            UserInfo[]       users = GetUsers();
            UsernameComparer comp  = new UsernameComparer();

            for (int i = 0; i < users.Length; i++)
            {
                if (comp.Compare(users[i], user) == 0)
                {
                    return(true);
                }
            }
            return(false);
        }
コード例 #3
0
        /// <summary>
        /// Removes a User.
        /// </summary>
        /// <param name="user">The User to remove.</param>
        /// <returns>True if the User has been removed successfully.</returns>
        /// <exception cref="ArgumentNullException">If <b>user</b> is <c>null</c>.</exception>
        public bool RemoveUser(UserInfo user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            lock (this)
            {
                UserInfo[]       users = GetUsers( );
                UsernameComparer comp  = new UsernameComparer( );
                int idx = -1;
                for (int i = 0; i < users.Length; i++)
                {
                    if (comp.Compare(users[i], user) == 0)
                    {
                        idx = i;
                        break;
                    }
                }
                if (idx < 0)
                {
                    return(false);
                }

                // Remove user's data
                string        lowercaseUsername = user.Username.ToLowerInvariant( );
                string[]      lines             = File.ReadAllLines(GetFullPath(UsersDataFile));
                List <string> newLines          = new List <string>(lines.Length);
                string[]      fields;
                for (int i = 0; i < lines.Length; i++)
                {
                    fields = lines[i].Split('|');
                    if (fields[0].ToLowerInvariant( ) != lowercaseUsername)
                    {
                        newLines.Add(lines[i]);
                    }
                }
                File.WriteAllLines(GetFullPath(UsersDataFile), newLines.ToArray( ));

                // Remove user
                List <UserInfo> tmp = new List <UserInfo>(users);
                tmp.Remove(tmp[idx]);
                DumpUsers(tmp.ToArray( ));

                _usersCache = null;
            }
            return(true);
        }
コード例 #4
0
        /// <summary>
        /// Modifies a User.
        /// </summary>
        /// <param name="user">The Username of the user to modify.</param>
        /// <param name="newDisplayName">The new display name (can be <c>null</c>).</param>
        /// <param name="newPassword">The new Password (<c>null</c> or blank to keep the current password).</param>
        /// <param name="newEmail">The new Email address.</param>
        /// <param name="newActive">A value indicating whether the account is active.</param>
        /// <returns>The correct <see cref="T:UserInfo"/> object or <c>null</c>.</returns>
        /// <exception cref="ArgumentNullException">If <b>user</b> or <b>newEmail</b> are <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If <b>newEmail</b> is empty.</exception>
        public UserInfo ModifyUser(UserInfo user, string newDisplayName, string newPassword, string newEmail, bool newActive)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            if (newEmail == null)
            {
                throw new ArgumentNullException("newEmail");
            }

            if (newEmail.Length == 0)
            {
                throw new ArgumentException("New Email cannot be empty", "newEmail");
            }

            lock (this) {
                LocalUserInfo local = LoadLocalInstance(user);
                if (local == null)
                {
                    return(null);
                }

                UserInfo[]       allUsers = GetUsers();
                UsernameComparer comp     = new UsernameComparer();

                usersCache = null;

                for (int i = 0; i < allUsers.Length; i++)
                {
                    if (comp.Compare(allUsers[i], user) == 0)
                    {
                        LocalUserInfo result = new LocalUserInfo(user.Username, newDisplayName, newEmail,
                                                                 newActive, user.DateTime, this,
                                                                 string.IsNullOrEmpty(newPassword) ? local.PasswordHash : Hash.Compute(newPassword));
                        result.Groups = allUsers[i].Groups;
                        allUsers[i]   = result;
                        DumpUsers(allUsers);
                        return(result);
                    }
                }
            }

            return(null);
        }
コード例 #5
0
        /// <summary>
        /// Removes a User.
        /// </summary>
        /// <param name="user">The User to remove.</param>
        /// <returns>True if the User has been removed successfully.</returns>
        /// <exception cref="ArgumentNullException">If <b>user</b> is <c>null</c>.</exception>
        public bool RemoveUser(UserInfo user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            lock (this)
            {
                var users = GetUsers();

                var comp = new UsernameComparer();
                if (!users.Any(u => comp.Compare(u, user) == 0))
                {
                    return(false);
                }

                // Remove user's data
                var      lowercaseUsername = user.Username.ToLowerInvariant();
                var      lines             = File.ReadAllLines(GetFullPath(UsersDataFile));
                var      newLines          = new List <string>(lines.Length);
                string[] fields;
                for (var i = 0; i < lines.Length; i++)
                {
                    fields = lines[i].Split('|');
                    if (fields[0].ToLowerInvariant() != lowercaseUsername)
                    {
                        newLines.Add(lines[i]);
                    }
                }
                File.WriteAllLines(GetFullPath(UsersDataFile), newLines.ToArray());

                // Remove user
                var tmp = new List <UserInfo>(users);
                DumpUsers(users.Where(u => comp.Compare(u, user) != 0));

                usersCache = null;
            }
            return(true);
        }
コード例 #6
0
 /// <summary>
 /// Searches for a User.
 /// </summary>
 /// <param name="user">The User to search for.</param>
 /// <returns>True if the User already exists.</returns>
 private bool UserExists(UserInfo user)
 {
     UserInfo[] users = GetUsers();
     UsernameComparer comp = new UsernameComparer();
     for(int i = 0; i < users.Length; i++) {
         if(comp.Compare(users[i], user) == 0) return true;
     }
     return false;
 }
コード例 #7
0
 /// <summary>
 /// Loads a proper local instance of a user account.
 /// </summary>
 /// <param name="user">The user account.</param>
 /// <returns>The local instance, or <c>null</c>.</returns>
 private LocalUserInfo LoadLocalInstance(UserInfo user)
 {
     UserInfo[] users = GetUsers();
     UsernameComparer comp = new UsernameComparer();
     for(int i = 0; i < users.Length; i++) {
         if(comp.Compare(users[i], user) == 0) return users[i] as LocalUserInfo;
     }
     return null;
 }
コード例 #8
0
        /// <summary>
        /// Removes a User.
        /// </summary>
        /// <param name="user">The User to remove.</param>
        /// <returns>True if the User has been removed successfully.</returns>
        /// <exception cref="ArgumentNullException">If <b>user</b> is <c>null</c>.</exception>
        public bool RemoveUser(UserInfo user)
        {
            if(user == null) throw new ArgumentNullException("user");

            lock(this) {
                UserInfo[] users = GetUsers();
                UsernameComparer comp = new UsernameComparer();
                int idx = -1;
                for(int i = 0; i < users.Length; i++) {
                    if(comp.Compare(users[i], user) == 0) {
                        idx = i;
                        break;
                    }
                }
                if(idx < 0) return false;

                // Remove user's data
                string lowercaseUsername = user.Username.ToLowerInvariant();
                string[] lines = File.ReadAllLines(GetFullPath(UsersDataFile));
                List<string> newLines = new List<string>(lines.Length);
                string[] fields;
                for(int i = 0; i < lines.Length; i++) {
                    fields = lines[i].Split('|');
                    if(fields[0].ToLowerInvariant() != lowercaseUsername) {
                        newLines.Add(lines[i]);
                    }
                }
                File.WriteAllLines(GetFullPath(UsersDataFile), newLines.ToArray());

                // Remove user
                List<UserInfo> tmp = new List<UserInfo>(users);
                tmp.Remove(tmp[idx]);
                DumpUsers(tmp.ToArray());

                usersCache = null;
            }
            return true;
        }
コード例 #9
0
        /// <summary>
        /// Modifies a User.
        /// </summary>
        /// <param name="user">The Username of the user to modify.</param>
        /// <param name="newDisplayName">The new display name (can be <c>null</c>).</param>
        /// <param name="newPassword">The new Password (<c>null</c> or blank to keep the current password).</param>
        /// <param name="newEmail">The new Email address.</param>
        /// <param name="newActive">A value indicating whether the account is active.</param>
        /// <returns>The correct <see cref="T:UserInfo"/> object or <c>null</c>.</returns>
        /// <exception cref="ArgumentNullException">If <b>user</b> or <b>newEmail</b> are <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If <b>newEmail</b> is empty.</exception>
        public UserInfo ModifyUser(UserInfo user, string newDisplayName, string newPassword, string newEmail, bool newActive)
        {
            if(user == null) throw new ArgumentNullException("user");
            if(newEmail == null) throw new ArgumentNullException("newEmail");
            if(newEmail.Length == 0) throw new ArgumentException("New Email cannot be empty", "newEmail");

            lock(this) {
                LocalUserInfo local = LoadLocalInstance(user);
                if(local == null) return null;

                UserInfo[] allUsers = GetUsers();
                UsernameComparer comp = new UsernameComparer();

                usersCache = null;

                for(int i = 0; i < allUsers.Length; i++) {
                    if(comp.Compare(allUsers[i], user) == 0) {
                        LocalUserInfo result = new LocalUserInfo(user.Username, newDisplayName, newEmail,
                            newActive, user.DateTime, this,
                            string.IsNullOrEmpty(newPassword) ? local.PasswordHash : Hash.Compute(newPassword));
                        result.Groups = allUsers[i].Groups;
                        allUsers[i] = result;
                        DumpUsers(allUsers);
                        return result;
                    }
                }
            }

            return null;
        }
コード例 #10
0
ファイル: Users.cs プロジェクト: robhughadams/screwturnwiki
        /// <summary>
        /// Merges two arrays of users, removing duplicates.
        /// </summary>
        /// <param name="array1">The first array.</param>
        /// <param name="array2">The second array.</param>
        /// <returns>The merged users.</returns>
        private static UserInfo[] MergeArrays(UserInfo[] array1, UserInfo[] array2)
        {
            List<UserInfo> result = new List<UserInfo>(array1.Length + array2.Length);
            result.AddRange(array1);

            UsernameComparer comp = new UsernameComparer();
            foreach(UserInfo user in array2) {
                bool found = false;
                foreach(UserInfo present in result) {
                    if(comp.Compare(present, user) == 0) {
                        found = true;
                        break;
                    }
                }

                if(!found) {
                    result.Add(user);
                }
            }

            return result.ToArray();
        }