Пример #1
0
  private void validateCreateOrgData(
      String orgName,
      String password,
      String firstName,
      String lastName,
      String email,
      Boolean usePamAuth) {

    Map<String, String> values = new HashMap<String, String>();
    values.put("orgName", orgName);
    values.put("desiredPassword", password);
    values.put("desiredPasswordConfirm", password);
    values.put("firstNames", firstName);
    values.put("lastName", lastName);

    ValidatorResult result =
        RhnValidationHelper.validate(
            this.getClass(), values, new LinkedList<String>(values.keySet()), VALIDATION_XSD);

    if (!result.isEmpty()) {
      log.error("Validation errors:");
      for (ValidatorError error : result.getErrors()) {
        log.error("   " + error.getMessage());
      }
      // Multiple errors could return here, but we'll have to just throw an
      // exception for the first one and return that to the user.
      ValidatorError e = result.getErrors().get(0);
      throw new ValidationException(e.getMessage());
    }

    if (!usePamAuth && StringUtils.isEmpty(password)) {
      throw new FaultException(
          -501, "passwordRequiredOrUsePam", "Password is required if not using PAM authentication");
    }
  }
Пример #2
0
 /**
  * create an exception from a ValidatorResult
  *
  * @param errorIn error number
  * @param labelIn label
  * @param resultIn validator result
  * @return new FaultException
  */
 public static FaultException create(int errorIn, String labelIn, ValidatorResult resultIn) {
   for (Iterator<ValidatorError> iter = resultIn.getErrors().iterator(); iter.hasNext(); ) {
     ValidatorError ve = iter.next();
     return new FaultException(errorIn, labelIn, ve.getKey(), ve.getValues());
   }
   for (Iterator<ValidatorWarning> iter = resultIn.getWarnings().iterator(); iter.hasNext(); ) {
     ValidatorWarning vw = iter.next();
     return new FaultException(errorIn, labelIn, vw.getKey(), vw.getValues());
   }
   return new FaultException(errorIn, labelIn, "");
 }
Пример #3
0
  /**
   * Creates a new user
   *
   * @param loggedInUser The current user
   * @param desiredLogin The login for the new user
   * @param desiredPassword The password for the new user
   * @param firstName The first name of the new user
   * @param lastName The last name of the new user
   * @param email The email address for the new user
   * @param usePamAuth Should this user authenticate via PAM?
   * @return Returns 1 if successful (exception otherwise)
   * @throws FaultException A FaultException is thrown if the loggedInUser doesn't have permissions
   *     to create new users in thier org.
   * @xmlrpc.doc Create a new user.
   * @xmlrpc.param #param("string", "sessionKey")
   * @xmlrpc.param #param_desc("string", "desiredLogin", "Desired login name, will fail if already
   *     in use.")
   * @xmlrpc.param #param("string", "desiredPassword")
   * @xmlrpc.param #param("string", "firstName")
   * @xmlrpc.param #param("string", "lastName")
   * @xmlrpc.param #param_desc("string", "email", "User's e-mail address.")
   * @xmlrpc.param #param_desc("int", "usePamAuth", "1 if you wish to use PAM authentication for
   *     this user, 0 otherwise.")
   * @xmlrpc.returntype #return_int_success()
   */
  public int create(
      User loggedInUser,
      String desiredLogin,
      String desiredPassword,
      String firstName,
      String lastName,
      String email,
      Integer usePamAuth)
      throws FaultException {
    // Logged in user must be an org admin and we must be on a sat to do this.
    ensureOrgAdmin(loggedInUser);
    ensurePasswordOrPamAuth(usePamAuth, desiredPassword);

    boolean pamAuth = BooleanUtils.toBoolean(usePamAuth, new Integer(1), new Integer(0));

    if (pamAuth) {
      desiredPassword = getDefaultPasswordForPamAuth();
    }

    CreateUserCommand command = new CreateUserCommand();
    command.setUsePamAuthentication(pamAuth);
    command.setLogin(desiredLogin);
    command.setPassword(desiredPassword);
    command.setFirstNames(firstName);
    command.setLastName(lastName);
    command.setEmail(email);
    command.setOrg(loggedInUser.getOrg());
    command.setCompany(loggedInUser.getCompany());

    // Validate the user to be
    ValidatorError[] errors = command.validate();
    if (errors.length > 0) {
      StringBuilder errorString = new StringBuilder();
      LocalizationService ls = LocalizationService.getInstance();
      // Build a sane error message here
      for (int i = 0; i < errors.length; i++) {
        ValidatorError err = errors[i];
        errorString.append(ls.getMessage(err.getKey(), err.getValues()));
        if (i != errors.length - 1) {
          errorString.append(" :: ");
        }
      }
      // Throw a BadParameterException with our message string
      throw new BadParameterException(errorString.toString());
    }

    command.storeNewUser();
    return 1;
  }
Пример #4
0
  /**
   * Set an organizations entitlement allocation for a channel family.
   *
   * <p>If increasing the entitlement allocation, the default organization must have a sufficient
   * number of free entitlements.
   *
   * @param sessionKey User's session key.
   * @param orgId Organization ID to set allocation for.
   * @param channelFamilyLabel Channel family to set allocation for.
   * @param allocation New flex entitlement allocation.
   * @return 1 on success.
   * @xmlrpc.doc Set an organization's flex entitlement allocation for the given software
   *     entitlement.
   *     <p>If increasing the flex entitlement allocation, the default organization (i.e. orgId=1)
   *     must have a sufficient number of free flex entitlements.
   * @xmlrpc.param #param("string", "sessionKey")
   * @xmlrpc.param #param("int", "orgId")
   * @xmlrpc.param #param_desc("string", "label", "Software entitlement label.")
   * @xmlrpc.param #param("int", "allocation")
   * @xmlrpc.returntype #return_int_success()
   */
  public int setSoftwareFlexEntitlements(
      String sessionKey, Integer orgId, String channelFamilyLabel, Integer allocation) {

    getSatAdmin(sessionKey);
    Org org = verifyOrgExists(orgId);
    lookupChannelFamily(channelFamilyLabel);

    UpdateOrgSoftwareEntitlementsCommand cmd =
        new UpdateOrgSoftwareEntitlementsCommand(
            channelFamilyLabel, org, null, Long.valueOf(allocation));
    ValidatorError ve = cmd.store();
    if (ve != null) {
      throw new ValidationException(ve.getMessage());
    }

    return 1;
  }
 public void testDuplicateKeyCreation() throws Exception {
   String keyName = "Hey!";
   ActivationKeyManager.getInstance()
       .createNewActivationKey(user, keyName, null, null, null, false);
   try {
     ActivationKeyManager.getInstance()
         .createNewActivationKey(user, keyName, "Cool Duplicate", null, null, false);
     String msg = "Duplicate Key exception not raised..";
     fail(msg);
   } catch (ValidatorException e) {
     for (ValidatorError er : e.getResult().getErrors()) {
       if (er.getKey().equals("activation-key.java.exists")) {
         // sweet duplicate object exception
         return;
       }
     }
     throw e;
   }
 }
Пример #6
0
  /**
   * Set an organizations entitlement allocation for a channel family.
   *
   * <p>If increasing the entitlement allocation, the default organization (i.e. orgId=1) must have
   * a sufficient number of free entitlements.
   *
   * @param sessionKey User's session key.
   * @param orgId Organization ID to set allocation for.
   * @param systemEntitlementLabel System entitlement to set allocation for.
   * @param allocation New entitlement allocation.
   * @return 1 on success.
   * @xmlrpc.doc Set an organization's entitlement allocation for the given software entitlement.
   *     <p>If increasing the entitlement allocation, the default organization (i.e. orgId=1) must
   *     have a sufficient number of free entitlements.
   * @xmlrpc.param #param("string", "sessionKey")
   * @xmlrpc.param #param("int", "orgId")
   * @xmlrpc.param #param_desc("string", "label", "System entitlement label. Valid values include:")
   *     #options() #item("enterprise_entitled") #item("monitoring_entitled")
   *     #item("provisioning_entitled") #item("virtualization_host")
   *     #item("virtualization_host_platform") #options_end()
   * @xmlrpc.param #param("int", "allocation")
   * @xmlrpc.returntype #return_int_success()
   */
  public int setSystemEntitlements(
      String sessionKey, Integer orgId, String systemEntitlementLabel, Integer allocation) {

    getSatAdmin(sessionKey);

    Org org = verifyOrgExists(orgId);

    Entitlement ent = EntitlementManager.getByName(systemEntitlementLabel);
    if (ent == null
        || (!EntitlementManager.getAddonEntitlements().contains(ent)
            && !EntitlementManager.getBaseEntitlements().contains(ent))) {
      throw new InvalidEntitlementException();
    }

    UpdateOrgSystemEntitlementsCommand cmd =
        new UpdateOrgSystemEntitlementsCommand(ent, org, new Long(allocation));
    ValidatorError ve = cmd.store();
    if (ve != null) {
      throw new ValidationException(ve.getMessage());
    }

    return 1;
  }