예제 #1
0
 public static User GetUser(string loginName)
 {
     using (MyDbContext db = new MyDbContext())
     {
         var user = db.Users.Where(r => string.Compare(r.LoginName, loginName, true) == 0).SingleOrDefault();
         if (user == null)
         {
             var adUser = ADService.GetUserEntity(loginName);
             if (adUser != null)
             {
                 adUser.CreatedBy   = loginName;
                 adUser.CreatedTime = DateTime.Now;
                 adUser.UpdatedBy   = adUser.CreatedBy;
                 adUser.UpdatedTime = adUser.CreatedTime;
                 db.Users.Add(adUser);
                 db.SaveChanges();
                 user = db.Users.Where(r => string.Compare(r.LoginName, loginName, true) == 0).SingleOrDefault();
             }
         }
         return(user);
     }
 }
예제 #2
0
        //add user to database
        public static void SyncNewUsersFromAdService(string loginName)
        {
            List <User>   lstAdUsers      = ADService.GetUserEntities();
            List <User>   lstDbUsers      = GetAllUsers();
            List <string> lstDbLoginNames = lstDbUsers.Select(r => r.LoginName.ToLower()).ToList();
            List <string> lstDcLoginNames = lstAdUsers.Select(r => r.LoginName.ToLower()).ToList();

            //delete user from database
            using (MyDbContext db = new MyDbContext())
            {
                var DbUsers        = db.Users.OrderBy(r => r.FullName).ToList();
                var lstDeleteUsers = new List <User>();

                foreach (var user in DbUsers)
                {
                    if (!lstDcLoginNames.Contains(user.LoginName.ToLower()) && user.IsExcluded == true)
                    {
                        lstDeleteUsers.Add(user);
                    }
                }

                if (lstDeleteUsers.Count > 0)
                {
                    db.Users.RemoveRange(lstDeleteUsers);
                    db.SaveChanges();
                }
            }
            //add new user to database
            List <User> lstNewUsers = new List <User>();

            foreach (var user in lstAdUsers)
            {
                if (!lstDbLoginNames.Contains(user.LoginName.ToLower()))
                {
                    user.CreatedBy   = loginName;
                    user.CreatedTime = DateTime.Now;
                    user.UpdatedBy   = user.CreatedBy;
                    user.UpdatedTime = user.CreatedTime;
                    lstNewUsers.Add(user);
                }
            }
            if (lstNewUsers.Count > 0)
            {
                using (MyDbContext db = new MyDbContext())
                {
                    db.Users.AddRange(lstNewUsers);
                    db.SaveChanges();
                }
            }


            List <User> lstNewExcludedUsers = new List <User>();
            var         lstAdLoginNames     = lstAdUsers.Select(r => r.LoginName);

            foreach (var user in lstDbUsers.Where(r => !r.IsExcluded))
            {
                if (!lstAdLoginNames.Contains(user.LoginName, StringComparer.InvariantCultureIgnoreCase))
                {
                    user.IsExcluded = true;
                    lstNewExcludedUsers.Add(user);
                }
            }

            if (lstNewExcludedUsers.Count > 0)
            {
                using (MyDbContext db = new MyDbContext())
                {
                    foreach (var user in lstNewExcludedUsers)
                    {
                        db.Users.Attach(user);
                        db.Entry(user).State = EntityState.Modified;
                    }
                    db.SaveChanges();
                }
            }
        }