public ViewResult GetUserDetails(String email, String selectedrole)
 {
     User user = userRepository.GetUserByEmail(email);
     AccountManagerModel model = new AccountManagerModel();
     model.UserName = user.UserName;
     model.Email = user.Email;
     model.FirstName = user.FirstName;
     model.LastName = user.LastName;
     model.Gender = user.Gender;
     model.Organisation = user.Organisation;
     model.Position = user.Position;
     model.City = user.City;
     model.Country = user.Country;
     model.SelectedRole = selectedrole;
     model.GenericEmails = new List<GenericEmail>();
     model.IsEditable = false;
     model.IsNew = true;
     return View("AccountManagerContainer", model);
 }
 public ViewResult GetNewUserDetails(String username, String email, String firstname, String lastname, 
     String organisation, String position, String city, String country, String selectedrole, String gender,
     String isgeneric)
 {
     AccountManagerModel model = new AccountManagerModel();
     model.UserName = username;
     model.Email = email;
     model.FirstName = firstname;
     model.LastName = lastname;
     model.Organisation = organisation;
     model.Position = position;
     model.City = city;
     model.Country = country;
     model.SelectedRole = selectedrole;
     model.IsEditable = true;
     model.IsNew = true;
     model.IsGeneric = isgeneric == "true" ? true : false;
     model.GenericEmails = new List<GenericEmail>();
     if (gender == "Male") model.Gender = Gender.Male;
     else if (gender == "Female") model.Gender = Gender.Female;
     return View("AccountManagerContainer", model);
 }
 public ActionResult AccountManager(String username, String role)
 {
     if (username == null && role == null)
     {
         return View(DefaultMake());
     }
     else if (username != null)
     {
         var user = userRepository.GetUserByUsername(username);
         var model = new AccountManagerModel();
         model.UserName = user.UserName;
         model.FirstName = user.FirstName;
         model.LastName = user.LastName;
         model.City = user.City;
         model.Country = user.Country;
         model.Email = user.Email;
         model.Gender = user.Gender;
         model.GenericEmails = user.GenericEmails.ToList();
         model.IsGeneric = user.IsGeneric;
         model.Organisation = user.Organisation;
         model.Position = user.Position;
         model.AccountExpiryDate = user.AccountExpiryDate;
         model.IsEditable = false;
         model.IsNew = false;
         if (user.Roles.Contains(roleRepository.GetRoleByName("System Administrator")))
             model.SelectedRole = "Poll Administrator";
         else if (user.Roles.Contains(roleRepository.GetRoleByName("Poll Administrator")))
         {
             model.SelectedRole = "Poll Administrator";
             model.InUsersGroup = userRepository.GetPollAdministratorsInUsersGroup(model.UserName).ToList<User>();
             model.NotInUsersGroup = userRepository.GetPollAdministratorsNotInUsersGroup(model.UserName).ToList<User>();
         }
         else if (user.Roles.Contains(roleRepository.GetRoleByName("Poll Creator")))
             model.SelectedRole = "Poll Creator";
         else if (user.Roles.Contains(roleRepository.GetRoleByName("Poll Master")))
             model.SelectedRole = "Poll Master";
         else if (user.Roles.Contains(roleRepository.GetRoleByName("Poll User")))
             model.SelectedRole = "Poll User";
         return View(model);
     }
     else
     {
         String[] roles = { "Poll Administrator", "Poll Creator", "Poll Master", "Poll User" };
         if (!roles.Contains(role)) return View(DefaultMake());
         var user = userRepository.GetUserByUsername(User.Identity.Name);
         if (!user.Roles.Contains(roleRepository.GetRoleByName("System Administrator")) && role == "Poll Administrator")
             return View(DefaultMake());
         if (!user.Roles.Contains(roleRepository.GetRoleByName("Poll Administrator")) && role == "Poll Creator")
             return View(DefaultMake());
         if (!user.Roles.Contains(roleRepository.GetRoleByName("Poll Creator")) && role == "Poll Master")
             return View(DefaultMake());
         if (!user.Roles.Contains(roleRepository.GetRoleByName("Poll Master")) && role == "Poll User")
             return View(DefaultMake());
         var model = new AccountManagerModel();
         model.GenericEmails = new List<GenericEmail>();
         model.IsGeneric = false;
         model.IsEditable = true;
         model.IsNew = true;
         model.SelectedRole = role;
         if (model.SelectedRole == "Poll Administrator")
         {
             model.AccountExpiryDate = DateTime.Now.AddYears(1);
             model.InUsersGroup = new List<User>();
             model.NotInUsersGroup = userRepository.GetUsersByRoleName("Poll Administrator").ToList();
         }
         return View(model);
     }
 }
 public AccountManagerModel DefaultMake()
 {
     var model = new AccountManagerModel();
     model.GenericEmails = new List<GenericEmail>();
     model.IsGeneric = false;
     model.IsEditable = true;
     model.IsNew = true;
     model.UserName = String.Empty;
     if (User.IsInRole("System Administrator"))
     {
         model.AccountExpiryDate = DateTime.Now.AddYears(1);
         model.SelectedRole = "Poll Administrator";
         model.InUsersGroup = new List<User>();
         model.NotInUsersGroup = userRepository.GetUsersByRoleName("Poll Administrator").ToList();
     }
     else if (User.IsInRole("Poll Administrator"))
         model.SelectedRole = "Poll Creator";
     else if (User.IsInRole("Poll Creator"))
         model.SelectedRole = "Poll Master";
     else if (User.IsInRole("Poll Master"))
         model.SelectedRole = "Poll User";
     return model;
 }
 public ViewResult AddPollAdministratorToGroup(String username1, String username2)
 {
     ClientCode code1 = clientCodeRepository.GetClientCodeByUserName(username1);
     ClientCode code2 = clientCodeRepository.GetClientCodeByUserName(username2);
     List<User> users1 = userRepository.GetUsersByClientCode(code1).ToList<User>();
     List<User> users2 = userRepository.GetUsersByClientCode(code2).ToList<User>();
     foreach (User user1 in users1)
     {
         if ((!user1.Roles.Contains(roleRepository.GetRoleByName("Poll Administrator"))
             && !user1.Roles.Contains(roleRepository.GetRoleByName("System Administrator")))
             || user1.UserName == username1)
         {
             user1.ClientCodes.Add(code2);
             userRepository.Update(user1);
         }
     }
     foreach (User user2 in users2)
     {
         if ((!user2.Roles.Contains(roleRepository.GetRoleByName("Poll Administrator"))
             && !user2.Roles.Contains(roleRepository.GetRoleByName("System Administrator")))
             || user2.UserName == username2)
         {
             user2.ClientCodes.Add(code1);
             userRepository.Update(user2);
         }
     }
     AccountManagerModel model = new AccountManagerModel();
     model.InUsersGroup = userRepository.GetPollAdministratorsInUsersGroup(username2).ToList<User>();
     model.NotInUsersGroup = userRepository.GetPollAdministratorsNotInUsersGroup(username2).ToList<User>();
     return View("PollAdministratorGroups", model);
 }
        public ActionResult AccountManager(AccountManagerModel model)
        {
            if (model.GenericEmails == null) model.GenericEmails = new List<GenericEmail>();
            if (ModelState.IsValid)
            {
                if (model.IsEditable)
                {
                    String password = Membership.GeneratePassword(8, 0);
                    MembershipCreateStatus createStatus = MembershipService.
                        CreateUser(model.UserName, password,
                        model.IsGeneric == false ? model.Email : Membership.GeneratePassword(8, 0), model.IsGeneric);
                    if (createStatus == MembershipCreateStatus.Success)
                    {
                        var user = userRepository.GetUserByUsername(model.UserName);
                        user.IsGeneric = model.IsGeneric;
                        user.GenericEmails = model.GenericEmails;
                        user.FirstName = model.FirstName;
                        user.LastName = model.LastName;
                        user.Gender = model.Gender;
                        user.Position = model.Position;
                        user.City = model.City;
                        user.Country = model.Country;
                        user.Organisation = model.Organisation;
                        user.AccountExpiryDate = model.AccountExpiryDate;
                        if (model.SelectedRole != "Poll Administrator")
                        {
                            model.AccountExpiryDate = DateTime.MaxValue;
                        }
                        if (model.SelectedRole == "Poll Administrator")
                        {
                            user.ClientCodes.Add(new ClientCode(user));
                            model.InUsersGroup = new List<User>();
                            model.NotInUsersGroup = new List<User>();
                            var owner = userRepository.GetUserByUsername(User.Identity.Name);
                            model.NotInUsersGroup.AddRange(userRepository.GetUsersUserCanManage(User.Identity.Name).
                                Where(u => u.Roles.Contains(roleRepository.GetRoleByName("Poll Administrator"))));
                        }
                        else
                        {
                            var creator = userRepository.GetUserByUsername(User.Identity.Name);
                            foreach (ClientCode code in creator.ClientCodes)
                            {
                                if (!user.ClientCodes.Contains(code))
                                {
                                    user.ClientCodes.Add(code);
                                }
                            }
                        }
                        userRepository.Update(user);
                        Roles.AddUserToRole(model.UserName, model.SelectedRole);
                        Role selectedRole = roleRepository.GetRoleByName(model.SelectedRole);
                        foreach (Role role in selectedRole.InheritedRoles)
                        {
                            if (!user.Roles.Contains(role))
                            {
                                Roles.AddUserToRole(model.UserName, role.RoleName);
                            }
                        }

                        if (model.IsGeneric)
                        {
                            Helpers.Email.SendTemplatedGenericEmails("*****@*****.**",
                                user.GenericEmails.ToList<GenericEmail>(),
                                "dbPoll Account Details",
                                Url.Action("GenericEmail", "Email", 0, "http") +
                                "?username="******"&password="******"&heading=" + HttpUtility.UrlEncode("Welcome to dbPoll") +
                                "&msg=" + HttpUtility.UrlEncode("You have just been invited to use dbPoll.  Your details needed to sign-in to dbPoll are as follows: "));
                        }
                        else
                        {
                            Helpers.Email.SendTemplatedEmail("*****@*****.**",
                                user.Email,
                                "dbPoll Account Details",
                                Url.Action("GenericEmail", "Email", 0, "http") +
                                "?username="******"&password="******"&heading=" + HttpUtility.UrlEncode("Welcome to dbPoll") +
                                "&msg=" + HttpUtility.UrlEncode("You have just been invited to use dbPoll.  Your details needed to sign-in to dbPoll are as follows: "));
                        }
                        model.IsNew = false;
                        model.IsEditable = false;
                        return PartialView("AccountManagerContainer", model);
                    }
                    else
                    {
                        ModelState.AddModelError("", "Error, " + AccountValidation.ErrorCodeToString(createStatus));
                        return PartialView("AccountManagerContainer", model);
                    }
                }
                else
                {
                    var user = userRepository.GetUserByUsername(model.UserName);

                    if (model.SelectedRole == "Poll Administrator")
                    {
                        user.ClientCodes.Add(new ClientCode(user));
                        user.City = model.City;
                        user.Country = model.Country;
                        user.FirstName = model.FirstName;
                        user.Gender = model.Gender;
                        user.LastName = model.LastName;
                        user.Organisation = model.Organisation;
                        user.Position = model.Position;
                        model.IsEditable = true;
                        model.InUsersGroup = userRepository.GetPollAdministratorsInUsersGroup(model.UserName).ToList<User>();
                        model.NotInUsersGroup = userRepository.GetPollAdministratorsNotInUsersGroup(model.UserName).ToList<User>();
                    }
                    else
                    {
                        var creator = userRepository.GetUserByUsername(User.Identity.Name);
                        foreach (ClientCode code in creator.ClientCodes)
                        {
                            if (!user.ClientCodes.Contains(code))
                            {
                                user.ClientCodes.Add(code);
                            }
                        }
                    }

                    if (!user.Roles.Contains(roleRepository.GetRoleByName(model.SelectedRole)))
                    {
                        Roles.AddUserToRole(model.UserName, model.SelectedRole);
                    }
                    Role selectedRole = roleRepository.GetRoleByName(model.SelectedRole);
                    foreach (Role role in selectedRole.InheritedRoles)
                    {
                        if (!user.Roles.Contains(roleRepository.GetRoleByName(role.RoleName)))
                        {
                            Roles.AddUserToRole(model.UserName, role.RoleName);
                        }
                    }

                    user.AccountExpiryDate = model.AccountExpiryDate == DateTime.MinValue ? DateTime.Now.AddYears(1) : model.AccountExpiryDate;
                    model.AccountExpiryDate = user.AccountExpiryDate;
                    user.IsDeleted = false;
                    userRepository.Update(user);
                    model.IsNew = false;
                    return PartialView("AccountManagerContainer", model);

                }
            }
            return PartialView("AccountManagerContainer", model);
        }