@Override public BundleGroup updateBundleGroup(Subject subject, BundleGroup bundleGroup) throws Exception { BundleGroup attachedBundleGroup = entityManager.find(BundleGroup.class, bundleGroup.getId()); if (attachedBundleGroup == null) { throw new IllegalArgumentException("Cannot update " + bundleGroup + ", because no bundle group exists with id [" + bundleGroup.getId() + "]."); } // First update the simple fields and the permissions. attachedBundleGroup.setName(bundleGroup.getName()); attachedBundleGroup.setDescription(bundleGroup.getDescription()); Set<Bundle> newBundles = bundleGroup.getBundles(); if (newBundles != null) { // wrap in new HashSet to avoid ConcurrentModificationExceptions. Set<Bundle> currentBundles = attachedBundleGroup.getBundles(); Set<Bundle> BundlesToRemove = new HashSet<Bundle>(currentBundles); for (Bundle bg : newBundles) { BundlesToRemove.remove(bg); } for (Bundle bg : BundlesToRemove) { attachedBundleGroup.removeBundle(bg); } for (Bundle bg : newBundles) { Bundle attachedBundle = entityManager.find(Bundle.class, bg.getId()); attachedBundleGroup.addBundle(attachedBundle); } } // Fetch the lazy Set for the return attachedBundleGroup.getBundles().size(); return attachedBundleGroup; }
@Override @RequiredPermission(Permission.MANAGE_BUNDLE_GROUPS) public void deleteBundleGroups(Subject subject, int[] bundleGroupIds) throws Exception { for (int bundleGroupId : bundleGroupIds) { BundleGroup bundleGroup = this.entityManager.find(BundleGroup.class, bundleGroupId); if (null == bundleGroup) { return; } // unassign any bundles assigned to the bundle group // wrap in new HashSet to avoid ConcurrentModificationExceptions. Set<Bundle> bundlesToRemove = new HashSet<Bundle>(bundleGroup.getBundles()); for (Bundle b : bundlesToRemove) { bundleGroup.removeBundle(b); } // remove from any roles for (Role r : bundleGroup.getRoles()) { r.removeBundleGroup(bundleGroup); } bundleGroup = entityManager.merge(bundleGroup); // now remove the bundle group entityManager.remove(bundleGroup); } }
/** * This also updates the inverse relations * @param bundles */ public void setBundles(Set<Bundle> bundles) { for (Bundle bundle : getBundles()) { bundle.removeBundleGroup(this); } this.bundles.clear(); if (null != bundles) { for (Bundle bundle : bundles) { addBundle(bundle); } } }
@Override @RequiredPermission(Permission.MANAGE_BUNDLE_GROUPS) public BundleGroup createBundleGroup(Subject subject, BundleGroup bundleGroup) throws Exception { String name = bundleGroup.getName(); if (null == name || "".equals(name.trim())) { throw new IllegalArgumentException("Invalid bundleGroupName: " + name); } BundleGroupCriteria c = new BundleGroupCriteria(); c.addFilterName(name); c.setStrict(true); if (!bundleManager.findBundleGroupsByCriteria(subject, c).isEmpty()) { throw new IllegalArgumentException("Invalid bundleGroupName, bundle group already exists with name: " + name); } entityManager.persist(bundleGroup); Set<Bundle> bundles = bundleGroup.getBundles(); if (null != bundles) { int[] bundleIds = new int[bundles.size()]; int i = 0; for (Bundle b : bundles) { bundleIds[i++] = b.getId(); } assignBundlesToBundleGroups(subject, new int[] { bundleGroup.getId() }, bundleIds); } return bundleGroup; }
/** * This also updates the inverse relation (add this bundle group to role) * @param role */ public void addRole(Role role) { getRoles().add(role); role.addBundleGroup(this); }
/** * This also updates the inverse relation (remove this bundle group from bundle) * @param bundle * @return true if bundle was removed, otherwise false */ public boolean removeBundle(Bundle bundle) { boolean result = getBundles().remove(bundle); bundle.removeBundleGroup(this); return result; }
@Override public void assignBundlesToBundleGroups(Subject subject, int bundleGroupIds[], int[] bundleIds) { if (null == bundleGroupIds || null == bundleIds) { return; } for (int bundleGroupId : bundleGroupIds) { BundleGroup bundleGroup = entityManager.find(BundleGroup.class, bundleGroupId); if (null == bundleGroup) { throw new IllegalArgumentException("BundleGroup does not exist for bundleGroupId [" + bundleGroupId + "]"); } checkAssignBundleGroupAuthz(subject, bundleGroupId, bundleIds); for (int bundleId : bundleIds) { Bundle bundle = entityManager.find(Bundle.class, bundleId); if (null == bundle) { throw new IllegalArgumentException("Bundle does not exist for bundleId [" + bundleId + "]"); } bundleGroup.addBundle(bundle); } } }
public BundleGroup(String name) { setName(name); }
@Override @RequiredPermission(Permission.MANAGE_SECURITY) public void setAssignedBundleGroups(Subject subject, int roleId, int[] bundleGroupIds) { Role role = getRole(subject, roleId); List<Integer> currentBundleGroups = new ArrayList<Integer>(); for (BundleGroup group : role.getBundleGroups()) { currentBundleGroups.add(group.getId()); } List<Integer> newBundleGroups = ArrayUtils.wrapInList(bundleGroupIds); // members needing addition newBundleGroups.removeAll(currentBundleGroups); int[] newBundleGroupIds = ArrayUtils.unwrapCollection(newBundleGroups); roleManager.addBundleGroupsToRole(subject, roleId, newBundleGroupIds); List<Integer> removedBundleGroups = new ArrayList<Integer>(currentBundleGroups); // members needing removal removedBundleGroups.removeAll(ArrayUtils.wrapInList(bundleGroupIds)); int[] removedGroupIds = ArrayUtils.unwrapCollection(removedBundleGroups); roleManager.removeBundleGroupsFromRole(subject, roleId, removedGroupIds); }
@Override public void unassignBundlesFromBundleGroups(Subject subject, int[] bundleGroupIds, int[] bundleIds) { if (null == bundleGroupIds || null == bundleIds) { return; } for (int bundleGroupId : bundleGroupIds) { BundleGroup bundleGroup = entityManager.find(BundleGroup.class, bundleGroupId); if (null == bundleGroup) { throw new IllegalArgumentException("BundleGroup does not exist for bundleGroupId [" + bundleGroupId + "]"); } checkUnassignBundleGroupAuthz(subject, bundleGroupId, bundleIds); for (int bundleId : bundleIds) { Bundle bundle = entityManager.find(Bundle.class, bundleId); if (null == bundle) { throw new IllegalArgumentException("Bundle does not exist for bundleId [" + bundleId + "]"); } bundleGroup.removeBundle(bundle); } } }
/** * This also updates the inverse relation (remove this bundle group from role) * @param role * @return true if role was removed, otherwise false */ public boolean removeRole(Role role) { role.removeBundleGroup(this); return getRoles().remove(role); }
/** * This also updates the inverse relation (add this bundle group to bundle) * @param bundle */ public void addBundle(Bundle bundle) { getBundles().add(bundle); bundle.addBundleGroup(this); }
bundleGroup.addBundle(bundle);
BundleGroup attachedBundleGroup = entityManager.find(BundleGroup.class, bg.getId()); attachedRole.addBundleGroup(attachedBundleGroup);
@Override @SuppressWarnings("unchecked") public void deleteBundle(Subject subject, int bundleId) throws Exception { Bundle bundle = this.entityManager.find(Bundle.class, bundleId); if (null == bundle) { return; } checkDeleteBundleAuthz(subject, bundleId); Query q = entityManager.createNamedQuery(BundleVersion.QUERY_FIND_BY_BUNDLE_ID); q.setParameter("bundleId", bundleId); List<BundleVersion> bvs = q.getResultList(); for (BundleVersion bv : bvs) { bundleManager.deleteBundleVersion(subject, bv.getId(), false); entityManager.flush(); } // remove bundle from any assigned bundle groups // wrap in new HashSet to avoid ConcurrentModificationExceptions. Set<BundleGroup> BundleGroupsToRemove = new HashSet<BundleGroup>(bundle.getBundleGroups()); for (BundleGroup bg : BundleGroupsToRemove) { bg.removeBundle(bundle); } // we need to whack the Repo once the Bundle no longer refers to it Repo bundleRepo = bundle.getRepo(); this.entityManager.remove(bundle); this.entityManager.flush(); // delete the repo as overlord, this allows users without MANAGE_INVENTORY permission to delete bundles repoManager.deleteRepo(subjectManager.getOverlord(), bundleRepo.getId()); }
@Override @RequiredPermission(Permission.MANAGE_SECURITY) public void removeRolesFromBundleGroup(Subject subject, int bundleGroupId, int[] roleIds) { if ((roleIds != null) && (roleIds.length > 0)) { BundleGroup bundleGroup = entityManager.find(BundleGroup.class, bundleGroupId); if (bundleGroup == null) { throw new IllegalArgumentException("Could not find BundleGroup[" + bundleGroupId + "] in order to remove roles"); } bundleGroup.getRoles().size(); // load them in for (Integer roleId : roleIds) { Role doomedRole = entityManager.find(Role.class, roleId); if (doomedRole == null) { throw new IllegalArgumentException("Tried to remove role[" + roleId + "] from BundleGroup[" + bundleGroupId + "], but role was not found"); } doomedRole.removeBundleGroup(bundleGroup); } } return; }
@Override @RequiredPermission(Permission.MANAGE_SECURITY) public void addRolesToBundleGroup(Subject subject, int bundleGroupId, int[] roleIds) { if ((roleIds != null) && (roleIds.length > 0)) { BundleGroup bundleGroup = entityManager.find(BundleGroup.class, bundleGroupId); if (bundleGroup == null) { throw new IllegalArgumentException("Could not find bundleGroup[" + bundleGroupId + "] in order to add roles"); } bundleGroup.getRoles().size(); // load them in for (Integer roleId : roleIds) { Role role = entityManager.find(Role.class, roleId); if (role == null) { throw new IllegalArgumentException("Tried to add role[" + roleId + "] to bundleGroup[" + bundleGroupId + "], but role was not found"); } role.addBundleGroup(bundleGroup); } } return; }