Example #1
0
        public IActionResult UpdateEmployees()
        {
            ResultUserUpdate result = ldapService.UpdateEmployees();

            return(View("ResultUpdateEmployees", result));
        }
Example #2
0
        public ResultUserUpdate UpdateEmployees()
        {
            StringBuilder    sb     = new StringBuilder();
            ResultUserUpdate result = new ResultUserUpdate();

            try
            {
                connection.Connect(config.Url, LdapConnection.DEFAULT_PORT);
                connection.Bind(config.BindDn, config.BindCredentials);
            }
            catch (Exception ex)
            {
                result.Errors.Add(ex.ToString());
                logger.LogError(ex.ToString());
                return(result);
            }

            try
            {
                foreach (string orgUnit in config.OrgUnits)
                {
                    List <LdapEntry> resultSearch = connection.Search(
                        orgUnit,
                        LdapConnection.SCOPE_SUB,
                        config.SearchFilter,
                        new[] { MemberOfAttribute, DisplayNameAttribute, SAMAccountNameAttribute, TitleAttribute, MailAttribute },
                        false
                        ).ToList();

                    foreach (LdapEntry user in resultSearch)
                    {
                        if (user != null)
                        {
                            result.SearchedAccountsCount++;
                            string account  = user.getAttribute(SAMAccountNameAttribute).StringValue;
                            string fullName = user.getAttribute(DisplayNameAttribute)?.StringValue ?? "";
                            string email    = user.getAttribute(MailAttribute)?.StringValue ?? "";
                            string position = user.getAttribute(TitleAttribute)?.StringValue ?? "";
                            if (fullName == "" || email == "" || position == "")
                            {
                                result.NotValidAccountCount++;
                                result.NotValidAccounts.Add(account);
                                continue;
                            }

                            if (!employeesRepository.Employees.Any(em => em.Account.Equals(account, comparisonType: StringComparison.InvariantCultureIgnoreCase)))
                            {
                                Employee employee = new Employee
                                {
                                    Account  = account,
                                    Email    = email,
                                    FullName = fullName,
                                    Position = position
                                };

                                try
                                {
                                    employeesRepository.SaveEmployee(employee);
                                    result.NewUserCount++;
                                    result.NewEmployees.Add(new EmployeeViewModel(employee));
                                }
                                catch (Exception ex)
                                {
                                    result.Errors.Add($"Не удалось добавить сотрудника {account}: {ex.ToString()}");
                                    logger.LogError(ex.ToString());
                                }
                            }
                            else
                            {
                                Employee employee = employeesRepository.Employees.FirstOrDefault(e => e.Account == account);
                                if (employee == null)
                                {
                                    continue;
                                }

                                bool updated = false;
                                if (employee.Email != email)
                                {
                                    employee.Email = email;
                                    updated        = true;
                                }
                                if (employee.FullName != fullName)
                                {
                                    employee.FullName = fullName;
                                    updated           = true;
                                }
                                if (employee.Position != position)
                                {
                                    employee.Position = position;
                                    updated           = true;
                                }
                                if (updated)
                                {
                                    result.UpdatedUserCount++;
                                    result.UpdatedEmployees.Add(new EmployeeViewModel(employee));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Errors.Add(ex.ToString());
                logger.LogError(ex.ToString());
            }
            connection.Disconnect();
            return(result);
        }