//ADD public static bool AddAssociationPermissions(association_permissions aP) { Context.association_permissions.Add(aP); try { Context.SaveChanges(); } catch (DbUpdateException dbEx) { return false; } catch (DbEntityValidationException ex) { foreach (DbEntityValidationResult item in ex.EntityValidationErrors) { // Get entry DbEntityEntry entry = item.Entry; string entityTypeName = entry.Entity.GetType().Name; // Display or log error messages foreach (DbValidationError subItem in item.ValidationErrors) { string message = string.Format("Error '{0}' occurred in {1} at {2}", subItem.ErrorMessage, entityTypeName, subItem.PropertyName); Console.WriteLine(message); } // Rollback changes switch (entry.State) { case EntityState.Added: entry.State = EntityState.Detached; break; case EntityState.Modified: entry.CurrentValues.SetValues(entry.OriginalValues); entry.State = EntityState.Unchanged; break; case EntityState.Deleted: entry.State = EntityState.Unchanged; break; } } return false; } Context.Entry(aP).Reload(); return true; }
protected void BtnAddUser_OnClick(object sender, EventArgs e) { int assoId; int commId; int.TryParse(HiddenFieldAssociationId.Value, out assoId); int.TryParse(HiddenFieldCommunityId.Value, out commId); var currentAssociation = AssociationDB.GetAssociationById(assoId); var currentCommunity = CommunityDB.GetCommunityById(commId); var selectedUser = UserDB.GetUserByUsername(UserList.SelectedValue); var selectedRole = RoleList.SelectedValue; if (selectedUser == null) { ActionStatus.Text = "Selected user does not exist!"; return; } if (Membership.GetUser(selectedUser.Username) == null) { ActionStatus.Text = "Selected user does not exist in the membership database!"; return; } if (String.IsNullOrWhiteSpace(selectedRole)) { ActionStatus.Text = "Selected role value is empty!"; return; } if(!Roles.RoleExists(selectedRole)) { ActionStatus.Text = "Selected role does not exist!"; return; } if (currentAssociation != null && HiddenFieldWebPageType.Value == "A") { 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)) { ActionStatus.Text = "New permission for " + currentAssociation.Name + " was successfully added!"; } else { ActionStatus.Text = "New permission for " + currentAssociation.Name + " could not be added!"; } } BindPermissionsToPermissionUserList(); return; } if (currentCommunity != null && HiddenFieldWebPageType.Value == "C") { 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)) { ActionStatus.Text = "New permission for " + currentCommunity.Name + " was successfully added!"; } else { ActionStatus.Text = "New permission for " + currentCommunity.Name + " could not be added!"; } } BindPermissionsToPermissionUserList(); return; } }
// UPDATE public static int UpdateAssociationPermissions(association_permissions aP) { association_permissions aPToUpdate = GetAssociationPermissionsById(aP.Id); aPToUpdate.users = aP.users; aPToUpdate.associations = aP.associations; aPToUpdate.Role = aP.Role; int affectedRows; try { affectedRows = Context.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (DbEntityValidationResult item in ex.EntityValidationErrors) { // Get entry DbEntityEntry entry = item.Entry; string entityTypeName = entry.Entity.GetType().Name; // Display or log error messages foreach (DbValidationError subItem in item.ValidationErrors) { string message = string.Format("Error '{0}' occurred in {1} at {2}", subItem.ErrorMessage, entityTypeName, subItem.PropertyName); Console.WriteLine(message); } // Rollback changes switch (entry.State) { case EntityState.Added: entry.State = EntityState.Detached; break; case EntityState.Modified: entry.CurrentValues.SetValues(entry.OriginalValues); entry.State = EntityState.Unchanged; break; case EntityState.Deleted: entry.State = EntityState.Unchanged; break; } } return affectedRows = 0; } Context.Entry(aPToUpdate).Reload(); return affectedRows; }