public ActionResult EditGroup(string id, FormCollection fields) { INestedRoleProvider nested = Roles.Provider as INestedRoleProvider; ExtendedRole role = nested.ExtendedGetRole(id); string origName = role.Name; TryUpdateModel(role, new[] { "EmailAddress", "Name" }); ModelState.SetModelValue("Owners", new ValueProviderResult(fields["Owners"], fields["Owners"], CultureInfo.CurrentUICulture)); role.Owners.Clear(); role.Owners.AddRange((fields["Owners"] ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(f => new Guid(f.Trim()))); ModelState.SetModelValue("Destinations", new ValueProviderResult(fields["Destinations"], fields["Destinations"], CultureInfo.CurrentUICulture)); role.Destinations.Clear(); role.Destinations.AddRange((fields["Destinations"] ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim())); try { nested.UpdateRole(role, origName); ViewData["success"] = "Saved OK"; } catch (Exception e) { ViewData["error"] = e.ToString(); } return(View(role)); }
public ContentResult ContactList(string id) { INestedRoleProvider nested = Roles.Provider as INestedRoleProvider; ExtendedRole role = nested.ExtendedGetRole(id); return(Content(string.Join("; ", nested.GetUsersInRole(id, true).Select(f => Membership.GetUser(f).Email).Distinct().ToArray()), "text/plain")); }
private bool CanUserEditRoleMembership(string id) { if (User.IsInRole("site.accounts")) { return(true); } INestedRoleProvider nested = Roles.Provider as INestedRoleProvider; if (nested == null) { return(false); } ExtendedRole role = nested.ExtendedGetRole(id); return(role.Owners.Contains(Permissions.UserId)); }
public ActionResult GroupMembership(string id, FormCollection fields) { INestedRoleProvider nested = Roles.Provider as INestedRoleProvider; if (!CanUserEditRoleMembership(id)) { return(CreateLoginRedirect()); } if (!string.IsNullOrEmpty(fields["AddGroup"])) { if (nested == null) { throw new InvalidOperationException("Configuration is using a RoleProvider that doesn't support nested groups"); } foreach (string child in fields.GetValues("OtherGroups")) { nested.AddRoleToRole(child, id); } } else if (!string.IsNullOrEmpty(fields["RemoveGroup"])) { if (nested == null) { throw new InvalidOperationException("Configuration is using a RoleProvider that doesn't support nested groups"); } foreach (string child in fields.GetValues("CurrentGroups")) { nested.RemoveRoleFromRole(child, id); } } else if (!string.IsNullOrEmpty(fields["AddUser"])) { Roles.AddUsersToRole(fields.GetValues("OtherUsers"), id); } else if (!string.IsNullOrEmpty(fields["RemoveUser"])) { Roles.RemoveUsersFromRole(fields.GetValues("CurrentUsers"), id); } return(RedirectToAction("GroupMembership", new { id = id })); }
public ActionResult GroupMembership(string id) { ExtendedRole group = Kcsar.Membership.RoleProvider.GetRole(id); INestedRoleProvider nested = Roles.Provider as INestedRoleProvider; ViewData["Title"] = "Membership for group " + id + " " + User.IsInRole("cdb.admins").ToString() + User.IsInRole("site.accounts").ToString() + User.IsInRole("admins").ToString(); GroupMembershipView view = new GroupMembershipView { GroupName = group.Name, ShowGroups = false, CanEdit = CanUserEditRoleMembership(id) }; var users = Membership.GetAllUsers(); var currentUsers = Roles.GetUsersInRole(id); view.CurrentUsers = new SelectList(currentUsers); List <string> otherUsers = new List <string>(); foreach (MembershipUser u in Membership.GetAllUsers()) { if (!currentUsers.Contains(u.UserName)) { otherUsers.Add(u.UserName); } } view.OtherUsers = new SelectList(otherUsers); if (nested != null) { view.ShowGroups = true; var currentGroups = nested.GetRolesInRole(id, false); view.CurrentGroups = new SelectList(currentGroups); view.OtherGroups = new SelectList(Roles.GetAllRoles().Where(g => !currentGroups.Contains(g))); } return(View(view)); }
//[Authorize] //public ActionResult ReconcileSubscriptions() //{ // string msgs = string.Empty; // INestedRoleProvider nested = Roles.Provider as INestedRoleProvider; // Dictionary<string, List<Guid>> visitedGroups = new Dictionary<string, List<Guid>>(); // using (var ctx = GetContext()) // { // foreach (var role in Kcsar.Membership.RoleProvider.GetRoles()) // { // msgs += "============= " + role.Name + "==================\n"; // if (string.IsNullOrWhiteSpace(role.EmailAddress)) // { // continue; // } // msgs += "============= " + role.Name + "==================\n"; // ReconcileSubscriptions(role.Name, nested, ctx, visitedGroups, ref msgs); // } // //ctx.SaveChanges(); // } // return new ContentResult { Content = "Done\n" + msgs, ContentType = "text/plain" }; //} //private void ReconcileSubscriptions(string role, INestedRoleProvider nested, KcsarContext ctx, Dictionary<string, List<Guid>> visited, ref string msgs) //{ // List<Guid> users = new List<Guid>(); // BuildMembershipListForSubscriptions(role, nested, users, visited, ref msgs); // Guid lastUser = Guid.Empty; // foreach (var row in (from s in ctx.PersonSubscription where s.ListName == role select new { User = s.Person.Id, Record = s }).OrderBy(f => f.User)) // { // // Keep track of the users we've seen so we can figure out which ones in the 'users' list are new. // // Because a member can have multiple subscriptions per list, we need to only // // act at the transition to a new user // if (row.User != lastUser && lastUser != Guid.Empty) // { // msgs += string.Format("-- {0}: users.Remove({1})\n", role, lastUser); // users.Remove(lastUser); // } // lastUser = row.User; // if (!users.Contains(row.User)) // { // string.Format("-- {0}: ctx.DeleteObject({1})\n", role, row.Record); // ctx.DeleteObject(row.Record); // } // } // if (lastUser != Guid.Empty) // { // msgs += string.Format("-- {0}: users.Remove({1})\n", role, lastUser); // users.Remove(lastUser); // } // foreach (Guid user in users) // { // PersonSubscription sub = new PersonSubscription // { // Person = (from m in ctx.Members where m.Id == user select m).First(), // ListName = role // }; // System.Diagnostics.Debug.WriteLine(string.Format("{0} {1}", role, user)); // msgs += string.Format("-- {0}: ctx.AddToPersonSubscription({1})\n", role, sub.Person.FullName); // ctx.AddToPersonSubscription(sub); // try // { // //ctx.SaveChanges(); // } // catch // { // } // } //} /// <summary> /// Gets the membership of a group, with special logic for recursion. For each member of this group: /// - If the child is a user, add the user to the list. /// - If the child is a group, and the group has an email address, only add the email address of the group /// - If the child is a group, and there is no email address, recursively add group's children. /// </summary> /// <param name="role"></param> /// <param name="nested"></param> /// <param name="users"></param> /// <param name="visited"></param> private void BuildMembershipListForSubscriptions(string role, INestedRoleProvider nested, List <Guid> users, Dictionary <string, List <Guid> > visited, ref string msgs) { List <Guid> myUsers = new List <Guid>(); // If we've already cached this group, then we don't need to rebuild it. if (!visited.ContainsKey(role)) { // Get all users in the group foreach (var username in nested.GetUsersInRole(role, false)) { Guid?key = Kcsar.Membership.MembershipProvider.UsernameToMemberKey(username); if (key.HasValue && !users.Contains(key.Value)) { myUsers.Add(key.Value); } } foreach (var child in nested.GetRolesInRole(role, false)) { ExtendedRole r = Kcsar.Membership.RoleProvider.GetRole(child); if (string.IsNullOrEmpty(r.EmailAddress)) { BuildMembershipListForSubscriptions(child, nested, users, visited, ref msgs); } } visited.Add(role, myUsers); } // Add any new children to the group. foreach (Guid user in myUsers) { if (!users.Contains(user)) { users.Add(user); } } }
//[Authorize] //public ActionResult ReconcileSubscriptions() //{ // string msgs = string.Empty; // INestedRoleProvider nested = Roles.Provider as INestedRoleProvider; // Dictionary<string, List<Guid>> visitedGroups = new Dictionary<string, List<Guid>>(); // using (var ctx = GetContext()) // { // foreach (var role in Kcsar.Membership.RoleProvider.GetRoles()) // { // msgs += "============= " + role.Name + "==================\n"; // if (string.IsNullOrWhiteSpace(role.EmailAddress)) // { // continue; // } // msgs += "============= " + role.Name + "==================\n"; // ReconcileSubscriptions(role.Name, nested, ctx, visitedGroups, ref msgs); // } // //ctx.SaveChanges(); // } // return new ContentResult { Content = "Done\n" + msgs, ContentType = "text/plain" }; //} //private void ReconcileSubscriptions(string role, INestedRoleProvider nested, KcsarContext ctx, Dictionary<string, List<Guid>> visited, ref string msgs) //{ // List<Guid> users = new List<Guid>(); // BuildMembershipListForSubscriptions(role, nested, users, visited, ref msgs); // Guid lastUser = Guid.Empty; // foreach (var row in (from s in ctx.PersonSubscription where s.ListName == role select new { User = s.Person.Id, Record = s }).OrderBy(f => f.User)) // { // // Keep track of the users we've seen so we can figure out which ones in the 'users' list are new. // // Because a member can have multiple subscriptions per list, we need to only // // act at the transition to a new user // if (row.User != lastUser && lastUser != Guid.Empty) // { // msgs += string.Format("-- {0}: users.Remove({1})\n", role, lastUser); // users.Remove(lastUser); // } // lastUser = row.User; // if (!users.Contains(row.User)) // { // string.Format("-- {0}: ctx.DeleteObject({1})\n", role, row.Record); // ctx.DeleteObject(row.Record); // } // } // if (lastUser != Guid.Empty) // { // msgs += string.Format("-- {0}: users.Remove({1})\n", role, lastUser); // users.Remove(lastUser); // } // foreach (Guid user in users) // { // PersonSubscription sub = new PersonSubscription // { // Person = (from m in ctx.Members where m.Id == user select m).First(), // ListName = role // }; // System.Diagnostics.Debug.WriteLine(string.Format("{0} {1}", role, user)); // msgs += string.Format("-- {0}: ctx.AddToPersonSubscription({1})\n", role, sub.Person.FullName); // ctx.AddToPersonSubscription(sub); // try // { // //ctx.SaveChanges(); // } // catch // { // } // } //} /// <summary> /// Gets the membership of a group, with special logic for recursion. For each member of this group: /// - If the child is a user, add the user to the list. /// - If the child is a group, and the group has an email address, only add the email address of the group /// - If the child is a group, and there is no email address, recursively add group's children. /// </summary> /// <param name="role"></param> /// <param name="nested"></param> /// <param name="users"></param> /// <param name="visited"></param> private void BuildMembershipListForSubscriptions(string role, INestedRoleProvider nested, List<Guid> users, Dictionary<string, List<Guid>> visited, ref string msgs) { List<Guid> myUsers = new List<Guid>(); // If we've already cached this group, then we don't need to rebuild it. if (!visited.ContainsKey(role)) { // Get all users in the group foreach (var username in nested.GetUsersInRole(role, false)) { Guid? key = Kcsar.Membership.MembershipProvider.UsernameToMemberKey(username); if (key.HasValue && !users.Contains(key.Value)) { myUsers.Add(key.Value); } } foreach (var child in nested.GetRolesInRole(role, false)) { ExtendedRole r = Kcsar.Membership.RoleProvider.GetRole(child); if (string.IsNullOrEmpty(r.EmailAddress)) { BuildMembershipListForSubscriptions(child, nested, users, visited, ref msgs); } } visited.Add(role, myUsers); } // Add any new children to the group. foreach (Guid user in myUsers) { if (!users.Contains(user)) { users.Add(user); } } }