protected void AddUserToCommunity_OnClick(object sender, EventArgs e) { int commId; int.TryParse(CommunityList.SelectedValue, out commId); var currentCommunity = CommunityDB.GetCommunityById(commId); var selectedUser = UserDB.GetUserByUsername(UserToAddList2.SelectedValue); var selectedRole = RoleToAddList2.SelectedValue; if (selectedUser == null) { ActionStatusPermissions2.Text = "Selected user does not exist!"; return; } if (Membership.GetUser(selectedUser.Username) == null) { ActionStatusPermissions2.Text = "Selected user does not exist in the membership database!"; return; } if (String.IsNullOrWhiteSpace(selectedRole)) { ActionStatusPermissions2.Text = "Selected role value is empty!"; return; } if (!Roles.RoleExists(selectedRole)) { ActionStatusPermissions2.Text = "Selected role does not exist!"; return; } var newCommPermission = new community_permissions { communities = currentCommunity, communities_Id = currentCommunity.Id, users = selectedUser, users_Id = selectedUser.Id, Role = selectedRole }; if (!Roles.IsUserInRole(selectedUser.Username, selectedRole)) { Roles.AddUserToRole(selectedUser.Username, selectedRole); } if ( !CommunityPermissionsDB.HasUserPermissionForCommunityWithRole(selectedUser, currentCommunity, selectedRole)) { if (CommunityPermissionsDB.AddCommunityPermissions(newCommPermission)) { ActionStatusPermissions2.Text = "New permission for " + currentCommunity.Name + " was successfully added!"; } else { ActionStatusPermissions2.Text = "New permission for " + currentCommunity.Name + " could not be added!"; } } BindPermissionsToCommunityUserList(); SelectCommunitiesForSelectedUserAndRole(); }
// För att skapa en ny community protected void ButtonCreateComm_OnClick(object sender, EventArgs e) { bool isUniqueName = true; //Kontrollera att namnet inte redan finns foreach (communities commChecking in CommunityDB.GetAllCommunities()) { if (commChecking.Name == TextBoxCommNameCreate.Text) { isUniqueName = false; } }; if (!isUniqueName) { LabelCreateComm.Text = "Community was not created. A community with the same name already exists. "; LabelCreateComm.Style.Add(HtmlTextWriterStyle.Color, "red"); return; } var comm = new communities { Name = TextBoxCommNameCreate.Text, LogoUrl = TextBoxCommLogoUrl.Text, CreatedBy = HttpContext.Current.User.Identity.Name, UpdatedBy = HttpContext.Current.User.Identity.Name }; if (CommunityDB.AddCommunity(comm)) { webpages wp = new webpages { Title = TextBoxCommNameCreate.Text, CommunityId = CommunityDB.GetCommunityByName(comm.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)) { MultiViewCommDetails.ActiveViewIndex = 0; MultiViewCommCreate.ActiveViewIndex = -1; ShowCommunityDetails(CommunityDB.GetCommunityByName(comm.Name)); } else { LabelCreateComm.Text = "Components could not be created. Please try again!"; LabelCreateComm.Style.Add(HtmlTextWriterStyle.Color, "red"); } } else { LabelCreateComm.Text = "Webpage could not be created. Try again!"; LabelCreateComm.Style.Add(HtmlTextWriterStyle.Color, "red"); } //Välj admin för communityn som skapas if (UserDB.GetUserByUsername(ddlAdminUser.SelectedValue) != null) { community_permissions cP = new community_permissions { users_Id = UserDB.GetUserByUsername(ddlAdminUser.SelectedValue).Id, communities_Id = CommunityDB.GetCommunityByName(comm.Name).Id, //comm.Id Role = "Administrators" }; CommunityPermissionsDB.AddCommunityPermissions(cP); if (!Roles.IsUserInRole(ddlAdminUser.SelectedValue, "Administrators")) { Roles.AddUserToRole(ddlAdminUser.SelectedValue, "Administrators"); } } } else { LabelCreateComm.Text = "Community could not be created. Try again!"; LabelCreateComm.Style.Add(HtmlTextWriterStyle.Color, "red"); } PopulateCommunityDropDownList(DropDownListCommunity); ListBoxAsso.Items.Clear(); LabelCommSave.Text = string.Empty; //Use string.Empty instead of "". ALWAYS. }