public UserRow(User user)
        {
            this.name = user.name;
            this.roles = user.roles;
            roleRows = new BindingList<RoleRow>();

            foreach (string roleName in user.roles)
            {
                roleRows.Add(new RoleRow(roleName));
            }
        }
        public static void RefreshRoles(AerospikeClient client, User user, bool admin)
        {
            if (admin)
            {
                // Query all roles
                Roles = client.QueryRoles(null);
            }
            else
            {
                List<Role> list = new List<Role>(user.roles.Count);

                foreach (string roleName in user.roles)
                {
                    Role role = client.QueryRole(null, roleName);
                    list.Add(role);
                }
                Roles = list;
            }
        }
        public AdminForm(AerospikeClient client, User user, bool admin)
        {
            this.client = client;
            InitializeComponent();
            this.Text += " by " + user.name;
            usersControl.Init(client, user, admin);
            rolesControl.Init(client, user, admin);
            rolesControl.Visible = false;

            TreeNode usersNode = new TreeNode("Users");

            TreeNode rootNode = new TreeNode("Manage", new TreeNode[] {
                    usersNode,
                    new TreeNode("Roles"),
                });

            sideView.Nodes.Add(rootNode);
            sideView.ExpandAll();
            sideView.SelectedNode = usersNode;
        }
        public void Init(AerospikeClient client, User user, bool admin)
        {
            this.client = client;
            this.user = user;
            this.admin = admin;

            InitRightClickMenu(admin);

            if (!admin)
            {
                rolesFilter = user.roles;
                createMenuItem.Enabled = false;
                dropMenuItem.Enabled = false;
                editMenuItem.Enabled = false;
                createButton.Enabled = false;
            }

            // Initialize role grid
            gridRoles.AutoGenerateColumns = false;
            RoleNameColumn.DataPropertyName = "RoleName";

            bindingSourceRoles = new BindingSource();
            gridRoles.DataSource = bindingSourceRoles;

            // Initialize privilege grid
            gridPrivs.AutoGenerateColumns = false;
            PrivilegeCodeColumn.DataPropertyName = "CodeString";
            NamespaceColumn.DataPropertyName = "Namespace";
            SetNameColumn.DataPropertyName = "SetName";

            bindingSourcePrivs = new BindingSource();
            gridPrivs.DataSource = bindingSourcePrivs;

            // Load data
            ReadRoles();

            if (roles.Count > 0)
            {
                bindingSourcePrivs.DataSource = roles[0].privilegeRows;
            }
        }
        public void Init(AerospikeClient client, User user, bool admin)
        {
            this.client = client;
            InitRightClickMenu(admin);

            if (!admin)
            {
                userNameFilter = user.name;
                createUserButton.Enabled = false;
                createUserMenuItem.Enabled = false;
                dropUserMenuItem.Enabled = false;
                editUserMenuItem.Enabled = false;
            }

            // Initialize user grid
            gridUsers.AutoGenerateColumns = false;
            UserNameColumn.DataPropertyName = "UserName";

            bindingSourceUsers = new BindingSource();
            gridUsers.DataSource = bindingSourceUsers;

            // Initialize role grid
            gridRoles.AutoGenerateColumns = false;
            RoleNameColumn.DataPropertyName = "RoleName";

            bindingSourceRoles = new BindingSource();
            gridRoles.DataSource = bindingSourceRoles;

            // Load data
            ReadUsers(user);

            if (users.Count > 0)
            {
                bindingSourceRoles.DataSource = users[0].roleRows;
            }
        }
            internal void ParseRoles(User user)
            {
                int size = base.dataBuffer[base.dataOffset++];
                user.roles = new List<string>(size);

                for (int i = 0; i < size; i++)
                {
                    int len = base.dataBuffer[base.dataOffset++];
                    string role = ByteUtil.Utf8ToString(base.dataBuffer, base.dataOffset, len);
                    base.dataOffset += len;
                    user.roles.Add(role);
                }
            }
            internal override int ParseBlock(int receiveSize)
            {
                base.dataOffset = 0;

                while (base.dataOffset < receiveSize)
                {
                    int resultCode = base.dataBuffer[base.dataOffset + 1];

                    if (resultCode != 0)
                    {
                        return resultCode;
                    }

                    User user = new User();
                    int fieldCount = base.dataBuffer[base.dataOffset + 3];
                    base.dataOffset += HEADER_REMAINING;

                    for (int i = 0; i < fieldCount; i++)
                    {
                        int len = ByteUtil.BytesToInt(base.dataBuffer, base.dataOffset);
                        base.dataOffset += 4;
                        int id = base.dataBuffer[base.dataOffset++];
                        len--;

                        if (id == USER)
                        {
                            user.name = ByteUtil.Utf8ToString(base.dataBuffer, base.dataOffset, len);
                            base.dataOffset += len;
                        }
                        else if (id == ROLES)
                        {
                            ParseRoles(user);
                        }
                        else
                        {
                            base.dataOffset += len;
                        }
                    }

                    if (user.name == null && user.roles == null)
                    {
                        continue;
                    }

                    if (user.roles == null)
                    {
                        user.roles = new List<string>(0);
                    }
                    list.Add(user);
                }
                return 0;
            }
        private void ReadUsers(User currentUser)
        {
            List<UserRow> list;

            if (userNameFilter == null)
            {
                // Query all users
                List<User> source = client.QueryUsers(null);
                list = new List<UserRow>(source.Count);

                foreach (User user in source)
                {
                    list.Add(new UserRow(user));
                }
            }
            else
            {
                // Query own user.
                if (currentUser == null)
                {
                    currentUser = client.QueryUser(null, userNameFilter);
                }
                list = new List<UserRow>(1);

                if (currentUser != null)
                {
                    list.Add(new UserRow(currentUser));
                }
            }

            users = new BindingList<UserRow>(list);
            bindingSourceUsers.DataSource = users;
        }