protected void AddUserToAssociation_OnClick(object sender, EventArgs e) { int assoId; int.TryParse(AssociationList.SelectedValue, out assoId); var currentAssociation = AssociationDB.GetAssociationById(assoId); var selectedUser = UserDB.GetUserByUsername(UserToAddList1.SelectedValue); var selectedRole = RoleToAddList1.SelectedValue; if (selectedUser == null) { ActionStatusPermissions1.Text = "Selected user does not exist!"; return; } if (Membership.GetUser(selectedUser.Username) == null) { ActionStatusPermissions1.Text = "Selected user does not exist in the membership database!"; return; } if (String.IsNullOrWhiteSpace(selectedRole)) { ActionStatusPermissions1.Text = "Selected role value is empty!"; return; } if (!Roles.RoleExists(selectedRole)) { ActionStatusPermissions1.Text = "Selected role does not exist!"; return; } var newAssoPermission = new association_permissions { associations = currentAssociation, associations_Id = currentAssociation.Id, users = selectedUser, users_Id = selectedUser.Id, Role = selectedRole }; if (!Roles.IsUserInRole(selectedUser.Username, selectedRole)) { Roles.AddUserToRole(selectedUser.Username, selectedRole); } if ( !AssociationPermissionsDB.HasUserPermissionForAssociationWithRole(selectedUser, currentAssociation, selectedRole)) { if (AssociationPermissionsDB.AddAssociationPermissions(newAssoPermission)) { ActionStatusPermissions1.Text = "New permission for " + currentAssociation.Name + " was successfully added!"; } else { ActionStatusPermissions1.Text = "New permission for " + currentAssociation.Name + " could not be added!"; } } // Refresh the GridView BindPermissionsToAssociationUserList(); // Refresh the "by user" interface SelectAssociationsForSelectedUserAndRole(); }
// För att SKAPA en ny förening protected void ButtonCreateAsso_OnClick(object sender, EventArgs e) { bool isUniqueName = true; //Kontrollera att förening inte redan finns foreach (associations assoChecking in AssociationDB.GetAllAssociations()) { if (assoChecking.Name == TextBoxCreateAssoName.Text) { isUniqueName = false; } } if (!isUniqueName) { LabelErrorMessage.Text = "Association was not created. Name already exists. "; LabelErrorMessage.Style.Add(HtmlTextWriterStyle.Color, "red"); return; } associations asso = new associations { Name = TextBoxCreateAssoName.Text, ParentAssociationId = string.IsNullOrWhiteSpace(DropDownListCreateParAsso.SelectedValue) ? (int?) null : int.Parse(DropDownListCreateParAsso.SelectedItem.Value), LogoUrl = TextBoxAssoImgUrl.Text, CreatedBy = HttpContext.Current.User.Identity.Name, UpdatedBy = HttpContext.Current.User.Identity.Name, Communities_Id = int.Parse(DropDownListCommunity.SelectedItem.Value) }; if (AssociationDB.AddAssociation(asso)) { webpages wp = new webpages { Title = TextBoxCreateAssoName.Text, AssociationId = AssociationDB.GetAssociationByName(asso.Name).Id, //Layout och style - fixa dropdownlistor senare! CreatedBy = HttpContext.Current.User.Identity.Name, UpdatedBy = HttpContext.Current.User.Identity.Name }; if (WebPageDB.AddWebPage(wp)) { components compCal = new components { webpages_Id = wp.Id, OrderingNumber = 1, controls_Id = 3 //Calendar }; components compAbout = new components { webpages_Id = wp.Id, OrderingNumber = 2, controls_Id = 1 //About }; if (ComponentDB.AddComponent(compCal) && ComponentDB.AddComponent(compAbout)) { MultiViewAssoCreate.ActiveViewIndex = -1; PopulateAssociationListBox(); LabelErrorMessage.Text = asso.Name + " has been successfully created! (^o^)/"; LabelErrorMessage.Style.Add(HtmlTextWriterStyle.Color, "#217ebb"); } else { LabelCreateComm.Text = "Components could not be created. Please try again!"; } } else { LabelErrorMessage.Text = "Webpage could not be created. Try again!"; LabelErrorMessage.Style.Add(HtmlTextWriterStyle.Color, "red"); } //Välj admin för association som skapas if (UserDB.GetUserByUsername(ddlAdminUserAsso.SelectedValue) != null) { association_permissions aP = new association_permissions { users_Id = UserDB.GetUserByUsername(ddlAdminUserAsso.SelectedValue).Id, associations_Id = AssociationDB.GetAssociationByName(asso.Name).Id, Role = "Administrators" }; AssociationPermissionsDB.AddAssociationPermissions(aP); if (!Roles.IsUserInRole(ddlAdminUserAsso.SelectedValue, "Administrators")) { Roles.AddUserToRole(ddlAdminUserAsso.SelectedValue, "Administrators"); } LabelErrorMessage.Text += "User was successfully added as administrator in Association"; } else { LabelErrorMessage.Text += "No user was added as administrator."; LabelErrorMessage.Style.Add(HtmlTextWriterStyle.Color, "red"); } } else { LabelErrorMessage.Text = "Association could not be created. Try again!"; LabelErrorMessage.Style.Add(HtmlTextWriterStyle.Color, "red"); } PopulateAssociationListBox(); }