/// <summary>
        /// adds the new users and clients to userClients
        /// </summary>
        /// <param name="users"></param>
        /// <param name="clients"></param>
        private static void Allocate(IEnumerable <int> users, IEnumerable <int> clients, int actionUser)
        {
            using (EverestPortalContext context = new EverestPortalContext())
            {
                foreach (var userId in users)
                {
                    foreach (var clientId in clients)
                    {
                        if (!context.userClient.Any(x => (x.UserID == userId && x.ClientID == clientId)))//Checks If already User Assigned to Client
                        {
                            context.userClient.Add(new UserClient {
                                UserID = userId, ClientID = clientId
                            });
                        }
                    }
                    ;
                }
                context.SaveChanges();

                foreach (var userId in users)
                {
                    var       objClient = context.Users.SingleOrDefault(p => p.UserID == userId);
                    IAuditLog log       = AuditFactory.GetInstance(typeof(User).Name);
                    log.SaveVersion <User>(objClient, actionUser);
                }
            }
        }
        public void DeleteUser(UserDTO uObj)
        {
            User Objuser     = dbContext.Users.SingleOrDefault(p => p.UserID == uObj.UserID);
            var  userRoles   = dbContext.userRole.Where(u => u.UserID == uObj.UserID);
            var  userClients = dbContext.userClient.Where(item => item.UserID == uObj.UserID).ToList();
            var  passwords   = dbContext.PasswordHistories.Where(item => item.UserID == uObj.UserID).ToList();
            var  tokens      = dbContext.ResetTokens.Where(item => item.UserID == uObj.UserID).ToList();

            using (var transaction = dbContext.Database.BeginTransaction())
            {
                //delete from UserClients
                dbContext.userClient.RemoveRange(userClients);
                //delete from UserRole
                dbContext.userRole.RemoveRange(userRoles);
                //delete password history entries
                dbContext.PasswordHistories.RemoveRange(passwords);
                //delete password history entries
                dbContext.ResetTokens.RemoveRange(tokens);
                //delete from Users
                dbContext.Users.Remove(Objuser);

                //add entry in audit
                IAuditLog log = AuditFactory.GetInstance(typeof(User).Name);
                log.SaveVersion <User>(Objuser, uObj.ActionUser);

                dbContext.SaveChanges();
                transaction.Commit();
            }
        }
        public void SubmitMarketbaseDetailsInformation(int MarketBaseId)
        {
            var       identity  = (ClaimsIdentity)User.Identity;
            int       uid       = Convert.ToInt32(identity.Claims.FirstOrDefault(c => c.Type == "userid").Value.ToString());
            var       objClient = _db.MarketBases.Where(u => u.Id == MarketBaseId).ToList <MarketBase>().FirstOrDefault();
            IAuditLog log       = AuditFactory.GetInstance(typeof(MarketBase).Name);

            log.SaveVersion <MarketBase>(objClient, uid);
        }
 public HttpResponseMessage SubmitMarketBase(int[] MarketBaseIds, int userId)
 {
     foreach (int MarketBaseId in MarketBaseIds)
     {
         var       objClient = _db.MarketBases.Where(u => u.Id == MarketBaseId).ToList <MarketBase>().FirstOrDefault();
         IAuditLog log       = AuditFactory.GetInstance(typeof(MarketBase).Name);
         log.SaveVersion <MarketBase>(objClient, userId);
     }
     return(Request.CreateResponse(HttpStatusCode.OK));
 }
        public HttpResponseMessage SubmitSubscription(int[] subscriptionIds, int UserId)
        {
            foreach (int subscriptionId in subscriptionIds)
            {
                var objClient = dbContext.subscription.Where(u => u.SubscriptionId == subscriptionId).ToList().FirstOrDefault();

                IAuditLog log = AuditFactory.GetInstance(typeof(Subscription).Name);
                log.SaveVersion <Subscription>(objClient, UserId);
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
        /// <summary>
        /// Deletes the user clients for the supllied users and clients
        /// </summary>
        /// <param name="users"></param>
        /// <param name="clients"></param>
        private static void DeleteAllocation(IEnumerable <int> users, IEnumerable <int> clients, int actionUser)
        {
            using (EverestPortalContext context = new EverestPortalContext())
            {
                var userClientsToDelete = context.userClient.Where(x => users.Contains(x.UserID) && clients.Contains(x.ClientID));
                context.userClient.RemoveRange(userClientsToDelete);
                context.SaveChanges();
            }

            using (EverestPortalContext context = new EverestPortalContext())
            {
                foreach (int userid in users)
                {
                    var       objClient = context.Users.SingleOrDefault(p => p.UserID == userid);
                    IAuditLog log       = AuditFactory.GetInstance(typeof(User).Name);
                    log.SaveVersion <User>(objClient, actionUser);
                }
            }
        }
        public void AddUser(UserDTO uObj)
        {
            User Objuser = dbContext.Users.SingleOrDefault(p => p.UserID == uObj.UserID);

            using (var transaction = dbContext.Database.BeginTransaction())
            {
                string randomInput = System.Web.Security.Membership.GeneratePassword(8, 2);
                string password    = GenerateHashedPassword(randomInput);

                User newUser = null;
                if (Objuser == null)
                {
                    // Create new user
                    newUser = new User()
                    {
                        UserName               = uObj.UserName,
                        FirstName              = uObj.FirstName,
                        LastName               = uObj.LastName,
                        Email                  = uObj.Email,
                        IsActive               = true,
                        UserTypeID             = 1,
                        ReceiveEmail           = true,
                        Password               = password,
                        MaintenancePeriodEmail = uObj.MaintenancePeriodEmail,
                        NewsAlertEmail         = uObj.NewsAlertEmail,
                        PasswordCreatedDate    = DateTime.Now,
                        IsPasswordVerified     = false,
                        FailedPasswordAttempt  = 0
                    };
                    dbContext.Users.Add(newUser);
                    dbContext.userRole.Add(new UserRole()
                    {
                        UserID = newUser.UserID,
                        RoleID = uObj.RoleID
                    });
                    if (uObj.ClientID != 0)
                    {
                        dbContext.userClient.Add(new UserClient()
                        {
                            UserID   = newUser.UserID,
                            ClientID = uObj.ClientID
                        });
                    }
                }
                else
                {
                    //Update existing user
                    Objuser.FirstName = uObj.FirstName;
                    Objuser.LastName  = uObj.LastName;
                    Objuser.UserName  = uObj.UserName;
                    Objuser.Email     = uObj.Email;
                    Objuser.IsActive  = uObj.IsActive;
                    Objuser.MaintenancePeriodEmail = uObj.MaintenancePeriodEmail;
                    Objuser.NewsAlertEmail         = uObj.NewsAlertEmail;
                    var rObj = dbContext.userRole.SingleOrDefault(p => p.UserID == Objuser.UserID);
                    if (rObj == null)
                    {
                        dbContext.userRole.Add(new UserRole()
                        {
                            UserID = Objuser.UserID,
                            RoleID = uObj.RoleID
                        });
                    }
                    else
                    {
                        rObj.RoleID = uObj.RoleID;
                    }
                    if (uObj.ClientID != 0)
                    {
                        var userClient = dbContext.userClient.FirstOrDefault(x => x.UserID == Objuser.UserID);
                        if (userClient == null)
                        {
                            dbContext.userClient.Add(new UserClient()
                            {
                                UserID   = Objuser.UserID,
                                ClientID = uObj.ClientID
                            });
                        }
                        else
                        {
                            var userClientlist = dbContext.userClient.Where(i => i.UserID == Objuser.UserID);
                            dbContext.userClient.RemoveRange(userClientlist);
                            // userClient.ClientID = uObj.ClientID;
                            dbContext.userClient.Add(new UserClient()
                            {
                                UserID   = Objuser.UserID,
                                ClientID = uObj.ClientID
                            });
                        }
                    }
                }
                dbContext.SaveChanges();
                transaction.Commit();

                var       objClient = (newUser == null) ? dbContext.Users.SingleOrDefault(p => p.UserID == uObj.UserID) : dbContext.Users.SingleOrDefault(p => p.UserID == newUser.UserID);
                IAuditLog log       = AuditFactory.GetInstance(typeof(User).Name);
                log.SaveVersion <User>(objClient, uObj.ActionUser);

                // Send account verification email to new user
                if (Objuser == null && newUser != null)
                {
                    string emailBody = CreateVerificationEmailBody(newUser);
                    SendEmail(newUser.Email, "Everest: Account verification", emailBody);
                }
            }
        }