public void CleanupSessions()
 {
     if (Utils.IsSiteDisposed(this))
     {
         throw new InvalidOperationException("Invalid operation: this site is disposed.");
     }
     using (var context = new AuctionSiteContext(ConnectionString))
     {
         foreach (var iUser in GetUsers())
         {
             var user = iUser as UserBLL;
             if (user?.Session != null)
             {
                 if (!user.Session.IsValid())
                 {
                     var dbUser = context.Users.Find(user.UserID);
                     if (null == dbUser)
                     {
                         throw new InvalidOperationException("Invalid operation: user not found.");
                     }
                     dbUser.Session              = null;
                     dbUser.SessionId            = null;
                     context.Entry(dbUser).State = EntityState.Modified;
                     context.Entry(context.Sessions.Find(user.Session.Id)).State = EntityState.Deleted;
                 }
             }
         }
         context.SaveChanges();
     }
 }
 private SessionBLL GetUserSession(UserBLL user)
 {
     using (var context = new AuctionSiteContext(ConnectionString))
     {
         var session = context.Sessions.Find(Utils.CreateSessionId(this, user));
         if (null == session)
         {
             user.Session = Utils.CreateNewSession(this, user);
             return(user.Session);
         }
         var sessionBLL = new SessionBLL(session, user);
         if (!sessionBLL.IsValid())
         {
             sessionBLL.Logout();
             user.Session = Utils.CreateNewSession(this, user);
         }
         else
         {
             var validUntil = AlarmClock.Now.AddSeconds(SessionExpirationInSeconds);
             session.ValidUntil           = validUntil;
             user.Session.ValidUntil      = validUntil;
             context.Entry(session).State = EntityState.Modified;
             context.SaveChanges();
         }
         return(user.Session);
     }
 }
        public ISession Login(string username, string password)
        {
            if (Utils.IsSiteDisposed(this))
            {
                throw new InvalidOperationException("Invalid operation: this site is disposed.");
            }
            if (null == username)
            {
                throw new ArgumentNullException($"{nameof(username)} cannot be null");
            }
            if (null == password)
            {
                throw new ArgumentNullException($"{nameof(password)} cannot be null");
            }
            if (!Utils.IsValidUsername(username))
            {
                throw new ArgumentException($"{nameof(username)} is not valid.");
            }
            if (!Utils.IsValidPassword(password))
            {
                throw new ArgumentException($"{nameof(password)} is not valid.");
            }

            var user = GetUserByUsername(username);

            if (null == user)
            {
                return(null);
            }

            if (!Utils.ArePasswordsEquals(user.Password, password, Convert.FromBase64String(user.Salt)))
            {
                return(null);
            }

            var userSession = GetUserSession(user);

            //user.Session = userSession;
            using (var context = new AuctionSiteContext(ConnectionString))
            {
                var dbUser = context.Users.Find(user.UserID);
                if (null == dbUser)
                {
                    throw new InvalidOperationException("Invalid operation: user not found.");
                }
                dbUser.SessionId            = userSession.Id;
                context.Entry(dbUser).State = EntityState.Modified;
                context.SaveChanges();
            }

            return(userSession);
        }
Example #4
0
 private static void UpdateAuction(AuctionSiteContext context, Auction auction, ISession session, double offer, double?currentPrice)
 {
     auction.LastBid = offer;
     if (null != session)
     {
         auction.CurrentWinnerId = ((UserBLL)session.User).UserID;
     }
     if (null != currentPrice)
     {
         auction.CurrentPrice = (double)currentPrice;
     }
     context.Entry(auction).State = EntityState.Modified;
     context.SaveChanges();
 }
Example #5
0
        public void Delete()
        {
            if (Utils.IsAuctionDisposed(this))
            {
                throw new InvalidOperationException("Invalid operation: user is disposed.");
            }
            var sellerBLL = (UserBLL)Seller;

            using (var context = new AuctionSiteContext(sellerBLL.Site.ConnectionString))
            {
                var auction = context.Auctions.Find(Id);
                context.Entry(auction).State = EntityState.Deleted;
                context.SaveChanges();
            }
            IsDeleted = true;
        }
 public void Delete()
 {
     if (Utils.IsSiteDisposed(this))
     {
         throw new InvalidOperationException("Invalid operation: this site is disposed.");
     }
     DeleteUsers();
     DeleteAuctions();
     using (var context = new AuctionSiteContext(ConnectionString))
     {
         var site = context.Sites.Find(this.Name);
         context.Entry(site).State = EntityState.Deleted;
         context.SaveChanges();
     }
     IsDeleted = true;
 }
Example #7
0
        public void UpdateDeletedUser()
        {
            var sellerBLL = (UserBLL)Seller;

            using (var context = new AuctionSiteContext(sellerBLL.Site.ConnectionString))
            {
                var auction = context.Auctions.Find(Id);
                if (null == auction)
                {
                    throw new InvalidOperationException("Invalid operation: auction not found.");
                }
                auction.CurrentWinner        = null;
                auction.CurrentWinnerId      = null;
                context.Entry(auction).State = EntityState.Modified;
                context.SaveChanges();
            }
        }
        /* AUX METHODS */

        private void AddUser(string username, string password, byte[] salt)
        {
            User user;

            using (var context = new AuctionSiteContext(ConnectionString))
            {
                user = new User()
                {
                    Username = username,
                    Password = Convert.ToBase64String(Utils.HashPassword(password, salt)),
                    Salt     = Convert.ToBase64String(salt),
                    SiteName = Name
                };
                context.Users.Add(user);
                context.SaveChanges();
            }
        }
Example #9
0
        public void Delete()
        {
            if (Utils.IsUserDisposed(this))
            {
                throw new InvalidOperationException("User disposed.");
            }
            foreach (var auction in Site.GetAuctions(true))
            {
                if (Equals(auction.CurrentWinner()))
                {
                    throw new InvalidOperationException("User cannot be deleted because is a current winner in an auction.");
                }
                if (Equals(auction.Seller))
                {
                    throw new InvalidOperationException("User cannot be deleted because is a seller.");
                }
            }
            foreach (var auction in Site.GetEndedAuctions())
            {
                var auctionBLL = (AuctionBLL)auction;
                if (Equals(auctionBLL.CurrentWinner()))
                {
                    auctionBLL.UpdateDeletedUser();
                }
                else if (Equals(auctionBLL.Seller))
                {
                    auctionBLL.Delete();
                }
            }
            Session?.Logout();
            using (var context = new AuctionSiteContext(Site.ConnectionString))
            {
                var user = context.Users.Find(UserID);
                context.Entry(user).State = EntityState.Deleted;
                context.SaveChanges();
            }

            IsDeleted = true;
        }
        public void Logout()
        {
            if (IsLoggedOut())
            {
                throw new InvalidOperationException("User already logged out.");
            }

            var userBLL = User as UserBLL;

            using (var context = new AuctionSiteContext(userBLL.Site.ConnectionString))
            {
                var session = context.Sessions.Find(Id);
                if (null == session)
                {
                    throw new InvalidOperationException("invalid operation: session not found.");
                }
                context.Users.Where(u => u.UserID == userBLL.UserID).Load();
                context.Sessions.Remove(session);
                context.SaveChanges();
            }

            LoggedOut = true;
        }
Example #11
0
        public void CreateSiteOnDb(string connectionString, string name, int timezone, int sessionExpirationTimeInSeconds,
                                   double minimumBidIncrement)
        {
            if (null == connectionString)
            {
                throw new ArgumentNullException($"{nameof(connectionString)} cannot be null.");
            }

            if (null == name)
            {
                throw new ArgumentNullException($"{nameof(name)} cannot be null.");
            }

            if (!IsValidSiteName(name))
            {
                throw new ArgumentException($"{nameof(name)} is not a valid site name.");
            }

            if (!IsValidTimezone())
            {
                throw new ArgumentOutOfRangeException($"{nameof(timezone)} is not a valid timezone.");
            }

            if (!IsPositiveSessionExpiration())
            {
                throw new ArgumentOutOfRangeException($"{nameof(sessionExpirationTimeInSeconds)} must be a positive number.");
            }

            if (!IsPositiveMinimumBidIncrement())
            {
                throw new ArgumentOutOfRangeException($"{nameof(minimumBidIncrement)} must be a positive number.");
            }

            using (var context = new AuctionSiteContext(connectionString))
            {
                if (!ExistsDb(context))
                {
                    throw new UnavailableDbException("Database connection error.");
                }
                if (Utils.SiteNameAlreadyExists(context, name))
                {
                    throw new NameAlreadyInUseException($"{nameof(name)}: {name} already in use.");
                }

                var site = new Site()
                {
                    Name     = name,
                    Timezone = timezone,
                    SessionExpirationInSeconds = sessionExpirationTimeInSeconds,
                    MinimumBidIncrement        = minimumBidIncrement
                };
                context.Sites.Add(site);
                context.SaveChanges();
            }

            bool IsPositiveMinimumBidIncrement()
            {
                return(minimumBidIncrement > 0);
            }

            bool IsPositiveSessionExpiration()
            {
                return(sessionExpirationTimeInSeconds > 0);
            }

            bool IsValidTimezone()
            {
                return(timezone >= DomainConstraints.MinTimeZone && timezone <= DomainConstraints.MaxTimeZone);
            }
        }
        public IAuction CreateAuction(string description, DateTime endsOn, double startingPrice)
        {
            if (!IsValid())
            {
                throw new InvalidOperationException("Session not valid.");
            }
            if (Utils.IsSessionDisposed(this))
            {
                throw new InvalidOperationException("Session disposed.");
            }
            if (null == description)
            {
                throw new ArgumentNullException($"{nameof(description)} cannot be null.");
            }
            if (string.Empty == description)
            {
                throw new ArgumentException($"{nameof(description)} cannot be an empty string.");
            }
            if (IsStartingPriceNegative())
            {
                throw new ArgumentOutOfRangeException($"{nameof(startingPrice)} cannot be a negative number.");
            }
            if (endsOn < AlarmClock.Now)
            {
                throw new UnavailableTimeMachineException($"{endsOn} cannot be in the past.");
            }
            var     userBLL = User as UserBLL;
            Auction auction;
            var     validUntil = AlarmClock.Now.AddSeconds(userBLL.Site.SessionExpirationInSeconds);

            using (var context = new AuctionSiteContext(userBLL.Site.ConnectionString))
            {
                auction = new Auction()
                {
                    Description     = description,
                    EndsOn          = endsOn,
                    SiteName        = userBLL.Site.Name,
                    SellerId        = userBLL.UserID,
                    CurrentWinnerId = null,
                    LastBid         = null,
                    StartingPrice   = startingPrice,
                    CurrentPrice    = startingPrice
                };
                context.Auctions.Add(auction);

                var session = context.Sessions.Find(Id);
                if (null == session)
                {
                    throw new InvalidOperationException("Invalid operation: session not found.");
                }
                session.ValidUntil           = validUntil;
                context.Entry(session).State = EntityState.Modified;
                context.SaveChanges();
            }
            ValidUntil = validUntil;
            return(new AuctionBLL(auction, User));

            bool IsStartingPriceNegative()
            {
                return(startingPrice < 0);
            }
        }