public UserStorageView[] GetUsers()
        {
            UserStorageView[] allUsers;
            IObjectContainer  container = GetStorage();

            try
            {
                IList <UserStorageView> users =
                    container.Query <UserStorageView>(delegate(UserStorageView userFinder)
                {
                    return(true);
                });


                if (users.Count > 0)
                {
                    allUsers = new UserStorageView[users.Count];
                    users.CopyTo(allUsers, 0);
                }
                else
                {
                    allUsers = new UserStorageView[0];
                }
            }
            finally
            {
                container.Close();
            }
            return(allUsers);
        }
        public UserStorageView GetUser(string username)
        {
            UserStorageView user = null;

            IObjectContainer container = GetStorage();

            try
            {
                IList <UserStorageView> users =
                    container.Query <UserStorageView>(delegate(UserStorageView userFinder)
                {
                    return(userFinder.Username == username);
                });

                if (users.Count > 0)
                {
                    user = users[0];
                }
            }
            finally
            {
                container.Close();
            }
            return(user);
        }
        public UserStorageView[] GetGroupUsers(int groupId)
        {
            UserStorageView[] groupUsers;
            IObjectContainer  container = GetStorage();

            try
            {
                IList <UserStorageView> users =
                    container.Query <UserStorageView>(delegate(UserStorageView userFinder)
                {
                    return(userFinder.GroupId == groupId);
                });


                if (users.Count > 0)
                {
                    groupUsers = new UserStorageView[users.Count];
                    users.CopyTo(groupUsers, 0);
                }
                else
                {
                    groupUsers = new UserStorageView[0];
                }
            }
            finally
            {
                container.Close();
            }
            return(groupUsers);
        }
        public void UpdateUsers(UserStorageView[] updates)
        {
            if (updates == null)
            {
                return;
            }

            IObjectContainer container = GetStorage();

            try
            {
                foreach (UserStorageView userInUpdates in updates)
                {
                    UserStorageView         user  = null;
                    IList <UserStorageView> users = container.Query <UserStorageView>(delegate(UserStorageView userFinder)
                    {
                        return(userFinder.Username == userInUpdates.Username);
                    });
                    if (users.Count > 0)
                    {
                        user          = users[0];
                        user.Password = userInUpdates.Password;
                        user.GroupId  = userInUpdates.GroupId;
                        container.Set(user);
                    }
                }
            }
            finally
            {
                container.Close();
            }
        }
Exemple #5
0
        private void SetupApplicationsGroupsAndUsers(Permission permission)
        {
            // add permissions
            int groupId = 12;

            GroupStorageView[] groups = new GroupStorageView[1];

            groups[0] = new GroupStorageView(groupId, "test1");

            UserStorageView[] users = new UserStorageView[1];

            users[0] = new UserStorageView("username1", "password1", groupId);

            _managerStorage.AddGroups(groups);

            _managerStorage.AddUsers(users);

            _managerStorage.AddGroupPermission(groupId, permission);

            SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashType.MD5));

            // add applications, only one assigned to this user

            _managerStorage.AddApplication(new ApplicationStorageView("username1"));
            _managerStorage.AddApplication(new ApplicationStorageView("username2"));
            _managerStorage.AddApplication(new ApplicationStorageView("username3"));
        }
Exemple #6
0
        public void EnsurePermissionTestSimpleScenario()
        {
            int groupId = 12;

            GroupStorageView[] groups = new GroupStorageView[1];

            groups[0] = new GroupStorageView(groupId, "test1");

            UserStorageView[] users = new UserStorageView[1];

            users[0] = new UserStorageView("username1", "password1", groupId);

            _managerStorage.AddGroups(groups);

            _managerStorage.AddUsers(users);

            _managerStorage.AddGroupPermission(groupId, Permission.ExecuteThread);

            SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashType.MD5));

            EnsurePermission(sc, Permission.ExecuteThread);

            // the above throws an exception if something is wrong so we are doing OK if we get this far
            Assert.IsTrue(true);
        }
Exemple #7
0
        private void btnAdd_Click(object sender, System.EventArgs e)
        {
            try
            {
                SearchForm srch = new SearchForm(this.console, SearchForm.SearchMode.Group);
                srch.ShowDialog(this);

                //get list of groups from the search form.
                ListView.SelectedListViewItemCollection items = srch.lvMembers.SelectedItems;

                //first update the database, then get it from there.
                //for now only one item can be included
                if (items != null && items.Count > 0)
                {
                    _User.GroupId = ((GroupItem)items[0]).GroupView.GroupId;

                    UserStorageView[] users = new UserStorageView[1];
                    users[0] = this._User;

                    console.Manager.Admon_UpdateUsers(console.Credentials, users);
                }

                GetGroupMembershipData();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error changing membership:" + ex.Message, "User Properties", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void DeleteUser(UserStorageView userToDelete)
        {
            if (userToDelete == null)
            {
                return;
            }

            IObjectContainer container = GetStorage();

            try
            {
                IList <UserStorageView> users =
                    container.Query <UserStorageView>(delegate(UserStorageView userFinder)
                {
                    return(userFinder.Username == userToDelete.Username);
                });

                if (users.Count > 0)
                {
                    container.Delete(users[0]);
                }
            }
            finally
            {
                container.Close();
            }
        }
Exemple #9
0
        public void PasswordMd5HashTestHasComputing()
        {
            const string password = "******";

            UserStorageView user = new UserStorageView("test", password, 1);

            Assert.AreEqual(UserMD5Hash, user.PasswordMd5Hash);
        }
        /// <summary>
        /// Create the default objects to complete initializing the manager storage.
        /// </summary>
        /// <param name="managerStorage"></param>
        protected void CreateDefaultObjects(IManagerStorage managerStorage)
        {
            // create default groups
            ArrayList        defaultGroups = new ArrayList();
            GroupStorageView newGroup;

            newGroup             = new GroupStorageView(c_AdminsGroupId, "Administrators");
            newGroup.Description = "Administrators Group";
            newGroup.IsSystem    = true;
            defaultGroups.Add(newGroup);

            newGroup             = new GroupStorageView(c_ExecutorsGroupId, "Executors");
            newGroup.Description = "Executors Group";
            newGroup.IsSystem    = true;
            defaultGroups.Add(newGroup);

            newGroup             = new GroupStorageView(c_UsersGroupId, "Users");
            newGroup.Description = "Users Group";
            newGroup.IsSystem    = true;
            defaultGroups.Add(newGroup);

            managerStorage.AddGroups((GroupStorageView[])defaultGroups.ToArray(typeof(GroupStorageView)));

            // set default permissions

            //permissions for admins group
            managerStorage.AddGroupPermission(c_AdminsGroupId, Permission.ExecuteThread);
            managerStorage.AddGroupPermission(c_AdminsGroupId, Permission.ManageOwnApp);
            managerStorage.AddGroupPermission(c_AdminsGroupId, Permission.ManageAllApps);
            managerStorage.AddGroupPermission(c_AdminsGroupId, Permission.ManageUsers);

            //permissions for executors group
            managerStorage.AddGroupPermission(c_ExecutorsGroupId, Permission.ExecuteThread);

            //permissions for users group
            managerStorage.AddGroupPermission(c_UsersGroupId, Permission.ManageOwnApp);

            // create default users
            ArrayList       defaultUsers = new ArrayList();
            UserStorageView newUser;

            newUser          = new UserStorageView("admin", "admin", c_AdminsGroupId);
            newUser.IsSystem = true;
            defaultUsers.Add(newUser);

            newUser          = new UserStorageView("executor", "executor", c_ExecutorsGroupId);
            newUser.IsSystem = true;
            defaultUsers.Add(newUser);

            newUser          = new UserStorageView("user", "user", c_UsersGroupId);
            newUser.IsSystem = true;
            defaultUsers.Add(newUser);

            managerStorage.AddUsers((UserStorageView[])defaultUsers.ToArray(typeof(UserStorageView)));
        }
Exemple #11
0
        public void PasswordMd5HashTestSettingHashDirectly()
        {
            UserStorageView user = new UserStorageView("test");

            user.PasswordMd5Hash = UserMD5Hash;

            // confirm that the hash is properly set even if the password was not set
            Assert.AreEqual(UserMD5Hash, user.PasswordMd5Hash);

            // the clear-text password should be removed if a hash is set directly
            Assert.IsNull(user.Password);
        }
Exemple #12
0
        public void PasswordMd5HashTestHashRecomputedAfterPasswordChanged()
        {
            UserStorageView user = new UserStorageView("test");

            user.PasswordMd5Hash = UserMD5Hash;

            // confirm that the hash is properly initiated
            Assert.AreEqual(UserMD5Hash, user.PasswordMd5Hash);

            user.Password = "******";

            // confirm that the hash is computed again if the password was changed
            Assert.AreEqual(AdminMD5Hash, user.PasswordMd5Hash);
        }
Exemple #13
0
        public void SetData(UserStorageView User)
        {
            this._User       = User;
            this.Text        = User.Username + " Properties";
            this.lbName.Text = User.Username;

            GetGroupMembershipData();

            if (User.IsSystem)
            {
                //we cant change group membership
                btnAdd.Enabled    = false;
                btnRemove.Enabled = false;
            }
        }
        public bool CheckPermission(SecurityCredentials sc, Permission perm)
        {
            if (sc == null)
            {
                return(false);
            }
            UserStorageView  user      = null;
            IObjectContainer container = GetStorage();
            int groupId = -1;

            try
            {
                IList <UserStorageView> users = container.Query <UserStorageView>(delegate(UserStorageView userFinder)
                {
                    return(String.Compare(userFinder.Username, sc.Username, true) == 0 && userFinder.PasswordMd5Hash == sc.Password);
                });
                if (users.Count > 0)
                {
                    user = users[0];
                }

                if (user == null)
                {
                    return(false);
                }
                else
                {
                    groupId = user.GroupId;
                }
            }
            finally
            {
                container.Close();
            }

            foreach (Permission permission in GetGroupPermissions(groupId))
            {
                // in the SQL implementation the higher leverl permissions are considered to
                // include the lower leverl permissions
                if ((int)permission >= (int)perm)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #15
0
        public void AuthenticateUserTestSimpleScenario()
        {
            int groupId = 12;

            UserStorageView[] users = new UserStorageView[1];

            users[0] = new UserStorageView("username1", "password1", groupId);

            _managerStorage.AddUsers(users);

            SecurityCredentials sc = new SecurityCredentials("username1", HashUtil.GetHash("password1", HashType.MD5));

            AuthenticateUser(sc);

            // the above throws an exception if something is wrong so we are doing OK if we get this far
            Assert.IsTrue(true);
        }
Exemple #16
0
        private UserStorageView[] GetUsers()
        {
            UserStorageView[] users    = new UserStorageView[1];
            string            username = Utils.MakeSqlSafe(txUsername.Text);
            string            password = Utils.MakeSqlSafe(txPwd.Text);
            int groupId = -1;

            foreach (GroupStorageView group in allGroups)
            {
                if (group.GroupName == cboGroup.SelectedItem.ToString())
                {
                    groupId = group.GroupId;
                    break;
                }
            }
            users[0] = new UserStorageView(username, password, groupId);

            return(users);
        }
        public bool AuthenticateUser(SecurityCredentials sc)
        {
            if (sc == null || m_users == null)
            {
                return(false);
            }

            for (int index = 0; index < m_users.Count; index++)
            {
                UserStorageView user = (UserStorageView)m_users[index];

                if (user.Username == sc.Username && user.PasswordMd5Hash == sc.Password)
                {
                    return(true);
                }
            }

            return(false);
        }
        public void UpdateUsers(UserStorageView[] updates)
        {
            if (m_users == null || updates == null)
            {
                return;
            }

            for (int indexInList = 0; indexInList < m_users.Count; indexInList++)
            {
                UserStorageView userInList = (UserStorageView)m_users[indexInList];

                foreach (UserStorageView userInUpdates in updates)
                {
                    if (userInList.Username == userInUpdates.Username)
                    {
                        userInList.Password = userInUpdates.Password;
                        userInList.GroupId  = userInUpdates.GroupId;
                    }
                }
            }
        }
        public void DeleteUser(UserStorageView userToDelete)
        {
            if (m_users == null || userToDelete == null)
            {
                return;
            }

            ArrayList remainingUsers = new ArrayList();

            for (int indexInList = 0; indexInList < m_users.Count; indexInList++)
            {
                UserStorageView userInList = (UserStorageView)m_users[indexInList];

                if (userInList.Username != userToDelete.Username)
                {
                    remainingUsers.Add(userInList);
                }
            }

            m_users = remainingUsers;
        }
Exemple #20
0
        private void btnChgPwd_Click(object sender, EventArgs e)
        {
            bool changed = false;

            try
            {
                PasswordForm pwdform = new PasswordForm();
                pwdform.ShowDialog(this);
                //try to change the password for this user.
                if (pwdform.Password != null)
                {
                    UserStorageView[] users = new UserStorageView[1];
                    users[0]       = _User;
                    _User.Password = pwdform.Password;
                    console.Manager.Admon_UpdateUsers(console.Credentials, users);

                    changed = true;

                    //update the console credentials if needed
                    if (console.Credentials.Username == _User.Username)
                    {
                        console.Credentials.Password = pwdform.Password;
                    }
                }
            }
            catch (Exception ex)
            {
                changed = false;
                MessageBox.Show("Error changing password:"******"Change Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (changed)
                {
                    MessageBox.Show("Password changed successfully.", "Change Password", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
Exemple #21
0
        private void UpdateUser()
        {
            int groupId = -1;

            try
            {
                //get the groupId from the listview.
                if (lvGrp.Items != null && lvGrp.Items.Count > 0)
                {
                    if (lvGrp.Items[0] is GroupItem)
                    {
                        GroupItem grpItem = (GroupItem)lvGrp.Items[0];
                        groupId = grpItem.GroupView.GroupId;                         //set the group Id. For now, a user can be part of one group only.
                    }
                }

                if ((groupId != _User.GroupId) && (groupId != -1))
                {
                    UserStorageView[] users = new UserStorageView[1];
                    users[0] = _User;
                    console.Manager.Admon_UpdateUsers(console.Credentials, users);
                }
                else
                {
                    if (groupId == -1)
                    {
                        //dont update the user.
                        MessageBox.Show("Cannot update user: The User is not assigned to any group!", "Edit User", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error updating user:"******"Update User", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// THIS FUNCTIONALITY IS NOT FULLY IMPLEMENTED AND IT MIGHT BE DISCARDED ALTOGETHER
        ///
        /// Loading from an XML file is the perfect tool for complex storage setups which would be useful for more in-depth unit testing
        /// Saving to an XML file could be used to dump the current storage state for troubleshooting, for example to receive faulty storages from the field.


        /// <summary>
        /// Save the current storage state into an XML file.
        /// It is important that the file format is easily editable by humans so test cases can easily be maintained.
        /// For this reason we do not use the build-in persistence modules.
        /// </summary>
        /// <param name="filename"></param>
        public void SaveToHumanReadableXmlFile(String filename)
        {
            const String storageDocumentTemplate = "<storage><users/><groups/><group_permissions/><executors/><applications/><threads/></storage>";
            XmlDocument  storageDocument         = new XmlDocument();

            storageDocument.LoadXml(storageDocumentTemplate);

            XmlNode usersNode  = storageDocument.SelectSingleNode("/storage/users");
            XmlNode groupsNode = storageDocument.SelectSingleNode("/storage/groups");
            //XmlNode groupPermissionsNode = storageDocument.SelectSingleNode("/storage/group_permissions");
            XmlNode executorsNode    = storageDocument.SelectSingleNode("/storage/executors");
            XmlNode applicationsNode = storageDocument.SelectSingleNode("/storage/applications");
            XmlNode threadsNode      = storageDocument.SelectSingleNode("/storage/threads");

            if (m_users != null)
            {
                IEnumerator usersEnumerator = m_users.GetEnumerator();

                while (usersEnumerator.MoveNext())
                {
                    UserStorageView user = usersEnumerator.Current as UserStorageView;

                    XmlElement userElement = storageDocument.CreateElement("user");

                    userElement.SetAttribute("username", user.Username);
                    userElement.SetAttribute("password", user.Password);
                    userElement.SetAttribute("groupid", user.GroupId.ToString());

                    usersNode.AppendChild(userElement);
                }
            }

            if (m_groups != null)
            {
                IEnumerator groupsEnumerator = m_groups.GetEnumerator();

                while (groupsEnumerator.MoveNext())
                {
                    GroupStorageView group = groupsEnumerator.Current as GroupStorageView;

                    XmlElement groupElement = storageDocument.CreateElement("group");

                    groupElement.SetAttribute("groupname", group.GroupName);
                    groupElement.SetAttribute("groupid", group.GroupId.ToString());

                    groupsNode.AppendChild(groupElement);
                }
            }

            //		private Hashtable m_groupPermissions;
//			if (m_groupPermissions != null)
//			{
//				IEnumerator groupPermissionsEnumerator = m_groupPermissions.GetEnumerator();
//
//				while(groupPermissionsEnumerator.MoveNext())
//				{
//					GroupPermissionStorageView group = groupPermissionsEnumerator.Current as GroupStorageView;
//
//					XmlElement groupElement = storageDocument.CreateElement("group");
//
//					groupElement.SetAttribute("groupname", group.GroupName);
//					groupElement.SetAttribute("groupid", group.GroupId.ToString());
//
//					groupsNode.AppendChild(groupElement);
//				}
//			}


            if (m_executors != null)
            {
                IEnumerator executorsEnumerator = m_executors.GetEnumerator();

                while (executorsEnumerator.MoveNext())
                {
                    ExecutorStorageView executor = executorsEnumerator.Current as ExecutorStorageView;

                    XmlElement executorElement = storageDocument.CreateElement("executor");

                    executorElement.SetAttribute("executorid", executor.ExecutorId);
                    executorElement.SetAttribute("dedicated", executor.Dedicated.ToString());
                    executorElement.SetAttribute("connected", executor.Connected.ToString());
                    executorElement.SetAttribute("pingtime", executor.PingTime.ToString());
                    executorElement.SetAttribute("hostname", executor.HostName);
                    executorElement.SetAttribute("port", executor.Port.ToString());
                    executorElement.SetAttribute("username", executor.Username);
                    executorElement.SetAttribute("maxcpu", executor.MaxCpu.ToString());
                    executorElement.SetAttribute("cpuusage", executor.CpuUsage.ToString());
                    executorElement.SetAttribute("availablecpu", executor.AvailableCpu.ToString());
                    executorElement.SetAttribute("totalcpuusage", executor.TotalCpuUsage.ToString());
                    executorElement.SetAttribute("maxmemory", executor.MaxMemory.ToString());
                    executorElement.SetAttribute("maxdisk", executor.MaxDisk.ToString());
                    executorElement.SetAttribute("numberofcpu", executor.NumberOfCpu.ToString());
                    executorElement.SetAttribute("os", executor.Os);
                    executorElement.SetAttribute("architecture", executor.Architecture);

                    executorsNode.AppendChild(executorElement);
                }
            }

            if (m_applications != null)
            {
                IEnumerator applicationsEnumerator = m_applications.GetEnumerator();

                while (applicationsEnumerator.MoveNext())
                {
                    ApplicationStorageView application = applicationsEnumerator.Current as ApplicationStorageView;

                    XmlElement applicationElement = storageDocument.CreateElement("application");

                    applicationElement.SetAttribute("applicationid", application.ApplicationId);
                    applicationElement.SetAttribute("state", application.State.ToString());
                    applicationElement.SetAttribute("timecreated", application.TimeCreated.ToString());
                    applicationElement.SetAttribute("primary", application.Primary.ToString());
                    applicationElement.SetAttribute("username", application.Username.ToString());
                    applicationElement.SetAttribute("totalthreads", application.TotalThreads.ToString());
                    applicationElement.SetAttribute("unfinishedthreads", application.UnfinishedThreads.ToString());

                    applicationsNode.AppendChild(applicationElement);
                }
            }

            if (m_threads != null)
            {
                IEnumerator threadsEnumerator = m_threads.GetEnumerator();

                while (threadsEnumerator.MoveNext())
                {
                    ThreadStorageView thread = threadsEnumerator.Current as ThreadStorageView;

                    XmlElement threadElement = storageDocument.CreateElement("thread");

                    threadElement.SetAttribute("internalthreadid", thread.InternalThreadId.ToString());
                    threadElement.SetAttribute("applicationid", thread.ApplicationId);
                    threadElement.SetAttribute("executorid", thread.ExecutorId);
                    threadElement.SetAttribute("threadid", thread.ThreadId.ToString());
                    threadElement.SetAttribute("state", thread.State.ToString());
                    threadElement.SetAttribute("timestarted", thread.TimeStarted.ToString());
                    threadElement.SetAttribute("timefinished", thread.TimeFinished.ToString());
                    threadElement.SetAttribute("priority", thread.Priority.ToString());
                    threadElement.SetAttribute("failed", thread.Failed.ToString());

                    threadsNode.AppendChild(threadElement);
                }
            }

            storageDocument.Save(filename);
        }