public ActionResult CreateAccount(string memberId)
        {
            string result = "ok";

            try
            {
                LdapUserConfig uc = new LdapUserConfig(memberId, new Lcps.UI.Models.LcpsUiContext());
                uc.SyncUser(false);
            }
            catch(Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                result = ec.ToUL();
            }

            return Content(result);
        }
        public ActionResult SyncGroups(Guid id)
        {
            string result = "ok";

            try
            {
                LdapUserConfig uc = new LdapUserConfig(id.ToString(), new Lcps.UI.Models.LcpsUiContext());
                uc.SyncGroupMemberships(true);
            }
            catch(Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                result = ec.ToUL();
            }

            return Content(result);
        }
        public ActionResult RenameFolder(string currentName, string memberId, PersonalFolderIdFormats format)
        {
            string result = "ok";

            try
            {
                DirectoryInfo d = new DirectoryInfo(currentName);
                DirectoryMember m = _contextManager.DirectoryContext.DirectoryMembers.GetByID(memberId);

                string newName = LdapUserConfig.FormatFolderName(m, format);
                string thisPath = Path.GetDirectoryName(d.FullName);
                string newPath = Path.Combine(thisPath, newName);
                d.MoveTo(newPath);
            }
            catch(Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                result = ec.ToUL();
            }

            return Content(result);
        }
        public ActionResult CreateGroupConfig(string caption, long filter, Guid id)
        {
            string result = "ok";


            try
            {
                if (filter == 0)
                    throw new Exception("Please select a category to filter members by");

                if (id.Equals(Guid.Empty))
                {
                    GroupAssignmentConfig config = new GroupAssignmentConfig()
                    {
                        Caption = caption,
                        MembershipScope = filter,
                        GroupAssignmentKey = Guid.NewGuid()
                    };

                    ldapContext.GroupAssignmentConfigs.Insert(config);
                }
                else
                {
                    GroupAssignmentConfig config = ldapContext.GroupAssignmentConfigs.GetByID(id);
                    config.Caption = caption;
                    config.MembershipScope = filter;
                    ldapContext.GroupAssignmentConfigs.Update(config);
                }

            }
            catch (Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                result = ec.ToUL();
            }

            return Content(result);

        }
        public ActionResult Submit(string p, long s, Guid id, PersonalFolderIdFormats nf)
        {
            string result = "ok";

            try
            {
                PersonalFolder f = null;
                
                if(id.Equals(Guid.Empty))
                {

                    f = new PersonalFolder()
                    {
                        PersonalFolderKey = Guid.NewGuid(),
                        FolderPath = p,
                        NameFormat = nf,
                        MembershipScope = s
                    };

                    cm.LdapContext.PersonalFolders.Insert(f);
                }
                else
                {
                    f = cm.LdapContext.PersonalFolders.GetByID(id);
                    f.MembershipScope = s;
                    f.FolderPath = p;
                    f.NameFormat = nf;
                    cm.LdapContext.PersonalFolders.Update(f);
                }
            }
            catch(Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                result = ec.ToUL();
            }

            return Content(result);
        }
        public ActionResult Update([Bind(Include = "DomainPrincipalName,UserName,Password")] LdapConfig config)
        {
            ViewBag.Result = "ok";

            LdapConfig test = context.LdapConfigs.First(x => x.DomainPrincipalName.ToLower().Equals(config.DomainPrincipalName.ToLower()));

            if(ModelState.IsValid)
            {
                try
                {
                    if (test == null)
                        context.LdapConfigs.Insert(config);
                    else
                        context.LdapConfigs.Update(config);
                }
                catch (Exception ex)
                {
                    ExceptionCollector ec = new ExceptionCollector(ex);
                    ViewBag.Result = ec.ToUL();
                }
            }

            return View("Index");
        }
        public ActionResult CreateAssignment(string ou, long filter)
        {
            string result = "ok";

            OuAssignment assignment = new OuAssignment() { 
                OuAssignmentKey = Guid.NewGuid(),
                MembershipScope = filter,
                OuDistinguishedName = ou
            };

            try
            {
                context.OuAssignments.Insert(assignment);

            }
            catch(Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                result = ec.ToUL();
            }

            return Content(result);

        }
        public ActionResult AssignRights(string currentName, string memberId)
        {
            string result = "ok";
            try
            {
                LdapUserConfig cfg = new LdapUserConfig(memberId, new Lcps.UI.Models.LcpsUiContext());
                DirectoryEntry de = (DirectoryEntry)cfg.LdapUser.GetUnderlyingObject();

                LdapUserConfig.GrantFullAccessToFolder(currentName, de, cfg.Member.UserName, cfg.LdapConfig.DomainPrincipalName);


            }
            catch(Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                result = ec.ToUL();
            }

            return Content(result);
        }
        public string DecryptPassword()
        {
            try
            {
                Anvil.RijndaelEnhanced enc = new Anvil.RijndaelEnhanced(GuidKey);
                string pwd = enc.Decrypt(this.InitialPassword);
                return pwd;
            }
            catch (Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                return ec.ToUL();

            }
        }
        public ActionResult AddAssignment(Guid configId, string groupDn)
        {
            string result = "ok";

            try
            {
                GroupAssignment g = new GroupAssignment()
                {
                    GroupAssignmentKey = Guid.NewGuid(),
                    GroupDN = groupDn,
                    GroupConfigKey = configId

                };

                ldapContext.GroupAssignments.Insert(g);

            }
            catch (Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                result = ec.ToUL();
            }

            return Content(result);
        }
        public ActionResult RemoveAssignment(Guid id)
        {
            string result = "ok";

            try
            {
                ldapContext.GroupAssignments.Delete(id);
            }
            catch(Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                result = ec.ToUL();
            }

            return Content(result);
        }
        public ActionResult CreateStudentAsp()
        {
            string result = "ok";

            try
            {
                List<StudentCandidate> users = context.GetStudentsWithNoAsp()
                        .OrderBy(x => x.Surname).ThenBy(x => x.GivenName).ThenBy(x => x.MiddleName)
                        .ToList();

                foreach(IDirectoryMember i in users)
                {
                    DirectoryMember m = new DirectoryMember(i);
                    m.ConfirmPassword = m.InitialPassword;
                    cm.MemberContext.Insert(m);
                }
            }
            catch(Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                result = ec.ToUL();
            }

            return Content(result);
        }
        public ActionResult SyncLdapAccount(Guid id)
        {
            string result = "ok";

            LdapUserConfig cfg = new LdapUserConfig(id.ToString(), _dbContext);
            
            try
            {
                cfg.SyncUser(false);
            }
            catch (Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                result = ec.ToUL();
            }

            return Content(result);
        }
        public ActionResult Edit([Bind(Include = "Id,InternalId,Surname,GivenName,MiddleName,DOB,Gender,Email,Title")] DirectoryMember member)
        {
            ViewBag.Error = null;

            DirectoryMember m = _contextManager.MemberContext.GetByID(member.Id);

            try
            {
                m.InternalId = member.InternalId;
                m.Surname = member.Surname;
                m.GivenName = member.GivenName;
                m.MiddleName = member.MiddleName;
                m.DOB = member.DOB;
                m.Gender = member.Gender;
                m.Email = member.Email;
                m.Title = member.Title;

                _contextManager.MemberContext.Update(m);

                ViewBag.Success = true;

                return View(member);
            }            
            catch(Exception ex)
            {
                ExceptionCollector ec = new ExceptionCollector(ex);
                foreach(string s in ec)
                {
                    ModelState.AddModelError("", s);
                }

                ViewBag.Success = false;

                return View(member);

            }
            

        }