Beispiel #1
0
        public IActionResult Update([FromBody] OrgUnitRegistration ou, [FromHeader] string cvr, [FromHeader] string apiKey)
        {
            if ((cvr = AuthorizeAndFetchCvr(cvr, apiKey)) == null)
            {
                return(Unauthorized());
            }

            // setting it will revert to default if no value is supplied, so we can read a valid value afterwards
            OrganisationRegistryProperties.SetCurrentMunicipality(cvr);
            cvr = OrganisationRegistryProperties.GetCurrentMunicipality();

            string error;

            if ((error = ValidateOU(ou)) == null)
            {
                try
                {
                    orgUnitDao.Save(ou, OperationType.UPDATE, cvr);
                }
                catch (Exception ex)
                {
                    log.Error("Failed to save OrgUnit", ex);

                    return(BadRequest(ex.Message));
                }
            }
            else
            {
                return(BadRequest(error));
            }

            return(Ok(ou));
        }
Beispiel #2
0
        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 Create([FromBody] ImportInputType input, [FromHeader] string cvr, [FromHeader] string apiKey)
        {
            if ((cvr = AuthorizeAndFetchCvr(cvr, apiKey)) == null)
            {
                return(Unauthorized());
            }

            // setting it will revert to default if no value is supplied, so we can read a valid value afterwards
            OrganisationRegistryProperties.SetCurrentMunicipality(cvr);
            cvr = OrganisationRegistryProperties.GetCurrentMunicipality();

            importerResponse result = rawOrganisationEnhedStub.Create(input);

            return(Ok(result));
        }
Beispiel #4
0
        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"));
            }
        }
Beispiel #5
0
        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));
        }
Beispiel #6
0
        private static void TestListAndReadOUs()
        {
            // small hack to ensure this test passes (the search parameters will find all ous in the organisation, and we need to test that it hits the required amount)
            OrganisationRegistryProperties properties = OrganisationRegistryProperties.GetInstance();
            string oldUuid = properties.MunicipalityOrganisationUUID[OrganisationRegistryProperties.GetCurrentMunicipality()];

            properties.MunicipalityOrganisationUUID[OrganisationRegistryProperties.GetCurrentMunicipality()] = Uuid();

            OrgUnitRegistration registration1 = OUReg();

            registration1.Name              = "magic";
            registration1.Email             = "*****@*****.**";
            registration1.ParentOrgUnitUuid = Uuid();
            orgUnitService.Update(registration1);

            orgUnitService.Read(registration1.Uuid);

            OrgUnitRegistration registration2 = OUReg();

            registration2.Name              = "magic";
            registration2.Email             = "*****@*****.**";
            registration2.ParentOrgUnitUuid = Uuid();
            orgUnitService.Update(registration2);

            registration2.Name = "different name";
            orgUnitService.Update(registration2);

            // TODO: a KMD bug prevents this test from working...
            OrgUnitRegistration registration3 = OUReg();

            registration3.Name              = "ou3";
            registration3.Email             = "*****@*****.**";
            registration3.ParentOrgUnitUuid = Uuid();
            orgUnitService.Update(registration3);
            orgUnitService.Delete(registration3.Uuid, DateTime.Now);

            List <string> ous = orgUnitService.List();

            if (ous.Count != 2)
            {
                throw new Exception("List() returned " + ous.Count + " ous, but 2 was expected");
            }

            foreach (var uuid in ous)
            {
                OrgUnitRegistration registration = orgUnitService.Read(uuid);

                if (uuid.Equals(registration1.Uuid))
                {
                    if (!registration1.Name.Equals(registration.Name))
                    {
                        throw new Exception("Name does not match");
                    }

                    if (!registration1.ParentOrgUnitUuid.Equals(registration.ParentOrgUnitUuid))
                    {
                        throw new Exception("ParentOU UUID does not match");
                    }

                    if (!registration1.Email.Equals(registration.Email))
                    {
                        throw new Exception("Email does not match");
                    }
                }
                else if (uuid.Equals(registration2.Uuid))
                {
                    if (!registration2.Name.Equals(registration.Name))
                    {
                        throw new Exception("Name does not match");
                    }

                    if (!registration2.ParentOrgUnitUuid.Equals(registration.ParentOrgUnitUuid))
                    {
                        throw new Exception("ParentOU UUID does not match");
                    }

                    if (!registration2.Email.Equals(registration.Email))
                    {
                        throw new Exception("Email does not match");
                    }
                }
                else
                {
                    throw new Exception("List returned the uuid of an unexpected ou");
                }
            }

            properties.MunicipalityOrganisationUUID[OrganisationRegistryProperties.GetCurrentMunicipality()] = oldUuid;
        }
Beispiel #7
0
        private static void TestListAndReadUsers()
        {
            // small hack to ensure this test passes (the search parameters will find all users in the organisation, and we need to test that it hits the required amount)
            OrganisationRegistryProperties properties = OrganisationRegistryProperties.GetInstance();
            string oldUuid = properties.MunicipalityOrganisationUUID[OrganisationRegistryProperties.GetCurrentMunicipality()];

            properties.MunicipalityOrganisationUUID[OrganisationRegistryProperties.GetCurrentMunicipality()] = Uuid();

            UserRegistration registration1 = UserReg();

            registration1.UserId      = "userId1";
            registration1.Email       = "*****@*****.**";
            registration1.Person.Name = "Name of Person 1";
            registration1.Positions.Add(new Position()
            {
                Name        = "Position 1",
                OrgUnitUuid = Uuid()
            });
            registration1.Positions.Add(new Position()
            {
                Name        = "Position 2",
                OrgUnitUuid = Uuid()
            });
            userService.Update(registration1);

            UserRegistration registration2 = UserReg();

            registration2.UserId      = "userId2";
            registration2.Email       = "*****@*****.**";
            registration2.Person.Name = "Name of Person 2";
            registration2.Positions.Add(new Position()
            {
                Name        = "Position 3",
                OrgUnitUuid = Uuid()
            });
            registration2.Positions.Add(new Position()
            {
                Name        = "Position 4",
                OrgUnitUuid = Uuid()
            });
            userService.Update(registration2);

            UserRegistration registration3 = UserReg();

            registration3.UserId      = "userId3";
            registration3.Email       = "*****@*****.**";
            registration3.Person.Name = "Name of Person 3";
            registration3.Positions.Add(new Position()
            {
                Name        = "Position 5",
                OrgUnitUuid = Uuid()
            });
            userService.Update(registration3);
            userService.Delete(registration3.Uuid, DateTime.Now);

            List <string> users = userService.List();

            if (users.Count != 2)
            {
                throw new Exception("List() returned " + users.Count + " users, but 2 was expected");
            }

            foreach (var uuid in users)
            {
                UserRegistration registration = userService.Read(uuid);

                if (uuid.Equals(registration1.Uuid))
                {
                    if (!registration1.UserId.Equals(registration.UserId))
                    {
                        throw new Exception("userId does not match");
                    }

                    if (!registration1.Person.Name.Equals(registration.Person.Name))
                    {
                        throw new Exception("Name does not match");
                    }

                    if (!registration1.Email.Equals(registration.Email))
                    {
                        throw new Exception("Email does not match");
                    }

                    if (registration1.Positions.Count != registration.Positions.Count)
                    {
                        throw new Exception("Amount of positions does not match");
                    }

                    foreach (var position in registration1.Positions)
                    {
                        bool found = false;

                        foreach (var readPosition in registration.Positions)
                        {
                            if (readPosition.Name.Equals(position.Name) && readPosition.OrgUnitUuid.Equals(position.OrgUnitUuid))
                            {
                                found = true;
                            }
                        }

                        if (!found)
                        {
                            throw new Exception("Missing position");
                        }
                    }
                }
                else if (uuid.Equals(registration2.Uuid))
                {
                    if (!registration2.UserId.Equals(registration.UserId))
                    {
                        throw new Exception("userId does not match");
                    }

                    if (!registration2.Person.Name.Equals(registration.Person.Name))
                    {
                        throw new Exception("Name does not match");
                    }

                    if (!registration2.Email.Equals(registration.Email))
                    {
                        throw new Exception("Email does not match");
                    }

                    if (registration2.Positions.Count != registration.Positions.Count)
                    {
                        throw new Exception("Amount of positions does not match");
                    }

                    foreach (var position in registration2.Positions)
                    {
                        bool found = false;

                        foreach (var readPosition in registration.Positions)
                        {
                            if (readPosition.Name.Equals(position.Name) && readPosition.OrgUnitUuid.Equals(position.OrgUnitUuid))
                            {
                                found = true;
                            }
                        }

                        if (!found)
                        {
                            throw new Exception("Missing position");
                        }
                    }
                }
                else
                {
                    throw new Exception("List returned the uuid of an unexpected user");
                }
            }

            properties.MunicipalityOrganisationUUID[OrganisationRegistryProperties.GetCurrentMunicipality()] = oldUuid;
        }