All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.internet2.middleware.grouper.GrouperHelper Maven / Gradle / Ivy

There is a newer version: 5.13.5
Show newest version
/*******************************************************************************
 * Copyright 2012 Internet2
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
/*
Copyright 2004-2007 University Corporation for Advanced Internet Development, Inc.
Copyright 2004-2007 The University Of Bristol

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package edu.internet2.middleware.grouper;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.internet2.middleware.grouper.attr.AttributeDef;
import edu.internet2.middleware.grouper.attr.AttributeDefName;
import edu.internet2.middleware.grouper.attr.assign.AttributeAssign;
import edu.internet2.middleware.grouper.cfg.GrouperConfig;
import edu.internet2.middleware.grouper.exception.AttributeDefNotFoundException;
import edu.internet2.middleware.grouper.exception.CompositeNotFoundException;
import edu.internet2.middleware.grouper.exception.GrantPrivilegeException;
import edu.internet2.middleware.grouper.exception.GroupNotFoundException;
import edu.internet2.middleware.grouper.exception.InsufficientPrivilegeException;
import edu.internet2.middleware.grouper.exception.MemberAddException;
import edu.internet2.middleware.grouper.exception.MemberDeleteException;
import edu.internet2.middleware.grouper.exception.MemberNotFoundException;
import edu.internet2.middleware.grouper.exception.MembershipNotFoundException;
import edu.internet2.middleware.grouper.exception.QueryException;
import edu.internet2.middleware.grouper.exception.SchemaException;
import edu.internet2.middleware.grouper.exception.SessionException;
import edu.internet2.middleware.grouper.exception.StemNotFoundException;
import edu.internet2.middleware.grouper.filter.GroupAnyAttributeFilter;
import edu.internet2.middleware.grouper.filter.GroupAttributeFilter;
import edu.internet2.middleware.grouper.filter.GrouperQuery;
import edu.internet2.middleware.grouper.filter.StemNameAnyFilter;
import edu.internet2.middleware.grouper.misc.GrouperDAOFactory;
import edu.internet2.middleware.grouper.privs.AccessPrivilege;
import edu.internet2.middleware.grouper.privs.NamingPrivilege;
import edu.internet2.middleware.grouper.privs.Privilege;
import edu.internet2.middleware.grouper.privs.PrivilegeHelper;
import edu.internet2.middleware.grouper.subj.LazySubject;
import edu.internet2.middleware.grouper.subj.SubjectHelper;
import edu.internet2.middleware.grouper.subj.UnresolvableSubject;
import edu.internet2.middleware.grouper.ui.GroupOrStem;
import edu.internet2.middleware.grouper.ui.GrouperUiFilter;
import edu.internet2.middleware.grouper.ui.PersonalStem;
import edu.internet2.middleware.grouper.ui.UIThreadLocal;
import edu.internet2.middleware.grouper.ui.util.GroupAsMap;
import edu.internet2.middleware.grouper.ui.util.MembershipAsMap;
import edu.internet2.middleware.grouper.ui.util.ObjectAsMap;
import edu.internet2.middleware.grouper.ui.util.SubjectAsMap;
import edu.internet2.middleware.subject.Source;
import edu.internet2.middleware.subject.Subject;
import edu.internet2.middleware.subject.SubjectNotFoundException;
import edu.internet2.middleware.subject.SubjectNotUniqueException;
import edu.internet2.middleware.subject.SubjectType;
import edu.internet2.middleware.subject.provider.SourceManager;

/**
 * High level wrapper methods for the Grouper API, including workarounds. 
 * Primarily developed for the UI.

* Occasionally takes advantage of protected access to the Grouper API, however, * this access should not be required once the API is fully implemented. *

* * @author Gary Brown. * @version $Id: GrouperHelper.java,v 1.64 2009-11-07 12:20:32 isgwb Exp $ */ public class GrouperHelper { /** logger */ protected static final Log LOG = edu.internet2.middleware.grouper.util.GrouperUtil.getLog(GrouperHelper.class); private static Map superPrivs = null; //Privs automatically granted to the // system user private static List personSources = null; //Subject sources which source // 'people' public static final String HIER_DELIM = ":"; //Currently : // (name // separator) public static final String NS_ROOT = "Grouper.NS_ROOT"; //Initialise system user privs static { superPrivs = new HashMap(); superPrivs.put("read", Boolean.TRUE); superPrivs.put("view", Boolean.TRUE); superPrivs.put("update", Boolean.TRUE); superPrivs.put("admin", Boolean.TRUE); superPrivs.put("groupAttrRead", Boolean.TRUE); superPrivs.put("groupAttrUpdate", Boolean.TRUE); superPrivs.put("create", Boolean.TRUE); superPrivs.put("stemAdmin", Boolean.TRUE); superPrivs.put("stemAttrRead", Boolean.TRUE); superPrivs.put("stemAttrUpdate", Boolean.TRUE); //superPrivs.put("OPTIN", Boolean.TRUE); //superPrivs.put("OPTOUT", Boolean.TRUE); } //Privs which relate to Groups - access privileges private static String[] groupPrivs = { "admin", "update","read","view","optin","optout","groupAttrRead","groupAttrUpdate" }; // Privs which relate to Groups - access privileges + member private static String[] groupPrivsWithMember = { "member", "admin", "update","read","view","optin","optout","groupAttrRead","groupAttrUpdate"}; //Privs which relate to Stems - naming privileges //CH 20080324 change for UI from: "STEM", "CREATE" //private static String[] stemPrivs = {"Create Group", "Create Folder"}; //GB 20080415 changed back, but UI looks up display name now for select options private static String[] stemPrivs = {"create", "stemAdmin", "stemAttrRead", "stemAttrUpdate"}; public static void main(String args[]) throws Exception{ Subject subj = SubjectFinder.findById("GrouperSystem", true); GrouperSession s = GrouperSession.start(subj); //GroupType type = GroupType.createType(s,"teaching"); GroupType type = GroupTypeFinder.find("committee", true); /*type.addField(s,"enforcer",FieldType.ATTRIBUTE,Privilege.getInstance("read"),Privilege.getInstance("update"),true); type = GroupTypeFinder.find("mailingList"); type.addField(s,"alias",FieldType.ATTRIBUTE,Privilege.getInstance("read"),Privilege.getInstance("update"),true); type = GroupTypeFinder.find("studentUnion"); type.addField(s,"campus",FieldType.ATTRIBUTE,Privilege.getInstance("read"),Privilege.getInstance("update"),true); type = GroupTypeFinder.find("personal"); type.addField(s,"proxy",FieldType.ATTRIBUTE,Privilege.getInstance("read"),Privilege.getInstance("update"),true); */ type = GroupTypeFinder.find("community", true); type.addList(s,"contributors",Privilege.getInstance("read"),Privilege.getInstance("update")); type.addAttribute(s,"scope",true); type = GroupTypeFinder.find("staff", true); type.addAttribute(s,"dept",true); /*type.addField(s,"staff",FieldType.LIST,Privilege.getInstance("read"),Privilege.getInstance("update"),false); type.addField(s,"clerical",FieldType.LIST,Privilege.getInstance("read"),Privilege.getInstance("update"),false); type.addField(s,"faculty_code",FieldType.ATTRIBUTE,Privilege.getInstance("read"),Privilege.getInstance("update"),true); type.addField(s,"org_code",FieldType.ATTRIBUTE,Privilege.getInstance("read"),Privilege.getInstance("update"),true); Group g = new Group(s,StemFinder.findRootStem(s),"tGroup","Teaching Group"); g.addType(type); g.setAttribute("faculty_code","ARTF"); g.setAttribute("org_code","FREN"); Set x = SubjectFinder.findAll("keith"); Iterator it = x.iterator(); Subject subject; while(it.hasNext()) { subject=(Subject)it.next(); g.addMember(subject,FieldFinder.find("staff")); } x = SubjectFinder.findAll("fiona"); it = x.iterator(); while(it.hasNext()) { subject=(Subject)it.next(); g.addMember(subject,FieldFinder.find("clerical")); } */ s.stop(); } /** * 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 * @return List of all stems and groups for stemId */ public static List getChildren(GrouperSession s, String stemId) throws StemNotFoundException{ Stem stem =null; if("".equals(stemId)) { stem=StemFinder.findRootStem(s); }else{ stem=StemFinder.findByName(s, stemId, true); } ArrayList res = new ArrayList(); Set children = stem.getChildStems(); Iterator it = children.iterator(); Stem childStem = null; while(it.hasNext()) { childStem=(Stem)it.next(); res.add(GroupOrStem.findByStem(s,childStem)); } children=stem.getChildGroups(); it = children.iterator(); Group childGroup = null; while(it.hasNext()) { childGroup=(Group)it.next(); res.add(GroupOrStem.findByGroup(s,childGroup)); } return res; } /** * Given a list of GrouperAttributes, return a list of GrouperStems * which the attributes belong to, and load all atributes for these Stems * * @param s GrouperSession for authenticated user * @param list of GrouperAttributes * @return List of GrouperGroups or GrouperStems */ /*public static List instantiateStems(GrouperSession s, List list) { return instantiateGroups(s, list); }*/ /** * Given a list of GrouperAttributes, return a list of GrouperGroups * which the attributes belong to, and load all atributes for these groups * * @param s GrouperSession for authenticated user * @param list of GrouperAtributes * @return List of GrouperGroups or GrouperStems */ /*public static List instantiateGroups(GrouperSession s, List list) { List instantiated = new ArrayList(); Attribute attr = null; String key; Stem stem = null; for (int i = 0; i < list.size(); i++) { attr = (Attribute) list.get(i); key = attr.key(); //stem=Cache.instance().getGroup(s,key); if (stem == null) { try { stem = (GrouperGroup) GrouperGroup.loadByKey(s, key); }catch(InsufficientPrivilegeException e) { throw new RuntimeException(e); } //Cache.instance().put(s,key,stem); } /** * Given a list of GrouperList objects return a list of instantiated * GrouperGroups as Maps * * @param s GrouperSession for authenticated user * @param groups List of GrouperLists * @return List of GrouperGroups wrapped as Maps */ public static List groups2Maps(GrouperSession s, List groups) throws GroupNotFoundException{ List maps = new ArrayList(); Object obj; for (int i = 0; i < groups.size(); i++) { //Just in case something goes wrong - Group doesn't exist but still // a pointer to it try { obj = groups.get(i); if (obj instanceof Membership) obj = ((Membership) obj).getGroup(); maps.add(group2Map(s, (Group) obj)); } catch (NullPointerException e) { //@TODO What should happen? } } return maps; } /** * Given a ist of GrouperList objects return a list of instantiated GrouperStems * as Maps * @param s GrouperSession for authenticated user * @param stems List of GrouperLists * @return List of GrouperStems wrapped as Maps */ public static List stems2Maps(GrouperSession s, List stems) throws GroupNotFoundException{ List maps = new ArrayList(); Object obj; for (int i = 0; i < stems.size(); i++) { //Just in case something goes wrong - Group doesn't exist but still // a pointer to it try { obj = stems.get(i); maps.add(stem2Map(s, (Stem) obj)); } catch (NullPointerException e) { //@TODO What should happen? } } return maps; } /** * Given a GrouperStem return a Map representing it * @param s GrouperSession for authenticated user * @param stem GrouperStem to wrap * @return GrouperStem wrapped as a Map */ public static Map stem2Map(GrouperSession s, Stem stem) { Map stemMap = ObjectAsMap.getInstance("StemAsMap", stem, s); if("".equals(stem.getName())) { stemMap.put("isRootStem",Boolean.TRUE); } return stemMap; } /** * Given a Stem return a Map representing it * @param s GrouperSession for authenticated user * @param stem GrouperStem to wrap * @return Stem wrapped as a Map */ public static Map group2Map(GrouperSession s, Stem stem) { return ObjectAsMap.getInstance("StemAsMap", stem, s); } /** * Given a GroupOrStem return a Map representing it * @param s GrouperSession for authenticated user * @param groupOrStem GroupOrStem to wrap * @return GroupOrStem wrapped as a Map */ public static Map group2Map(GrouperSession s, GroupOrStem groupOrStem) { return groupOrStem.getAsMap(); } /** * Given a Group return * a Map representation of it * @param s GrouperSession for authenticated user * @param group Group to wrap * @return Group wrapped as a Map */ public static Map group2Map(GrouperSession s, Group group){ ObjectAsMap map = new GroupAsMap(group, s); if (map.get("displayExtension") == null) map.put("displayExtension", map.get("extension")); return (Map) map; } /** * Given a GrouperGroup or GrouperStem return a list of ancestor GrouperStems * as Maps * * @param s GrouperSession for authenticated user * @param groupOrStem GrouperGroup or GrouperStem * @return List of ancestor GrouperStems wrapped as Maps */ public static List parentStemsAsMaps(GrouperSession s, GroupOrStem groupOrStem) throws StemNotFoundException{ List path = new ArrayList(); if(groupOrStem==null) return path; Map map = group2Map(s, groupOrStem); Stem curStem = null; while (!GrouperHelper.NS_ROOT.equals(map.get("stem"))) { curStem = StemFinder.findByName(s, (String) map.get("stem"), true); if (curStem != null) { map = stem2Map(s, curStem); path.add(0, map); } } return path; } /** * Given a GrouperGroup or GrouperStem return a Map where the keys * are access or naming privileges that s.subject() has. * * @param s GrouperSession for authenticated user * @param groupOrStem GrouperGroup or GroupeStem for which privileges are being requested * @return Map representing privileges */ public static Map hasAsMap(GrouperSession s, GroupOrStem groupOrStem) throws MemberNotFoundException{ return hasAsMap(s, groupOrStem, false); } /** * Given a GrouperGroup or GrouperStem return a Map where the keys are * access or naming privileges that s.subject() has. If isMortal == false * and the subject is the system user, all privileges are returned. * @param s GrouperSession for authenticated user * @param groupOrStem GrouperGroup or GroupeStem for which privileges are being requested * @param isMortal if system user should they be teated as such * @return Map representing privileges */ public static Map hasAsMap(GrouperSession s, GroupOrStem groupOrStem, boolean isMortal) throws MemberNotFoundException{ Map privs = null; Group g = null; Stem stem = null; boolean isActiveWheelGroupMember = Boolean.TRUE.equals(UIThreadLocal.get("isActiveWheelGroupMember")); privs = new HashMap(); if (!isMortal && ("GrouperSystem".equals(s.getSubject().getId()) || isActiveWheelGroupMember)) { privs.putAll(superPrivs); if(groupOrStem==null) return privs; if (groupOrStem.isGroup()) { g = groupOrStem.getGroup(); if (g.hasMember(s.getSubject())) { privs.put("member", Boolean.TRUE); } } else { stem = groupOrStem.getStem(); if(stem.isRootStem()) privs.remove("create"); } if (privs == null) privs = superPrivs; return privs; } if("GrouperSystem".equals(s.getSubject().getId()) ||isActiveWheelGroupMember) { privs = new HashMap(); privs.put("stemAdmin",Boolean.TRUE); if(groupOrStem!=null && groupOrStem.isStem()&& !"".equals(groupOrStem.getStem().getName())) { privs.put("create",Boolean.TRUE); } return privs; } if(groupOrStem==null) return new HashMap(); g = groupOrStem.getGroup(); stem = groupOrStem.getStem(); Set privList = null; if (g != null) { privList = g.getPrivs(s.getSubject()); } else { privList = stem.getPrivs(s.getSubject()); } if(privList !=null) { Iterator it = privList.iterator(); Object p = null; while(it.hasNext()){ p=it.next(); if(p instanceof AccessPrivilege) { privs.put(((AccessPrivilege)p).getName(), Boolean.TRUE); }else if(p instanceof NamingPrivilege) { privs.put(((NamingPrivilege)p).getName(), Boolean.TRUE); }else{ privs.put(it.next(), Boolean.TRUE); } } } if (g != null) { if (g.hasMember(s.getSubject())) privs.put("member", Boolean.TRUE); } return privs; } /** * Given a GrouperGroup or GrouperStem return a Map where the keys * are access or naming privileges that member.subject() has. * * @param s GrouperSession for authenticated user * @param groupOrStem GrouperGroup or GroupeStem for which privileges are being requested * @param member Subject who privileges were granted to * @return Map representing privileges */ public static Map hasAsMap(GrouperSession s, GroupOrStem groupOrStem, Member member,Field field) throws SubjectNotFoundException,SchemaException{ Map privs = null; if ("GrouperSystem".equals(member.getSubjectId())) { //@TODO Review //return superPrivs; } Set privList = null; privs = new HashMap(); Group group = null; Stem stem = null; if (groupOrStem.isGroup()) { group = groupOrStem.getGroup(); privList = group.getPrivs(member.getSubject()); } else { stem = groupOrStem.getStem(); privList = stem.getPrivs(member.getSubject()); } Iterator it = privList.iterator(); Object obj; String p; while(it.hasNext()) { obj=it.next(); if(obj instanceof AccessPrivilege) { p = ((AccessPrivilege)obj).getName(); }else{ p = ((NamingPrivilege)obj).getName(); } privs.put(p, Boolean.TRUE); } if (group != null) { if (group.hasMember(member.getSubject(),field)) privs.put("member", Boolean.TRUE); } return privs; } /** Given a Subject return a Map representation of it * @param subject to be wrapped * @return Subject wrapped as a Map */ public static Map subject2Map(Subject subject) { //@TODO what should happen if Group - see next method SubjectAsMap map = (SubjectAsMap)ObjectAsMap.getInstance("SubjectAsMap", subject); return (Map) map; } /** Given a Subject return a Map representation of it * @param subject to be wrapped * @param addAttr Map of additional attributes * @return Subject wrapped as a Map */ public static Map subject2Map(Subject subject,Map addAttr) { //@TODO what should happen if Group - see next method SubjectAsMap map = (SubjectAsMap)ObjectAsMap.getInstance("SubjectAsMap", subject); if(addAttr !=null) map.putAll(addAttr); return (Map) map; } /** * Given a subject id and subject type and a Map, return a Map representation of the * subject and add the key/value pairs from the input Map. * * @param s GrouperSession for authenticated user * @param subjectId Subject id * @param subjectType Subject type e.g. person, group * @param addAttr Map of aditional attributes * @return Subject wrapped as a Map */ public static Map subject2Map(GrouperSession s, String subjectId, String subjectType,String sourceId,Map addAttr) throws SubjectNotFoundException{ Map subjectMap = subject2Map(s,subjectId,subjectType,sourceId); if(addAttr !=null) subjectMap.putAll(addAttr); return subjectMap; } /** * Given a subject id and subject type return a Map representation of it. * * @param s GrouperSession for authenticated user * @param subjectId Subject id * @param subjectType Subject type e.g. person, group * @return Subject wrapped as a Map */ public static Map subject2Map(GrouperSession s, String subjectId, String subjectType,String sourceId) throws SubjectNotFoundException{ if (!"group".equals(subjectType)) { Subject subject = null; try { subject = SubjectFinder.findById(subjectId, subjectType,sourceId, true); } catch (Exception e) { LOG.error(e); subject = new UnresolvableSubject(subjectId,subjectType,sourceId); } SubjectAsMap map =(SubjectAsMap)ObjectAsMap.getInstance("SubjectAsMap", subject); return (Map) map; } try { Group group = GroupFinder.findByUuid(s, subjectId, true); Map groupMap = group2Map(s, group); return groupMap; }catch(GroupNotFoundException e) { throw new SubjectNotFoundException(e.getMessage(), e); } } /** * Given an array of Subjects return a List of Maps representing those subjects * * @param objects array of Subjects * @param addAttr Map of additional attributes * @return List of Subjects wrapped as Maps */ public static List subjects2Maps(Object[] objects,Map addAttr) { if (objects instanceof Subject[]) return subjects2Maps((Subject[]) objects); Subject[] subjects = new Subject[objects.length]; for (int i = 0; i < objects.length; i++) { subjects[i] = (Subject) objects[i]; } return subjects2Maps(subjects,addAttr); } /** * Given an array of Subjects return a List of Maps representing those subjects * * @param objects array of Subjects * @return List of Subjects wrapped as Maps */ public static List subjects2Maps(Object[] objects) { return subjects2Maps(objects,null); } /** * Given an array of Subjects return a List of Maps representing those subjects * * @param subjects array of Subjects * @param addAttr Map of aditional attributes * @return List of Subjects wrapped as Maps */ public static List> subjects2Maps(Subject[] subjects,Map addAttr) { List maps = new ArrayList(); for (int i = 0; i < subjects.length; i++) { if(subjects[i]!=null) maps.add(subject2Map(subjects[i],addAttr)); } return maps; } /** * Given an array of Subjects return a List of Maps representing those subjects * * @param subjects array of Subjects * @return List of Subjects wrapped as Maps */ public static List subjects2Maps(Subject[] subjects) { List maps = new ArrayList(); for (int i = 0; i < subjects.length; i++) { maps.add(subject2Map(subjects[i])); } return maps; } /** * Given a list of GrouperList objects return a sublist from * start to start + pageSize of each GrouperList.member() as a Map * * @param s GrouperSession for authenticated user * @param members List of GrouperLists or GrouperMembers * @param start 0 based start index * @param pageSize number of results to return * @return List of Subjects wrapped as Maps */ public static List groupList2SubjectsMaps(GrouperSession s, List members, int start, int pageSize) throws GroupNotFoundException,SubjectNotFoundException, MemberNotFoundException,SubjectNotUniqueException{ return groupList2SubjectsMaps(s, members, null, start, pageSize); } /** * Given a list of GrouperMembers return a list of Map representations of them * @param s GrouperSession for authenticated user * @param members List of GrouperLists or GrouperMembers * @return List of Subjects wrapped as Maps */ public static List groupList2SubjectsMaps(GrouperSession s, List members) throws GroupNotFoundException,SubjectNotFoundException, MemberNotFoundException,SubjectNotUniqueException{ return groupList2SubjectsMaps(s, members, null); } /** * Given a list of GrouperMembers return a list of Map representations of them * where the key asMemberOf is set to the value of the parameter asMemberOf * @param s GrouperSession for authenticated user * @param members List of GrouperLists or GrouperMembers * @param asMemberOf GrouperGroup id identifying context * @return List of Subjects wrapped as Maps */ public static List groupList2SubjectsMaps(GrouperSession s, List members, String asMemberOf) throws GroupNotFoundException,SubjectNotFoundException, MemberNotFoundException,SubjectNotUniqueException{ return groupList2SubjectsMaps(s, members, asMemberOf, 0, members.size()); } /** * Given a list of GrouperMembers return a list of Map representations of * a sublist of them (from start to start + pageSize) where the key * asMemberOf is set to the value of the parameter asMemberOf * @param s GrouperSession for authenticated user * @param members List of GrouperLists or GrouperMembers * @param asMemberOf GrouperGroup id identifying context * @param start of sublist * @param pageSize number of results to return * @return List of Subjects wrapped as Maps */ public static List groupList2SubjectsMaps(GrouperSession s, List members, String asMemberOf, int start, int pageSize) throws GroupNotFoundException,SubjectNotFoundException, MemberNotFoundException,SubjectNotUniqueException{ int end = start + pageSize; if (end > members.size()) end = members.size(); List maps = new ArrayList(); Membership list = null; Member member = null; Subject subject=null; Map subjMap = null; Object listItem; Group via = null; Set chain = null; Object chainItem = null; Group firstInChain = null; Member chainMember = null; String[] chainGroupIds = null; int chainSizeAdjustment=1; String[] emptyStrArray=new String[]{}; boolean isChainSameAsList = false; for (int i = start; i < end; i++) { chainGroupIds = emptyStrArray; listItem = members.get(i); if (listItem instanceof Membership) { list = (Membership) listItem; try{ via = (Group) list.getViaGroup(); }catch(GroupNotFoundException e){via=null;} chain = list.getChildMemberships(); if (chain != null && chain.size() > 0) { //chainGroupIds = getChainGroupIds(s,list); } else { firstInChain = null; } member = list.getMember(); try { subject = member.getSubject(); } catch (Exception e) { throw new RuntimeException(e); } } else if (listItem instanceof Membership) { member = (Member) list.getMember(); try { subject = member.getSubject(); } catch (Exception e) { throw new RuntimeException(e); } }else if(listItem instanceof Subject) { subject = (Subject)listItem; }else if(listItem instanceof Group) { Subject subj = SubjectFinder.findById(asMemberOf, true); Map gSubjMap = subject2Map(subj); Map gMap = group2Map(s,(Group)listItem); gSubjMap.put("memberOfGroup",gMap); gSubjMap.put("asMemberOf",((Group)listItem).getUuid()); maps.add(gSubjMap); continue; }else if(listItem instanceof Stem) { Subject subj = SubjectFinder.findById(asMemberOf, true); Map sSubjMap = subject2Map(subj); Map sMap = stem2Map(s,(Stem)listItem); sSubjMap.put("memberOfGroup",sMap); sSubjMap.put("asMemberOf",((Stem)listItem).getUuid()); maps.add(sSubjMap); continue; } if (subject.getType().getName().equals("group")) { Group group = GroupFinder.findByUuid(s, subject.getId()); subjMap = group2Map(s, group); } else { subjMap = subject2Map(subject); } if (firstInChain != null) subjMap.put("via", group2Map(s, firstInChain)); //Group group = list.getGroup(); if (asMemberOf != null) { try{ subjMap.put("memberOfGroup",group2Map(s,GroupFinder.findByUuid(s,asMemberOf))); subjMap.put("asMemberOf", asMemberOf); }catch(GroupNotFoundException e){} }else{ if(list !=null) { subjMap.put("memberOfGroup",group2Map(s,list.getGroup())); subjMap.put("asMemberOf", list.getGroup().getUuid()); } //subjMap.put("asMemberOf", group.getUuid()); } if (chain != null) { subjMap.put("chain", chain); //subjMap.put("chainSize", new Integer(chain.size())); //subjMap.put("chainGroupIds", chainGroupIds); } if(via!=null)subjMap.put("via", via); maps.add(subjMap); } return maps; } public static String[] getChainGroupIds(GrouperSession s,Membership list) throws MemberNotFoundException,GroupNotFoundException{ Set chainIds = new LinkedHashSet(); Member chainMember; Set chain = list.getChildMemberships(); String[] chainGroupIds={}; Group via = list.getViaGroup(); if(via ==null && (chain==null ||chain.size()==0)) return chainGroupIds; if (via !=null) chainIds.add(via.getUuid()); Membership gl; Membership mv; Iterator it = chain.iterator(); while(it.hasNext()) { try { gl =(Membership) it.next(); //gl=mv.toList(s); chainMember = gl.getMember(); chainIds.add(chainMember.getSubjectId()); }catch(NullPointerException npe) { //chainGroupIds[j] = "!"; } } chainGroupIds = (String[])chainIds.toArray(chainGroupIds); return chainGroupIds; } /** * Given a GroupeGroup or GrouperStem id, an array of subjects and an array of * privileges, grant the privileges to each subject for the GrouperStem or * GrouperGroup * * @param s GrouperSession for authenticated user * @param stemOrGroupId GrouperGroup or GrouperStem id * @param members array of Subjects * @param privileges array of privileges * @param forStems indicates GrouperStem */ public static void assignPrivileges(GrouperSession s, String stemOrGroupId, Subject[] members, String[] privileges, boolean forStems) throws SchemaException,MemberAddException,InsufficientPrivilegeException,MemberNotFoundException, GrantPrivilegeException{ assignPrivileges(s,stemOrGroupId,members,privileges,forStems,FieldFinder.find("members")); } /** * Given a GroupeGroup or GrouperStem id, an array of subjects and an array of * privileges, grant the privileges to each subject for the GrouperStem or * GrouperGroup * * @param s GrouperSession for authenticated user * @param stemOrGroupId GrouperGroup or GrouperStem id * @param members array of Subjects * @param privileges array of privileges * @param forStems indicates GrouperStem */ public static void assignPrivileges(GrouperSession s, String stemOrGroupId, Subject[] members, String[] privileges, boolean forStems,Field field) throws SchemaException,MemberAddException,InsufficientPrivilegeException,MemberNotFoundException, GrantPrivilegeException{ Group group = null; Stem stem = null; Subject subject; GroupOrStem groupOrStem=GroupOrStem.findByID(s,stemOrGroupId); stem = groupOrStem.getStem(); group = groupOrStem.getGroup(); boolean circular = false; for (int i = 0; i < members.length; i++) { subject = members[i]; for (int j = 0; j < privileges.length; j++) { try { if ("member".equals(privileges[j].toLowerCase()) && !group.hasImmediateMember(subject,field)) { if(group.toSubject().equals(subject) && field.getName().equals("members")) { circular=true; }else{ group.addMember(subject,field); } } else if (groupOrStem.isStem()) { stem.grantPriv(subject,Privilege.getInstance(privileges[j])); } else if(!"member".equals(privileges[j].toLowerCase())){ group.grantPriv(subject,Privilege.getInstance(privileges[j])); } } catch (GrantPrivilegeException e) { //@TODO Expect different type of Exception in future if (e.getMessage()==null || e.getMessage().indexOf("membership already exists") == -1) throw e; } } } if(circular) { throw new IllegalArgumentException("Circular membership"); } } /** * Given the UI browsing mode return a Map where each key is a valid stem * for browsing. The Map is used to filter out stems which would lead to a * dead end.

* In a future version, this code should be factored into a new Class with interface * so that new browse modes can be defined and implemented. * * @param s GrouperSession for authenticated user * @param browseMode - UI browse mode * @return Map where keys are valid stems */ /*public static Map getValidStems(GrouperSession s, String browseMode) { Map stems = new HashMap(); List groups = null; GrouperAccess accessImpl = s.access(); GrouperNaming namingImpl = s.naming(); if ("".equals(browseMode)) { GrouperMember member = null; try { member = GrouperMember.load(s, s.subject().getId(), s.subject() .getType().getName()); } catch (SubjectNotFoundException e) { throw new RuntimeException(e); } groups = member.listVals(); } else if ("Create".equals(browseMode)) { groups = namingImpl.has(s, Grouper.PRIV_CREATE); List stemmable = namingImpl.has(s, Grouper.PRIV_STEM); groups.addAll(stemmable); } else if ("Manage".equals(browseMode)) { groups = accessImpl.has(s, Grouper.PRIV_ADMIN); List others = accessImpl.has(s, Grouper.PRIV_UPDATE); groups.addAll(others); others = accessImpl.has(s, Grouper.PRIV_READ); groups.addAll(others); List creatable = namingImpl.has(s, Grouper.PRIV_CREATE); List stemmable = namingImpl.has(s, Grouper.PRIV_STEM); groups.addAll(creatable); groups.addAll(stemmable); } else if ("Join".equals(browseMode)) { groups = accessImpl.has(s, Grouper.PRIV_OPTIN); } else if ("All".equals(browseMode)) { //return new HashMap(); groups = accessImpl.has(s, Grouper.PRIV_VIEW); //@TODO add OPTINS? } GrouperGroup group; GrouperList grouperList; String groupKey; String stem; int pos = 0; String partStem; String gkey; String name; for (int i = 0; i < groups.size(); i++) { grouperList = (GrouperList) groups.get(i); name = grouperList.group().name(); if (!stems.containsKey(name)) { stems.put(name, Boolean.TRUE); pos = 0; while (name.indexOf(HIER_DELIM, pos) > -1) { pos = name.indexOf(HIER_DELIM, pos); partStem = name.substring(0, pos); pos++; stems.put(partStem, Boolean.TRUE); } } } return stems; }*/ /** * Is s.subject() the system user? * * @param s GrouperSession for authenticated user * @return boolean */ public static boolean isSuperUser(GrouperSession s) { return s.getSubject().getId().equals("GrouperSystem")||Boolean.TRUE.equals(UIThreadLocal.get("isActiveWheelGroupMember")); } /** * Given a Subject id and SubjectType return a Subject - or null if one * not found * * @param subjectId Subject id * @param subjectType Subject type * @return Subject */ public static Subject getSubjectFromIdAndType(String subjectId, String subjectType) { try { return SubjectFinder.findById(subjectId, subjectType); } catch (Exception e) { } return null; } /** * Given a GrouperGroup id return the GrouperGroup * * @param s GrouperSession for authenticated user * @param id GrouperGroup id * @return GrouperGroup */ public static Group groupLoadById(GrouperSession s, String id) throws GroupNotFoundException{ Group group = null; group = GroupFinder.findByUuid(s, id); return group; } /** * Return an array of all access privileges + member * * @param s GrouperSession for authenticated user * @return array of privilege names */ public static String[] getGroupPrivsWithMember(GrouperSession s) { return groupPrivsWithMember; } /** * Return an array of all access privileges * * @param s GrouperSession for authenticated user * @return array of privilege names */ public static String[] getGroupPrivs(GrouperSession s) { return groupPrivs; } /** * Return an array of all naming privileges * @param s GrouperSession for authenticated user * @return array of privilege names */ public static String[] getStemPrivs(GrouperSession s) { return stemPrivs; } /** * Return a Collection of all naming privileges * @param bundle ResourceBundle to lookup display name * @return Collection of Maps of privilege names and display names */ public static Collection getStemPrivsWithLabels(ResourceBundle bundle) { List> privs = new ArrayList>(); String displayName=null; for(int i=0;i> privs = new ArrayList>(); String displayName=null; for(int i=0;i * The browseMode filtering needs to be factored into a new Class with interface * so that new browse modes can be added easily * * @param s GrouperSession for authenticated user * @param query to search for * @param from Stem which scopes search * @param searchInDisplayNameOrExtension name=displayName / extension=displayExtension * @param searchInNameOrExtension name=name / extension=extension * @param browseMode UI browse mode to filter results by * @return List of GrouperGroups matched */ public static List searchGroups(GrouperSession s, String query, String from, String searchInDisplayNameOrExtension, String searchInNameOrExtension,String browseMode) throws Exception{ List groups = searchGroupsHelper(s, query, from, searchInDisplayNameOrExtension, searchInNameOrExtension, browseMode); return groups; } /** * Given simple query, scoping stem and ui browseMode return list of * matching groups, pruned to give results relevant to browseMode.

* The browseMode filtering needs to be factored into a new Class with interface * so that new browse modes can be added easily * * @param s GrouperSession for authenticated user * @param query to search for * @param from Stem which scopes search * @param searchInDisplayNameOrExtension name=displayName / extension=displayExtension * @param searchInNameOrExtension name=name / extension=extension * @param browseMode UI browse mode to filter results by * @return List of GrouperGroups matched */ private static List searchGroupsHelper(GrouperSession s, String query, String from, String searchInDisplayNameOrExtension, String searchInNameOrExtension,String browseMode) throws Exception{ String type = null; List res = searchGroups(s, query, from,searchInDisplayNameOrExtension,searchInNameOrExtension); if (res != null) return res; List returnRes = new ArrayList(); Set allowedSet = null; if ("All".equals(browseMode)) { return res; } else if ("".equals(browseMode)) { allowedSet = GrouperHelper.getMembershipsSet(s); } else if ("Manage".equals(browseMode)) { allowedSet = GrouperHelper.getGroupsForPrivileges(s, new String[] { "admin", "update", "read" }); } else if ("Join".equals(browseMode)) { allowedSet = GrouperHelper.getGroupsForPrivileges(s, new String[] { "optin" }); } if (allowedSet != null) { Map allowed = new HashMap(); Iterator it = allowedSet.iterator(); Group group; while (it.hasNext()) { group = (Group) it.next(); allowed.put(group.getUuid(),Boolean.TRUE); } for (int i = 0; i < res.size(); i++) { group = (Group) res.get(i); if (allowed.containsKey(group.getUuid())) returnRes.add(group); } } return returnRes; } /* * public static List getNestedStemChildren(GrouperSession s,String stem) { * return Group.getNestedChildren(s,stem); } */ /** * Given a simple query and scoping stem and attribute, search for matching stems and return as List * @param s GrouperSession for authenticated user * @param query to search for * @param from stem which scopes search * @param attr name of attribute to search * @return List of stems matched */ public static List searchStemsByAttribute(GrouperSession s, String query, String from,String attr) { return new ArrayList(); /*String type = null; GrouperQuery grouperQuery = new GrouperQuery(s); if (from == null && !grouperQuery.stemAttr(attr,query)) { List empty = new ArrayList(); return empty; } if (from != null && !grouperQuery.stemAttr(from,attr,query)) { List empty = new ArrayList(); return empty; } List res = grouperQuery.getStems(); return res;*/ } /** * Given a simple query and scoping stem search for matching stems and return as List * @param s GrouperSession for authenticated user * @param query to search for * @param from stem which scopes search * @param searchInDisplayNameOrExtension name=displayName / extemsion=displayExtension * @param searchInNameOrExtension name=name / extemsion=extension * @return List of stems matched */ public static List searchStems(GrouperSession s, String query, String from,String searchInDisplayNameOrExtension,String searchInNameOrExtension) throws StemNotFoundException,QueryException{ GrouperQuery q = GrouperQuery.createQuery(s,new StemNameAnyFilter(query,StemFinder.findByName(s,from))); Set res = q.getStems(); List displayResults = new ArrayList(res); /*List nonDisplayResults=null; String attr = null; if(!"".equals(searchInDisplayNameOrExtension)) { if("name".equals(searchInDisplayNameOrExtension)) { attr="displayName"; }else{ attr="displayExtension"; } displayResults = searchStemsByAttribute(s,query,from,attr); } if(!"".equals(searchInNameOrExtension)) { if("name".equals(searchInNameOrExtension)) { attr="name"; }else{ attr="extension"; } nonDisplayResults = searchStemsByAttribute(s,query,from,attr); } if(displayResults==null && nonDisplayResults==null) return new ArrayList(); if(displayResults==null && nonDisplayResults!=null) return nonDisplayResults; if(displayResults!=null && nonDisplayResults==null) return displayResults; Object obj; for(int i=0;i getMembershipsSet(GrouperSession s) { return getMembershipsSet(s, 0, 100000, null); } /** * Given a GrouperSession s, determine all the GrouperGroups s.subject() * is a member of and return a Map where the keys are GrouperGroup keys * * @param s GrouperSession for authenticated user * @return Map where keys are GrouperGroup keys */ public static Map getMemberships(GrouperSession s) { Map memberships = null; memberships = new HashMap(); Member member = null; try { member = MemberFinder.findBySubject(s, s.getSubject()); } catch (Exception e) { throw new RuntimeException(e); } Set vals = member.getGroups(); Group group; Iterator it = vals.iterator(); while(it.hasNext()){ group = (Group) it.next(); memberships.put(group.getUuid(), Boolean.TRUE); } return memberships; } /** * Given a GrouperSession s, return a subset of Groups where * s.subject is a member, determined by start and pageSize.

* totalCount is a dubious means to return a second value to allow the UI * to page results * * @param s GrouperSession for authenticated user * @param start where subset begins * @param pageSize no of groups to return * @param totalCount number of overall results * @return Set - subset of groups */ public static Set getMembershipsSet(GrouperSession s, int start, int pageSize, StringBuffer totalCount) { Set memberships = new LinkedHashSet(); Member member = null; try { member = MemberFinder.findBySubject(s, s.getSubject()); } catch (Exception e) { throw new RuntimeException(e); } Set vals = member.getGroups(); Group group; int end = start + pageSize; if (end > vals.size()) end = vals.size(); if (totalCount != null) { totalCount.setLength(0); totalCount.append("" + vals.size()); } Iterator it = vals.iterator(); while(it.hasNext()){ group = (Group) it.next(); memberships.add(group); } return memberships; } /** * Given a GrouperSession s, and an array of privileges, get all groups * where s.subject() has atleast one of the privileges * * @param s GrouperSession for authenticated user * @param privs privileges to test * @return Set of Grouper Groups */ public static Set getGroupsForPrivileges(GrouperSession s, String[] privs) throws MemberNotFoundException{ Set groups = getGroupsForPrivileges(s, privs, 0, 100000, null); return groups; } /** * Given a GrouperSession s, and an array of privileges, get subset of groups * where s.subject() has atleast one of the privileges, determined by start and pageSize

* resultCount is a dubious means to return a second value to allow the UI * to page results * * @param s GrouperSession for authenticated user * @param privs privileges to test * @param start of subset * @param pageSize number of GrouperGroups to return * @param resultCount overall number of GrouperGroups * @return Set - subset of GrouperGroups */ public static Set getGroupsForPrivileges(GrouperSession s, String[] privs, int start, int pageSize, StringBuffer resultCount) throws MemberNotFoundException{ Set groupSet = new LinkedHashSet(); Set allSet = new LinkedHashSet(); Member member = MemberFinder.findBySubject(s,s.getSubject()); for (int i = 0; i < privs.length; i++) { allSet.addAll(getGroupsWhereMemberHasPriv(member,privs[i])); } int end = start + pageSize; if (end > allSet.size()) end = allSet.size(); if (resultCount != null) { resultCount.setLength(0); resultCount.append("" + allSet.size()); } Iterator it = allSet.iterator(); Group group = null; Object obj; while(it.hasNext()){ obj = it.next(); if(obj instanceof Group) { groupSet.add(obj); } } return groupSet; } /** * Given a GrouperSession s, and an array of privileges, get all stems * where s.subject() has atleast one of the privileges * * @param s GrouperSession for authenticated user * @param privs privileges to test * @return Set of GrouperStems */ public static Set getStemsForPrivileges(GrouperSession s, String[] privs) throws MemberNotFoundException{ Set groups = null;//Cache.instance().getSet(s,sb.toString()); if (groups != null) return null; groups = getStemsForPrivileges(s, privs, 0, 100000, null); //Cache.instance().put(s,sb.toString(),groups); return groups; } /** * Given a GrouperSession s, and an array of privileges, get subset of stems * where s.subject() has atleast one of the privileges, determined by start and pageSize

* resultCount is a dubious means to return a second value to allow the UI * to page results * * @param s GrouperSession for authenticated user * @param privs privileges to test * @param start where subset begins * @param pageSize number of GrouperStems to return * @param resultCount overall number of GrouperStems * @return Set of stems where session subject has one or more of Naming privileges specified by privs */ public static Set getStemsForPrivileges(GrouperSession s, String[] privs, int start, int pageSize, StringBuffer resultCount) throws MemberNotFoundException{ Set stemSet = new LinkedHashSet(); Set allSet = new LinkedHashSet(); Member member = MemberFinder.findBySubject(s,s.getSubject()); for (int i = 0; i < privs.length; i++) { allSet.addAll(getGroupsOrStemsWhereMemberHasPriv(member,privs[i])); } int end = start + pageSize; if (end > allSet.size()) end = allSet.size(); if (resultCount != null) { resultCount.setLength(0); resultCount.append("" + allSet.size()); } Iterator it = allSet.iterator(); Stem stem = null; while(it.hasNext()){ stem = (Stem) it.next(); stemSet.add(stem); } return stemSet; } /** * Given a GrouperStem delete it and any children * TODO remove - redundant * @param s GrouperSession for authenticated user * @param stem GrouperStem to delete * @return boolean indicating success * @throws Exception */ private static boolean stemDelete(GrouperSession s, Stem stem) throws Exception { if (stem == null || !stem.hasStem(s.getSubject())) { return false; } String stemStr = stem.getName() + HIER_DELIM; //@TODO: when searching scoped by stem fix List children = new ArrayList();//getNestedStemChildren(s,stemStr); if (children.size() > 100) throw new Exception("Too many children (" + children.size() + ") - must be <=100"); Object[] res; Group g = null; boolean deleted = true; GrouperSession sysSession = null; try { sysSession = GrouperSession.start(SubjectFinder.findById("GrouperSystem")); } catch (Exception e) { throw new RuntimeException(e); } for (int i = 0; i < children.size(); i++) { res = (Object[]) children.get(i); g = (Group) res[1]; if (!groupDelete(sysSession, GroupOrStem.findByGroup(s,g))) { sysSession.stop(); return false; } } sysSession.stop(); return groupDelete(s, GroupOrStem.findByStem(s,stem)); } /** * Given a GrouperGroup or GroupeStem delete it. GroupeStem must not have any children. * * @param s GrouperSession for authenticated user * @param groupOrStem GrouperGroup or GrouperStem to delete * @return boolean indicating success */ public static boolean groupDelete(GrouperSession s, GroupOrStem groupOrStem) throws InsufficientPrivilegeException,MemberNotFoundException, SubjectNotFoundException,MemberDeleteException,SessionException{ Group group = groupOrStem.getGroup(); Stem stem = groupOrStem.getStem(); boolean deleted = true; if (groupOrStem == null) return false; if (groupOrStem.isStem()) { if(!stem.hasStem(s.getSubject())) { return false; } try { //Stem.delete(s, stem); } catch (Exception e) { deleted = false; } return deleted; } else { if (!group.hasAdmin(s.getSubject())) { return false; } } Set memberships = group.getMemberships();//Eff? Member member; Iterator it = memberships.iterator(); while(it.hasNext()) { member = (Member) it.next(); try { group.deleteMember(member.getSubject()); //group.listDelVal(member); } catch (RuntimeException e) { if (!"List value does not exist".equals(e.getMessage())) throw e; } } /* * members = group.listVals(); * * for(int i=0;i getOneMembershipPerSubjectOrGroup(Set memberships,String type,Map count,Map sources,int membersFilterLimit) throws MemberNotFoundException,GroupNotFoundException{ //won't pass back values but will give 'unique' list if(count==null) count=new HashMap(); List res = new ArrayList(); Iterator it = memberships.iterator(); Membership m; String id=null; Integer curCount; Object obj; while(it.hasNext()) { obj=it.next(); if(!(obj instanceof Membership)) { res.add(obj); continue; } m = (Membership)obj; if("subject".equals(type)){ id = m.getGroup().getUuid(); }else if("group".equals(type)) { //id =m.getMember().getSubjectId(); id=m.getMemberUuid(); }else{ throw new IllegalArgumentException("type must be 'subject' or 'group'"); } curCount=(Integer)count.get(id); if(curCount==null) { curCount = new Integer(1); res.add(m); }else{ curCount = new Integer(curCount.intValue()+1); } count.put(id,curCount); if(memberships.size() < membersFilterLimit) sources.put(m.getMember().getSubjectSource().getId(), m.getMember().getSubjectSource().getName()); } return res; } /** * Given a trimmed list of memberships which are now Maps, add noWays -> number of * direct and indirect memberships * @param membershipMaps * @param type * @param count * @throws GroupNotFoundException * @throws MemberNotFoundException */ public static void setMembershipCountPerSubjectOrGroup(List membershipMaps,String type,Map count) throws GroupNotFoundException,MemberNotFoundException{ if(count==null) return; MembershipAsMap mMap; Map gMap; //Map sMap; String id; Integer curCount; Membership m=null; for(int i=0;i0) { res.add(field.getName()); } } } accumulateFields(res); return res; } public static Map listOfFieldsToMap(List fields) { Map map = new HashMap(); String name; for(int i=0;i names=new ArrayList(); for (int i=0;i fields) { Set accumulated = (Set)UIThreadLocal.get("accumulatedFields"); if(accumulated==null) { accumulated = new HashSet(); UIThreadLocal.put("accumulatedFields", accumulated); } accumulated.addAll(fields); } /** * When we query fields we 'accumulate' them so we can check if tere are any new ones. If there are * we refresh the session list * @param fieldList Map of FieldAsMaps from the HttpSession * @throws SchemaException */ public static void fixSessionFields(Map fieldList) throws SchemaException{ Set accumulated = (Set)UIThreadLocal.get("accumulatedFields"); if(accumulated==null) { return; } for(String fieldName : accumulated) { if(!fieldList.containsKey(fieldName)) { Map newFields = GrouperHelper.getFieldsAsMap(); fieldList.clear(); fieldList.putAll(newFields); break; } } } /** * Retrieve Map of attributes which can be searched * @return Map of searchable fields * @throws SchemaException */ public static Map getFieldsAsMap() throws SchemaException{ Set fields = FieldFinder.findAll(); Iterator it = fields.iterator(); Field field; Map fieldMap; Map map = new LinkedHashMap(); ResourceBundle bundle = GrouperUiFilter.retrieveSessionNavResourceBundle(); while(it.hasNext()) { field = (Field) it.next(); fieldMap=ObjectAsMap.getInstance("FieldAsMap",field); map.put(field.getName(),fieldMap); } for (AttributeDefName legacyAttribute : GroupTypeFinder.internal_findAllLegacyAttributes()) { map.put(legacyAttribute.getName(), ObjectAsMap.getInstance("FieldAsMap", legacyAttribute)); } String[] primaryFields = new String[] {"extension","displayExtension","name","displayName","description"}; for (int i=0;i assignmentsMap = group.internal_getGroupTypeAssignments(); for (String groupTypeName : assignmentsMap.keySet()) { GroupType legacyGroupType = GroupTypeFinder.find(groupTypeName, true); AttributeAssign groupTypeAssignment = assignmentsMap.get(groupTypeName); try { AttributeDef attributeDef = legacyGroupType.internal_getAttributeDefForAttributes(); if (attributeDef != null) { Set attrs = GrouperDAOFactory.getFactory().getAttributeDefName().findByAttributeDef(attributeDef.getId()); for (AttributeDefName attr : attrs) { if (attr.getLegacyAttributeName(false) != null) { groupTypeAssignment.getAttributeDelegate().assertCanUpdateAttributeDefName(attr); return true; } } } } catch (InsufficientPrivilegeException e) { // don't have access } catch (AttributeDefNotFoundException e) { // don't have access } } return false; } private static String[] searchableStemFields=new String[] {"displayExtension","extension","displayName","name"}; private static String[] searchableGroupFields=new String[] {"displayExtension","extension","displayName","name"}; /** * Returns a list of Maps representing name / displayNames for stem fields * Stems don't have fields in the way Groups do. This approach allows for similar * code for advanced group and stem searching * @param bundle * @return a list of Maps representing name / displayNames for stem fields */ public static List getSearchableStemFields(ResourceBundle bundle) { List res = new ArrayList(); for (int i=0;i types=g.getTypes(); int count=0; for(GroupType type : types) { Set fields = type.getFields(); for(Field field : fields) { try { if(!field.equals(f) && field.isGroupListField() && g.canReadField(field)) { count++; } }catch(SchemaException e) { LOG.error(e); } } } return count > 0 || !f.getName().equals("members"); } /*public static List query(String sql) throws Exception{ Connection con = null; try { List results = new ArrayList(); Session hs = HibernateDAO.getSession(); con = hs.connection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); Object obj; while(rs.next()) { obj = rs.getObject(1); results.add(obj); } return results; } catch (Exception e) { throw new QueryException("error finding groups: " + e.getMessage(), e); }finally { try { con.close(); }catch(Exception e){} } }*/ /** * Return true if the following parameter values would allow a subject to * copy the specified stem to another location. * @param stem * @param canCopy This is false if there's a security group limiting the subjects * that can copy stems and this subject is not in the group. * @return boolean */ public static boolean canCopyStem(Stem stem, boolean canCopy) { if (!canCopy || stem.isRootStem()) { return false; } return true; } /** * Returns true if the following parameter values would allow a subject to * move the specified stem to another location. * @param stem * @param canMove This is false if there's a security group limiting the subjects * that can move stems and this subject is not in the group. * @param privs The naming privileges that the subject has on the stem. * @return boolean */ public static boolean canMoveStem(Stem stem, boolean canMove, Set privs) { if (!canMove || stem.isRootStem()) { return false; } if (privs.contains(NamingPrivilege.STEM_ADMIN)) { return true; } return false; } /** * Returns true if the following parameter values would allow a subject to * copy a stem to the specified stem. * @param canCopy This is false if there's a security group limiting the subjects * that can copy stems and this subject is not in the group. * @param privs The naming privileges that the subject has on the specified stem. * @return boolean */ public static boolean canCopyOtherStemToStem(Stem stem, boolean canCopy, Set privs) { if (!canCopy) { return false; } if (privs.contains(NamingPrivilege.STEM_ADMIN)) { return true; } return false; } /** * Returns true if the following parameters would allow a subject to * move a stem to the specified stem. * @param canMove This is false if there's a security group limiting the subjects * that can move stems and this subject is not in the group. * @param privs The naming privileges that the subject has on the specified stem. * @return boolean */ public static boolean canMoveOtherStemToStem(Stem stem, boolean canMove, Set privs) { if (!canMove) { return false; } if (privs.contains(NamingPrivilege.STEM_ADMIN)) { return true; } return false; } /** * Returns true if the following parameters would allow a subject to * copy a group to the specified stem. * @param stem * @param privs The naming privileges that the subject has on the stem. * @return boolean */ public static boolean canCopyGroupToStem(Stem stem, Set privs) { if (stem.isRootStem()) { return false; } if (privs.contains(NamingPrivilege.CREATE)) { return true; } return false; } /** * Returns true if the following parameters would allow a subject to * move a group to the specified stem. * @param stem * @param privs The naming privileges that the subject has on the stem. * @return boolean */ public static boolean canMoveGroupToStem(Stem stem, Set privs) { if (stem.isRootStem()) { return false; } if (privs.contains(NamingPrivilege.CREATE)) { return true; } return false; } /** * Copy a group * @param group * @param destinationStem * @param selections * @return */ public static Group copyGroup(Group group, Stem destinationStem, String[] selections) { List selectionsList = new LinkedList(); if (selections != null) { for (int i = 0; i < selections.length; i++) { selectionsList.add(selections[i]); } } GroupCopy groupCopy = new GroupCopy(group, destinationStem); // set options for copy if (selectionsList.contains("copyPrivilegesOfGroup")) { groupCopy.copyPrivilegesOfGroup(true); } else { groupCopy.copyPrivilegesOfGroup(false); } if (selectionsList.contains("copyGroupAsPrivilege")) { groupCopy.copyGroupAsPrivilege(true); } else { groupCopy.copyGroupAsPrivilege(false); } if (selectionsList.contains("copyListMembersOfGroup")) { groupCopy.copyListMembersOfGroup(true); } else { groupCopy.copyListMembersOfGroup(false); } if (selectionsList.contains("copyListGroupAsMember")) { groupCopy.copyListGroupAsMember(true); } else { groupCopy.copyListGroupAsMember(false); } if (selectionsList.contains("copyAttributes")) { groupCopy.copyAttributes(true); } else { groupCopy.copyAttributes(false); } Group newGroup = groupCopy.save(); return newGroup; } /** * Move a group * @param group * @param destinationStem * @param selections */ public static void moveGroup(Group group, Stem destinationStem, String[] selections) { List selectionsList = new LinkedList(); if (selections != null) { for (int i = 0; i < selections.length; i++) { selectionsList.add(selections[i]); } } GroupMove groupMove = new GroupMove(group, destinationStem); // set options for move if (selectionsList.contains("assignAlternateName")) { groupMove.assignAlternateName(true); } else { groupMove.assignAlternateName(false); } groupMove.save(); } /** * Copy a stem * @param stemToCopy * @param destinationStem * @param selections * @return */ public static Stem copyStem(Stem stemToCopy, Stem destinationStem, String[] selections) { List selectionsList = new LinkedList(); if (selections != null) { for (int i = 0; i < selections.length; i++) { selectionsList.add(selections[i]); } } StemCopy stemCopy = new StemCopy(stemToCopy, destinationStem); // set options if (selectionsList.contains("copyPrivilegesOfStem")) { stemCopy.copyPrivilegesOfStem(true); } else { stemCopy.copyPrivilegesOfStem(false); } if (selectionsList.contains("copyPrivilegesOfGroup")) { stemCopy.copyPrivilegesOfGroup(true); } else { stemCopy.copyPrivilegesOfGroup(false); } if (selectionsList.contains("copyGroupAsPrivilege")) { stemCopy.copyGroupAsPrivilege(true); } else { stemCopy.copyGroupAsPrivilege(false); } if (selectionsList.contains("copyListMembersOfGroup")) { stemCopy.copyListMembersOfGroup(true); } else { stemCopy.copyListMembersOfGroup(false); } if (selectionsList.contains("copyListGroupAsMember")) { stemCopy.copyListGroupAsMember(true); } else { stemCopy.copyListGroupAsMember(false); } if (selectionsList.contains("copyAttributes")) { stemCopy.copyAttributes(true); } else { stemCopy.copyAttributes(false); } Stem newStem = stemCopy.save(); return newStem; } /** * Move a stem * @param stemToMove * @param destinationStem * @param selections */ public static void moveStem(Stem stemToMove, Stem destinationStem, String[] selections) { List selectionsList = new LinkedList(); if (selections != null) { for (int i = 0; i < selections.length; i++) { selectionsList.add(selections[i]); } } StemMove stemMove = new StemMove(stemToMove, destinationStem); // set options if (selectionsList.contains("assignAlternateName")) { stemMove.assignAlternateName(true); } else { stemMove.assignAlternateName(false); } stemMove.save(); } public static String getMemberDisplayValue(Member member, ResourceBundle bundle) { String field="description"; String value="unknown"; try { field = bundle.getString("subject.display." + member.getSubjectSourceId()); }catch (MissingResourceException e) { try { field = bundle.getString("subject.display.default"); }catch(MissingResourceException ee) { } } try { value=member.getSubject().getAttributeValue(field); }catch(Exception e) { } if(value==null){ value=member.getSubjectId().toString(); } return value; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy