/** * Create a GroupMembership linking this profile to the given group, within the given * organisation. Is immediately saved * * <p>If a duplicate membership exists an exception is thrown * * <p>Check for existing membership by calling getGroupMembership(group, hasGruopInOrg, session) * * @param g * @param hasGroupInOrg * @param session * @return */ public GroupMembership createGroupMembership( Group g, Organisation hasGroupInOrg, Session session) { GroupMembership gm = getGroupMembership(g, hasGroupInOrg, session); if (gm != null) { return gm; } gm = new GroupMembership(); gm.setCreatedDate(new Date()); gm.setGroupEntity(g); gm.setMember(this); gm.setWithinOrg(hasGroupInOrg); gm.setModifiedDate(new Date()); session.save(gm); if (g.getGroupMemberships() == null) { g.setGroupMemberships(new ArrayList<GroupMembership>()); } g.getGroupMemberships().add(gm); // Need to create a subordinate record for each parent organisation Organisation subordinateTo = hasGroupInOrg; while (subordinateTo != null) { createSubordinate(subordinateTo, gm, session); subordinateTo = subordinateTo.getOrganisation(); } if (getMemberships() == null) { setMemberships(new ArrayList<GroupMembership>()); } getMemberships().add(gm); return gm; }
/** * Find out if this user is associated with the group in an organisation which is within the * membership group * * @param groupName * @param org * @return */ public boolean isInGroup(String groupName, Organisation org) { if (getMemberships() != null) { for (GroupMembership m : getMemberships()) { if (m.getGroupEntity().getName().equals(groupName)) { if (org.isWithin(m.getWithinOrg())) { return true; } } } } return false; }
public boolean hasRole(String roleName, Organisation org) { if (getMemberships() != null) { for (GroupMembership m : getMemberships()) { if (org.isWithin(m.getWithinOrg())) { for (GroupRole r : m.getGroupEntity().getGroupRoles()) { if (r.getRoleName().equals(roleName)) { return true; } } } } } return false; }
public GroupMembership getGroupMembership(Group g, Organisation hasGroupInOrg, Session session) { if (getMemberships() != null) { for (GroupMembership gm : getMemberships()) { if (gm.getGroupEntity().getId() == g.getId()) { // same group if (gm.getWithinOrg().getId() == hasGroupInOrg.getId()) { // and same org, so its a duplicate return gm; } } } } return null; }
public void delete(Session session) { if (getGroupRoles() != null) { for (GroupRole gr : getGroupRoles()) { gr.delete(session); } } if (getGroupMemberships() != null) { List<GroupMembership> list = new ArrayList<>(getGroupMemberships()); for (GroupMembership gm : list) { gm.delete(session); session.flush(); } } Organisation org = getOrganisation(); if (org != null) { if (org.getGroups() != null) { org.getGroups().remove(this); } } for (GroupInWebsite giw : GroupInWebsite.findByGroup(this, session)) { session.delete(giw); } session.delete(this); }
/** * Find a user who has a membership in the given organisation * * @param org * @param name * @param session * @return */ public static Profile find(Organisation org, String name, Session session) { Criteria crit = session.createCriteria(Profile.class); crit.setCacheable(true); // join to group membership, then subordinate, then restrict on org Criteria critMembership = crit.createCriteria("memberships"); Criteria critSubordinate = critMembership.createCriteria("subordinates"); crit.add(Restrictions.eq("name", name)); critSubordinate.add(Restrictions.eq("withinOrg", org)); List list = crit.list(); if (list == null || list.isEmpty()) { log.warn("Profile not found: " + name + " in org: " + org.getOrgId()); return null; } else { return (Profile) list.get(0); } }