Esempio n. 1
0
        private void addMachineButton_Click(object sender, EventArgs e)
        {
            string macName = this.machineNameComboBox.Text;

            if (!string.IsNullOrEmpty(macName) &&
                macName.ToLowerInvariant().Contains(CASHAM_DOMAIN))
            {
                macName = macName.Trim();
                var res = MessageBox.Show("Are you sure you want to add " + macName + " to PAWNSEC™ ?",
                                          PawnStoreSetupForm.SETUPALERT_TXT,
                                          MessageBoxButtons.YesNo,
                                          MessageBoxIcon.Question);
                if (res == DialogResult.No)
                {
                    return;
                }

                //Ensure that this machine does not exist in pawn sec already for this store or
                //any other store
                var fndMachine = false;
                var pgFm       = new InProgressForm("* VERIFYING UNIQUENESS OF MACHINE *");
                if (CollectionUtilities.isNotEmpty(this.storeData.PawnSecData.ClientMachines))
                {
                    var fndMac = this.storeData.PawnSecData.ClientMachines.Find(
                        x => x.Machine.MachineName.Equals(macName, StringComparison.OrdinalIgnoreCase));
                    if (fndMac != null)
                    {
                        fndMachine = true;
                    }
                    if (fndMachine == false &&
                        !string.IsNullOrEmpty(ipAddressTextBox.Text))
                    {
                        var trimIp = ipAddressTextBox.Text.Trim();
                        if (!string.IsNullOrEmpty(trimIp))
                        {
                            //See if IP address is used, and if so, ensure uniqueness
                            var fndIp =
                                this.storeData.PawnSecData.ClientMachines.Find(
                                    x => x.Machine.IPAddress.Equals(trimIp));
                            if (fndIp != null)
                            {
                                fndMachine = true;
                            }
                        }
                    }

                    if (fndMachine == false &&
                        !string.IsNullOrEmpty(macAddressTextBox.Text))
                    {
                        var trimMac = macAddressTextBox.Text.Trim();
                        if (!string.IsNullOrEmpty(trimMac))
                        {
                            var fndMacAddr =
                                this.storeData.PawnSecData.ClientMachines.Find(
                                    x =>
                                    x.Machine.MACAddress.Equals(trimMac, StringComparison.OrdinalIgnoreCase));
                            if (fndMacAddr != null)
                            {
                                fndMachine = true;
                            }
                        }
                    }
                }

                //Now check globally against all entries
                if (!fndMachine)
                {
                    var whereClause = "machinename = '" + macName + "'";
                    if (!string.IsNullOrEmpty(ipAddressTextBox.Text))
                    {
                        var trimIp = ipAddressTextBox.Text.Trim();
                        if (!string.IsNullOrEmpty(trimIp))
                        {
                            whereClause += " or ipaddress = '" + trimIp + "'";
                        }
                    }
                    if (!string.IsNullOrEmpty(macAddressTextBox.Text))
                    {
                        var trimMac = macAddressTextBox.Text.Trim();
                        if (!string.IsNullOrEmpty(trimMac))
                        {
                            whereClause += " or macaddress = '" + trimMac + "'";
                        }
                    }
                    DataReturnSet dS;
                    if (DataAccessService.ExecuteQuery(false,
                                                       string.Format(
                                                           "select machinename from clientregistry where {0}",
                                                           whereClause),
                                                       "clientregistry",
                                                       PawnStoreSetupForm.PAWNSEC,
                                                       out dS,
                                                       ref this.dAPawnSec))
                    {
                        if (dS != null && dS.NumberRows > 0)
                        {
                            fndMachine = true;
                        }
                    }
                    pgFm.HideMessage();
                }

                if (fndMachine)
                {
                    pgFm.Dispose();
                    MessageBox.Show(
                        "This machine already exists in the pawn security database. " + Environment.NewLine +
                        "  If this machine has moved to a different store, please " + Environment.NewLine +
                        "remove it from that store's security database first.",
                        PawnStoreSetupForm.SETUPALERT_TXT);
                    return;
                }
                pgFm.Dispose();
                this.addMachineButton.Enabled    = false;
                this.removeButton.Enabled        = false;
                this.machineNameComboBox.Enabled = false;
                var newMac     = new PawnSecVO.ClientPawnSecMachineVO();
                var newMapping = new PawnSecVO.ClientStoreMapVO();
                newMac.Machine.MachineName = macName;
                int perIdx = macName.IndexOf(".");
                if (perIdx != -1)
                {
                    newMac.Machine.WorkstationName = macName.Substring(0, perIdx);
                }
                //Set terminal number
                if (CollectionUtilities.isNotEmpty(this.storeData.PawnSecData.ClientMachines))
                {
                    newMac.StoreMachine.TerminalNumber =
                        this.storeData.PawnSecData.ClientMachines.Max(
                            x => x.StoreMachine.TerminalNumber) + 1;
                }
                else
                {
                    newMac.StoreMachine.TerminalNumber = 1;
                }
                //Set store client config id
                //ulong storeMachineId = 0;
                //this.storeData.PawnSecData.NextIdSet.GetNextIds(PawnSecVO.PawnSecNextIdVO.SELECTOR.STOCLICFG_ID, ref storeMachineId);
                newMac.StoreMachine.Id = 0;//storeMachineId;

                //Set client id
                //ulong clientRegId = 0;
                //this.storeData.PawnSecData.NextIdSet.GetNextIds(PawnSecVO.PawnSecNextIdVO.SELECTOR.CLIREG_ID, ref clientRegId);
                newMac.Machine.ClientId = 0;

                //Set mapping id
                //ulong newMappingId = 1;
                //this.storeData.PawnSecData.NextIdSet.GetNextIds(PawnSecVO.PawnSecNextIdVO.SELECTOR.CLISTOMAP_ID, ref newMappingId);
                newMapping.Id = 0;
                newMapping.ClientRegistryId = newMac.Machine.ClientId;

                //Set store config id
                newMapping.StoreClientConfigId = newMac.StoreMachine.Id;

                var curSto = this.getStoreInformation();
                if (curSto == null)
                {
                    MessageBox.Show("Cannot find store to add this client to for mapping purposes",
                                    PawnStoreSetupForm.SETUPALERT_TXT);
                    return;
                }
                newMapping.StoreSiteId   = curSto.StoreSiteId;
                newMapping.StoreConfigId = curSto.StoreConfiguration.Id;
                newMapping.StoreNumber   = this.storeNumber;
                this.storeData.PawnSecData.ClientMachines.Add(newMac);
                this.storeData.PawnSecData.ClientStoreMapList.Add(newMapping);
                this.storeData.PawnSecData.GenerateMaps();
            }
            else
            {
                MessageBox.Show(
                    "Please enter a valid machine name. It must contain the full domain name: " + CASHAM_DOMAIN,
                    PawnStoreSetupForm.SETUPALERT_TXT);
            }
        }
        private bool addNewUserToLDAP()
        {
            if (PawnLDAPAccessor.Instance.State ==
                PawnLDAPAccessor.LDAPState.CONNECTED)
            {
                bool validDataSet = false;

                while (!validDataSet)
                {
                    var dataReqForm = new DataRequestForm();
                    dataReqForm.DataObject = NewLDAPUser;
                    dataReqForm.Text       = "Add LDAP User";
                    var res = dataReqForm.ShowDialog(this);
                    if (res == DialogResult.OK)
                    {
                        var completedData = (LDAPUserVO)dataReqForm.DataObject;
                        var sB            = new StringBuilder(256);
                        var errorOccurred = false;
                        if (string.IsNullOrEmpty(completedData.UserName))
                        {
                            sB.AppendLine("You must enter a valid user name");
                            errorOccurred = true;
                        }
                        var errMsg = string.Empty;
                        if (string.IsNullOrEmpty(completedData.Password) ||
                            PawnLDAPAccessor.Instance.PasswordPolicy == null ||
                            !PawnLDAPAccessor.Instance.PasswordPolicy.IsValid(completedData.Password, null, out errMsg))
                        {
                            sB.AppendLine("You must enter a valid password");
                            if (!string.IsNullOrEmpty(errMsg))
                            {
                                sB.AppendLine("Password invalid because: " + errMsg);
                            }
                            errorOccurred = true;
                        }

                        if (string.IsNullOrEmpty(completedData.EmployeeHomeStore) ||
                            completedData.EmployeeHomeStore.Length < PawnStoreSetupForm.MIN_STORENUM_LENGTH ||
                            completedData.EmployeeHomeStore.Length > PawnStoreSetupForm.MAX_STORENUM_LENGTH)
                        {
                            sB.AppendLine("You must enter a valid store number");
                            errorOccurred = true;
                        }
                        else if (!string.IsNullOrEmpty(completedData.EmployeeHomeStore) &&
                                 completedData.EmployeeHomeStore.Length >= PawnStoreSetupForm.MIN_STORENUM_LENGTH &&
                                 completedData.EmployeeHomeStore.Length <= PawnStoreSetupForm.MAX_STORENUM_LENGTH)
                        {
                            string stoNumStr = completedData.EmployeeHomeStore;
                            if (stoNumStr.StartsWith("0"))
                            {
                                stoNumStr = stoNumStr.Substring(1);
                            }
                            int stoNum;
                            if (!Int32.TryParse(stoNumStr, out stoNum))
                            {
                                sB.AppendLine(
                                    "You must enter a valid store number. A store number must be comprised of numbers only.");
                                errorOccurred = true;
                            }
                        }

                        if (string.IsNullOrEmpty(completedData.DisplayName))
                        {
                            sB.AppendLine("You must enter a valid display name");
                            errorOccurred = true;
                        }

                        int empNum;
                        if (string.IsNullOrEmpty(completedData.EmployeeNumber) ||
                            completedData.EmployeeNumber.Length < 4 ||
                            !Int32.TryParse(completedData.EmployeeNumber, out empNum))
                        {
                            sB.AppendLine("You must enter a valid employee number");
                            errorOccurred = true;
                        }

                        if (string.IsNullOrEmpty(completedData.EmployeeType))
                        {
                            sB.AppendLine("You must enter a valid employee type");
                            errorOccurred = true;
                        }

                        if (errorOccurred)
                        {
                            MessageBox.Show(
                                "The data provided for the LDAP user add operation is invalid.  Please check the following reasons and correct: " +
                                sB,
                                PawnStoreSetupForm.SETUPALERT_TXT);
                            continue;
                        }

                        //We have a valid data set if we made it here
                        validDataSet = true;
                        NewLDAPUser  = completedData;
                    }
                    else
                    {
                        var cancelRes = MessageBox.Show(
                            "You have unsaved changes. Are you sure you want to cancel the user add operation?",
                            PawnStoreSetupForm.SETUPALERT_TXT,
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Warning);
                        if (cancelRes == DialogResult.Yes)
                        {
                            validDataSet = false;
                            break;
                        }
                    }
                }

                if (validDataSet)
                {
                    var    progBox = new InProgressForm("* ADDING USER TO LDAP *");
                    string errMsg;
                    bool   addedUserToLdap = true;
                    if (!PawnLDAPAccessor.Instance.CreateUser(
                            NewLDAPUser.UserName,
                            NewLDAPUser.Password,
                            NewLDAPUser.DisplayName,
                            NewLDAPUser.EmployeeNumber,
                            NewLDAPUser.EmployeeType,
                            out errMsg))
                    {
                        progBox.HideMessage();
                        MessageBox.Show("The LDAP Add User operation has failed: " + errMsg +
                                        ".  Will attempt to validate against existing credentials to " +
                                        "verify if the user is already in the LDAP system");
                        addedUserToLdap = false;
                    }

                    int      numTries = 0;
                    DateTime pwdLastModified;
                    string[] pwdHistory;
                    string   dispName;
                    bool     lockedOut;
                    if (!addedUserToLdap &&
                        PawnLDAPAccessor.Instance.AuthorizeUser(
                            NewLDAPUser.UserName,
                            NewLDAPUser.Password,
                            ref numTries,
                            out pwdLastModified,
                            out pwdHistory, out dispName, out lockedOut))
                    {
                        addedUserToLdap = true;
                    }

                    if (addedUserToLdap)
                    {
                        if (NewLDAPUser.EmployeeHomeStore.Length == PawnStoreSetupForm.MIN_STORENUM_LENGTH)
                        {
                            NewLDAPUser.EmployeeHomeStore = NewLDAPUser.EmployeeHomeStore.PadLeft(5, '0');
                        }
                        NewUser.UserName            = NewLDAPUser.UserName;
                        NewUser.UserCurrentPassword = NewLDAPUser.Password;
                        NewUser.EmployeeNumber      = NewLDAPUser.EmployeeNumber;
                        NewUser.FacNumber           = NewLDAPUser.EmployeeHomeStore;
                        NewUser.StoreNumber         = NewUser.FacNumber;
                        NewUser.UserID          = "0";
                        NewUser.UserRole        = new RoleVO();
                        NewUser.UserRole.RoleId = PawnStoreSetupForm.DEFAULT_ROLE_NAME;
                        progBox.HideMessage();
                        MessageBox.Show("Successfully added " + NewLDAPUser.UserName + " to the LDAP server.");
                        return(true);
                    }
                }
            }
            else
            {
                MessageBox.Show("Please set up the connection to LDAP prior to adding users.",
                                PawnStoreSetupForm.SETUPALERT_TXT);
            }
            return(false);
        }