예제 #1
0
        public void login(String username, String password, Session session)
        {
            String         encrypted = DBSubscribedUser.getInstance().encryptPassword(password);
            SubscribedUser sub       = DBSubscribedUser.getInstance().getSubscribedUser(username);

            if (sub == null)
            {
                throw new LoginException("Username does not exist");
            }
            SubscribedUser loggedIn = DBSubscribedUser.getInstance().getloggedInUser(username);

            if (loggedIn != null)
            {
                throw new LoginException("Username already logged in");
            }
            if (!Equals(sub.getPassword(), encrypted))
            {
                throw new LoginException("Incorrect password");
            }
            session.setSubscribedUser(sub);
            if (Equals(username, "admin"))
            {
                session.setState(new Admin());
            }
            else
            {
                session.setState(new LoggedIn());
            }
            session.setShoppingBasket(sub.getShoppingBasket());
            DBSubscribedUser.getInstance().login(sub);
        }
예제 #2
0
        public void updateShoppingBasket()
        {
            try
            {
                //SqlConnection connection = Connector.getInstance().getSQLConnection();
                lock (connection)
                {
                    connection.Open();
                    foreach (KeyValuePair <string, SubscribedUser> pair in users)
                    {
                        string         username = pair.Key;
                        SubscribedUser su       = pair.Value;

                        string         sql = "SELECT * FROM BasketCart WHERE username=@username;";
                        var            c2  = connection.Query <BasketCartEntry>(sql, new { username = username });
                        ShoppingBasket sb  = su.getShoppingBasket();
                        if (Enumerable.Count(c2) > 0)
                        {
                            for (int i = 0; i < Enumerable.Count(c2); i++)
                            {
                                BasketCartEntry bc      = c2.ElementAt(i);
                                int             storeID = bc.getStoreID();
                                sql = "SELECT * FROM CartProduct WHERE storeID=@storeID AND username=@username;";
                                var c3 = connection.Query <CartProductEntry>(sql, new { storeID, username });

                                for (int j = 0; j < Enumerable.Count(c3); j++)
                                {
                                    CartProductEntry cp = c3.ElementAt(j);
                                    int     productID   = cp.getProductID();
                                    int     amount      = cp.getAmount();
                                    Product p           = DBProduct.getInstance().getProductByID(productID);
                                    sb.addToCartNoDBUpdate(p, amount, storeID);
                                }
                            }
                        }
                        List <StoreRole> storeRoles = su.getStoreRoles();

                        foreach (StoreRole sr in DBStore.getInstance().getAllStoreRoles(username))
                        {
                            if (sr.getUser().getUsername() == username)
                            {
                                storeRoles.Add(sr);
                            }
                        }
                    }
                    connection.Close();
                }
            }
            catch (Exception e)
            {
                connection.Close();
            }
        }
예제 #3
0
        public void loginAfterRegister(String username, String password, Session session)
        {
            String         encrypted = password;
            SubscribedUser sub       = DBSubscribedUser.getInstance().getSubscribedUser(username);

            if (sub == null)
            {
                throw new LoginException("Error: Username does not exist");
            }
            DBSubscribedUser.getInstance().updateStoreRole(sub);
            //SubscribedUser loggedIn = DBSubscribedUser.getInstance().getloggedInUser(username);
            //if( loggedIn != null)
            //    throw new LoginException("Error: Username already logged in");
            if (!Equals(sub.getPassword(), encrypted))
            {
                throw new LoginException("Error: Incorrect password");
            }
            ////////////erase

            // Store st = new Store("bb", "cc");
            //DBStore.getInstance().addStore(st);



            ////////erase
            session.setSubscribedUser(sub);

            if (Equals(username, "u1"))
            {
                session.setState(new Admin());
            }
            else
            {
                session.setState(new LoggedIn());
            }
            session.setShoppingBasket(new ShoppingBasket(sub.getUsername()));
            session.setShoppingBasket(sub.getShoppingBasket());
            DBSubscribedUser.getInstance().login(sub);
        }