public IActionResult ReadResult(string uuid, [FromHeader] string apiKey) { if (!ApiKeyFilter.ValidApiKey(apiKey)) { return(Unauthorized()); } try { if (cache.ContainsKey(uuid)) { var result = cache[uuid]; cache.Remove(uuid); return(Ok(result)); } return(NotFound()); } catch (Exception ex) { log.Error("Failed to load hierarchy", ex); return(BadRequest()); } }
protected string AuthorizeAndFetchCvr(string cvr, string apiKey) { if (!ApiKeyFilter.ValidApiKey(apiKey)) { return(null); } string defaultCvr = OrganisationRegistryProperties.GetInstance().DefaultMunicipality; if (!string.IsNullOrEmpty(defaultCvr)) { if (!string.IsNullOrEmpty(cvr) && !cvr.Equals(defaultCvr)) { log.Warn("CVR supplied through HTTP HEADER (" + cvr + ") was overwritten by configured default (" + defaultCvr + ")"); } cvr = defaultCvr; } // if no CVR is supplied or configured, stop execution if (string.IsNullOrEmpty(cvr)) { log.Warn("No CVR supplied or configured!"); throw new System.Exception("No CVR supplied or configured!"); } OrganisationRegistryProperties.SetCurrentMunicipality(cvr); return(OrganisationRegistryProperties.GetCurrentMunicipality()); }
public IActionResult Read([FromHeader] string cvr, [FromHeader] string apiKey) { if (!ApiKeyFilter.ValidApiKey(apiKey)) { return(Unauthorized()); } List <UserDTO> result = new List <UserDTO>(); try { // set cvr on thread if supplied as header (will revert to default if null) OrganisationRegistryProperties.SetCurrentMunicipality(cvr); log.Info("Fetching users in OrgUnits with a DTR-ID for " + OrganisationRegistryProperties.GetCurrentMunicipality()); // read OUs List <global::IntegrationLayer.OrganisationFunktion.FiltreretOejebliksbilledeType> allUnitRoles; List <OU> ous = service.ReadOUHierarchy(cvr, out allUnitRoles, null, ReadTasks.NO, ReadManager.YES, ReadAddresses.YES, ReadPayoutUnit.NO, ReadPositions.NO, ReadContactForTasks.NO); log.Info("Found " + ous.Count() + " orgUnits in total"); // filter OUs so we only get those with a DTRID registered on them ous = ous.Where(ou => ou.Addresses.Where(a => a is DtrId).Count() > 0).ToList(); log.Info("Filtered to " + ous.Count() + " orgUnits with a DTR ID assigned"); // TODO: could optimize this with some parallel lookup // read positions from OrgUnits service.LoadPositions(ous, allUnitRoles); // read users var userUuids = service.FindAllUsers(ous).Distinct().ToList(); log.Info("Identified " + userUuids.Count + " users - reading details"); var users = service.ReadUsers(cvr, userUuids, allUnitRoles, null, ReadAddresses.YES, ReadParentDetails.NO); log.Info("Found " + users.Count + " users"); foreach (var ou in ous) { var dtrIdAddress = ou.Addresses.Where(a => a is DtrId).FirstOrDefault(); if (dtrIdAddress == null) { continue; } string dtrId = dtrIdAddress.Value; // load manager if available if (!string.IsNullOrEmpty(ou.Manager?.Uuid)) { log.Info("Reading manager for " + ou.Name); try { var manager = service.ReadUserObject(ou.Manager.Uuid, ReadAddresses.YES, ReadParentDetails.NO); var emailAddress = manager.Addresses.Where(a => a is Email).FirstOrDefault(); var phoneAddress = manager.Addresses.Where(a => a is Phone).FirstOrDefault(); var email = (emailAddress != null) ? emailAddress.Value : null; var phone = (phoneAddress != null) ? phoneAddress.Value : null; UserDTO userDTO = new UserDTO(); userDTO.dtrId = dtrId; userDTO.email = email; userDTO.phone = phone; userDTO.ssn = manager.Person?.Cpr; userDTO.userId = manager.UserId; userDTO.uuid = manager.Uuid.ToLower(); userDTO.manager = true; result.Add(userDTO); } catch (Exception ex) { log.Warn("Manager did not exist: " + ou.Manager.Uuid + " - " + ex.Message); } } foreach (var user in users) { if (user.Positions.Where(p => string.Compare(p.OU?.Uuid, ou.Uuid) == 0).Count() > 0) { var emailAddress = user.Addresses.Where(a => a is Email).FirstOrDefault(); var phoneAddress = user.Addresses.Where(a => a is Phone).FirstOrDefault(); var email = (emailAddress != null) ? emailAddress.Value : null; var phone = (phoneAddress != null) ? phoneAddress.Value : null; UserDTO userDTO = new UserDTO(); userDTO.dtrId = dtrId; userDTO.email = email; userDTO.phone = phone; userDTO.ssn = user.Person?.Cpr; userDTO.userId = user.UserId; userDTO.uuid = user.Uuid.ToLower(); userDTO.manager = false; result.Add(userDTO); } } } return(Ok(result)); } catch (Exception ex) { log.Error("Failed to build Hierarchy for " + OrganisationRegistryProperties.GetCurrentMunicipality(), ex); return(BadRequest("Error - se logs for details")); } }
public IActionResult Read([FromHeader] string cvr, [FromHeader] string apiKey) { if (!ApiKeyFilter.ValidApiKey(apiKey)) { return(Unauthorized()); } string uuid = Guid.NewGuid().ToString().ToLower(); new Thread(() => { try { // set cvr on thread if supplied as header (will revert to default if null) OrganisationRegistryProperties.SetCurrentMunicipality(cvr); log.Info("Fetching hierarchy for " + OrganisationRegistryProperties.GetCurrentMunicipality()); // read OUs List <global::IntegrationLayer.OrganisationFunktion.FiltreretOejebliksbilledeType> allUnitRoles; var ous = service.ReadOUHierarchy(cvr, out allUnitRoles, null, ReadTasks.NO, ReadManager.NO, ReadAddresses.NO, ReadPayoutUnit.NO, ReadPositions.YES, ReadContactForTasks.NO); // read users var userUuids = service.FindAllUsers(ous).Distinct().ToList(); var users = service.ReadUsers(cvr, userUuids, allUnitRoles, null, ReadAddresses.YES, ReadParentDetails.NO); log.Info("Found " + users.Count + " users"); // construct result var res = new Hierarchy(); // ous can be mapped in a simple manner res.OUs = ous.Select(ou => new BasicOU() { Name = ou.Name, ParentOU = ou.ParentOU?.Uuid, Uuid = ou.Uuid }).ToList(); // users has a slightly more complex structure foreach (var user in users) { if (string.IsNullOrEmpty(user.Person?.Name)) { log.Warn("User with uuid " + user.Uuid + " does not have a Person.Name for CVR: " + cvr); continue; } BasicUser basicUser = new BasicUser(); basicUser.Name = user.Person.Name; basicUser.UserId = user.UserId; basicUser.Uuid = user.Uuid; if (user.Addresses != null) { foreach (var address in user.Addresses) { if (address is Email) { basicUser.Email = address.Value; } else if (address is Phone) { basicUser.Telephone = address.Value; } } } if (user.Positions != null) { foreach (var position in user.Positions) { basicUser.Positions.Add(new BasicPosition() { Name = position.Name, Uuid = position.OU.Uuid }); } } res.Users.Add(basicUser); } log.Info("Hierarchy build for " + OrganisationRegistryProperties.GetCurrentMunicipality() + ". Adding to cache with uuid: " + uuid); cache.Add(uuid, new HierarchyWrapper() { Created = DateTime.Now, Result = res, Status = Status.SUCCESS }); } catch (Exception ex) { log.Error("Failed to build Hierarchy for " + OrganisationRegistryProperties.GetCurrentMunicipality(), ex); cache.Add(uuid, new HierarchyWrapper() { Created = DateTime.Now, Result = null, Status = Status.FAILURE }); } }).Start(); return(Ok(uuid)); }