// return the list of groups if the item is only accessible to specific groups // null if it's accessible to the whole site. public List<String> getGroups(boolean nocache) { // don't need cache, since simplepagebean is now caching groups // List<String>ret = (List<String>)topicCache.get(id); // if (!nocache && ret != null) { // if (ret.size() == 0) // return null; // else // return ret; // } else { // } if (type != TYPE_FORUM_TOPIC) return null; List<String> ret = new ArrayList<String>(); if (topic == null) topic = getTopicById(true, id); if (topic == null) return null; Set<DBMembershipItem> oldMembershipItemSet = uiPermissionsManager.getTopicItemsSet((DiscussionTopic) topic); Collection<Group> groups = null; try { Site site = SiteService.getSite(ToolManager.getCurrentPlacement().getContext()); groups = site.getGroups(); } catch (Exception e) { System.out.println("Unable to get site info for getGroups " + e); } // now change any existing ones into null for (DBMembershipItem item : oldMembershipItemSet) { if (item.getPermissionLevelName().equals("Contributor") && item.getType().equals(MembershipItem.TYPE_GROUP)) { String name = item.getName(); // oddly, this is the actual name, not the ID for (Group group : groups) { if (name.equals(group.getTitle())) ret.add(group.getId()); } } } // topicCache.put(id, ret, DEFAULT_EXPIRATION); if (ret.size() == 0) return null; else return ret; }
// set the item to be accessible only to the specific groups. // null to make it accessible to the whole site public void setGroups(Collection<String> groups) { if (type != TYPE_FORUM_TOPIC) return; // Setgroups with a non-null list: we set all contributor entries to none, and then set the // specified groups to contribtor. By only handling groups, we avoid interfering with // anything you might do in the tool. But the moment you use access control, we take // over. Sorry. Once we've done that you could go back into the tool and hack, but I // don't recommend that. // Setgroups with a null list: we set all contributor entries to none, and then set all roles // other than maintain to contributor. setMasks(); // System.out.println("topic 1 " + topic + " " + groups); // if (topic == null) topic = getTopicById(true, id); // System.out.println("topic 2 " + topic); if (topic == null) return; Site site = null; try { site = SiteService.getSite(ToolManager.getCurrentPlacement().getContext()); } catch (Exception e) { System.out.println("Unable to get site info for AddEntityControl " + e); return; } // topicCache.remove(id); // old entries Set<DBMembershipItem> oldMembershipItemSet = uiPermissionsManager.getTopicItemsSet((DiscussionTopic) topic); DBMembershipItem membershipItem = null; boolean haveOwner = false; boolean changed = false; if (groups != null && groups.size() > 0) { // this is the groups we've been asked to use // remove groups form this as we see them if they already have access // so at the end we just add the ones remaining List<String> groupNames = new ArrayList<String>(); Set<String> addGroupNames = new HashSet<String>(); for (String groupId : groups) { groupNames.add(site.getGroup(groupId).getTitle()); addGroupNames.add(site.getGroup(groupId).getTitle()); } // System.out.println("groups " + groups + " " + groupNames + " " + addGroupNames); // System.out.println("oldMembership " + oldMembershipItemSet.size()); // delete groups from here as they are done. // if we've seen an owner. Otherwise set the maintain role as owner // Setgroups with a non-null list: we set all contributor entries to none, and then set the // specified groups to contribtor. However we don't touch owner. // By only handling groups, we avoid interfering with // anything you might do in the tool. But the moment you use access control, we take // over. Sorry. Once we've done that you could go back into the tool and hack, but I // don't recommend that. for (DBMembershipItem item : oldMembershipItemSet) { // kill everything except our own groups // this will leave the owner but remove all other roles // System.out.println("item " + item.getType() + " " + item.getName() + " " + // item.getPermissionLevelName()); if (item.getType().equals(MembershipItem.TYPE_GROUP) && groupNames.contains(item.getName())) { // System.out.println("found group " + item.getName()); addGroupNames.remove(item.getName()); // we've seen it // if it's one of our groups make it a contributor if it's not already an owner if (!item.getPermissionLevelName().equals("Contributor") && !item.getPermissionLevelName().equals("Owner")) { PermissionLevel contributorLevel = permissionLevelManager.createPermissionLevel( "Contributor", IdManager.createUuid(), contributorMask); permissionLevelManager.savePermissionLevel(contributorLevel); item.setPermissionLevel(contributorLevel); item.setPermissionLevelName("Contributor"); permissionLevelManager.saveDBMembershipItem(item); } } else if (!item.getPermissionLevelName() .equals("Owner")) { // only group members are contributors // remove contributor from anything else, both groups and roles // System.out.println("set none"); // System.out.println("setgroups make none " + item.getName()); PermissionLevel noneLevel = permissionLevelManager.createPermissionLevel( "None", IdManager.createUuid(), noneMask); permissionLevelManager.savePermissionLevel(noneLevel); item.setPermissionLevel(noneLevel); item.setPermissionLevelName("None"); permissionLevelManager.saveDBMembershipItem(item); } } for (String newGroupName : addGroupNames) { // System.out.println("addgroup " + newGroupName); changed = true; PermissionLevel contributorLevel = permissionLevelManager.createPermissionLevel( "Contributor", IdManager.createUuid(), contributorMask); permissionLevelManager.savePermissionLevel(contributorLevel); membershipItem = permissionLevelManager.createDBMembershipItem( newGroupName, "Contributor", MembershipItem.TYPE_GROUP); membershipItem.setPermissionLevel(contributorLevel); permissionLevelManager.saveDBMembershipItem(membershipItem); oldMembershipItemSet.add(membershipItem); } } else { // Setgroups with a null list: we set all contributor entries to none, and then set all roles // to contributor. However we don't touch Owners. for (DBMembershipItem item : oldMembershipItemSet) { if (item.getPermissionLevelName().equals("Owner")) { haveOwner = true; } else if (item.getType().equals(MembershipItem.TYPE_ROLE)) { // default state has all roles except owner as contributor if (!item.getPermissionLevelName().equals("Contributor")) { PermissionLevel contributorLevel = permissionLevelManager.createPermissionLevel( "Contributor", IdManager.createUuid(), contributorMask); permissionLevelManager.savePermissionLevel(contributorLevel); item.setPermissionLevel(contributorLevel); item.setPermissionLevelName("Contributor"); permissionLevelManager.saveDBMembershipItem(item); } } else if (!item.getPermissionLevelName().equals("None")) { // kill other contributors PermissionLevel noneLevel = permissionLevelManager.createPermissionLevel( "None", IdManager.createUuid(), noneMask); permissionLevelManager.savePermissionLevel(noneLevel); item.setPermissionLevel(noneLevel); item.setPermissionLevelName("None"); permissionLevelManager.saveDBMembershipItem(item); } } } if (changed) { // System.out.println("changed"); // have to refresh the topic or the save won't work topic = getTopicById(true, id); topic.setMembershipItemSet(oldMembershipItemSet); forumManager.saveDiscussionForumTopic((DiscussionTopic) topic); // topic.setVersion(null); // try { // System.out.println("simplepagetool dao " + simplePageToolDao); // hibernateTemplate.merge(topic); // } catch (Exception e){ // System.out.println("Unable to save forum topic " + e); // } } }
// seems not to be used anymore public boolean removeEntityControl(String siteId, String groupId) throws IOException { if (type != TYPE_FORUM_TOPIC) return false; setMasks(); if (topic == null) topic = getTopicById(true, id); if (topic == null) return false; Set<DBMembershipItem> oldMembershipItemSet = uiPermissionsManager.getTopicItemsSet((DiscussionTopic) topic); Set membershipItemSet = new HashSet(); String groupName = null; String maintainRole = null; try { Site site = SiteService.getSite(ToolManager.getCurrentPlacement().getContext()); groupName = site.getGroup(groupId).getTitle(); maintainRole = authzGroupService.getAuthzGroup("/site/" + site.getId()).getMaintainRole(); } catch (Exception e) { System.out.println("Unable to get site info for AddEntityControl " + e); } PermissionLevel ownerLevel = permissionLevelManager.createPermissionLevel( "Owner", typeManager.getOwnerLevelType(), ownerMask); permissionLevelManager.savePermissionLevel(ownerLevel); DBMembershipItem membershipItem = permissionLevelManager.createDBMembershipItem( maintainRole, "Owner", MembershipItem.TYPE_ROLE); membershipItem.setPermissionLevel(ownerLevel); permissionLevelManager.saveDBMembershipItem(membershipItem); membershipItemSet.add(membershipItem); // now change any existing ones into null for (DBMembershipItem item : oldMembershipItemSet) { if (item.getType().equals(MembershipItem.TYPE_ROLE)) { if (!maintainRole.equals(item.getName())) { // that was done above, other roles contributor PermissionLevel contributorLevel = permissionLevelManager.createPermissionLevel( "Contributor", typeManager.getContributorLevelType(), contributorMask); permissionLevelManager.savePermissionLevel(contributorLevel); membershipItem = permissionLevelManager.createDBMembershipItem( item.getName(), "Contributor", item.getType()); membershipItem.setPermissionLevel(contributorLevel); permissionLevelManager.saveDBMembershipItem(membershipItem); membershipItemSet.add(membershipItem); } } else { // everything else off PermissionLevel noneLevel = permissionLevelManager.createPermissionLevel( "None", typeManager.getNoneLevelType(), noneMask); permissionLevelManager.savePermissionLevel(noneLevel); membershipItem = permissionLevelManager.createDBMembershipItem(item.getName(), "None", item.getType()); membershipItem.setPermissionLevel(noneLevel); permissionLevelManager.saveDBMembershipItem(membershipItem); membershipItemSet.add(membershipItem); } } permissionLevelManager.deleteMembershipItems(oldMembershipItemSet); topic.setMembershipItemSet(membershipItemSet); discussionForumManager.saveTopic((DiscussionTopic) topic); return true; };