/* (non-Javadoc) * @see edu.internet2.middleware.grouper.ui.RepositoryBrowser#getParentStems(edu.internet2.middleware.grouper.ui.GroupOrStem) */ public List getParentStems(GroupOrStem groupOrStem) throws Exception { List path = new ArrayList(); if (groupOrStem == null) return path; Map map = GrouperHelper.group2Map(s, groupOrStem); Stem curStem = null; String endPoint = GrouperHelper.NS_ROOT; boolean isEndPointReached = false; if (isHidePreRootNode()) { endPoint = getRootNode(); if (map.get("name").equals(endPoint)) isEndPointReached = true; } while (!isEndPointReached && !"".equals(map.get("stem")) && !GrouperHelper.NS_ROOT.equals(map.get("stem"))) { curStem = StemFinder.findByName(s, (String) map.get("stem"), true); if (curStem != null) { map = GrouperHelper.stem2Map(s, curStem); path.add(0, map); if (curStem.getName().equals(endPoint)) isEndPointReached = true; } } if (!isEndPointReached) { path.add(0, GrouperHelper.stem2Map(s, StemFinder.findRootStem(s))); } return path; }
public void setUp() { super.setUp(); // add etc:attribute:courses:courseType attribute Stem etcStem = StemFinder.findByName(GrouperSession.staticGrouperSession(), "etc:attribute", true); Stem coursesStem = etcStem.addChildStem("courses", "Courses"); AttributeDef attributeDef = coursesStem.addChildAttributeDef("courseType", AttributeDefType.attr); attributeDef.setAssignToGroup(true); attributeDef.setMultiValued(true); attributeDef.setValueType(AttributeDefValueType.string); attributeDef.store(); coursesStem.addChildAttributeDefName(attributeDef, "courseType", "courseType"); }
/** * Check if subject has privilege. * * <p> * * @param i BeanShell interpreter. * @param stack BeanShell call stack. * @param name Check for privilege on this {@link Group} or {@link Stem}. * @param subjId Check if this {@link Subject} has privilege. * @param priv Check this {@link AccessPrivilege}. * @return True if succeeds. * @throws GrouperShellException * @since 0.0.1 */ public static boolean invoke( Interpreter i, CallStack stack, String name, String subjId, Privilege priv) throws GrouperShellException { GrouperShell.setOurCommand(i, true); try { GrouperSession s = GrouperShell.getSession(i); Subject subj = SubjectFinder.findByIdOrIdentifier(subjId, true); if (Privilege.isAccess(priv)) { Group g = GroupFinder.findByName(s, name, true); if (priv.equals(AccessPrivilege.ADMIN)) { return g.hasAdmin(subj); } else if (priv.equals(AccessPrivilege.OPTIN)) { return g.hasOptin(subj); } else if (priv.equals(AccessPrivilege.OPTOUT)) { return g.hasOptout(subj); } else if (priv.equals(AccessPrivilege.READ)) { return g.hasRead(subj); } else if (priv.equals(AccessPrivilege.UPDATE)) { return g.hasUpdate(subj); } else if (priv.equals(AccessPrivilege.GROUP_ATTR_READ)) { return g.hasGroupAttrRead(subj); } else if (priv.equals(AccessPrivilege.GROUP_ATTR_UPDATE)) { return g.hasGroupAttrUpdate(subj); } else if (priv.equals(AccessPrivilege.VIEW)) { return g.hasView(subj); } else { throw new RuntimeException("Not expecting privilege: " + priv); } } else if (Privilege.isNaming(priv)) { Stem ns = StemFinder.findByName(s, name, true); if (priv.equals(NamingPrivilege.CREATE)) { return ns.hasCreate(subj); } else if (priv.equals(NamingPrivilege.STEM_ATTR_READ)) { return ns.hasStemAttrRead(subj); } else if (priv.equals(NamingPrivilege.STEM_ATTR_UPDATE)) { return ns.hasStemAttrUpdate(subj); } else if (priv.equals(NamingPrivilege.STEM) || priv.equals(NamingPrivilege.STEM_ADMIN)) { return ns.hasStemAdmin(subj); } else { throw new RuntimeException("Not expecting privilege: " + priv); } } else if (Privilege.isAttributeDef(priv)) { AttributeDef attributeDef = AttributeDefFinder.findByName(name, true); if (priv.equals(AttributeDefPrivilege.ATTR_ADMIN)) { return attributeDef.getPrivilegeDelegate().hasAttrAdmin(subj); } else if (priv.equals(AttributeDefPrivilege.ATTR_OPTIN)) { return attributeDef.getPrivilegeDelegate().hasAttrOptin(subj); } else if (priv.equals(AttributeDefPrivilege.ATTR_OPTOUT)) { return attributeDef.getPrivilegeDelegate().hasAttrOptout(subj); } else if (priv.equals(AttributeDefPrivilege.ATTR_READ)) { return attributeDef.getPrivilegeDelegate().hasAttrRead(subj); } else if (priv.equals(AttributeDefPrivilege.ATTR_UPDATE)) { return attributeDef.getPrivilegeDelegate().hasAttrUpdate(subj); } else if (priv.equals(AttributeDefPrivilege.ATTR_DEF_ATTR_READ)) { return attributeDef.getPrivilegeDelegate().hasAttrDefAttrRead(subj); } else if (priv.equals(AttributeDefPrivilege.ATTR_DEF_ATTR_UPDATE)) { return attributeDef.getPrivilegeDelegate().hasAttrDefAttrUpdate(subj); } else if (priv.equals(AttributeDefPrivilege.ATTR_VIEW)) { return attributeDef.getPrivilegeDelegate().hasAttrView(subj); } else { throw new RuntimeException("Not expecting privilege: " + priv); } } else { throw new RuntimeException("Invalid privilege type: " + priv); } } catch (GroupNotFoundException eGNF) { GrouperShell.error(i, eGNF); } catch (StemNotFoundException eNSNF) { GrouperShell.error(i, eNSNF); } catch (SubjectNotFoundException eSNF) { GrouperShell.error(i, eSNF); } catch (SubjectNotUniqueException eSNU) { GrouperShell.error(i, eSNU); } return false; } // public static boolean invoke(i, stack, name, subjId, priv)
/** * Given a GrouperStem id return a list of stems and groups for which the GrouperStem is an * immediate parent * * @param s GrouperSession for authenticated user * @param stemId GrouperStem id * @param inPrivSet set of privileges the subject must have in each row * @param start * @param pageSize * @param resultSize result size of whole resultset * @return List of all stems and groups for stemId * @throws StemNotFoundException */ public List<GroupOrStem> getChildren( GrouperSession s, String stemId, int start, int pageSize, int[] resultSize) throws StemNotFoundException { Stem stem = null; if ("".equals(stemId)) { stem = StemFinder.findRootStem(s); } else { stem = StemFinder.findByName(s, stemId); } ArrayList res = new ArrayList(); Set children = getChildStems(stem); int stemCount = children.size(); int counter = 0; Iterator it = children.iterator(); Stem childStem = null; while (it.hasNext()) { childStem = (Stem) it.next(); if (counter >= start && counter < start + pageSize) { // Page the stems res.add(GroupOrStem.findByStem(s, childStem)); } counter++; } // Strategy is to find a page size where all required resultscan be retrieved in a single page // Therefore likely to have a different page size and pagenumber each time and are likely // to return results ant start and/or end which are not required for UI // Values below will often be reset before use boolean abortGroups = false; int groupPage = 1; int groupPageSize = pageSize; int groupStart = 1; int end = 0; int groupLowOffset = 0; // how many initial results to discard - assuming page size larger than results required int groupHighOffset = pageSize; // where to start discarding end results - - assuming page size larger than // results required if (start + pageSize <= stemCount) { // Enough stems to satisfy paging without getting to groups abortGroups = true; // 0 indexed end = start + pageSize - 1; } else if (start + pageSize > stemCount && start <= stemCount) { // show last of stems and first of groups end = start + pageSize - stemCount; groupHighOffset = end; } else if (start > stemCount) { // Not showing any stems groupStart = start - stemCount; end = groupStart + pageSize - 1; // Figures out the page size, number and the start/end index of groups to be returnedeturned int[] pageDetails = determineGroupPage(groupStart, end + 1, pageSize); groupPage = pageDetails[0]; groupPageSize = pageDetails[1]; int recStart = pageDetails[2]; int recEnd = pageDetails[3]; groupLowOffset = groupStart - recStart + 1; groupHighOffset = recEnd; } if (groupPage <= 0) { groupPage = 1; } QueryOptions queryOptions = null; if (this.pagedQuery()) { QueryPaging queryPaging = new QueryPaging(); queryPaging.setPageSize(groupPageSize); queryPaging.setPageNumber(groupPage); queryOptions = new QueryOptions().paging(queryPaging); queryOptions.retrieveCount(true); } children = getChildGroups(stem, queryOptions); if (GrouperUtil.length(resultSize) >= 1) { if (this.pagedQuery()) { // note: add in the size of the stems resultSize[0] = queryOptions.getCount().intValue() + stemCount; if (abortGroups) { return res; } } } it = children.iterator(); Group childGroup = null; int groupCounter = 0; while (it.hasNext()) { groupCounter++; childGroup = (Group) it.next(); if (groupCounter >= groupLowOffset && groupCounter <= groupHighOffset) { res.add(GroupOrStem.findByGroup(s, childGroup)); } } return res; }
/* (non-Javadoc) * @see edu.internet2.middleware.grouper.ui.RepositoryBrowser#advancedSearch(edu.internet2.middleware.grouper.GrouperSession, java.lang.String, java.util.Map, java.util.List) */ public List advancedSearch(GrouperSession s, String from, Map attr, List outTerms) throws Exception { List res = new ArrayList(); String maxCountStr = getSingle("maxFields", attr); int maxCount = Integer.parseInt(maxCountStr); String lastQuery = null; String lastField = null; String lastAndOrNot = null; String field; String query; String andOrNot; Map fieldMaps = GrouperHelper.getFieldsAsMap(); String lastFieldDisplayName = null; QueryFilter queryFilter = null; if (outTerms == null) outTerms = new ArrayList(); Stem fromStem = StemFinder.findByName(s, from, true); for (int i = 1; i <= maxCount; i++) { field = getSingle("searchField." + i, attr); query = getSingle("searchField." + i + ".query", attr); if (i == 1 && (field == null || query == null)) { if (getSingle("searchType.1", attr) != null) break; throw new IllegalArgumentException( "The first search field and query value must be enetered"); } andOrNot = getSingle("searchField." + i + ".searchAndOrNot", attr); if (query == null || "".equals(query)) query = lastQuery; if (i > 1) { if (queryFilter == null) { queryFilter = getGroupAttributeFilter(lastField, lastQuery, fromStem); outTerms.add(lastQuery); lastFieldDisplayName = (String) ((Map) fieldMaps.get(lastField)).get("displayName"); outTerms.add(lastFieldDisplayName); } if (field == null && i == 2) { break; } if (field == null && i > 2) break; if ("and".equals(lastAndOrNot)) { queryFilter = new IntersectionFilter(queryFilter, getGroupAttributeFilter(field, query, fromStem)); } else if ("or".equals(lastAndOrNot)) { queryFilter = new UnionFilter(queryFilter, getGroupAttributeFilter(field, query, fromStem)); } else { queryFilter = new ComplementFilter(queryFilter, getGroupAttributeFilter(field, query, fromStem)); } outTerms.add(lastAndOrNot); outTerms.add(query); outTerms.add(field); } lastQuery = query; lastField = field; lastAndOrNot = andOrNot; } // Now add GroupTYpe filter String groupTypeText = navBundle.getString("find.results.group-type"); String groupType = null; maxCountStr = getSingle("maxTypes", attr); try { maxCount = Integer.parseInt(maxCountStr); } catch (NumberFormatException e) { maxCount = 0; } GroupType gt = null; for (int i = 1; i <= maxCount; i++) { groupType = getSingle("searchType." + i, attr); if (groupType == null) break; gt = GroupTypeFinder.find(groupType, true); andOrNot = getSingle("searchType." + i + ".searchAndOrNot", attr); if (queryFilter == null) { queryFilter = new GroupTypeFilter(gt, fromStem); outTerms.add(groupTypeText); outTerms.add(gt.getName()); } else { if ("and".equals(andOrNot)) { queryFilter = new IntersectionFilter(queryFilter, new GroupTypeFilter(gt, fromStem)); } else if ("or".equals(andOrNot)) { queryFilter = new UnionFilter(queryFilter, new GroupTypeFilter(gt, fromStem)); } else { queryFilter = new ComplementFilter(queryFilter, new GroupTypeFilter(gt, fromStem)); } outTerms.add(andOrNot); outTerms.add(groupTypeText); outTerms.add(groupType); } } GrouperQuery q = GrouperQuery.createQuery(s, queryFilter); res.addAll(q.getGroups()); return res; }
/** * Only accessible from Create groups, implements stem search logic * * @param s * @param from * @param attr * @param outTerms * @return list of Stems matching search criteria * @throws Exception */ public List advancedStemSearch(GrouperSession s, String from, Map attr, List outTerms) throws Exception { List res = new ArrayList(); String maxCountStr = getSingle("maxFields", attr); int maxCount = Integer.parseInt(maxCountStr); String lastQuery = null; String lastField = null; String lastAndOrNot = null; String field; String query; String andOrNot; Map fieldMaps = (Map) GrouperHelper.getFieldsAsMap().get("stems"); String lastFieldDisplayName = null; QueryFilter queryFilter = null; if (outTerms == null) outTerms = new ArrayList(); Stem fromStem = StemFinder.findByName(s, from, true); for (int i = 1; i <= maxCount; i++) { field = getSingle("searchField." + i, attr); query = getSingle("searchField." + i + ".query", attr); if (i == 1 && (field == null || query == null)) { if (getSingle("searchType.1", attr) != null) break; throw new IllegalArgumentException( "The first search field and query value must be enetered"); } andOrNot = getSingle("searchField." + i + ".searchAndOrNot", attr); if (query == null || "".equals(query)) query = lastQuery; if (i > 1) { if (queryFilter == null) { queryFilter = getStemAttributeFilter(lastField, lastQuery, fromStem); outTerms.add(lastQuery); lastFieldDisplayName = (String) fieldMaps.get(lastField); outTerms.add(lastFieldDisplayName); } if (field == null && i == 2) { break; } if (field == null && i > 2) break; if ("and".equals(lastAndOrNot)) { queryFilter = new IntersectionFilter(queryFilter, getStemAttributeFilter(field, query, fromStem)); } else if ("or".equals(lastAndOrNot)) { queryFilter = new UnionFilter(queryFilter, getStemAttributeFilter(field, query, fromStem)); } else { queryFilter = new ComplementFilter(queryFilter, getStemAttributeFilter(field, query, fromStem)); } outTerms.add(lastAndOrNot); outTerms.add(query); outTerms.add(field); } lastQuery = query; lastField = field; lastAndOrNot = andOrNot; } GrouperQuery q = GrouperQuery.createQuery(s, queryFilter); res.addAll(q.getStems()); return res; }