public void testOrgDefautRegistrationToken() throws Exception {
    User user = UserTestUtils.findNewUser("testUser", "testOrg", true);
    Org orig = user.getOrg();
    orig.setName("org created by OrgFactory test: " + TestUtils.randomString());
    // build the channels set
    Channel channel1 = ChannelFactoryTest.createTestChannel(orig);
    flushAndEvict(channel1);
    orig.addOwnedChannel(channel1);
    orig = OrgFactory.save(orig);
    assertTrue(orig.getId().longValue() > 0);

    assertNull(orig.getToken());
    ActivationKey key = ActivationKeyTest.createTestActivationKey(user);
    // Token is hidden behind activation key so we have to look it up
    // manually:
    Token token = TokenFactory.lookupById(key.getId());
    orig.setToken(token);
    orig = OrgFactory.save(orig);
    Long origId = orig.getId();
    flushAndEvict(orig);

    Org lookup = OrgFactory.lookupById(origId);
    assertEquals(token.getId(), lookup.getToken().getId());
    lookup.setToken(null);
    flushAndEvict(lookup);

    lookup = OrgFactory.lookupById(origId);
    assertNull(lookup.getToken());
  }
  public void testCustomDataKeys() {
    User user = UserTestUtils.findNewUser("testuser", "testorg");
    Org org = user.getOrg();

    Set keys = org.getCustomDataKeys();
    int sizeBefore = keys.size();

    CustomDataKey key = CustomDataKeyTest.createTestCustomDataKey(user);
    assertFalse(keys.contains(key));
    assertFalse(org.hasCustomDataKey(key.getLabel()));
    assertFalse(org.hasCustomDataKey("foo" + System.currentTimeMillis()));
    assertFalse(org.hasCustomDataKey(null));

    org.addCustomDataKey(key);

    keys = org.getCustomDataKeys();
    int sizeAfter = keys.size();

    assertTrue(keys.contains(key));
    assertTrue(sizeBefore < sizeAfter);
    assertTrue(org.hasCustomDataKey(key.getLabel()));

    CustomDataKey key2 = OrgFactory.lookupKeyByLabelAndOrg(key.getLabel(), org);
    assertNotNull(key2);

    key2 = OrgFactory.lookupKeyByLabelAndOrg(null, org);
    assertNull(key2);
  }
 public void testAddVirtualization() throws Exception {
   Org org1 = UserTestUtils.findNewOrg("testOrg" + this.getClass().getSimpleName());
   org1.getEntitlements().add(OrgFactory.getEntitlementVirtualization());
   TestUtils.saveAndFlush(org1);
   org1 = (Org) reload(org1);
   assertTrue(org1.hasEntitlement(OrgFactory.getEntitlementVirtualization()));
 }
 /**
  * @return upper range for which a org can get entitlements (org1 max - org1 consumed + org max)
  */
 public Long getUpperRange() {
   Long defaultMax =
       EntitlementManager.getMaxEntitlements(this.getEntitlement(), OrgFactory.getSatelliteOrg());
   Long defaultCur =
       EntitlementManager.getUsedEntitlements(this.getEntitlement(), OrgFactory.getSatelliteOrg());
   Long upper = getMaxEntitlements() + (defaultMax - defaultCur);
   return upper;
 }
 public void testCommitOrg() throws Exception {
   Org org1 = UserTestUtils.findNewOrg("testOrg" + this.getClass().getSimpleName());
   String changedName = "OrgFactoryTest testCommitOrg " + TestUtils.randomString();
   org1.setName(changedName);
   org1 = OrgFactory.save(org1);
   Long id = org1.getId();
   flushAndEvict(org1);
   Org org2 = OrgFactory.lookupById(id);
   assertEquals(changedName, org2.getName());
 }
 public void testStagingContent() throws Exception {
   Org org1 = createTestOrg();
   boolean staging = org1.getOrgConfig().isStagingContentEnabled();
   Long id = org1.getId();
   org1.getOrgConfig().setStagingContentEnabled(!staging);
   OrgFactory.save(org1);
   assertEquals(!staging, org1.getOrgConfig().isStagingContentEnabled());
   flushAndEvict(org1);
   Org org2 = OrgFactory.lookupById(id);
   assertEquals(!staging, org2.getOrgConfig().isStagingContentEnabled());
 }
 private Org createTestOrg() throws Exception {
   Org org1 = OrgFactory.createOrg();
   org1.setName("org created by OrgFactory test: " + TestUtils.randomString());
   org1 = OrgFactory.save(org1);
   // build the channels set
   Channel channel1 = ChannelFactoryTest.createTestChannel(org1);
   flushAndEvict(channel1);
   org1.addOwnedChannel(channel1);
   assertTrue(org1.getId().longValue() > 0);
   return org1;
 }
 /**
  * Constructor
  *
  * @param channelFamilyLabel label of entitlement to update
  * @param orgIn to update totals for
  * @param newTotalIn This is the *proposed* new total for the org you are passing in.
  * @param newFlexTotalIn This is the *proposed* new flex total for the org you are passing in.
  */
 public UpdateOrgSoftwareEntitlementsCommand(
     String channelFamilyLabel, Org orgIn, Long newTotalIn, Long newFlexTotalIn) {
   if (orgIn.getId().equals(OrgFactory.getSatelliteOrg().getId())) {
     throw new IllegalArgumentException("Cant update the default org");
   }
   this.org = orgIn;
   this.newTotal = newTotalIn;
   this.newFlexTotal = newFlexTotalIn;
   this.channelFamily =
       ChannelFamilyFactory.lookupByLabel(channelFamilyLabel, OrgFactory.getSatelliteOrg());
   if (this.channelFamily == null) {
     throw new IllegalArgumentException("ChannelFamily not found: [" + channelFamilyLabel + "]");
   }
 }
Example #9
0
  /**
   * Delete an organization.
   *
   * @param sessionKey User's session key.
   * @param orgId ID of organization to delete.
   * @return 1 on success, exception thrown otherwise.
   * @xmlrpc.doc Delete an organization. The default organization (i.e. orgId=1) cannot be deleted.
   * @xmlrpc.param #param("string", "sessionKey")
   * @xmlrpc.param #param("int", "orgId")
   * @xmlrpc.returntype #return_int_success()
   */
  public int delete(String sessionKey, Integer orgId) {
    User user = getSatAdmin(sessionKey);
    Org org = verifyOrgExists(orgId);

    // Verify we're not trying to delete the default org (id 1):
    Org defaultOrg = OrgFactory.getSatelliteOrg();
    if (orgId.longValue() == defaultOrg.getId().longValue()) {
      throw new SatelliteOrgException();
    }

    OrgFactory.deleteOrg(org.getId(), user);

    return 1;
  }
 /**
  * Test the addition of an entitlement to an org This code should be refactored into a business
  * method of some sort if it becomes necessary to actually add entitlements progmatically from
  * within the Java code. For now we need this test because new Orgs don't have any entitlements.
  */
 public void testAddEntitlement() throws Exception {
   // Create a new Org and add an Entitlement
   Org org1 = UserTestUtils.findNewOrg("testOrg" + this.getClass().getSimpleName());
   Set entitlements = org1.getEntitlements();
   OrgEntitlementType oet = OrgFactory.lookupEntitlementByLabel("sw_mgr_enterprise");
   entitlements.add(oet);
   org1.setEntitlements(entitlements);
   org1 = OrgFactory.save(org1);
   Long orgId = org1.getId();
   // Re-lookup the object and test it
   flushAndEvict(org1);
   Org org2 = OrgFactory.lookupById(orgId);
   assertTrue(org2.hasEntitlement(oet));
 }
Example #11
0
  /**
   * List an organization's allocation of a system entitlement.
   *
   * @param sessionKey User's session key.
   * @param label System entitlement label.
   * @param includeUnentitled If true, the result will include both organizations that have the
   *     entitlement as well as those that do not; otherwise, the result will only include
   *     organizations that have the entitlement.
   * @return a list of Maps having the system entitlements info.
   * @since 10.4
   * @xmlrpc.doc List each organization's allocation of a system entitlement.
   * @xmlrpc.param #param("string", "sessionKey")
   * @xmlrpc.param #param("string", "label")
   * @xmlrpc.param #param_desc("boolean", "includeUnentitled", "If true, the result will include
   *     both organizations that have the entitlement as well as those that do not; otherwise, the
   *     result will only include organizations that have the entitlement.")
   * @xmlrpc.returntype #array() #struct("entitlement usage") #prop("int", "org_id") #prop("string",
   *     "org_name") #prop("int", "allocated") #prop("int", "unallocated") #prop("int", "used")
   *     #prop("int", "free") #struct_end() #array_end()
   */
  public List<Map> listSystemEntitlements(
      String sessionKey, String label, Boolean includeUnentitled) {

    getSatAdmin(sessionKey);
    verifyEntitlementExists(label);

    DataList<Map> result = null;
    if (includeUnentitled) {
      result = OrgManager.allOrgsSingleEntitlementWithEmptyOrgs(label);
    } else {
      result = OrgManager.allOrgsSingleEntitlement(label);
    }

    List<Map> details = new LinkedList<Map>();
    for (Map row : result) {
      Map<String, Object> map = new HashMap<String, Object>();
      Org org = OrgFactory.lookupById((Long) row.get("orgid"));
      map.put(ORG_ID_KEY, new Integer(org.getId().intValue()));
      map.put(ORG_NAME_KEY, org.getName());
      map.put(ALLOCATED_KEY, ((Long) row.get("total")).intValue());
      map.put(USED_KEY, row.get("usage"));
      long free = (Long) row.get("total") - (Long) row.get("usage");
      map.put(FREE_KEY, free);
      long unallocated = (Long) row.get("upper") - (Long) row.get("total");
      map.put(UN_ALLOCATED_KEY, unallocated);
      details.add(map);
    }
    return details;
  }
Example #12
0
 private Org verifyOrgExists(String name) {
   Org org = OrgFactory.lookupByName(name);
   if (org == null) {
     throw new NoSuchOrgException(name);
   }
   return org;
 }
 /** @return OrgEntitlementDto object */
 public OrgEntitlementDto getDto() {
   if (this.orgEntDto == null) {
     Org orgObj = OrgFactory.lookupById(new Long(this.orgid.longValue()));
     this.orgEntDto = new OrgEntitlementDto(EntitlementManager.getByName(this.entname), orgObj);
   }
   return this.orgEntDto;
 }
  public void testLookupOrgsWithServersInFamily() throws Exception {
    Server s = ServerTestUtils.createTestSystem();
    Channel chan = s.getChannels().iterator().next();
    ChannelFamily family = chan.getChannelFamily();

    List<Org> orgs = OrgFactory.lookupOrgsUsingChannelFamily(family);
    assertEquals(1, orgs.size());
  }
Example #15
0
 private Org verifyOrgExists(Number orgId) {
   if (orgId == null) {
     throw new NoSuchOrgException("null Id");
   }
   Org org = OrgFactory.lookupById(orgId.longValue());
   if (org == null) {
     throw new NoSuchOrgException(orgId.toString());
   }
   return org;
 }
 private ValidatorError storeFlex() {
   // Check available entitlements
   Org satOrg = OrgFactory.getSatelliteOrg();
   return store(
       channelFamily.getMaxFlex(satOrg),
       channelFamily.getMaxFlex(org),
       channelFamily.getCurrentFlex(satOrg),
       channelFamily.getCurrentFlex(org),
       newFlexTotal,
       true);
 }
 private ValidatorError storeRegular() {
   // Check available entitlements
   Org satOrg = OrgFactory.getSatelliteOrg();
   return store(
       channelFamily.getMaxMembers(satOrg),
       channelFamily.getMaxMembers(org),
       channelFamily.getCurrentMembers(satOrg),
       channelFamily.getCurrentMembers(org),
       newTotal,
       false);
 }
Example #18
0
  private int enableAccess(User loggedInUser, String channelLabel, Integer orgId, boolean enable)
      throws FaultException {
    Channel channel = lookupChannelByLabel(loggedInUser, channelLabel);
    verifyChannelAdmin(loggedInUser, channel);

    if (!loggedInUser.getOrg().equals(channel.getOrg())) {
      // users are not allowed to alter properties for a channel that is in a
      // different org
      throw new NotPermittedByOrgException(
          loggedInUser.getOrg().getId().toString(),
          channel.getLabel(),
          channel.getOrg().getId().toString());
    }

    // protected mode only for modifying individual orgs
    if (!channel.getAccess().equals(Channel.PROTECTED)) {
      throw new InvalidChannelAccessException(channel.getAccess());
    }

    Org org = OrgFactory.lookupById(orgId.longValue());
    if (org == null) {
      throw new NoSuchOrgException(orgId.toString());
    }

    // need to validate that the org provided is in the list of orgs that may
    // be granted access
    List<OrgChannelDto> orgs = OrgManager.orgChannelTrusts(channel.getId(), loggedInUser.getOrg());
    boolean orgInTrust = false;

    for (OrgChannelDto orgDto : orgs) {
      if (orgDto.getId().equals(new Long(orgId))) {
        orgInTrust = true;
        break;
      }
    }

    if (orgInTrust) {
      if (enable) {
        channel.getTrustedOrgs().add(org);
      } else {
        channel.getTrustedOrgs().remove(org);
      }
      ChannelFactory.save(channel);
    } else {
      throw new OrgNotInTrustException(orgId);
    }

    return 1;
  }
Example #19
0
  /**
   * List the organizations associated with the given channel that may be trusted.
   *
   * @param loggedInUser The current user
   * @param channelLabel The label for the channel
   * @return List of map entries indicating the orgs available and if access is enabled.
   * @throws FaultException A FaultException is thrown if: - The sessionKey is invalid - The
   *     channelLabel is invalid - The user doesn't have channel admin permissions
   * @xmlrpc.doc List the organizations associated with the given channel that may be trusted.
   * @xmlrpc.param #session_key()
   * @xmlrpc.param #param_desc("string", "channelLabel", "label of the channel")
   * @xmlrpc.returntype #array() #struct("org") #prop("int", "org_id") #prop("string", "org_name")
   *     #prop("boolean", "access_enabled") #struct_end() #array_end()
   */
  public List list(User loggedInUser, String channelLabel) throws FaultException {

    Channel channel = lookupChannelByLabel(loggedInUser, channelLabel);
    verifyChannelAdmin(loggedInUser, channel);

    if (!loggedInUser.getOrg().equals(channel.getOrg())) {
      // users are not allowed to access properties for a channel that is in a
      // different org
      throw new NotPermittedByOrgException(
          loggedInUser.getOrg().getId().toString(),
          channel.getLabel(),
          channel.getOrg().getId().toString());
    }

    // retrieve the orgs available to be "trusted" for this channel
    List<OrgChannelDto> orgs = OrgManager.orgChannelTrusts(channel.getId(), loggedInUser.getOrg());
    // retrieve the orgs that are trusted for this channel
    Set<Org> trustedOrgs = channel.getTrustedOrgs();

    // populate a result that includes all orgs that could be trusted with a boolean
    // that indicates if the orgs is indeed trusted.
    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
    for (OrgChannelDto orgDto : orgs) {
      Org org = OrgFactory.lookupById(orgDto.getId());

      if (org != null) {
        Map<String, Object> entry = new HashMap<String, Object>();

        entry.put("org_id", org.getId().intValue());
        entry.put("org_name", org.getName());
        if (trustedOrgs.contains(org)) {
          entry.put("access_enabled", Boolean.TRUE);
        } else {
          entry.put("access_enabled", Boolean.FALSE);
        }
        result.add(entry);
      }
    }
    return result;
  }
 public void testOrgTrust() throws Exception {
   Org org = createTestOrg();
   Org trusted = createTestOrg();
   org.getTrustedOrgs().add(trusted);
   OrgFactory.save(org);
   flushAndEvict(org);
   org = OrgFactory.lookupById(org.getId());
   trusted = OrgFactory.lookupById(trusted.getId());
   assertContains(org.getTrustedOrgs(), trusted);
   assertContains(trusted.getTrustedOrgs(), org);
   org.getTrustedOrgs().remove(trusted);
   OrgFactory.save(org);
   flushAndEvict(org);
   org = OrgFactory.lookupById(org.getId());
   trusted = OrgFactory.lookupById(trusted.getId());
   assertFalse(org.getTrustedOrgs().contains(trusted));
   assertFalse(trusted.getTrustedOrgs().contains(org));
 }
 public void testGetOrgCount() throws Exception {
   ServerTestUtils.createTestSystem();
   long totalOrgs = OrgFactory.getTotalOrgCount();
   assertTrue(totalOrgs > 0);
 }
 public void testImpliedEntitlement() throws Exception {
   Org org1 = createTestOrg();
   assertTrue(org1.hasEntitlement(OrgFactory.getEntitlementSwMgrPersonal()));
 }
 public void setUp() throws Exception {
   super.setUp();
   setRequestPathInfo("/Search");
   user.getOrg().getEntitlements().add(OrgFactory.getEntitlementEnterprise());
 }
 public void testHasEntitlementFalse() throws Exception {
   Org org1 = createTestOrg();
   OrgEntitlementType oet = OrgFactory.lookupEntitlementByLabel("sw_mgr_enterprise");
   assertFalse(org1.hasEntitlement(oet));
 }
 /** @return Long total of the available Entitlements in the default Org */
 public Long getSatelliteTotal() {
   Org defaultOrg = OrgFactory.getSatelliteOrg();
   return EntitlementManager.getAvailableEntitlements(this.getEntitlement(), defaultOrg);
 }
 public void testLookupSatOrg() {
   assertNotNull(OrgFactory.getSatelliteOrg());
 }
  private ValidatorError store(
      Long satMax, Long orgMax, Long satCurrent, Long orgCurrent, Long proposed, boolean isFlex) {
    // No sense making the call if its the same.
    if (orgMax == proposed) {
      return null;
    }

    if (orgMax == null) {
      orgMax = 0L;
    }

    Long avail = 0L;
    if (satMax != null) {
      avail = satMax - satCurrent + orgMax;
    }

    if (proposed == null || proposed < 0 || avail < proposed) {
      String key =
          isFlex
              ? "org.entitlements.software.not_in_range.flex"
              : "org.entitlements.software.not_in_range";

      return new ValidatorError(key, this.org.getName(), this.channelFamily.getName(), 0, avail);
    }

    // Proposed cannot be lower than current members
    if (!forceUnentitlement(orgCurrent, proposed)) {
      return new ValidatorError(
          "org.entitlements.software.proposedwarning",
          this.channelFamily.getName(),
          this.org.getName());
    }

    Long toOrgId;
    Long fromOrgId;
    long actualTotal;
    // If we are decreasing the # of entitlements
    // we give back to the default org.
    if (orgMax.longValue() > proposed) {
      fromOrgId = this.org.getId();
      toOrgId = OrgFactory.getSatelliteOrg().getId();
      actualTotal = orgMax.longValue() - proposed;
    } else {
      toOrgId = this.org.getId();
      fromOrgId = OrgFactory.getSatelliteOrg().getId();
      actualTotal = proposed - orgMax.longValue();
    }

    Map in = new HashMap();
    // "group_label, from_org_id, to_org_id, quantity"
    in.put("channel_family_label", channelFamily.getLabel());
    in.put("from_org_id", fromOrgId);
    in.put("to_org_id", toOrgId);
    if (isFlex) {
      in.put("quantity", 0);
      in.put("flex_quantity", actualTotal);

    } else {
      in.put("quantity", actualTotal);
      in.put("flex_quantity", 0);
    }
    CallableMode m = ModeFactory.getCallableMode("Org_queries", "assign_software_entitlements");
    m.execute(in, new HashMap());
    return null;
  }
 public void testLookupAllOrgs() throws Exception {
   ServerTestUtils.createTestSystem();
   List<Org> totalOrgs = OrgFactory.lookupAllOrgs();
   assertTrue(totalOrgs.size() > 0);
 }
 public void testCreateOrg() throws Exception {
   Org org1 = createTestOrg();
   Org org2 = OrgFactory.lookupById(org1.getId());
   assertEquals(org2.getName(), org1.getName());
   assertNotNull(org2.getOwnedChannels());
 }