Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* 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