Esempio n. 1
0
        /// <summary>
        /// Command to add a user to the database
        /// </summary>
        /// <returns></returns>
        private async Task AddUserCommand(object password)
        {
            // Make sure all warnings are resetted
            IsWrongInput    = false;
            PasswordIsWrong = false;

            // Check if all fields are filled
            if (PNumber == null || FirstName == null || LastName == null || CurrentRole.roleID == 0)
            {
                ErrorText    = "Alla fält är inte ifyllda korrekt";
                IsWrongInput = true;

                return;
            }

            // Check if the password input is correct while everything else is fine
            if ((password as IHavePassword).SecurePassword.Length < 4)
            {
                // Set the flag
                PasswordIsWrong = true;
                return;
            }

            // Try to add a user
            if (!await CreateUser(PNumber, FirstName, LastName, CurrentRole.roleID, (password as IHavePassword).SecurePassword))
            {
                // If the creation of a user fails, tell the user
                ErrorText = "Något gick fel, \nkontrollera så att användaren inte finns!";

                // Display the text
                IsWrongInput = true;

                // And disable the button
                return;
            }

            // Close to reset visable property
            IoC.CreateInstance <ApplicationViewModel>().ClosePopUp();

            // Opens up to successfully added
            IoC.CreateInstance <ApplicationViewModel>().OpenPopUp(PopUpContents.Success);

            await Task.Delay(700);

            // Close popup
            IoC.CreateInstance <ApplicationViewModel>().ClosePopUp();

            // Fill the new data
            IoC.CreateInstance <TableControlViewModel>().LoadItems();

            // Closes the pop up
            IoC.CreateInstance <ApplicationViewModel>().ClosePopUp();

            // Resetting the input properties
            ViewModelHelpers.ResetInputProperties <AddUserControlViewModel>(nameof(PNumber), nameof(FirstName), nameof(LastName), nameof(CurrentRole));
        }
Esempio n. 2
0
        /// <summary>
        /// Command to close the pop up content
        /// </summary>
        /// <returns></returns>
        private async Task CloseAddUserControlCommand()
        {
            // Closing the pop up
            IoC.CreateInstance <ApplicationViewModel>().ClosePopUp();

            // Reset Properties
            ViewModelHelpers.ResetInputProperties <AddUserControlViewModel>(nameof(PNumber), nameof(FirstName), nameof(LastName), nameof(CurrentRole), nameof(ErrorText));
            IsWrongInput        = false;
            PasswordIsWrong     = false;
            ComboBoxPlaceholder = "Roll";
            CurrentRole         = new RoleViewModel();

            await Task.Delay(1);
        }
Esempio n. 3
0
        /// <summary>
        /// Logging in the user if credentials are correct
        /// </summary>
        /// <returns></returns>
        public async Task LoginAsUserCommandAsync(object password)
        {
            // Hide the textbox in the control
            ShowLoginFailedText = false;

            // Checking if the login is already running, used to avoid overload
            if (UserLoginIsRunning)
            {
                return;
            }

            // Indicate that the login is running
            UserLoginIsRunning = true;

            // The actual login trial
            try
            {
                // Temporarily used waiter to simulate animation
                await Task.Delay(500);

                // Check for inputs
                if (PNumber == null || (password as IHavePassword).SecurePassword == null)
                {
                    return;
                }

                // Get a user object from the database
                var loggedInUser = (await LoginHelpers.AttemptLogin(PNumber, (password as IHavePassword).SecurePassword));

                // If no user is returned...
                if (loggedInUser == null)
                {
                    // Show the textbox in the control
                    ShowLoginFailedText = true;
                    return;
                }

                var NewUser = loggedInUser.ToModel <IUser, UserViewModel>();

                // Set the logged in user
                IoC.CreateInstance <ApplicationViewModel>().CurrentUser = NewUser;
                IoC.CreateInstance <ApplicationViewModel>().SetCurrentUserRole();

                await CheckNotifications();

                // If the login is made from the start screen we go to the book page
                if (IoC.CreateInstance <ApplicationViewModel>().CurrentPage == ApplicationPages.MainPage)
                {
                    IoC.CreateInstance <ApplicationViewModel>().GoToPage(ApplicationPages.BookPage);
                }

                // Closes the popup if we're not in firstload-mode
                if (!IoC.CreateInstance <ApplicationViewModel>().IsLoading&& IoC.CreateInstance <ApplicationViewModel>().CurrentPage != ApplicationPages.MainPage)
                {
                    IoC.CreateInstance <ApplicationViewModel>().ClosePopUp();
                }

                IoC.CreateInstance <MyProfileControlViewModel>().GetMyLoansAndReservations();

                // Reset input properties
                ViewModelHelpers.ResetInputProperties <UserLoginControlViewModel>(nameof(PNumber));
            }

            finally
            {
                // When the login is done, no matter if it succeeds or not, the flag will be set to false
                UserLoginIsRunning = false;
            }
        }