private static void UpdateScopingForGuardians(int playerID) { // make sure all guardians have parent scope to this player, and remove old ones if not explicit // since we are deleting and re-adding we will use a transaction using (ClubManagement cm = new ClubManagement()) using (TrackrModels.UserManagement um = new TrackrModels.UserManagement()) { try { List<int> userIDs = cm.Guardians.Where(i => i.PlayerID == playerID && i.Person.UserID.HasValue && i.Active).Select(i => i.Person.UserID.Value).Distinct().ToList(); // delete assignments for this player IQueryable<ScopeAssignment> assignmentsToDelete = um.ScopeAssignments.Where(i => i.ResourceID == playerID && i.ScopeID == 4 && i.RoleID == 6 && !i.IsExplicit); um.Delete(assignmentsToDelete); // add assignments back in foreach (int userID in userIDs) { ScopeAssignment assignment = new ScopeAssignment() { IsDeny = false, ResourceID = playerID, RoleID = 6, ScopeID = 4, UserID = userID, IsExplicit = false }; um.Add(assignment); } // commit um.SaveChanges(); } catch (Exception ex) { um.ClearChanges(); ex.HandleException(); } } }
protected void btnCreateUserAccounts_Click(object sender, EventArgs e) { int personID = int.Parse(hdnPersonID.Value); string email = radioList.SelectedValue.Trim(); string password = txtPassword.Text.Trim(); List<int> playerIDs = NonUserGuardianResults.First(i=>i.PersonID == personID).PlayerIDs.Distinct().ToList(); using(TrackrModels.UserManagement um = new TrackrModels.UserManagement()) using (ClubManagement cm = new ClubManagement()) using (NewUserMappingsController numc = new NewUserMappingsController()) { MembershipCreateStatus status; MembershipUser user = Membership.CreateUser(email, password, email, null, null, true, out status); switch (status) { case MembershipCreateStatus.Success: int userID = (int)user.ProviderUserKey; um.WebUsers.First(i => i.UserID == userID).ClubID = CurrentUser.ClubID; um.SaveChanges(); var roleIDs = numc.GetWhere(i => i.ClubID == CurrentUser.ClubID).Select(i => i.RoleID).Distinct().ToList(); foreach (int roleID in roleIDs) { ScopeAssignment assignment = new ScopeAssignment() { IsDeny = false, ScopeID = 5, UserID = userID, ResourceID = userID, RoleID = roleID }; um.Add(assignment); } foreach (int playerID in playerIDs) { ScopeAssignment assignment = new ScopeAssignment() { IsDeny = false, ScopeID = 4, //player scope UserID = userID, ResourceID = playerID, RoleID = 6 //parent role }; um.Add(assignment); } cm.Add(new WebUserInfo() { FName = "", LName = "", UserID = userID }); try { // try to save scoep assignments, if it fails then rollback user. i.e. delete user um.SaveChanges(); cm.SaveChanges(); Person person =cm.People.First(i => i.PersonID == personID); person.UserID = userID; cm.SaveChanges(); try { List<Trackr.Utils.Messenger.EmailRecipient> recipients = new List<Trackr.Utils.Messenger.EmailRecipient>(); recipients.Add(new Trackr.Utils.Messenger.EmailRecipient() { Email = email, Name = person.FName + " " + person.LName, RecipientType = Trackr.Utils.Messenger.EmailRecipientType.TO }); List<Trackr.Utils.Messenger.TemplateVariable> variables = new List<Trackr.Utils.Messenger.TemplateVariable>(); variables.Add(new Trackr.Utils.Messenger.TemplateVariable() { VariableName = "PersonName", VariableContent = person.FName + " " + person.LName }); variables.Add(new Trackr.Utils.Messenger.TemplateVariable() { VariableName = "LoginEmail", VariableContent = email }); variables.Add(new Trackr.Utils.Messenger.TemplateVariable() { VariableName = "LoginPassword", VariableContent = password }); Trackr.Utils.Messenger.SendEmail("user-account-created", null, variables, recipients, false, false); AlertBox.AddAlert(string.Format("Successfully created user account for {0} and emailed them their login information.", email)); } catch (Exception ex) { Guid guid = ex.HandleException(); AlertBox.AddAlert(string.Format("Successfully created user account for {0} but failed to email them their login information. Reference #: " + guid, email), false, UI.AlertBoxType.Error); } finally { ScriptManager.RegisterStartupScript(this, this.GetType(), "modal", "$('.modal-backdrop').remove()", true); gvAllNonUsers.DataBind(); uP_Main.Update(); } } catch (Exception ex) { Guid guid = ex.HandleException(); CreateAlertBox.AddAlert("An error occurred while trying to create new user from guardian. Please try again. Reference #: " + guid, false, UI.AlertBoxType.Error); um.ClearChanges(); cm.ClearChanges(); // remove any assignments um.Delete(um.ScopeAssignments.Where(i => i.UserID == userID).ToList()); um.Delete(um.WebUsers.First(i => i.UserID == userID)); um.SaveChanges(); } break; default: CreateAlertBox.AddAlert("Unable to create new user from guardian for the following reason: " + status.ToString(), false, UI.AlertBoxType.Error); break; } } }