Beispiel #1
0
 public static Bundles.LederInfo TryCorrect(Bundles.LederInfo leder)
 {
     try
     {
         return(dict[leder.AD.ToUpper()]);
     }
     catch (KeyNotFoundException ke)
     {
         return(leder);
     }
 }
        public static Response <Bundles.LederInfo> GetLederBundle(int ansattNR)
        {
            try
            {
                using (DirectoryEntry searchRoot = new DirectoryEntry("", Username, Password))
                {
                    using (DirectorySearcher dSearcher = new DirectorySearcher(searchRoot))
                    {
                        dSearcher.PropertiesToLoad.Add("mail");
                        dSearcher.PropertiesToLoad.Add("department");
                        dSearcher.PropertiesToLoad.Add("displayName");
                        dSearcher.PropertiesToLoad.Add("sAMAccountName");
                        dSearcher.ReferralChasing = ReferralChasingOption.All;
                        dSearcher.SearchScope     = SearchScope.Subtree;
                        dSearcher.Filter          = $"employeeID={ansattNR}";

                        var resultAD = dSearcher.FindOne();

                        if (resultAD != null)
                        {
                            var mail = resultAD.GetDirectoryEntry().Properties["mail"].Value.ToString();

                            //var dept = resultAD.GetDirectoryEntry().Properties["department"];
                            //var deptValue = dept.Value;

                            var lederAD    = resultAD.GetDirectoryEntry().Properties["sAMAccountName"].Value.ToString();
                            var lederEnhet = resultAD.GetDirectoryEntry().Properties["department"].Value?.ToString();
                            var ledernavn  = resultAD.GetDirectoryEntry().Properties["displayName"].Value.ToString();

                            var bundle = new Bundles.LederInfo()
                            {
                                AD    = lederAD,
                                EPost = mail,
                                Enhet = lederEnhet,
                                Navn  = ledernavn
                            };

                            return(new Response <Bundles.LederInfo>(bundle, "OK", Codes.Code.OK));
                        }
                        else
                        {
                            return(new Response <Bundles.LederInfo>(Bundles.LederInfo.Empty, string.Format(StringConstants.ERROR_ID_NOT_FOUND_AD, ansattNR), Codes.Code.ERROR));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                return(new Response <Bundles.LederInfo>(Bundles.LederInfo.Empty, e.Message, Codes.Code.ERROR));
            }
        }
Beispiel #3
0
        public static Response <int> ModifySuperUser(Bundles.LederInfo superUser)
        {
            int index = Superbrukere.IndexOf(Superbrukere.Where(s => s.AD.ToUpper().Equals(superUser.AD.ToUpper())).FirstOrDefault());

            if (index != -1)
            {
                Superbrukere[index] = superUser;
            }
            var ioResult = WriteObject(Paths.SUPER_USERS, Superbrukere);

            if (ioResult.Success)
            {
                return(new Response <int>(0, StringConstants.SUPERUSER_ACCESS_MODIFY, Codes.Code.OK));
            }
            else
            {
                return(new Response <int>(1, ioResult.Message, Codes.Code.ERROR));
            }
        }
        public static Response <Bundles.LederInfo> GetInfoForLeder(int ansattNr)
        {
            try
            {
                using (DirectoryEntry searchRoot = new DirectoryEntry("", Username, Password))
                {
                    using (DirectorySearcher dSearcher = new DirectorySearcher(searchRoot))
                    {
                        dSearcher.PropertiesToLoad.Add("employeeID");
                        dSearcher.PropertiesToLoad.Add("manager");
                        dSearcher.PropertiesToLoad.Add("sAMAccountName");
                        dSearcher.ReferralChasing = ReferralChasingOption.All;
                        dSearcher.SearchScope     = SearchScope.Subtree;
                        // filtrerer ut deaktiverte brukere
                        // https://forums.asp.net/t/1172159.aspx?using+C+to+access+active+directory+and+pull+active+users+ 21.05.2019
                        dSearcher.Filter = $"(&(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(employeeID={ansattNr}))";

                        var resultAD = dSearcher.FindOne();

                        if (resultAD != null)
                        {
                            var entry  = resultAD.GetDirectoryEntry();
                            var manVar = entry.Properties["manager"].Value;
                            if (manVar != null)
                            {
                                var manager = manVar.ToString();

                                var lederAD = manager.Split(',')[0].Split('-').Last().Trim(); // på formen 'Fornavn Etternavn - FORETT'
                                                                                              //var lederEnhet = manager.Split(',')[1].Split('-').Last().Trim(); // på formen 'NAR_<kode> - Enhetsnavn'

                                dSearcher.Filter = $"sAMAccountName={lederAD}";

                                // nytt oppslag for å finne leders epostadresse
                                var resultLeder = dSearcher.FindOne();

                                if (resultLeder != null)
                                {
                                    var mail       = resultLeder.GetDirectoryEntry().Properties["mail"].Value.ToString();
                                    var lederEnhet = resultLeder.GetDirectoryEntry().Properties["department"].Value.ToString();

                                    var bundle = new Bundles.LederInfo()
                                    {
                                        AD    = lederAD,
                                        EPost = mail,
                                        Enhet = lederEnhet
                                    };

                                    return(new Response <Bundles.LederInfo>(bundle, "OK", Codes.Code.OK));
                                }
                                else
                                {
                                    return(new Response <Bundles.LederInfo>(Bundles.LederInfo.Empty, string.Format(StringConstants.ERROR_MANAGER_NOT_FOUND_AD, ansattNr), Codes.Code.ERROR));
                                }
                            }
                            else
                            {
                                return(new Response <Bundles.LederInfo>(Bundles.LederInfo.Empty, string.Format(StringConstants.ERROR_MANAGER_NOT_FOUND_AD, ansattNr), Codes.Code.ERROR));
                            }
                        }
                        else
                        {
                            return(new Response <Bundles.LederInfo>(Bundles.LederInfo.Empty, string.Format(StringConstants.ERROR_ID_NOT_FOUND_AD, ansattNr), Codes.Code.ERROR));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                return(new Response <Bundles.LederInfo>(Bundles.LederInfo.Empty, e.Message, Codes.Code.ERROR));
            }
        }