protected void cmdLock_Click(object sender, System.EventArgs e)
        {
            Entities.CustomPrincipal loggedOnUser = (Entities.CustomPrincipal)Page.User;
            Facade.UserAdmin         facUserAdmin = new Facade.UserAdmin();
            Facade.Security          facSecurity  = new Facade.Security();
            int IdentityId = Convert.ToInt32(Request.QueryString["identityId"]);

            if (cmdLock.Text == "Lock")
            {
                if (facUserAdmin.LockUser(IdentityId))
                {
                    cmdLock.Text = "Unlock";
                }
                else
                {
                    lblMessage.Text = "Failed to lock User.";
                }
            }
            else if (cmdLock.Text == "Unlock")
            {
                if (facUserAdmin.UnLockUser(IdentityId))
                {
                    cmdLock.Text = "Lock";
                }
                else
                {
                    lblMessage.Text = "Failed to unlock User.";
                }
            }
        }
        private void BindGrid()
        {
            Facade.UserAdmin      facUserAdmin = new Facade.UserAdmin();
            Facade.IReferenceData refData      = new Facade.ReferenceData();
            DataSet dsUserGroups = refData.GetAllRoles();

            dgUserGroups.DataSource = dsUserGroups;
            dgUserGroups.DataBind();
        }
        protected void btnRemove_Click(object sender, EventArgs e)
        {
            bool success = false;

            Entities.CustomPrincipal loggedOnUser = (Entities.CustomPrincipal)Page.User;

            Facade.IUserAdmin facUserAdmin = new Facade.UserAdmin();
            Facade.ISecurity  facSecurity  = new Facade.Security();

            success = facUserAdmin.UpdateUserState((int)ViewState["identityId"], (int)eIdentityStatus.Deleted, loggedOnUser.Name);

            if (success)
            {
                this.ReturnValue = "CloseAndRefresh";
                this.Close();
            }
            else
            {
                lblMessage.Text = "Update User failed. Please try again.";
            }
        }
        private void addUser()
        {
            if (Page.IsValid)
            {
                Entities.CustomPrincipal loggedOnUser = (Entities.CustomPrincipal)Page.User;

                Facade.IUserAdmin facUserAdmin = new Facade.UserAdmin();
                Facade.ISecurity  facSecurity  = new Facade.Security();

                int  organisationId = 0;
                int  teamId         = 0;
                bool plannerRemoved = false;

                if (m_isClient == true)
                {
                    organisationId = Convert.ToInt32(cboClient.SelectedValue);
                }
                else
                {
                    teamId = Convert.ToInt32(cboTeam.SelectedItem.Value);
                }
                int retIdentityId;

                if (string.IsNullOrEmpty(txtSelectedRoles.Value))
                {
                    lblMessage.Text = "Edit user failed.  At least one role must be selected.";
                    return;
                }

                string[] sRoles = txtSelectedRoles.Value.Substring(1).Split(',');
                int[]    iRoles = new int[sRoles.Length];

                for (int count = 0; count <= sRoles.Length - 1; count++)
                {
                    iRoles[count] = Convert.ToInt32(sRoles[count]);
                }

                var validateRolesResult = facUserAdmin.ValidateUserRoles(txtUserName.Text, iRoles);

                if (!validateRolesResult.Success)
                {
                    if (validateRolesResult.Infringements.Select(i => i.Description).Contains("PlannerRemoved") && validateRolesResult.Infringements.Count == 1)
                    {
                        plannerRemoved = true;
                    }
                    else
                    {
                        lblMessage.Text = string.Join("<br />", validateRolesResult.Infringements.Select(i => i.Description));
                        return;
                    }
                }

                if (btnAdd.Text == "Add")
                {
                    // Validate password
                    if (facSecurity.ValidatePassword(txtUserName.Text, txtPassword.Text))
                    {
                        rfvComplex.IsValid = true;
                    }
                    else
                    {
                        rfvComplex.IsValid = false;
                        return;
                    }

                    if (!m_isClient)
                    {
                        retIdentityId = facUserAdmin.AddUser(txtUserName.Text, txtPassword.Text, txtForenames.Text, txtSurname.Text, iRoles, teamId, loggedOnUser.Name, txtEmail.Text, chkCanAccessFromAnywhere.Checked, chkScannedLicense.Checked);
                    }
                    else
                    {
                        retIdentityId = facUserAdmin.AddUserForClient(txtUserName.Text, txtPassword.Text, txtForenames.Text, txtSurname.Text, iRoles, organisationId, loggedOnUser.Name, txtEmail.Text, chkScannedLicense.Checked);
                    }

                    if (retIdentityId > 0)
                    {
                        if (chkEmailDetails.Checked && m_isClient && pnlEmailDetails.Visible)
                        {
                            EmailClient();
                        }
                        this.ReturnValue = "CloseAndRefresh";
                        this.Close();
                    }
                    else if (retIdentityId == -1)
                    {
                        lblMessage.Text = "The Username has already been added to the application.";
                    }
                    else
                    {
                        lblMessage.Text = "Add new User failed. Please try again.";
                    }
                }
                else if (btnAdd.Text == "Update")
                {
                    bool success = false;
                    if (!m_isClient)
                    {
                        success = facUserAdmin.UpdateUser((int)ViewState["identityId"], txtPassword.Text, txtForenames.Text, txtSurname.Text, Convert.ToInt32(cboUserStatus.SelectedValue), iRoles, teamId, loggedOnUser.Name, txtEmail.Text, chkCanAccessFromAnywhere.Checked, chkScannedLicense.Checked, plannerRemoved);
                    }
                    else
                    {
                        success = facUserAdmin.UpdateUserForClient((int)ViewState["identityId"], txtPassword.Text, txtForenames.Text, txtSurname.Text, Convert.ToInt32(cboUserStatus.SelectedValue), iRoles, organisationId, loggedOnUser.Name, txtEmail.Text, chkScannedLicense.Checked);
                    }

                    if (success)
                    {
                        this.ReturnValue = "CloseAndRefresh";
                        this.Close();
                    }
                    else
                    {
                        lblMessage.Text = "Update User failed. Please try again.";
                    }
                }
            }
        }