예제 #1
0
        public bool Authenticate(string username, string password)
        {
            _userContext = _clientDataAccess.LogIn(username, ClassLib.DataStructures.HashClass.CreateFirstHash(password, username));

            if (_userContext != null)
            {
                const int timeout = 60;
                var ticket = new FormsAuthenticationTicket(
                    1,
                    username,
                    DateTime.Now,
                    DateTime.Now.AddMinutes(timeout),
                    true,
                    _userContext.Id.ToString()
                    );
                var encrypted = FormsAuthentication.Encrypt(ticket);
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);

                HttpContext.Current.Response.Cookies.Add(cookie);

                _authenticationCookie = HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);

                return true;
            }

            return false;
        }
예제 #2
0
        public bool EditWaiter(int managerId, UserContext waiterToEdit)
        {
            if (!CheckHasUserRole(managerId, UserRole.Manager))
                throw new SecurityException(String.Format("User id = {0} is not logged in or is no manager", managerId));

            if (waiterToEdit == null)
                throw new ArgumentNullException("waiterToEdit");

            if (waiterToEdit.Role != UserRole.Waiter)
                throw new ArgumentException(String.Format("User id = {0} is no waiter.", waiterToEdit.Id));

            using(var db = new DataAccessProvider())
            {
                UserContextEntity editedWaiterContext = db.Users.Find(waiterToEdit.Id);
                if (editedWaiterContext == null || editedWaiterContext.IsDeleted)
                    return false;

                //db.Entry(editedWaiterContext).State = System.Data.Entity.EntityState.Detached;
                //db.Users.Attach(waiterToEdit);
                //db.Entry(waiterToEdit).State = System.Data.Entity.EntityState.Modified;

                editedWaiterContext.CopyData(waiterToEdit);
                db.Entry(editedWaiterContext).State = EntityState.Modified;
                db.SaveChanges();
                return true;
            }
        }
예제 #3
0
        /// <summary>
        /// Metoda logująca użytkownika systemu.
        /// </summary>
        /// <param name="login">Login użytkownika</param>
        /// <param name="password">Hasło użytkownika</param>
        /// <remarks>Aby poprawnie przeprowadzić logowanie, haslo podane w tej metodzie musi być w postaci pierwszego hasza (haszowane klasą <href>HashClass</href>)</remarks>
        /// <returns></returns>
        UserContext IBaseDataAccessWCFService.LogIn(string login, string password)
        {
            if (String.IsNullOrEmpty(login))
                throw new ArgumentNullException("login");
            if (String.IsNullOrEmpty(password))
                throw new ArgumentNullException("password");

            CheckAreClientsAvailable();
            CheckAreWaitersAvailable();

            UserContextEntity userContextEntity = null;

            using (var db = new DataAccessProvider())
            {
                userContextEntity = db.Users.FirstOrDefault(u => u.Login.Equals(login));
                if (userContextEntity == null)
                    return null;

                var userPassword = db.Passwords.FirstOrDefault(pw => pw.UserId == userContextEntity.Id);
                if (userPassword == null)
                    return null;

                if (String.IsNullOrEmpty(userPassword.Hash))
                    return null;

                if(!HashClass.ValidatePassword(password, userPassword.Hash))
                    return null;
            }

            if (!CheckIsUserLoggedIn(userContextEntity.Id))
            {
                lock(loggedInUsersLockObject)
                    loggedInUsers.Add(userContextEntity);
            }

            UserContext userContext = new UserContext(userContextEntity);

            if (userContextEntity.Role == UserRole.Client)
                lock(clientRegistrationRecordsLockObject)
                    clientRegistrationRecords.Add(new ClientRegistrationRecord(userContextEntity.Id,
                        OperationContext.Current.GetCallbackChannel<IClientDataAccessCallbackWCFService>()));
            else if (userContextEntity.Role == UserRole.Waiter)
            {
                lock(waiterOrderDictionary)
                    waiterOrderDictionary.Add(
                        new WaiterRegistrationRecord(userContextEntity.Id,
                            OperationContext.Current.GetCallbackChannel<IWaiterDataAccessCallbackWCFService>()), null);

                Task.Run(() => TryAssignAwaitingOrder(userContext));
            }

            logger.Write(String.Format("User {0}({1}) logged in.", userContextEntity.Login, userContextEntity.Role), LoggingCategory.Information);
            OperationContext.Current.Channel.Faulted += Channel_Faulted;

            return userContext;
        }
예제 #4
0
        /// <summary>
        /// Przyporządkowuje zadanie kelnerom
        /// </summary>
        /// <param name="orderToAssign">Zadanie do przyporządkowania</param>
        /// <param name="waiterToAssign">Jeżeli nie jest nullem, to metoda spróbuje zadanie przyporządkować (tylko) temu kelnerowi</param>
        private void AssignOrder(Order orderToAssign, UserContext waiterToAssign = null)
        {
            if (orderToAssign == null)
                return;

            if (waiterToAssign != null && waiterToAssign.Role != UserRole.Waiter)
            {
                string errorMessage = String.Format("Trying to assign order to non-waiter, id={0}", waiterToAssign.Id);
                logger.Write(errorMessage, LoggingCategory.Warning);
                throw new ArgumentException(errorMessage, "waiterToAssign");
            }

            bool foundWaiter = false;

            ClientRegistrationRecord clientRegistrationRecord = null;

            lock(clientRegistrationRecordsLockObject)
                clientRegistrationRecord = clientRegistrationRecords.FirstOrDefault(r => r.ClientId == orderToAssign.Client.Id);

            CheckAreWaitersAvailable();

            lock (waiterOrderDictionary)
            {
                if (waiterToAssign != null)
                {
                    var waiterRegistrationRecord =
                        waiterOrderDictionary.Keys.FirstOrDefault(r => r.WaiterId == waiterToAssign.Id);
                    if(waiterRegistrationRecord != null && waiterOrderDictionary[waiterRegistrationRecord] == null)
                        if (waiterRegistrationRecord.Callback.AcceptNewOrder(orderToAssign))
                        {
                            foundWaiter = true;
                            AcceptOrderInternal(waiterRegistrationRecord, clientRegistrationRecord, orderToAssign);
                        }
                }
                else
                {
                    foreach (var registrationRecord in waiterOrderDictionary.Keys)
                    {
                        if (waiterOrderDictionary[registrationRecord] == null)
                        {
                            if (registrationRecord.Callback.AcceptNewOrder(orderToAssign))
                            {
                                foundWaiter = true;
                                AcceptOrderInternal(registrationRecord, clientRegistrationRecord, orderToAssign);
                                break;
                            }
                        }
                    }

                }
            }

            if (!foundWaiter)
            {
                //nie udało się przydzielić zamówienia żadnemu kelnerowi
                lock(awaitingOrderCollectionLockObject)
                    awaitingOrderCollection.Enqueue(orderToAssign);
                logger.Write(String.Format("Order {0} was added to awaitingOrderCollection", orderToAssign.Id), LoggingCategory.Information);
                //powiadamiamy klienta o braku dostępnych kelnerów
                if(clientRegistrationRecord != null)
                    Task.Run(() => clientRegistrationRecord.Callback.NotifyOrderOnHold(orderToAssign.Id));
            }
        }
예제 #5
0
        /// <summary>
        /// Próbuję przypasować kelnera do oczekującego zadania
        /// </summary>
        /// <param name="waiterToAssign">Kelner, któremu ma zostać przypisane zadanie.</param>
        /// <remarks>Jeżeli <href>waiterToAssign</href> jest równe null, to metoda leci po wszystkich wolnych kelnerach</remarks>
        private void TryAssignAwaitingOrder(UserContext waiterToAssign = null)
        {
            Order order = null;
            lock (awaitingOrderCollectionLockObject)
            {
                if (awaitingOrderCollection.Count == 0)
                    return;

                order = awaitingOrderCollection.Dequeue();
            }
            if(order != null)
                AssignOrder(order, waiterToAssign);
        }
예제 #6
0
        public void TestCleanup()
        {
            if (order1 != null)
                dataWipe.WipeOrder(order1.Id);

            if (managerContext1 != null)
                dataWipe.WipeUser(managerContext1.Id);

            if (clientContext1 != null)
                dataWipe.WipeUser(clientContext1.Id);

            if (waiterContext1 != null)
                dataWipe.WipeUser(waiterContext1.Id);

            if (waiterContext2 != null)
                dataWipe.WipeUser(waiterContext2.Id);

            if (menuItem1 != null)
                dataWipe.WipeMenuItem(menuItem1.Id);

            if (menuItem2 != null)
                dataWipe.WipeMenuItem(menuItem2.Id);

            if (menuItem3 != null)
                dataWipe.WipeMenuItem(menuItem3.Id);

            if (category1 != null)
                dataWipe.WipeMenuItemCategory(category1.Id);

            if (table1 != null)
                dataWipe.WipeTable(table1.Id);

            order1 = null;
            managerContext1 = null;
            waiterContext1 = null;
            waiterContext2 = null;
            clientContext1 = null;
            category1 = null;
            menuItem1 = null;
            menuItem2 = null;
            menuItem3 = null;
            table1 = null;
        }
예제 #7
0
        public void AddNewClientTest()
        {
            clientContext1 = clientDataAccess.AddClient(ClientFirstName1, ClientLastName1, ClientLogin1,
                HashClass.CreateFirstHash(ClientPassword1, ClientLogin1));

            Assert.IsNotNull(clientContext1);
            Assert.AreNotEqual(clientContext1.Id, 0);
            Assert.AreEqual(clientContext1.FirstName, ClientFirstName1);
            Assert.AreEqual(clientContext1.LastName, ClientLastName1);
            Assert.AreEqual(clientContext1.Role, UserRole.Client);
        }
예제 #8
0
        public void AddNewWaiterTest()
        {
            ManagerLogInTest();

            waiterContext1 = managerDataAccess.AddWaiter(managerContext1.Id, waiterFirstName1, waiterLastName1,
                waiterLogin1, waiterPassword1);

            Assert.IsNotNull(waiterContext1);
            Assert.AreNotEqual(waiterContext1.Id, 0);
            Assert.AreEqual(waiterContext1.FirstName, waiterFirstName1);
            Assert.AreEqual(waiterContext1.LastName, waiterLastName1);
            Assert.AreEqual(waiterContext1.Login, waiterLogin1);

            ManagerLogOutTest();
        }
예제 #9
0
        public void AddRepeatedWaiterTest()
        {
            if (waiterContext1 == null)
                AddNewWaiterTest();
            ManagerLogInTest();

            try
            {
                //Próba dodania drugiego kelnera o już istniejącym loginem
                waiterContext2 = managerDataAccess.AddWaiter(managerContext1.Id, waiterFirstName2, waiterLastName2,
                    waiterLogin1, waiterPassword2);
                Assert.Fail("waiter2Context should not be created!");
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is ArgumentException);
            }

            ManagerLogOutTest();
        }
예제 #10
0
 public void AddNewManagerTest()
 {
     managerContext1 = managerDataAccess.AddManager(ManagerFirstName1, ManagerLastName1, ManagerLogin1,
         HashClass.CreateFirstHash(ManagerPassword1, ManagerLogin1));
     Assert.IsNotNull(managerContext1);
     Assert.AreNotEqual(managerContext1.Id, 0);
     Assert.AreEqual(managerContext1.FirstName, ManagerFirstName1);
     Assert.AreEqual(managerContext1.LastName, ManagerLastName1);
     Assert.AreEqual(managerContext1.Role, UserRole.Manager);
 }
        /// <summary>
        /// Konstruktor kopiujący
        /// </summary>
        /// <param name="userContextToCopy">Kontekst uzytkownika do skopiowania</param>
        public UserContext(UserContext userContextToCopy)
            : base(userContextToCopy)
        {
            if(userContextToCopy == null)
                throw new ArgumentNullException("userContextToCopy");

            FirstName = userContextToCopy.FirstName;
            LastName = userContextToCopy.LastName;
            Login = userContextToCopy.Login;
            Role = userContextToCopy.Role;
        }
        /// <summary>
        /// Konstruktor kopiujący
        /// </summary>
        /// <param name="orderToCopy">Zamówienie do skopiowania</param>
        public Order(Order orderToCopy)
            : base(orderToCopy)
        {
            if(orderToCopy == null)
                throw new ArgumentNullException("orderToCopy");

            Client = new UserContext(orderToCopy.Client);

            if (orderToCopy.Waiter != null)
                Waiter = new UserContext(orderToCopy.Waiter);

            if (orderToCopy.Table != null)
                Table = new Table(orderToCopy.Table);

            if (orderToCopy.MenuItems != null)
            {
                MenuItems = new List<MenuItemQuantity>();
                foreach (var menuItemQuant in orderToCopy.MenuItems)
                    MenuItems.Add(new MenuItemQuantity(menuItemQuant));
            }

            State = orderToCopy.State;
            PlacingDate = orderToCopy.PlacingDate;
            ClosingDate = orderToCopy.ClosingDate;
        }
        public Order(OrderEntity order)
            : base(order)
        {
            Client = new UserContext(order.Client);

            if (order.Waiter != null)
                Waiter = new UserContext(order.Waiter);

            if (order.Table != null)
                Table = new Table(order.Table);

            if (order.MenuItems != null)
            {
                MenuItems = new List<MenuItemQuantity>();
                foreach (var menuItemQuant in order.MenuItems)
                    MenuItems.Add(new MenuItemQuantity(menuItemQuant));
            }

            State = order.State;
            PlacingDate = order.PlacingDate;
            ClosingDate = order.ClosingDate;
        }
예제 #14
0
 public bool EditWaiter(int managerId, UserContext waiterToEdit)
 {
     return EditWaiterWeb(managerId, waiterToEdit);
 }