Beispiel #1
0
        public void register(SubscribedUser user)
        {
            users.Add(user.getUsername(), user);
            string username = user.getUsername();
            string password = user.getPassword();

            try
            {
                lock (connection)
                {
                    connection.Open();
                    //SqlConnection connection = Connector.getInstance().getSQLConnection();
                    var c = connection.Query("SELECT username, password FROM [dbo].[Register] WHERE username=@username ", new { username = username });
                    //connection.Close();
                    if (Enumerable.Count(c) == 0)
                    {
                        string sql = "INSERT INTO [dbo].[Register] (username, password)" +
                                     " VALUES (@username, @password)";
                        connection.Execute(sql, new { username, password });
                    }
                    connection.Close();
                }
            }
            catch (Exception e)
            {
                connection.Close();
                SystemLogger.getErrorLog().Error("Connection error in function register in db subscribed user, user name: " + user.getUsername());
                throw new ConnectionException();
            }
        }
Beispiel #2
0
        public void login(SubscribedUser user)
        {
            loggedInUser[user.getUsername()] = user;
            string username = user.getUsername();
            string password = user.getPassword();

            try
            {
                lock (connection)
                {
                    connection.Open();
                    //SqlConnection connection = Connector.getInstance().getSQLConnection();
                    var c = connection.Query("SELECT username, password FROM [dbo].[Register] WHERE username=@username ", new { username = username });
                    //connection.Close();
                    if (Enumerable.Count(c) == 0)
                    {
                        throw new LoginException("Username " + user.getUsername() + "does not exist");
                    }
                    connection.Close();
                }
            }
            catch (Exception e)
            {
                if (e is ClientException)
                {
                    throw e;
                }
                else
                {
                    connection.Close();
                    SystemLogger.getErrorLog().Error("Connection error in function login in db subscribed user, user name: " + user.getUsername());
                    throw new ConnectionException();
                }
            }
        }
Beispiel #3
0
        public void updateStoreRole(SubscribedUser user)
        {
            string username = user.getUsername();

            foreach (StoreRole sr in DBStore.getInstance().getAllStoreRoles(username))
            {
                user.addStoreRole(sr);
            }
        }
Beispiel #4
0
        public void remove(SubscribedUser role)
        {
            StoreRole sr = role.getStoreRole(store);

            if (sr == null)
            {
                throw new RoleException("user " + role.getUsername() +
                                        " doesn't have a role in store "
                                        + store.getStoreName());
            }
            if (sr.getAppointedBy() != this.userName)
            {
                throw new RoleException("Error: User " + userName.getUsername() +
                                        " didn't appoint " +
                                        role.getUsername());
            }
            DBStore.getInstance().removeStoreRole(sr);
            role.removeStoreRole(sr);
            store.removeStoreRole(sr);
        }
Beispiel #5
0
 public void addProduct(Product product)
 {
     if (!permissions.editProduct())
     {
         throw new PermissionsException("Error:" + userName.getUsername() +
                                        " has no permissions to edit products in store " +
                                        store.getStoreName());
     }
     store.addProduct(product);
     DBProduct.getInstance().addProduct(product);
 }
Beispiel #6
0
        internal LinkedList <int> getSessionOfUserName(string username)
        {
            LinkedList <int> result = new LinkedList <int>();

            foreach (KeyValuePair <int, Session> s in sessions)
            {
                SubscribedUser su = s.Value.getSubscribedUser();
                if (su != null && su.getUsername() == username)
                {
                    result.AddFirst(s.Key);
                }
            }
            return(result);
        }
Beispiel #7
0
 //if owner -> close store and remove store role, if manager only removes store role
 public void removeStoreByUser(SubscribedUser user)
 {
     foreach (StoreRole sr in storeRole)
     {
         if ((sr.getUser()).getUsername() == user.getUsername())
         {
             if (sr is StoreOwner)
             {
                 closeStore(sr.getStore());
             }
             storeRole.Remove(sr);
         }
     }
 }
Beispiel #8
0
        public void addOwner(SubscribedUser owner)
        {
            StoreRole newOwner = new StoreOwner(this.user, owner, store);

            if (store.getStoreRole(owner) != null)
            {
                throw new RoleException("user " + owner.getUsername() +
                                        " already have a role in store " +
                                        store.getStoreName());
            }
            store.addStoreRole(newOwner);
            owner.addStoreRole(newOwner);
            appointedByMe.Add(newOwner);
            DBStore.getInstance().addStoreRole(newOwner);
        }
Beispiel #9
0
        //public void removeStoreRole(Store store, SubscribedUser user)
        //{
        //    StoreRole sr = getStoreRole(store, user);
        //    if (sr != null)
        //        storeRole.Remove(sr);

        //}

        public StoreRole getStoreRole(Store store, SubscribedUser user)
        {
            initStoresAndRolesForUserName(user.getUsername());
            foreach (StoreRole st in storeRole)
            {
                Store          s = st.getStore();
                SubscribedUser u = st.getUser();
                if (store.Equals(s) && user.Equals(u))
                {
                    return(st);
                }
            }

            return(null);
        }
Beispiel #10
0
        public void addManager(SubscribedUser manager, Permissions permissions)
        {
            StoreRole newManager = new StoreManager(this.user, store, manager, permissions);

            DBStore.getInstance().addStoreRole(newManager);
            if (store.getStoreRole(manager) != null)
            {
                throw new RoleException("user " + manager.getUsername() +
                                        " already have a role in store " +
                                        store.getStoreName());
            }
            store.addStoreRole(newManager);
            manager.addStoreRole(newManager);
            appointedByMe.Add(newManager);
        }
Beispiel #11
0
        public void remove(SubscribedUser user)
        {
            string username = user.getUsername();

            if (loggedInUser.ContainsKey(username))
            {
                loggedInUser.Remove(username);
            }
            users.Remove(username);
            string sql1 = "DELETE FROM Register WHERE username=@username";
            string sql2 = "DELETE FROM BasketCart WHERE username=@username";
            string sql3 = "DELETE FROM CartProduct WHERE username=@username";
            string sql4 = "DELETE FROM PendingOwners WHERE username=@username";
            string sql5 = "DELETE FROM Contracts WHERE username=@username";

            try
            {
                //SqlConnection connection = Connector.getInstance().getSQLConnection();
                lock (connection)
                {
                    connection.Open();
                    using (var transaction = connection.BeginTransaction())
                    {
                        connection.Execute(sql1, new { username }, transaction);
                        connection.Execute(sql2, new { username }, transaction);
                        connection.Execute(sql3, new { username }, transaction);
                        connection.Execute(sql4, new { username }, transaction);
                        connection.Execute(sql5, new { username }, transaction);
                        transaction.Commit();
                    }
                    connection.Close();
                }
                //connection.Close();
            }
            catch (Exception)
            {
                connection.Close();
                SystemLogger.getErrorLog().Error("Connection error in function remove in db subscribed user while removing " + user.getUsername());
                throw new ConnectionException();
            }
        }
Beispiel #12
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);
        }
Beispiel #13
0
        public void signContract(SubscribedUser pending)
        {
            if (DBStore.getInstance().hasContract(store.getStoreID(), pending.getUsername(), userName.getUsername()))
            {
                throw new AlreadyExistException("You have already signed a contract with " + pending.getUsername());
            }
            int approvedOwners = DBStore.getInstance().getContractNum(store.getStoreID(), pending.getUsername());

            if (approvedOwners == store.getNumberOfOwners() - 1)
            {
                StoreRole newOwner = new StoreOwner(this.userName, pending, store);
                //DBStore.getInstance().signContract(store.getStoreID(), userName.getUsername(), pending.getUsername(),true);
                //DBStore.getInstance().removePendingOwner(store.getStoreID(),pending.getUsername());
                //DBStore.getInstance().addStoreRole(newOwner);
                store.addStoreRoleFromInitOwner(newOwner);
                pending.addStoreRole(newOwner);
                appointedByMe.Add(newOwner);
                DBStore.getInstance().signAndAddOwner(store.getStoreID(), userName.getUsername(), pending.getUsername(), newOwner);
            }
            else
            {
                DBStore.getInstance().signContract(store.getStoreID(), userName.getUsername(), pending.getUsername(), false);
            }
        }
Beispiel #14
0
 public void register(SubscribedUser user)
 {
     users.Add(user.getUsername(), user);
 }
Beispiel #15
0
        //public void removeCouponFromStore(string couponCode)
        //{
        //    store.removeCoupon(couponCode);
        //}

        /* public void addCouponToStore(string couponCode, int percentage, string duration)
         * {
         *  store.addCoupon(couponCode, percentage, duration);
         * }
         */
        //public void addCouponToStore(string couponCode, double percentage, string duration)
        //{
        //    store.addCoupon(couponCode, percentage, duration);
        //}

        public void addPendingOwner(SubscribedUser pending)
        {
            DBStore.getInstance().addPendingOwner(store.getStoreID(), userName.getUsername(), pending.getUsername());
        }
Beispiel #16
0
 public void logout(SubscribedUser sub)
 {
     loggedInUser.Remove(sub.getUsername());
 }
Beispiel #17
0
 public void remove(SubscribedUser user)
 {
     users.Remove(user.getUsername());
 }
Beispiel #18
0
 public void declineContract(SubscribedUser pending)
 {
     DBStore.getInstance().declineContract(store.getStoreID(), pending.getUsername());
     //DBStore.getInstance().removeAllUserContracts(store.getStoreID(), pending.getUsername());
     //DBStore.getInstance().removePendingOwner(store.getStoreID(), pending.getUsername());
 }
Beispiel #19
0
        public void complain(string description, SubscribedUser subscribedUser)
        {
            Complaint complaint = new Complaint(subscribedUser.getUsername(), description);

            dbComplaint.addComplaint(complaint);
        }
Beispiel #20
0
 public void login(SubscribedUser user)
 {
     loggedInUser.Add(user.getUsername(), user);
 }