public ActionResult Create(FormCollection collection)
        {
            try
            {
                String FirstName = collection["FirstName"];
                String LastName = collection["LastName"];
                String EmailAddress = collection["EmailAddress"];
                String Description = collection["Description"];
                Boolean IsEnabled = collection["IsEnabled"].Contains("true");
                Boolean IsAdmin = collection["IsAdmin"].Contains("true");

                User user = new User();
                user.FirstName = FirstName;
                user.LastName = LastName;
                user.EmailAddress = EmailAddress;
                user.Description = Description;
                user.IsEnabled = IsEnabled;
                user.IsAdmin = IsAdmin;

                if (ModelState.IsValid)
                {
                    User createdUser = manager.Create(user);

                    dynamic email = new Email("Welcome");
                    email.Subject = "Welcome to LOVIS EOS";
                    email.User = user;
                    email.Deployment = ConfigurationManager.AppSettings["Deployment"];
                    email.FromName = ConfigurationManager.AppSettings["EmailFromName"];
                    email.FromEmail = ConfigurationManager.AppSettings["EmailFromEmail"];
                    email.Domain = ConfigurationManager.AppSettings["Domain"];
                    email.WebAccessServer = ConfigurationManager.AppSettings["WebAccessServer"];
                    email.ADFSUpdatePassword = ConfigurationManager.AppSettings["ADFSUpdatePassword"];
                    email.Send();

                    ViewBag.SuccessMessage = "The user has been successfully created.";
                    return View("Edit", createdUser);
                }
                return View(user);
            }
            catch (Exception Error)
            {
                ViewBag.ErrorMessage = Error.Message;
                return View("Error");
            }
        }
Example #2
0
 public IEnumerable<SharedFolder> GetSharedFolders(User user)
 {
     List<SharedFolder> sharedFolders = new List<SharedFolder>();
     using (PrincipalContext context = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["Domain"], GetClientDN()))
     {
         GroupPrincipal queryFilter = new GroupPrincipal(context);
         PrincipalSearcher searcher = new PrincipalSearcher(queryFilter);
         PrincipalSearchResult<Principal> results = searcher.FindAll();
         foreach (GroupPrincipal result in results)
         {
             if (result.Members.Contains(context, IdentityType.SamAccountName, user.UserName))
             {
                 SharedFolder sharedFolder = new SharedFolder()
                 {
                     Name = result.Name,
                     DistinguishedName = result.DistinguishedName,
                     Path = "Shared/BI"
                 };
                 sharedFolders.Add(sharedFolder);
             }
         }
     }
     return sharedFolders;
 }
Example #3
0
        public User ResetPassword(User user)
        {
            try
            {
                user.Password = GeneratePassword();

                using (PrincipalContext context = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["Domain"], GetClientDN()))
                {
                    UserPrincipal UpdateUser = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, user.UserName);
                    UpdateUser.SetPassword(user.Password);
                    UpdateUser.Save();
                    UpdateUser.Dispose();
                }
                return user;
            }
            catch (Exception Error)
            {
                throw Error;
            }
        }
Example #4
0
 public void Delete(User user)
 {
     string ClientPath = ConfigurationManager.AppSettings["ProfilesRoot"] + "\\" + GetClientName(GetClientDN());
     try
     {
         DirectoryInfo UserProfilePath = new DirectoryInfo(ClientPath + "\\" + user.UserName);
         if (UserProfilePath.Exists)
         {
             string DeletedUserPath = ClientPath + "\\zzz_deleted" + "\\" + user.UserName;
             if (Directory.Exists(DeletedUserPath))
             {
                 Directory.Delete(DeletedUserPath);
             }
             UserProfilePath.MoveTo(DeletedUserPath);
         }
         using (PrincipalContext context = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["Domain"], GetClientDN()))
         {
             UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, user.UserName).Delete();
         }
     }
     catch (DirectoryNotFoundException)
     {
         Directory.CreateDirectory(ClientPath + "\\zzz_deleted");
         Delete(user);
     }
     catch (Exception Error)
     {
         throw Error;
     }
 }
Example #5
0
        public void Update(User user)
        {
            try
            {
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["Domain"], GetClientDN()))
                {
                    UserPrincipal UpdateUser = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, user.UserName);
                    UpdateUser.GivenName = user.FirstName;
                    UpdateUser.Surname = user.LastName;
                    UpdateUser.DisplayName = user.FirstName + ' ' + user.LastName;
                    UpdateUser.EmailAddress = user.EmailAddress;
                    if (user.Description != "")
                    {
                        UpdateUser.Description = user.Description;
                    }
                    UpdateUser.Enabled = user.IsEnabled;
                    UpdateUser.Save();
                    UpdateUser.Dispose();
                }

                using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
                {
                    GroupPrincipal AdminGroup = GroupPrincipal.FindByIdentity(context, ConfigurationManager.AppSettings["AdminGroupName"]);
                    if (user.IsAdmin)
                    {
                        if (!AdminGroup.Members.Contains(context, IdentityType.SamAccountName, user.UserName))
                        {
                            AdminGroup.Members.Add(context, IdentityType.SamAccountName, user.UserName);
                            AdminGroup.Save();
                        }
                    }
                    else
                    {
                        if (AdminGroup.Members.Contains(context, IdentityType.SamAccountName, user.UserName))
                        {
                            AdminGroup.Members.Remove(context, IdentityType.SamAccountName, user.UserName);
                            AdminGroup.Save();
                        }
                    }
                    AdminGroup.Dispose();
                }
            }
            catch (Exception Error)
            {
                throw Error;
            }
        }
Example #6
0
 public User Find(string UserName)
 {
     User user = new User();
     using (PrincipalContext context = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["Domain"], GetClientDN()))
     {
         UserPrincipal adUser = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, UserName);
         user.FirstName = adUser.GivenName;
         user.LastName = adUser.Surname;
         user.UserName = adUser.SamAccountName;
         user.EmailAddress = adUser.EmailAddress;
         user.Description = adUser.Description;
         user.IsEnabled = (bool)adUser.Enabled;
         user.IsAdmin = adUser.IsMemberOf(GetAdminGroup());
     }
     return user;
 }
Example #7
0
        public User Create(User user)
        {
            try
            {
                string ClientPrefix = GetClientPrefix();
                string ClientName = GetClientName(GetClientDN());

                user.UserName = ClientPrefix + '.' + ToASCII(user.FirstName.Split(' ')[0].ToLower()) + '.' + ToASCII(user.LastName.Split(' ')[0].ToLower());
                if (user.UserName.Length >= 20)
                {
                    user.UserName = user.UserName.Substring(0, 20);
                }
                user.Password = GeneratePassword();

                // Create a confined context using the client's Organization Unit 
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["Domain"], GetClientDN()))
                {
                    // Create the user
                    UserPrincipal NewUser = new UserPrincipal(context);
                    NewUser.GivenName = user.FirstName;
                    NewUser.Surname = user.LastName;
                    NewUser.Name = user.FirstName + ' ' + user.LastName;
                    NewUser.DisplayName = user.FirstName + ' ' + user.LastName;
                    NewUser.SamAccountName = user.UserName;
                    NewUser.UserPrincipalName = user.UserName + '@' + ConfigurationManager.AppSettings["Domain"];
                    NewUser.EmailAddress = user.EmailAddress;
                    if (user.Description != "")
                    {
                        NewUser.Description = user.Description;
                    }
                    NewUser.SetPassword(user.Password);
                    NewUser.Enabled = user.IsEnabled;
                    NewUser.Save();
                    NewUser.Dispose();

                    // Add the user to the client's security group
                    GroupPrincipal ClientSecurityGroup = GroupPrincipal.FindByIdentity(context, ClientName);
                    ClientSecurityGroup.Members.Add(context, IdentityType.SamAccountName, user.UserName);
                    ClientSecurityGroup.Save();
                }

                // If the user has been marked as administrator, add it to the administrator group
                if (user.IsAdmin)
                {
                    using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
                    {
                        GroupPrincipal AdminGroup = GroupPrincipal.FindByIdentity(context, ConfigurationManager.AppSettings["AdminGroupName"]);
                        AdminGroup.Members.Add(context, IdentityType.SamAccountName, user.UserName);
                        AdminGroup.Save();
                    }
                }
                return user;
            }
            catch (Exception Error)
            {
                throw Error;
            }
        }
Example #8
0
        public IEnumerable<User> List()
        {
            try
            {
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["Domain"], GetClientDN()))
                {
                    UserPrincipal queryFilter = new UserPrincipal(context) { SamAccountName = GetClientPrefix() + ".*" };
                    PrincipalSearcher searcher = new PrincipalSearcher(queryFilter);
                    PrincipalSearchResult<Principal> results = searcher.FindAll();

                    GroupPrincipal AdminGroup = GetAdminGroup();
                    List<User> users = new List<User>();
                    foreach (UserPrincipal result in results)
                    {
                        User user = new User();
                        user.UserName = result.SamAccountName;
                        user.FirstName = result.GivenName;
                        user.LastName = result.Surname;
                        user.EmailAddress = result.EmailAddress;
                        user.IsEnabled = (bool)result.Enabled;
                        user.IsAdmin = result.IsMemberOf(AdminGroup);
                        users.Add(user);
                    }
                    users.Sort((x, y) => string.Compare(x.UserName, y.UserName));
                    return users;
                }
            }
            catch (Exception Error)
            {
                throw Error;
            }
        }