public StoreOwner(SubscribedUser appointedBy, SubscribedUser user, Store store) { this.appointedBy = appointedBy; this.user = user; this.store = store; appointedByMe = new List <StoreRole>(); }
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(); } }
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); }
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(); } } }
public Session() { subscribedUser = null; userState = new Guest(); shoppingBasket = new ShoppingBasket(); DBSession.getInstance().addSession(this); }
public void cleanDB() { users = new Dictionary <string, SubscribedUser>(); loggedInUser = new Dictionary <string, SubscribedUser>(); SubscribedUser admin = new SubscribedUser("admin", encryptPassword("1234"), new ShoppingBasket()); register(admin); }
public StoreManager(SubscribedUser appointedBy, Store store, SubscribedUser user, Permissions permissions) { this.appointedBy = appointedBy; this.store = store; this.userName = user; this.permissions = permissions; }
private void init() { SubscribedUser admin = new SubscribedUser("Admin", "1234", new ShoppingBasket()); DBSubscribedUser.getInstance().register(admin); PaymentService.getInstance().connectToSystem(); DeliveryService.getInstance().connectToSystem(); ConsistencySystem.getInstance().connectToSystem(); }
public void updateStoreRole(SubscribedUser user) { string username = user.getUsername(); foreach (StoreRole sr in DBStore.getInstance().getAllStoreRoles(username)) { user.addStoreRole(sr); } }
public void removeStoreRole(Store store, SubscribedUser user) { StoreRole sr = getStoreRole(store, user); if (sr != null) { storeRole.Remove(sr); } }
public void closeStoreRoles(Store store) { List <StoreRole> roles = store.getRoles(); foreach (StoreRole role in roles) { SubscribedUser sub = role.getUser(); sub.removeStoreRole(role); } }
public void removeUser(String user) { if (Equals(user, "u1")) { throw new UserException("admin cannot be removed"); } SubscribedUser subscribedUser = DBSubscribedUser.getInstance().getSubscribedUser(user); if (subscribedUser == null) { throw new UserException("user to be removed does not exist"); } try { Session session = dbSession.getSessionOfSubscribedUser(subscribedUser); if (session != null) { if (session.getState() is LoggedIn) { session.logout(); session.setSubscribedUser(null); } } } catch (DoesntExistException) { } LinkedList <StoreRole> toDelete = new LinkedList <StoreRole>(); LinkedList <Store> toDeleteStore = new LinkedList <Store>(); foreach (StoreRole role in subscribedUser.getStoreRoles()) { role.removeAllAppointedBy(); Store store = role.getStore(); SubscribedUser appointedBySubscribedUser = role.getAppointedBy(); toDelete.AddFirst(role); if (appointedBySubscribedUser != null) { StoreRole appointedByStoreRole = store.getStoreRole(role.getAppointedBy()); store.removeStoreRole(role); appointedByStoreRole.removeRoleAppointedByMe(role); } if (role is StoreOwner && role.getStore().getNumberOfOwners() == 1) { closeStore(role.getStore()); } //DBStore.getInstance().removeStoreRole(role); } foreach (StoreRole sr in toDelete) { DBStore.getInstance().removeStoreRole(sr); subscribedUser.removeStoreRole(sr); sr.getStore().removeStoreRole(sr); } dbSubscribedUser.remove(subscribedUser); }
public Session getSessionOfSubscribedUser(SubscribedUser sub) { foreach (Session s in sessions) { if (s.getSubscribedUser().Equals(sub)) { return(s); } } return(null); }
public StoreRole getStoreRole(SubscribedUser user) { foreach (StoreRole sr in roles) { if (sr.getUser() == user) { return(sr); } } return(null); }
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(); } }
//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); } } }
public void register(string username, string password, Session session) { String encrypted = DBSubscribedUser.getInstance().encryptPassword(password); SubscribedUser s = dbSubscribedUser.getSubscribedUser(username); if (s != null) { throw new RegisterException("username already exists"); } SubscribedUser sub = new SubscribedUser(username, encrypted, session.getShoppingBasket()); session.setSubscribedUser(sub); DBSubscribedUser.getInstance().register(sub); }
public StoreRole getStoreRole(Store store, SubscribedUser user) { foreach (StoreRole st in storeRole) { Store s = st.getStore(); SubscribedUser u = st.getUser(); if (store.Equals(s) && user.Equals(u)) { return(st); } } return(null); }
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); }
public void register(string username, string password, Session session) { SubscribedUser s = dbSubscribedUser.getSubscribedUser(username); if (s != null) { throw new RegisterException("Error: Username already exists"); } session.getShoppingBasket().setUsername(username); SubscribedUser sub = new SubscribedUser(username, password, session.getShoppingBasket()); //session.setSubscribedUser(sub); DBSubscribedUser.getInstance().register(sub); }
public Session getSessionOfSubscribedUser(SubscribedUser sub) { foreach (KeyValuePair <int, Session> s in sessions) { if (s.Value != null) { if (s.Value.getSubscribedUser() != null && s.Value.getSubscribedUser().Equals(sub)) { return(s.Value); } } } throw new DoesntExistException("session doesnt exist"); }
public Store createStore(String storeName, String description, SubscribedUser sub) { if (storeName == "") { throw new IllegalNameException(); } Store store = new Store(storeName, description); StoreOwner owner = new StoreOwner(null, sub, store); store.addStoreRole(owner); sub.addStoreRole(owner); DBStore.getInstance().addStore(store); DBStore.getInstance().addStoreRole(owner); return(store); }
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); }
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); }
//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); }
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); }
public void removeUser(String user) { if (Equals(user, "admin")) { throw new UserException("admin cannot be removed"); } SubscribedUser subscribedUser = DBSubscribedUser.getInstance().getSubscribedUser(user); if (subscribedUser == null) { throw new UserException("user to be removed does not exist"); } Session session = dbSession.getSessionOfSubscribedUser(subscribedUser); if (session != null) { if (session.getState() is LoggedIn) { session.logout(); } } foreach (StoreRole role in subscribedUser.getStoreRoles()) { role.removeAllAppointedBy(); Store store = role.getStore(); SubscribedUser appointedBySubscribedUser = role.getAppointedBy(); if (appointedBySubscribedUser != null) { StoreRole appointedByStoreRole = store.getStoreRole(role.getAppointedBy()); store.removeStoreRole(appointedByStoreRole); appointedByStoreRole.removeRoleAppointedByMe(role); } if (role is StoreOwner && role.getStore().getNumberOfOwners() == 0) { closeStore(role.getStore()); } DBStore.getInstance().removeStoreRole(role); } session.setSubscribedUser(null); dbSubscribedUser.remove(subscribedUser); }
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(); } }
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); }
public SubscribedUser getSubscribedUserForInitStore(string username) { if (users.ContainsKey(username)) { return(users[username]); } try { lock (connection) { connection.Open(); //SqlConnection connection = Connector.getInstance().getSQLConnection(); var c1 = connection.Query <RegisterEntry>("SELECT username, password FROM [dbo].[Register] WHERE username=@username ", new { username = username }); connection.Close(); if (Enumerable.Count(c1) == 1) { RegisterEntry re = c1.ElementAt(0); string password = re.getPassword(); ShoppingBasket sb = new ShoppingBasket(username); SubscribedUser su = new SubscribedUser(username, password, sb); users.Add(username, su); return(su); } else { return(null); } } } catch (Exception e) { connection.Close(); return(null); } }