Ejemplo n.º 1
0
        public void GetUser()
        {
            var adUser = adRepository.GetUser("sousajs", false);

            Assert.IsNotNull(adUser);
            Assert.AreEqual("sousajs", adUser.Login);
        }
Ejemplo n.º 2
0
        public IEnumerable <string> GetWindowsUserRoles(string userName)
        {
            var identity = ObjectIdentity.Parse(userName);

            var user = _activeDirectoryRepository.GetUser(identity.Name, identity.DomainName);

            return((user == null) ? new string[0] : user.Groups);
        }
Ejemplo n.º 3
0
        private Worker GetWorker(UserFilterDTO filter)
        {
            var query  = new List <Expression <Func <Worker, bool> > >();
            var worker = default(Worker);

            if (!string.IsNullOrEmpty(filter.Id))
            {
                query.Add(w => w.Id.ToLower() == filter.Id.ToLower());
            }

            if (!string.IsNullOrEmpty(filter.Login))
            {
                query.Add(w => w.Login.ToLower() == filter.Login.ToLower());
            }

            //Try to get workers data at ActiveDirectory
            if (!string.IsNullOrEmpty(filter.Login))
            {
                worker = _adRepo.GetUser(filter.Login, true).Map <BaseActiveDirectoryUser, Worker>();
            }

            //Validates basic infos and throws an error if it fails
            worker.Validate();
            worker.BranchLine = (worker.UserExtraInfo.Phone ?? worker.BranchLine);

            if (worker.Validation.HasErrors)
            {
                throw new ServiceException(worker.Validation.Results);
            }

            //Try to get Corporative Worker data
            try
            {
                var workerCorporate = default(Worker);

                //Initializes unit of work and connections for Corporate Database
                using (var uowCorporate = new UnitOfWork <CorporateContextFmw>())
                {
                    //Get aditional data from Corporate Database
                    using (var workerRepo = uowCorporate.GetRepository <Worker>())
                    {
                        workerCorporate = workerRepo.Get(query.BuildExpression());
                    }
                }

                //Maps corporate data null properties and other datas
                if (workerCorporate != null)
                {
                    //Maps the Corporate Id, if its null uses the same SSO Id
                    worker.Id = (workerCorporate.Id ?? worker.Id);
                    worker    = worker.MapOnlyNulls <Worker>(workerCorporate);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Exception(ex);
            }

            //Try to get SSO Worker data
            try
            {
                //Get worker at SSO database
                var workerSSO = _workerRepo.Get(query.BuildExpression());

                //Maps sso data null properties and other datas
                if (workerSSO != null)
                {
                    //Maps the SSO Id, if its null uses the same SSO Id
                    worker.Id = (workerSSO.Id ?? worker.Id);
                    worker    = worker.MapOnlyNulls <Worker>(workerSSO);
                }
            }
            catch (Exception ex)
            {
                LogHelper.Exception(ex);
            }

            return(worker);
        }
Ejemplo n.º 4
0
        public WorkerDTO GetWorker(WorkerFilterDTO filter)
        {
            var dto = default(WorkerDTO);

            try
            {
                var exprList = new List <Expression <Func <Worker, bool> > >();

                if (!string.IsNullOrEmpty(filter.Id))
                {
                    exprList.Add(wo => wo.Id.ToLower() == filter.Id.ToLower());
                }

                if (!string.IsNullOrEmpty(filter.Login))
                {
                    exprList.Add(wo => wo.Login.ToLower() == filter.Login.ToLower());
                }

                if (!string.IsNullOrEmpty(filter.NameOrDescription))
                {
                    exprList.Add(wo => wo.NameOrDescription.ToLower() == filter.NameOrDescription.ToLower());
                }

                if (exprList.Count <= default(int))
                {
                    throw new ServiceException(CommonExceptionType.ParameterException, "Id, Login or NameOrDescription");
                }

                var loadOptions = new List <string>
                {
                    "BudgetCode",
                    "BudgetCode.Area",
                    "BudgetCode.Site",
                    "BudgetCode.Dept",
                    "BudgetCode.Lbc"
                };

                if (filter.LoadManager)
                {
                    loadOptions.Add("Manager");
                    loadOptions.Add("Manager.Manager");
                }

                if (filter.LoadEmployees)
                {
                    loadOptions.Add("Employees");
                    loadOptions.Add("Employees.Employee");
                }

                //Executes query
                var worker = _workerRepo.Get(exprList.BuildExpression(), loadOptions.ToArray());

                //Validate worker basic data
                worker.Validate();

                if (worker.Validation.HasErrors)
                {
                    throw new ServiceException(worker.Validation.Results);
                }

                dto = worker.Map <Worker, WorkerDTO>();
                dto.SourceDatabase = CommonDatabase.DBALCAP.ToString();

                //Map employees data
                if (worker.Employees != null && worker.Employees.Count > 0)
                {
                    var employees = worker.Employees.Select(e => e.Employee).ToList();
                    dto.Employees = employees.Map <List <Worker>, List <WorkerLeafDTO> >();
                }

                // Map third partners' data
                if (filter.LoadManager || filter.LoadThirdPartners)
                {
                    var adUser = _adRepo.GetUser(dto.Login, filter.LoadManager, filter.LoadThirdPartners);

                    //Map manager data
                    if (worker.Manager != null && worker.Manager.FirstOrDefault() != null)
                    {
                        var manager = worker.Manager.FirstOrDefault().Manager;
                        dto.Manager = manager.Map <Worker, WorkerLeafDTO>();
                    }
                    else if (adUser.ActiveDirectoryManager.Login != null)
                    {
                        var manager = _workerRepo.Get(w => w.Login.ToUpper() == adUser.ActiveDirectoryManager.Login.ToUpper());
                        dto.Manager = manager.Map <Worker, WorkerLeafDTO>();
                    }

                    if (filter.LoadThirdPartners)
                    {
                        var logins        = adUser.ActiveDirectoryThirdPartners.Select(ad => ad.Login.ToUpper()).Distinct().ToList();
                        var thirdPartners = _workerRepo.SelectWhere(w => logins.Contains(w.Login.ToUpper())).ToList();
                        dto.ThirdPartners = thirdPartners.Map <List <Worker>, List <ThirdPartnerDTO> >();
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.ExceptionAndThrow(ex);
            }

            return(dto);
        }