public HttpResponseMessage GetUsers()
        {
            var adConsumer = new ActiveDirectoryHelper("opus.local");
            var users      = adConsumer.GetAllUsers();

            return(Request.CreateResponse(HttpStatusCode.OK, users));
        }
Example #2
0
        public async Task <string> ADSync()
        {
            // Расстановка сортировок и уровней вложенности по отделам
            var depts = await _context.Departments.ToListAsync();

            foreach (var d in depts)
            {
                var parentDept = depts.FirstOrDefault(x => x.Id == d.ParentId);
                if (parentDept != null)
                {
                    parentDept.Children.Add(d);
                }
            }
            depts.SortByDepth();

            await _context.SaveChangesAsync();

            var updated   = new List <Tuple <string, string, string> >();
            var employees = await _context.Employees.ToListAsync();

            var adusers = ActiveDirectoryHelper.GetAllUsers();

            foreach (var adu in adusers)
            {
                var emp = employees.FirstOrDefault(e => e.Idguid1C.ToString() == adu.GetProperty("ipPhone"));
                if (emp != null && emp.Email != adu.EmailAddress && !string.IsNullOrEmpty(adu.EmailAddress))
                {
                    updated.Add(new Tuple <string, string, string>(emp.Idguid1C.ToString(), emp.Email, adu.EmailAddress));
                    emp.Email = adu.EmailAddress;
                }
                if (emp == null || string.IsNullOrEmpty(adu.GetProperty("ipPhone")))
                {
                    emp = employees.FirstOrDefault(e => string.IsNullOrEmpty(e.Email) && e.Name.Trim() == adu.DisplayName.Trim());
                    if (emp != null && emp.Email != adu.EmailAddress && !string.IsNullOrEmpty(adu.EmailAddress))
                    {
                        updated.Add(new Tuple <string, string, string>(emp.Idguid1C.ToString(), emp.Email, adu.EmailAddress));
                        emp.Email = adu.EmailAddress;
                    }
                }

                // Добавление доступа к странице управления по ролям в AD
                var groups = adu.GetAuthorizationGroups();
                foreach (var g in groups)
                {
                    if (g.Name.Contains("Админ"))
                    {
                        emp.CanManage = true;
                    }
                }
            }

            return(JsonSerializer.Serialize(updated));
        }