<commit_before>﻿using NLog;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Web;
using System.Management;
using Microsoft.Exchange.WebServices.Data;
using ITSWebMgmt.Functions;
using ITSWebMgmt.Connectors;
using System.Web.UI.WebControls;
using ITSWebMgmt.Connectors.Active_Directory;

namespace ITSWebMgmt
{
    public partial class UserInfo : System.Web.UI.Page
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                ResultDiv.Visible = false;
                errordiv.Visible = false;

                String search = Request.QueryString["search"];
                if (search != null)
                {
                    lookupUser(search.Trim());
                    return;
                }

                String username = Request.QueryString["username"];
                if (username != null)
                {                
                    buildUserLookupFromUsername(username);
                    return;
                }

                String phoneNr = Request.QueryString["phone"];
                if (phoneNr != null)
                {               
                    buildUserLookupFromPhone(phoneNr);
                    return;
                }

            }
        }

        protected void lookupUser(string username)
        {

            
            int val;
            if (username.Length == 4 && int.TryParse(username, out val))
            {
                buildUserLookupFromPhone(username);
            }
            else
            {
                UserNameLabel.Text = username;
                buildUserLookupFromUsername(username);
            }
            
        }

        //Searhces on a phone numer (internal or external), and returns a upn (later ADsPath) to a use or null if not found
        protected string doPhoneSearch(string numberIn)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            string number = numberIn;
            //If number is a shot internal number, expand it :)
            if (number.Length == 4)
            {
                // format is 3452
                number = String.Format("+459940{0}", number);

            }
            else if (number.Length == 6)
            {
                //format is +453452 
                number = String.Format("+459940{0}", number.Replace("+45", ""));

            }
            else if (number.Length == 8)
            {
                //format is 99403442
                number = String.Format("+45{0}", number);

            } // else format is ok

            DirectoryEntry de = new DirectoryEntry("GC://aau.dk");
            //string filter = string.Format("(&(objectCategory=person)(telephoneNumber={0}))", number);
            string filter = string.Format("(&(objectCategory=person)(objectClass=user)(telephoneNumber={0}))", number);

            //logger.Debug(filter);

            DirectorySearcher search = new DirectorySearcher(de, filter);
            search.PropertiesToLoad.Add("userPrincipalName"); //Load something to speed up the object get?
            SearchResult r = search.FindOne();

            watch.Stop();
            System.Diagnostics.Debug.WriteLine("phonesearch took: " + watch.ElapsedMilliseconds);

            if (r != null)
            {
                //return r.Properties["ADsPath"][0].ToString(); //XXX handle if result is 0 (null exception)
                return r.Properties["ADsPath"][0].ToString().Replace("GC:","LDAP:").Replace("aau.dk/","");
            }
            else
            {
                return null;
            }

        }

        protected void buildgroupssegmentLabel(String[] groupsList, Label output)
        {
            StringBuilder sb = new StringBuilder();

            var helper = new HTMLTableHelper(2);
            sb.Append(helper.printStart());
            sb.Append(helper.printRow(new string[]{"Domain", "Name"}, true));

            var groupsAsList = groupsList.ToList<string>();

            Func<string[], string, bool> startsWith = delegate (string[] prefix, string value)
            {
                return prefix.Any<string>(x => value.StartsWith(x));
            };
            string[] prefixMBX_ACL = { "CN=MBX_", "CN=ACL_" };
            Func<string, bool> startsWithMBXorACL = (string value) => startsWith(prefixMBX_ACL, value);
            
            //Sort MBX and ACL Last
            groupsAsList.Sort((a, b) =>
            {
                if (startsWithMBXorACL(a) && startsWithMBXorACL(b))
                {
                    return a.CompareTo(b);
                } else if (startsWithMBXorACL(a))
                {
                    return 1;
                } else if (startsWithMBXorACL(b))
                {
                    return -1;
                } else
                {
                    return a.CompareTo(b);
                }<commit_msg>code cleanup from boegholm<commit_after>﻿using NLog;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Web;
using System.Management;
using Microsoft.Exchange.WebServices.Data;
using ITSWebMgmt.Functions;
using ITSWebMgmt.Connectors;
using System.Web.UI.WebControls;
using ITSWebMgmt.Connectors.Active_Directory;

namespace ITSWebMgmt
{
    public partial class UserInfo : System.Web.UI.Page
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                ResultDiv.Visible = false;
                errordiv.Visible = false;

                String search = Request.QueryString["search"];
                if (search != null)
                {
                    lookupUser(search.Trim());
                    return;
                }

                String username = Request.QueryString["username"];
                if (username != null)
                {                
                    buildUserLookupFromUsername(username);
                    return;
                }

                String phoneNr = Request.QueryString["phone"];
                if (phoneNr != null)
                {               
                    buildUserLookupFromPhone(phoneNr);
                    return;
                }

            }
        }

        protected void lookupUser(string username)
        {

            
            int val;
            if (username.Length == 4 && int.TryParse(username, out val))
            {
                buildUserLookupFromPhone(username);
            }
            else
            {
                UserNameLabel.Text = username;
                buildUserLookupFromUsername(username);
            }
            
        }

        //Searhces on a phone numer (internal or external), and returns a upn (later ADsPath) to a use or null if not found
        protected string doPhoneSearch(string numberIn)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            string number = numberIn;
            //If number is a shot internal number, expand it :)
            if (number.Length == 4)
            {
                // format is 3452
                number = String.Format("+459940{0}", number);

            }
            else if (number.Length == 6)
            {
                //format is +453452 
                number = String.Format("+459940{0}", number.Replace("+45", ""));

            }
            else if (number.Length == 8)
            {
                //format is 99403442
                number = String.Format("+45{0}", number);

            } // else format is ok

            DirectoryEntry de = new DirectoryEntry("GC://aau.dk");
            //string filter = string.Format("(&(objectCategory=person)(telephoneNumber={0}))", number);
            string filter = string.Format("(&(objectCategory=person)(objectClass=user)(telephoneNumber={0}))", number);

            //logger.Debug(filter);

            DirectorySearcher search = new DirectorySearcher(de, filter);
            search.PropertiesToLoad.Add("userPrincipalName"); //Load something to speed up the object get?
            SearchResult r = search.FindOne();

            watch.Stop();
            System.Diagnostics.Debug.WriteLine("phonesearch took: " + watch.ElapsedMilliseconds);

            if (r != null)
            {
                //return r.Properties["ADsPath"][0].ToString(); //XXX handle if result is 0 (null exception)
                return r.Properties["ADsPath"][0].ToString().Replace("GC:","LDAP:").Replace("aau.dk/","");
            }
            else
            {
                return null;
            }

        }

        protected void buildgroupssegmentLabel(String[] groupsList, Label output)
        {
            StringBuilder sb = new StringBuilder();

            var helper = new HTMLTableHelper(2);
            sb.Append(helper.printStart());
            sb.Append(helper.printRow(new string[]{"Domain", "Name"}, true));

            var groupsAsList = groupsList.ToList<string>();

            /*Func<string[], string, bool> startsWith = delegate (string[] prefix, string value)
            {
                return prefix.Any<string>(x => value.StartsWith(x));
            };
            string[] prefixMBX_ACL = { "CN=MBX_", "CN=ACL_" };
            Func<string, bool> startsWithMBXorACL = (string value) => startsWith(prefixMBX_ACL, value);*/


            bool StartsWith(string[] prefix, string value) => prefix.Any(value.StartsWith);
            string[] prefixMBX_ACL = { "CN=MBX_", "CN=ACL_" };
            Func<string, bool> startsWithMBXorACL = value => StartsWith(prefixMBX_ACL, value);

            

            //Sort MBX and ACL Last
            groupsAsList.Sort((a, b) =>
            {
                if (startsWithMBXorACL(a) && startsWithMBXorACL(b))
                {
                    return a.CompareTo(b);
                } else if (startsWithMBXorACL(a))
                {
                    return 1;
                } else if (startsWithMBXorACL(b))
                {
                    return -1;
                } else
                {
                    return a.CompareTo(b);
                }
{'fraction_non_alphanumeric': 0.10593423449333716, 'fraction_numerical': 0.009395072380404563, 'mean_word_length': 1.4980842911877394, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '488126', 'n_tokens_mistral': 2786, 'n_tokens_neox': 2568, 'n_words': 593}