/** * Find the closest parent organisation with a non-null admin domain, or this org if it has a * non-null admin domain * * @return */ public Organisation closestAdminOrg() { Organisation p = this; while (p.getAdminDomain() == null && p.getOrganisation() != null) { p = p.getOrganisation(); } return p; }
@Override public void delete(Session session) { // Delete links from this to each parent if (getParentOrgLinks() != null) { for (SubOrg so : getParentOrgLinks()) { session.delete(so); } setParentOrgLinks(null); } if (getWebsites() != null) { for (Website w : getWebsites()) { w.delete(session); } } setWebsites(null); if (getGroups() != null) { for (Group g : getGroups()) { g.delete(false, session); // do a hard delete } } for (Organisation childOrg : childOrgs()) { childOrg.delete(session); } session.delete(this); Organisation parent = getOrganisation(); if (parent != null) { parent.getChildOrgs().remove(this); session.save(parent); } }
/** * 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; }
/** * Updates the organisation reference and also any SubOrg links * * @param newParent * @param session */ public void setOrganisation(Organisation newParent, Session session) { log.info("setOrganisation: this=" + getOrgId()); Organisation oldParent = this.getOrganisation(); if (oldParent != null) { oldParent.getChildOrgs().remove(this); } if (newParent != null) { if (newParent.getChildOrgs() == null) { newParent.setChildOrgs(new ArrayList<Organisation>()); } newParent.getChildOrgs().add(this); } this.organisation = newParent; if (oldParent != null && oldParent != newParent) { log.info("update subs"); SubOrg.updateSubOrgs(this, SessionManager.session()); } if (oldParent != null) { session.save(oldParent); } if (newParent != null) { session.save(newParent); } session.save(this); log.info("finished move"); }
public Organisation childOrg(Long orgId, Session session) { Organisation child = get(orgId, session); if (child == null) { return null; } if (child.isWithin(this)) { return child; } return null; }
/** * Check if this organisation is within, or is, the given org * * @param withinOrg * @return */ public boolean isWithin(Organisation withinOrg) { if (withinOrg == null) { return false; } if (withinOrg == this) { return true; } Organisation parent = getOrganisation(); if (parent != null) { return parent.isWithin(withinOrg); } return false; }
public List<Organisation> childOrgs() { if (getChildOrgs() == null) { return Collections.EMPTY_LIST; } else { List<Organisation> list = new ArrayList<>(); for (Organisation o : getChildOrgs()) { if (!o.deleted()) { list.add(o); } } return list; } }
public Organisation createChildOrg(String orgId, String title, Session session) { Organisation o = new Organisation(); o.setOrganisation(this); if (orgId != null) { orgId = orgId.trim(); if (orgId.length() == 0) { orgId = null; } } o.setOrgId(orgId); o.setTitle(title); o.setCreatedDate(new Date()); o.setModifiedDate(new Date()); if (this.getChildOrgs() == null) { this.setChildOrgs(new ArrayList<>()); } this.getChildOrgs().add(o); session.save(o); if (o.getOrgId() == null || o.getOrgId().trim().length() == 0) { findUniqueOrgId(o, session); session.save(o); } SubOrg.updateSubOrgs(o, session); return o; }
private void findUniqueOrgId(Organisation o, Session session) { String candidate = "O" + System.currentTimeMillis(); int i = 0; while (!isOrgIdUniqueWithinAdmin(session)) { candidate = "O" + System.currentTimeMillis() + i++; } o.setOrgId(candidate); // hack, todo, check for uniqueness within the account }
private RootFolder resolve(String host) { if (host.contains(":")) { host = host.substring(0, host.indexOf(":")); } System.out.println("resolve: " + host); Session session = SessionManager.session(); String primaryDomainSuffix = "." + primaryDomain; if (host.endsWith(primaryDomainSuffix)) { String subdomain = Utils.stripSuffix(host, primaryDomainSuffix); System.out.println("subdoman: " + subdomain); // If starts with admin. then look for an organisation, will go to admin console if (subdomain.startsWith("admin.")) { String orgName = Utils.stripPrefix(subdomain, "admin."); System.out.println("look for org: " + orgName); Organisation org = Organisation.findByOrgId(orgName, session); if (org != null) { System.out.println("found: " + org.getName()); return new OrganisationRootFolder(applicationManager, org); } } // otherwise, look for a website with a name that matches the subdomain Website website = Website.findByName(subdomain, session); if (website != null) { System.out.println("found website: " + website.getName()); return new WebsiteRootFolder(applicationManager, website); } } // Didnt find anything matching primary domain, so look for an exact match on website Website website = Website.findByDomainName(host, session); if (website != null) { return new WebsiteRootFolder(applicationManager, website); } // Still nothing found, so drop to root org admin console Organisation org = OrganisationDao.getRootOrg(SessionManager.session()); if (org == null) { throw new RuntimeException("No root organisation"); } System.out.println("fall through to rootorg: " + org.getOrgId()); return new OrganisationRootFolder(applicationManager, org); }
/** * Remove all memberships of this profile within this organiosation or subordinate organisations * * @param profile * @param session */ public void removeMember(Profile profile, Session session) { log.info("removeMember: profileid=" + profile.getId() + " org=" + getOrgId()); List<GroupMembership> toDelete = new ArrayList<>(); if (profile.getMemberships() != null) { for (GroupMembership m : profile.getMemberships()) { Organisation memberWithin = m.getWithinOrg(); if (memberWithin.isWithin(this)) { log.info( "Remove membership of user: "******" from org: " + memberWithin.getOrgId()); toDelete.add(m); } } } for (GroupMembership m : toDelete) { m.delete(session); } }
/** * 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 OrgType createOrgType(String name, Session session) { OrgType existing = orgType(name); if (existing != null) { throw new RuntimeException("An organisation type with that name already exists"); } Organisation parent = getOrganisation(); while (parent != null) { if (parent.orgType(name) != null) { throw new RuntimeException( "An organisation type with that name exists in a parent organisation: " + parent.getOrgId()); } parent = parent.getOrganisation(); } OrgType ot = new OrgType(); ot.setName(name); ot.setDisplayName(name); ot.setOrganisation(this); getOrgTypes().add(ot); session.save(this); session.save(ot); return ot; }
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); } }
public static long countChildOrgs(Organisation organisation, Session session) { Criteria crit = session.createCriteria(Organisation.class); crit.setCacheable(true); Disjunction notDeleted = Restrictions.disjunction(); notDeleted.add(Restrictions.isNull("deleted")); notDeleted.add(Restrictions.eq("deleted", Boolean.FALSE)); crit.add(notDeleted); crit.add(Restrictions.ne("id", organisation.getId())); Criteria critParentLink = crit.createCriteria("parentOrgLinks"); critParentLink.add(Restrictions.eq("owner", organisation)); crit.setProjection(Projections.count("id")); Object result = crit.uniqueResult(); if (result == null) { return 0; } else if (result instanceof Integer) { Integer i = (Integer) result; return i.longValue(); } else { return (long) result; } }
/** * Check if the organisation ID on this org is unique within its administrative domain (ie first * parent org with a non-null adminDomain) * * @param session * @return */ public boolean isOrgIdUniqueWithinAdmin(Session session) { log.info("isOrgIdUniqueWithinAdmin: My OrgID={}", getOrgId()); Organisation admin = closestAdminOrg(); for (Organisation withSameOrgId : Organisation.findListByOrgId(admin, getOrgId(), session)) { if (withSameOrgId.getId() != this.getId()) { log.warn( "Found same orgID on record: " + withSameOrgId.getId() + " matching this record " + getId() + " in admin org= " + admin.getAdminDomain() + " id=" + admin.getId()); return false; } } log.info("isOrgIdUniqueWithinAdmin: All good"); return true; }