private void toggleEditMode(bool editing) // Note that when editing, this is called from setUpEdit rather than the other way round
        {
            try
            {
                if (editing && Globals.SelectedProjectProxy.IsOld)
                {
                    MessageFunctions.InvalidMessage("Staff cannot be amended for closed or cancelled projects.", "Project is Closed");
                    return;
                }

                AmendmentGrid.Visibility = CommitButton.Visibility = editing ? Visibility.Visible : Visibility.Hidden;
                StatusLabel.Visibility   = StatusCombo.Visibility = ProjectButton.Visibility = (editing) ? Visibility.Hidden : Visibility.Visible;
                NameLikeLabel.Visibility = NameLike.Visibility = RoleFilterLabel.Visibility = RoleFilterCombo.Visibility = StatusLabel.Visibility;
                toggleEditButtons(!editing && ProjectCombo.SelectedItem != null && ProjectCombo.SelectedItem != Globals.AllProjects);
                TeamDataGrid.Width            = editing ? TeamDataGrid.MinWidth : TeamDataGrid.MaxWidth;
                TimeGroup.HorizontalAlignment = editing ? HorizontalAlignment.Left : HorizontalAlignment.Right;
                ProjectCombo.IsEnabled        = TeamDataGrid.IsEnabled = !editing;
                if (!editing)
                {
                    Instructions.Content = defaultInstructions;
                }                                                             // Otherwise set up later depending on the mode
                CancelButtonText.Text = editing ? "Cancel" : "Close";
            }
            catch (Exception generalException) { MessageFunctions.Error("Error displaying required controls", generalException); }
        }
예제 #2
0
 private void suggestFormat()
 {
     try
     {
         int    avoidID         = (selectedRecord != null) ? selectedRecord.ID : 0;
         string suggestedFormat = ClientFunctions.SuggestCode(selectedEntityID, avoidID, "");
         if (suggestedFormat == "")
         {
             MessageFunctions.InvalidMessage(
                 "A format cannot be suggested as there are too few active client records in this Entity to analyse. Please view other client records manually if unsure.",
                 "Insufficient Data to Suggest Code Format");
             if (CodeSuggestion.Visibility == Visibility.Visible)
             {
                 toggleSuggestionMode(false);
             }                                                                                     // Possible if copying a record to another Entity
         }
         else
         {
             CodeSuggestion.Text    = suggestedFormat;
             SuggestionTips.Text    = ClientFunctions.SuggestionTips;
             CodeSuggestion.ToolTip = ClientFunctions.ExplainCode;
             toggleSuggestionMode(true);
         }
     }
     catch (Exception generalException) { MessageFunctions.Error("Error retrieving suggested code", generalException); }
 }
예제 #3
0
 private void commitAction(string userID, string password)
 {
     if (pageMode == PageFunctions.LogIn && SSOCheckBox.IsChecked == true && LoginFunctions.SingleSignon(userID))
     {
         pageSuccess = true;
     }
     else if (!LoginFunctions.CheckPassword(userID, password))
     {
         MessageFunctions.InvalidMessage("Incorrect existing username or password. Please check and try again.", "Incorrect Login");
     }
     else if (pageMode == PageFunctions.LogIn)
     {
         LoginFunctions.AttemptLogin(userID, password);
         pageSuccess = true;
     }
     else if (pageMode == PageFunctions.PassChange)
     {
         bool success = LoginFunctions.ChangeLoginDetails(Globals.MyStaffID, userID, NewPassword.Password, ConfirmPassword.Password);
         if (success)
         {
             MessageFunctions.SuccessAlert("Your password has been changed successfully.", "Password Changed");
             pageSuccess = true;
         }
     }
 }
예제 #4
0
        // Utilities //
        public static bool SqlInputOK(string inputText, bool mandatory, string fieldName, string fieldTitle = "", string invalidCharacters = "")
        {
            if (fieldTitle == "")
            {
                CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                TextInfo    textInfo    = cultureInfo.TextInfo;
                fieldTitle = textInfo.ToTitleCase(fieldName);
            }

            if (mandatory && inputText == "")
            {
                MessageFunctions.InvalidMessage(fieldName + "s cannot be blank. Please enter a value in the '" + fieldTitle + "' field and try again.", "No " + fieldTitle + " Entered");
                return(false);
            }
            else if (invalidCharacters != "")
            {
                List <char> inputChars   = inputText.ToCharArray().ToList();
                List <char> invalidChars = invalidCharacters.ToCharArray().ToList();

                foreach (char badChar in invalidChars)
                {
                    if (inputChars.Contains(badChar))
                    {
                        MessageFunctions.InvalidMessage(fieldName + " cannot contain the character '" + badChar.ToString() + "'. Please remove it and try again.", "Invalid Character");
                        return(false);
                    }
                }
            }
            return(true);
        }
예제 #5
0
        // ---------------------- //
        // -- Event Management -- //
        // ---------------------- //

        // Control-specific events //
        private void LogInButton_Click(object sender, RoutedEventArgs e)
        {
            string userID   = UserID.Text;
            string password = Password.Password;

            // First check that the details are correct
            if (userID == "")
            {
                MessageFunctions.InvalidMessage("Please enter your User ID in the 'UserID' text box.", "User ID Required");
                return;
            }
            else if (SSOCheckBox.IsChecked == false && password == "")
            {
                string strContext = (pageMode == PageFunctions.PassChange) ? "existing " : "";
                MessageFunctions.InvalidMessage("Please enter your " + strContext + "password in the 'Password' text box.", "Password Required");
                return;
            }

            /*
             * try
             * {
             *  DoubleAnimation spinAnimation = new DoubleAnimation(0, 365, new Duration(TimeSpan.FromSeconds(1)));
             *  Storyboard.SetTargetName(spinAnimation, "RectangleSpin");
             *  Storyboard.SetTargetProperty(spinAnimation, new PropertyPath(RotateTransform.AngleProperty));
             *  spinStoryboard = new Storyboard();
             *  spinStoryboard.RepeatBehavior = RepeatBehavior.Forever;
             *  spinStoryboard.Children.Add(spinAnimation);
             *  spinStoryboard.Begin(this, true);
             * }
             * catch (Exception generalException) { MessageFunctions.Error("Animation error", generalException); }
             */

            // Show the 'please wait' message and queue the next activity for afterwards
            manageCommit(userID, password);
        }
예제 #6
0
        public static bool AmendProduct(int productID, string productName, string productDescription, string version)
        {
            try
            {
                decimal versionNumber;

                if (!Decimal.TryParse(version, out versionNumber))
                {
                    MessageFunctions.InvalidMessage("Cannot amend product '" + productName + "': new version number is not a decimal.", "Invalid Version");
                    return(false);
                }

                try
                {
                    ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();
                    using (existingPtDb)
                    {
                        Products thisProduct = existingPtDb.Products.Find(productID);
                        if (thisProduct.LatestVersion > versionNumber)
                        {
                            bool carryOn = MessageFunctions.WarningYesNo("The new version number is lower than the existing one. Is that correct?", "Unexpected Version");
                            if (!carryOn)
                            {
                                return(false);
                            }
                        }
                        thisProduct.ProductName        = productName;
                        thisProduct.ProductDescription = productDescription;
                        thisProduct.LatestVersion      = versionNumber;

                        if (ValidateProduct(ref thisProduct, productID))
                        {
                            existingPtDb.SaveChanges();
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
                catch (Exception generalException)
                {
                    MessageFunctions.Error("Problem saving changes to product '" + productName + "'", generalException);
                    return(false);
                }
            }
            catch (Exception generalException)
            {
                MessageFunctions.Error("Error amending product '" + productName + "'", generalException);
                return(false);
            }
        }
예제 #7
0
        // Login functions

        public static bool AttemptLogin(string userID, string password)
        {
            try
            {
                int    entityID;
                string databaseLogin = DbUserPrefix + userID;
                if (HasSingleSignon(userID))
                {
                    changeDatabasePassword(userID, password);
                }                                                                          // Changes only the back-end password, to set a new temp password for SSO or reset to the stored one for non-SSO
                ProjectTileSqlDatabase userPtDb = SqlServerConnection.UserPtDbConnection(databaseLogin, password);
                using (userPtDb)
                {
                    Staff thisUser = userPtDb.Staff.First(s => s.UserID == userID);
                    entityID = (int)thisUser.DefaultEntity;
                    Entities currentEntity = userPtDb.Entities.Find(entityID);

                    if (thisUser.FirstName != "")
                    {
                        if (!thisUser.Active)
                        {
                            MessageFunctions.InvalidMessage("User is not active. Please contact your system administrator.", "Inactive User");
                        }
                        else if (thisUser.LeaveDate < DateTime.Now)
                        {
                            MessageFunctions.InvalidMessage("User has left. Please contact your system administrator.", "Not Current User");
                        }
                        else if (thisUser.StartDate > DateTime.Now)
                        {
                            MessageFunctions.InvalidMessage("User has not yet started. Please contact your system administrator.", "Not Current User");
                        }
                        else
                        {
                            LogIn(thisUser, currentEntity);
                        }
                    }
                    return(true);
                }
            }

            catch (SqlException sqlException)
            {
                MessageFunctions.Error("Error accessing the database", sqlException);
                return(false);
            }

            catch (Exception generalException)
            {
                MessageFunctions.Error("Error logging in", generalException);
                return(false);
            }
        }
예제 #8
0
 private void ProjectButton_Click(object sender, RoutedEventArgs e)
 {
     int[] allowedEntities = EntityFunctions.AllowedEntityIDs(selectedStaffID);
     if (!allowedEntities.Contains(Globals.CurrentEntityID))
     {
         MessageFunctions.InvalidMessage("This staff member does not have access to this Entity, so they cannot have any projects in it.", "Invalid Entity");
         return;
     }
     else
     {
         PageFunctions.ShowProjectTeamsPage(pageMode, selectedStaffID, "StaffPage");
     }
 }
        private void CommitButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //string roleDescription = "";
                string roleCode = "";
                //string defaultEntityName = "";
                int    defaultEntityID = 0;
                bool   active          = (ActiveCheckBox.IsChecked == true);
                string passwd          = NewPassword.Password;

                if (RoleCombo.SelectedItem != null)
                {
                    StaffRoles role = (StaffRoles)RoleCombo.SelectedItem;
                    roleCode = role.RoleCode;
                }
                if (EntityCombo.SelectedItem != null)
                {
                    Entities defaultEntity = (Entities)EntityCombo.SelectedItem;
                    defaultEntityID = defaultEntity.ID;
                }

                if (passwd != "" && passwd != ConfirmPassword.Password)
                {
                    MessageFunctions.InvalidMessage("New password does not match confirmation. Please check both fields and try again.", "Password Mismatch");
                    return;
                }

                int returnID = StaffFunctions.SaveStaffDetails(selectedStaffID, FirstName.Text, Surname.Text, roleCode, StartDate.SelectedDate, LeaveDate.SelectedDate,
                                                               UserID.Text, passwd, active, defaultEntityID, (SSOCheckBox.IsChecked == true), DomainUser.Text);

                if (returnID > 0)
                {
                    if (selectedStaffID == 0)
                    {
                        MessageFunctions.SuccessAlert("New staff member created successfully.", "Staff details saved");
                        selectedStaffID = returnID;
                    }
                    else
                    {
                        MessageFunctions.SuccessAlert("Changes saved successfully.", "Staff member amended");
                    }
                    StaffFunctions.ReturnToStaffPage(selectedStaffID);
                }
            }
            catch (Exception generalException) { MessageFunctions.Error("Error saving details", generalException); }
        }
예제 #10
0
        public static int NewProduct(string productName, string productDescription, string version)
        {
            try
            {
                decimal versionNumber;

                if (!Decimal.TryParse(version, out versionNumber))
                {
                    MessageFunctions.InvalidMessage("Cannot create new product: version number is not a decimal.", "Invalid Version");
                    return(0);
                }

                Products newProduct = new Products()
                {
                    ProductName = productName, ProductDescription = productDescription, LatestVersion = versionNumber
                };
                if (ValidateProduct(ref newProduct, 0))
                {
                    try
                    {
                        ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();
                        using (existingPtDb)
                        {
                            existingPtDb.Products.Add(newProduct);
                            existingPtDb.SaveChanges();
                            return(newProduct.ID);
                        }
                    }
                    catch (Exception generalException)
                    {
                        MessageFunctions.Error("Problem saving new product", generalException);
                        return(0);
                    }
                }
                else
                {
                    return(0);
                }
            }
            catch (Exception generalException)
            {
                MessageFunctions.Error("Error creating new product", generalException);
                return(0);
            }
        }
예제 #11
0
 public static void SwitchEntity(ref Entities selectedEntity, bool makeDefault = false)
 {
     if (selectedEntity == null)
     {
         MessageFunctions.InvalidMessage("Please select an Entity from the drop-down list.", "No Entity Selected");
         return;
     }
     try
     {
         UpdateCurrentEntity(ref selectedEntity);
         if (makeDefault)
         {
             SetDefaultEntity(ref selectedEntity);
             MessageFunctions.SuccessAlert("Your default Entity has now been set to '" + selectedEntity.EntityName + "'.", "Default Entity Changed");
         }
         PageFunctions.ShowTilesPage();
     }
     catch (Exception generalException) { MessageFunctions.Error("Error changing current Entity", generalException); }
 }
예제 #12
0
        public static bool PasswordComplexityOK(string userID, string newPassword)
        {
            bool passOK = false;

            if (newPassword.Length < 8)
            {
                MessageFunctions.InvalidMessage("Your new password must be at least 8 characters long. Please try again.", "Password Too Short");
            }
            else if (userID == newPassword)
            {
                MessageFunctions.InvalidMessage("Your new password cannot equal your UserID. Please try again.", "Duplicate Password");
            }
            else
            {
                passOK = true;
            }

            return(passOK);
        }
예제 #13
0
        // Default Entity functions

        public static void SetDefaultEntity(ref Entities selectedEntity, int staffID = 0)
        {
            if (selectedEntity == null)
            {
                MessageFunctions.InvalidMessage("Please select an Entity to amend from the drop-down list.", "No Entity Selected");
                return;
            }

            ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();

            using (existingPtDb)
            {
                try
                {
                    if (staffID == 0)
                    {
                        staffID = MyStaffID;
                    }

                    Staff thisUser = existingPtDb.Staff.Find(staffID);
                    thisUser.DefaultEntity = selectedEntity.ID;

                    existingPtDb.SaveChanges();
                    if (staffID == MyStaffID)
                    {
                        UpdateMyDefaultEntity(ref selectedEntity);
                    }
                }
                catch (SqlException sqlException)
                {
                    MessageFunctions.Error("SQL error saving new default Entity preference to the database", sqlException);
                    return;
                }
                catch (Exception generalException)
                {
                    MessageFunctions.Error("Error saving new default Entity preference to the database", generalException);
                    return;
                }
            }
        }
예제 #14
0
        public static void ChangeDefaultEntity(ref Entities selectedEntity, string displayName)
        {
            if (selectedEntity != null)
            {
                try
                {
                    SetDefaultEntity(ref selectedEntity);
                    string notCurrent = "";
                    if (displayName != CurrentEntityName)
                    {
                        notCurrent = " Note that you are still currently connected to '" + CurrentEntityName + "'.";
                    }

                    MessageFunctions.SuccessAlert("Your default Entity has now been set to '" + displayName + "'." + notCurrent, "Default Entity Changed");
                    PageFunctions.UpdateDetailsBlock();
                    PageFunctions.ShowTilesPage();
                }
                catch (Exception generalException) { MessageFunctions.Error("Error changing entity", generalException); }
            }
            else
            {
                MessageFunctions.InvalidMessage("Please select an Entity from the drop-down list.", "No Entity Selected");
            }
        }
예제 #15
0
        // Changes

        public static bool ValidateProduct(ref Products thisProduct, int existingID)
        {
            try
            {
                ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();
                using (existingPtDb)
                {
                    string productName = thisProduct.ProductName;
                    if (!PageFunctions.SqlInputOK(productName, true, "Product name"))
                    {
                        return(false);
                    }
                    Products checkNewName = existingPtDb.Products.FirstOrDefault(p => p.ID != existingID && p.ProductName == productName);
                    if (checkNewName == null)
                    {
                        thisProduct.ProductName = productName;
                    }
                    else
                    {
                        string errorText = (existingID > 0) ?
                                           "Could not amend Product. Another Product with name '" + productName + "' already exists." :
                                           "Could not create new Product. A Product with name '" + productName + "' already exists.";

                        MessageFunctions.InvalidMessage(errorText, "Duplicate Name");
                        return(false);
                    }

                    string productDescription = thisProduct.ProductDescription;
                    if (!PageFunctions.SqlInputOK(productDescription, true, "Product description"))
                    {
                        return(false);
                    }
                    Products checkNewDescription = existingPtDb.Products.FirstOrDefault(p => p.ID != existingID && p.ProductDescription == productDescription);
                    if (checkNewDescription == null)
                    {
                        thisProduct.ProductDescription = productDescription;
                    }
                    else
                    {
                        string errorText = (existingID > 0) ?
                                           "Could not amend product. Another product with description '" + productDescription + "' already exists." :
                                           "Could not create new product. A product with description '" + productDescription + "' already exists.";

                        MessageFunctions.InvalidMessage(errorText, "Duplicate Description");
                        return(false);
                    }

                    return(true);
                }
            }
            catch (SqlException sqlException)
            {
                MessageFunctions.Error("SQL error saving changes to the database", sqlException);
                return(false);
            }
            catch (Exception generalException)
            {
                MessageFunctions.Error("Error saving changes to the database", generalException);
                return(false);
            }
        }
예제 #16
0
        public static void NewEntity(string entityName, string entityDescription, bool switchTo, bool makeDefault)
        {
            int      newEntityID;
            Entities newEntity;

            if (!PageFunctions.SqlInputOK(entityName, true, "Entity name"))
            {
                return;
            }
            else if (!PageFunctions.SqlInputOK(entityDescription, true, "Entity description"))
            {
                return;
            }

            try
            {
                ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();
                using (existingPtDb)
                {
                    Entities checkNewName = existingPtDb.Entities.FirstOrDefault(ent => ent.EntityName == entityName);
                    if (checkNewName != null)
                    {
                        MessageFunctions.InvalidMessage("Could not create new Entity. An Entity with name '" + entityName + "' already exists.", "Duplicate Name");
                        return;
                    }

                    Entities checkNewDescription = existingPtDb.Entities.FirstOrDefault(ent => ent.EntityDescription == entityDescription);
                    if (checkNewDescription != null)
                    {
                        MessageFunctions.InvalidMessage("Could not create new Entity. An Entity with description '" + entityDescription + "' already exists.", "Duplicate Description");
                        return;
                    }

                    try
                    {
                        try
                        {
                            newEntity                   = new Entities();
                            newEntity.EntityName        = entityName;
                            newEntity.EntityDescription = entityDescription;

                            try
                            {
                                existingPtDb.Entities.Add(newEntity);
                                existingPtDb.SaveChanges();
                                newEntityID = newEntity.ID;
                            }
                            catch (Exception generalException)
                            {
                                MessageFunctions.Error("Problem creating entity ID", generalException);
                                return;
                            }
                        }
                        catch (Exception generalException)
                        {
                            MessageFunctions.Error("Error creating database record", generalException);
                            return;
                        }

                        try
                        {
                            Staff currentUser = MyStaffRecord;
                            AllowEntity(newEntityID, currentUser.ID);
                        }
                        catch (Exception generalException)
                        {
                            MessageFunctions.Error("Error providing access to the new database", generalException);
                            return;
                        }

                        try
                        {
                            existingPtDb.SaveChanges();
                            string switched = ". Use the 'Change Current Entity' function to log into it if you wish to work in this Entity.";

                            if (switchTo)
                            {
                                UpdateCurrentEntity(ref newEntity);
                                switched = " and you are now logged into it.";
                            }

                            if (makeDefault)
                            {
                                SetDefaultEntity(ref newEntity);
                            }

                            MessageFunctions.SuccessAlert("Entity '" + entityName + "' has been created" + switched, "New Entity Created");
                            PageFunctions.ShowTilesPage();
                        }
                        catch (SqlException sqlException)
                        {
                            MessageFunctions.Error("SQL error saving changes to the database", sqlException);
                            return;
                        }
                        catch (Exception generalException)
                        {
                            MessageFunctions.Error("Error saving changes to the database", generalException);
                            return;
                        }
                    }
                    catch (Exception generalException) { MessageFunctions.Error("Error creating new database", generalException); }
                }
            }
            catch (Exception generalException) { MessageFunctions.Error("Error checking new database details", generalException); }
        }
예제 #17
0
        public static void AmendEntity(ref Entities selectedEntity, string entityName, string entityDescription)
        {
            int intSelectedEntityID;

            if (selectedEntity == null)
            {
                MessageFunctions.InvalidMessage("Please select an Entity to amend from the drop-down list.", "No Entity Selected");
                return;
            }

            if (!PageFunctions.SqlInputOK(entityName, true, "Entity name"))
            {
                return;
            }
            else if (!PageFunctions.SqlInputOK(entityDescription, true, "Entity description"))
            {
                return;
            }

            try
            {
                ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();
                using (existingPtDb)
                {
                    intSelectedEntityID = selectedEntity.ID;

                    Entities checkNewName = existingPtDb.Entities.FirstOrDefault(ent => ent.EntityName == entityName && ent.ID != intSelectedEntityID);
                    if (checkNewName != null)
                    {
                        MessageFunctions.InvalidMessage("Could not amend Entity. Another Entity with name '" + entityName + "' already exists.", "Duplicate Name");
                        return;
                    }

                    Entities checkNewDescription = existingPtDb.Entities.FirstOrDefault(ent => ent.EntityDescription == entityDescription && ent.ID != intSelectedEntityID);
                    if (checkNewDescription != null)
                    {
                        MessageFunctions.InvalidMessage("Could not amend Entity. Another Entity with description '" + entityDescription + "' already exists.", "Duplicate Description");
                        return;
                    }

                    try
                    {
                        try
                        {
                            string nameChange   = "";
                            string originalName = selectedEntity.EntityName;

                            if (originalName != entityName)
                            {
                                nameChange = " to '" + entityName + "'";
                            }
                            ;

                            Entities changeDbEntity = existingPtDb.Entities.Find(intSelectedEntityID);
                            changeDbEntity.EntityName        = entityName;
                            changeDbEntity.EntityDescription = entityDescription;
                            existingPtDb.SaveChanges();

                            MessageFunctions.SuccessAlert("Entity '" + originalName + "' has been amended" + nameChange + ".", "Entity Amended");
                            if (changeDbEntity.ID == CurrentEntityID)
                            {
                                UpdateCurrentEntity(ref changeDbEntity);
                            }
                            if (changeDbEntity.ID == MyDefaultEntityID)
                            {
                                UpdateMyDefaultEntity(ref changeDbEntity);
                            }
                            PageFunctions.ShowTilesPage();
                        }
                        catch (Exception generalException)
                        {
                            MessageFunctions.Error("Error amending database record", generalException);
                            return;
                        }
                    }
                    catch (Exception generalException) { MessageFunctions.Error("Error creating new database", generalException); }
                }
            }
            catch (Exception generalException) { MessageFunctions.Error("Error checking new database details", generalException); }
        }
        private void setDefault()
        {
            if (editMode == ByStaff)
            {
                try
                {
                    if (EntitiesTo.SelectedItems == null)
                    {
                        MessageFunctions.Error("Error setting default Entity: no Entity selected.", null);
                    }
                    else if (EntitiesTo.SelectedItems.Count > 1)
                    {
                        MessageFunctions.InvalidMessage("Cannot set default Entity. Please ensure only one Entity is selected.", "Multiple Entities selected");
                    }
                    else
                    {
                        EntityProxy thisRecord = (EntityProxy)EntitiesTo.SelectedItem;
                        if (thisRecord.Default == false)
                        {
                            int  entityID = thisRecord.ID;
                            bool success  = StaffFunctions.ChangeDefault(entityID, selectedStaffID);
                            if (success)
                            {
                                refreshEntitySummaries(false);
                                CommitButton.IsEnabled = true;
                            }
                        }
                    }
                }
                catch (Exception generalException)
                {
                    MessageFunctions.Error("Error setting default Entity", generalException);
                }
            }
            else
            {
                try
                {
                    if (StaffTo.SelectedItems != null)
                    {
                        List <StaffProxySmall> defaultList = new List <StaffProxySmall>();
                        foreach (var selectedRow in StaffTo.SelectedItems)
                        {
                            defaultList.Add((StaffProxySmall)selectedRow);
                        }

                        bool success = StaffFunctions.MakeDefault(defaultList, selectedEntity);
                        if (success)
                        {
                            refreshStaffSummaries(false);
                            CommitButton.IsEnabled = true;
                        }
                    }
                    else
                    {
                        MessageFunctions.Error("Error setting default Entity: no staff selected.", null);
                    }
                }
                catch (Exception generalException)
                {
                    MessageFunctions.Error("Error setting default Entity", generalException);
                }
            }
        }
예제 #19
0
        public static bool SingleSignon(string userID)
        {
            Staff  thisUser    = null;
            string newPassword = "";

            try
            {
                // Log in as the administration user to find the user
                ProjectTileSqlDatabase defaultPtDb = SqlServerConnection.DefaultPtDbConnection();
                using (defaultPtDb)
                {
                    thisUser = defaultPtDb.Staff.FirstOrDefault(s => s.UserID == userID);
                }
                if (thisUser == null)
                {
                    MessageFunctions.InvalidMessage("No matching user found with UserID " + userID + ".", "Invalid UserID");
                    return(false);
                }
                else if (thisUser.SingleSignon && domainUser == thisUser.OSUser)
                {
                    char[] validChars = Enumerable.Range('A', 26)
                                        .Concat(Enumerable.Range('a', 26))
                                        .Concat(Enumerable.Range('0', 10))
                                        .Select(i => (char)i)
                                        .ToArray();

                    byte[] randomNumber             = new byte[64 + 1];
                    RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
                    using (crypto)
                    {
                        crypto.GetBytes(randomNumber);
                        int length = 32 + (int)(32 * (randomNumber[0] / (double)byte.MaxValue));
                        newPassword = new string(randomNumber
                                                 .Skip(1)
                                                 .Take(length)
                                                 .Select(b => (int)((validChars.Length - 1) * (b / (double)byte.MaxValue)))
                                                 .Select(i => validChars[i])
                                                 .ToArray()
                                                 );
                        newPassword = newPassword.Substring(0, Math.Min(newPassword.Length, 20));
                        crypto.Dispose();
                    }
                    AttemptLogin(userID, newPassword); // AttemptLogin will now handle the password change if needed
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (SqlException sqlException)
            {
                MessageFunctions.Error("Error accessing the database", sqlException);
                return(false);
            }
            catch (Exception generalException)
            {
                MessageFunctions.Error("Error finding matching staff member", generalException);
                return(false);
            }
        }
예제 #20
0
        public static bool ChangeLoginDetails(int staffID, string userID, string newPassword, string confirmPassword)
        {
            bool passwordChange = (newPassword != "");
            bool userIDChange   = false;

            if (userID == "")
            {
                MessageFunctions.Error("UserID has not been passed to this function.", null); // UserID is required to check complexity so that userID cannot equal password
                return(false);
            }

            if (passwordChange && newPassword != confirmPassword)
            {
                MessageFunctions.InvalidMessage("New password does not match confirmation. Please check both fields and try again.", "Password Mismatch");
                return(false);
            }
            else if (passwordChange && !PasswordComplexityOK(userID, newPassword))
            {
                return(false);
            }
            else
            {
                try
                {
                    // Log in as the administration user to allow the change to be made
                    ProjectTileSqlDatabase defaultPtDb = SqlServerConnection.DefaultPtDbConnection();
                    using (defaultPtDb)
                    {
                        try
                        {
                            Staff thisUser = defaultPtDb.Staff.FirstOrDefault(s => s.ID == staffID);
                            if (thisUser == null)
                            {
                                MessageFunctions.Error("Error amending login details in the database: user with ID " + staffID.ToString() + " not found.", null);
                                return(false);
                            }

                            if (passwordChange)
                            {
                                thisUser.Passwd = newPassword;
                            }
                            if (thisUser.UserID == null || thisUser.UserID != userID)
                            {
                                Staff checkUserID = defaultPtDb.Staff.FirstOrDefault(s => s.UserID == userID && s.ID != staffID);
                                if (checkUserID != null)
                                {
                                    MessageFunctions.InvalidMessage("A different staff member with UserID '" + userID +
                                                                    "' already exists. Please try a different one.", "Duplicate UserID");
                                    return(false);
                                }

                                userIDChange    = true;
                                thisUser.UserID = userID;
                            }
                            defaultPtDb.SaveChanges();

                            // Now amend any history records, to show that the user effectively made this change
                            DateTime timeFrom      = System.DateTime.Now.AddMinutes(-5);
                            int[]    auditEntryIDs = defaultPtDb.AuditEntries
                                                     .Where(ae => ae.TableName == "Staff" &&
                                                            ae.ChangeTime >= timeFrom &&
                                                            ae.ActionType == "Updated" &&
                                                            ae.PrimaryValue == staffID.ToString() &&
                                                            ae.UserName.Substring(0, 5) != DbUserPrefix &&
                                                            ((passwordChange && ae.ChangeColumn == "PasswordHash") || (userIDChange && ae.ChangeColumn == "UserID"))
                                                            )
                                                     .OrderByDescending(ae => ae.ChangeTime)
                                                     .Select(ae => (int)ae.ID)
                                                     .ToArray();

                            foreach (int entry in auditEntryIDs)
                            {
                                AuditEntries lastAuditEntry = defaultPtDb.AuditEntries.Find(entry);
                                lastAuditEntry.UserName = DbUserPrefix + MyUserID;
                                defaultPtDb.SaveChanges();
                            }

                            if (staffID == MyStaffID)
                            {
                                string databaseLogin            = DbUserPrefix + userID;
                                ProjectTileSqlDatabase userPtDb = SqlServerConnection.UserPtDbConnection(databaseLogin, newPassword); // Log in again so that future database calls have the new password
                            }

                            return(true);
                        }
                        catch (SqlException sqlException)
                        {
                            MessageFunctions.Error("Error amending login details in the database", sqlException);
                            return(false);
                        }
                        catch (Exception generalException)
                        {
                            MessageFunctions.Error("Error amending login details", generalException);
                            return(false);
                        }
                    }
                }
                catch (SqlException sqlException)
                {
                    MessageFunctions.Error("Error accessing the database", sqlException);
                    return(false);
                }
                catch (Exception generalException)
                {
                    MessageFunctions.Error("Error checking existing login", generalException);
                    return(false);
                }
            }
        }