Search in sources :

Example 6 with AttributeInfo

use of com.zimbra.cs.account.AttributeInfo in project zm-mailbox by Zimbra.

the class LdapProvisioning method getBasicDLAttrs.

private String[] getBasicDLAttrs() throws ServiceException {
    AttributeManager attrMgr = AttributeManager.getInstance();
    Set<String> dlAttrs = attrMgr.getAllAttrsInClass(AttributeClass.distributionList);
    Set<String> attrs = Sets.newHashSet(dlAttrs);
    attrs.add(Provisioning.A_objectClass);
    // the member attr
    attrs.remove(Provisioning.A_zimbraMailForwardingAddress);
    // does not apply to DL
    attrs.remove(Provisioning.A_zimbraMailTransport);
    // remove deprecated attrs
    for (Iterator<String> iter = attrs.iterator(); iter.hasNext(); ) {
        String attr = iter.next();
        AttributeInfo ai = attrMgr.getAttributeInfo(attr);
        if (ai != null && ai.isDeprecated()) {
            iter.remove();
        }
    }
    return Lists.newArrayList(attrs).toArray(new String[attrs.size()]);
}
Also used : AttributeInfo(com.zimbra.cs.account.AttributeInfo) AttributeManager(com.zimbra.cs.account.AttributeManager)

Example 7 with AttributeInfo

use of com.zimbra.cs.account.AttributeInfo in project zm-mailbox by Zimbra.

the class LdapProvisioning method modifyEphemeralAttrs.

private void modifyEphemeralAttrs(Entry entry, Map<String, Object> attrs, Map<String, AttributeInfo> ephemeralAttrMap) throws ServiceException {
    EphemeralLocation location = new LdapEntryLocation(entry);
    EphemeralStore store = EphemeralStore.getFactory().getStore();
    for (Map.Entry<String, Object> e : attrs.entrySet()) {
        String key = e.getKey();
        Object value = e.getValue();
        boolean doAdd = key.charAt(0) == '+';
        boolean doRemove = key.charAt(0) == '-';
        if (doAdd || doRemove) {
            key = key.substring(1);
            if (attrs.containsKey(key))
                throw ServiceException.INVALID_REQUEST("can't mix +attrName/-attrName with attrName", null);
        }
        AttributeInfo ai = ephemeralAttrMap.get(key.toLowerCase());
        AttributeConverter converter = null;
        if (ai == null) {
            continue;
        }
        if (ai.isDynamic()) {
            converter = AttributeMigration.getConverter(key);
            if (converter == null) {
                ZimbraLog.ephemeral.warn("Dynamic ephemeral attribute %s has no registered AttributeConverter, so cannot be modified with modifyAttrs", key);
                continue;
            }
        }
        if (value instanceof Collection) {
            Collection values = (Collection) value;
            if (values.size() == 0) {
                ZimbraLog.ephemeral.warn("Ephemeral attribute %s doesn't support deletion by key; only deletion by key+value is supported", key);
                continue;
            } else {
                for (Object v : values) {
                    if (v == null) {
                        continue;
                    }
                    String s = v.toString();
                    handleEphemeralAttrChange(store, key, s, location, ai, converter, doAdd, doRemove);
                }
            }
        } else if (value instanceof Map) {
            throw ServiceException.FAILURE("Map is not a supported value type", null);
        } else if (value != null) {
            String s = value.toString();
            handleEphemeralAttrChange(store, key, s, location, ai, converter, doAdd, doRemove);
        } else {
            ZimbraLog.ephemeral.warn("Ephemeral attribute %s doesn't support deletion by key; only deletion by key+value is supported", key);
        }
    }
}
Also used : LdapEntryLocation(com.zimbra.cs.ephemeral.LdapEntryLocation) AttributeInfo(com.zimbra.cs.account.AttributeInfo) AttributeConverter(com.zimbra.cs.ephemeral.migrate.AttributeConverter) EphemeralLocation(com.zimbra.cs.ephemeral.EphemeralLocation) Collection(java.util.Collection) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) EphemeralStore(com.zimbra.cs.ephemeral.EphemeralStore) LdapEphemeralStore(com.zimbra.cs.ephemeral.LdapEphemeralStore)

Example 8 with AttributeInfo

use of com.zimbra.cs.account.AttributeInfo in project zm-mailbox by Zimbra.

the class BUG_27075 method doEntry.

private void doEntry(ZLdapContext zlc, Entry entry, String entryName, AttributeClass klass) throws ServiceException {
    printer.println();
    printer.println("------------------------------");
    printer.println("Upgrading " + entryName + ": ");
    AttributeManager am = AttributeManager.getInstance();
    Set<String> attrs = am.getAttrsInClass(klass);
    Map<String, Object> attrValues = new HashMap<String, Object>();
    for (String attr : attrs) {
        AttributeInfo ai = am.getAttributeInfo(attr);
        if (ai == null)
            continue;
        List<Version> attrVersion = ai.getSince();
        if (needsUpgrade(am, attr)) {
            if (verbose) {
                printer.println("");
                printer.println("Checking " + entryName + " attribute: " + attr + attrVersion);
            }
            String curVal = entry.getAttr(attr);
            if (curVal != null) {
                // already has a value, skip it
                if (verbose) {
                    if (ai.getCardinality() == AttributeCardinality.multi)
                        curVal = formatMultiValue(entry.getMultiAttrSet(attr));
                    printer.println("    skipping - already has value: " + curVal);
                }
                continue;
            }
            /*
                 * use the upgrade values if set, otherwise use the default values
                 *
                 * Note, we support the case when we need to leave the value unset
                 * on upgrades, but set a value on new installs.  In AttributeManager,
                 * if <globalConfigValueUpgrade> or <defaultCOSValueUpgrade> element
                 * is present but does not have a value, AttributeInfo.getGlobalConfigValuesUpgrade()/
                 * getDefaultCosValuesUpgrade() will return an empty List.  If the upgrade
                 * element is not present, the two methods will return null.  We check
                 * null here and if it is null then use the same default value for new
                 * installs.
                 */
            List<String> values = null;
            if (klass == AttributeClass.globalConfig) {
                values = ai.getGlobalConfigValuesUpgrade();
                if (values == null)
                    values = ai.getGlobalConfigValues();
            } else if (klass == AttributeClass.cos) {
                values = ai.getDefaultCosValuesUpgrade();
                if (values == null)
                    values = ai.getDefaultCosValues();
            } else {
                printer.println("Internal error: invalid attribute class " + klass.name());
                return;
            }
            if (values == null || values.size() == 0) {
                if (verbose) {
                    printer.println("    skipping - does not have a default value");
                }
                continue;
            }
            attrValues.clear();
            if (ai.getCardinality() != AttributeCardinality.multi) {
                printer.println("    setting " + entryName + " attribute " + attr + attrVersion + " to: " + values.get(0));
                attrValues.put(attr, values.get(0));
            } else {
                printer.println("    setting " + entryName + " attribute " + attr + attrVersion + " to: " + formatMultiValue(values));
                attrValues.put(attr, values.toArray(new String[0]));
            }
            try {
                modifyAttrs(zlc, entry, attrValues);
            } catch (ServiceException e) {
                // log the exception and continue
                printer.println("Caught ServiceException while modifying " + entryName + " attribute " + attr);
                printer.printStackTrace(e);
            }
        }
    }
}
Also used : AttributeInfo(com.zimbra.cs.account.AttributeInfo) AttributeManager(com.zimbra.cs.account.AttributeManager) ServiceException(com.zimbra.common.service.ServiceException) HashMap(java.util.HashMap) Version(com.zimbra.common.util.Version)

Example 9 with AttributeInfo

use of com.zimbra.cs.account.AttributeInfo in project zm-mailbox by Zimbra.

the class ModifyPrefs method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Account account = getRequestedAccount(zsc);
    if (!canModifyOptions(zsc, account))
        throw ServiceException.PERM_DENIED("can not modify options");
    HashMap<String, Object> prefs = new HashMap<String, Object>();
    Map<String, Set<String>> name2uniqueAttrValues = new HashMap<String, Set<String>>();
    for (KeyValuePair kvp : request.listKeyValuePairs(AccountConstants.E_PREF, AccountConstants.A_NAME)) {
        String name = kvp.getKey(), value = kvp.getValue();
        char ch = name.length() > 0 ? name.charAt(0) : 0;
        int offset = ch == '+' || ch == '-' ? 1 : 0;
        if (!name.startsWith(PREF_PREFIX, offset))
            throw ServiceException.INVALID_REQUEST("pref name must start with " + PREF_PREFIX, null);
        AttributeInfo attrInfo = AttributeManager.getInstance().getAttributeInfo(name.substring(offset));
        if (attrInfo == null) {
            throw ServiceException.INVALID_REQUEST("no such attribute: " + name, null);
        }
        if (attrInfo.isCaseInsensitive()) {
            String valueLowerCase = Strings.nullToEmpty(value).toLowerCase();
            if (name2uniqueAttrValues.get(name) == null) {
                Set<String> set = new HashSet<String>();
                set.add(valueLowerCase);
                name2uniqueAttrValues.put(name, set);
                StringUtil.addToMultiMap(prefs, name, value);
            } else {
                Set<String> set = name2uniqueAttrValues.get(name);
                if (set.add(valueLowerCase)) {
                    StringUtil.addToMultiMap(prefs, name, value);
                }
            }
        } else {
            StringUtil.addToMultiMap(prefs, name, value);
        }
    }
    if (prefs.containsKey(Provisioning.A_zimbraPrefMailForwardingAddress)) {
        if (!account.getBooleanAttr(Provisioning.A_zimbraFeatureMailForwardingEnabled, false))
            throw ServiceException.PERM_DENIED("forwarding not enabled");
    }
    // call modifyAttrs and pass true to checkImmutable
    Provisioning.getInstance().modifyAttrs(account, prefs, true, zsc.getAuthToken());
    Element response = zsc.createElement(AccountConstants.MODIFY_PREFS_RESPONSE);
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) Set(java.util.Set) HashSet(java.util.HashSet) KeyValuePair(com.zimbra.common.soap.Element.KeyValuePair) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) AttributeInfo(com.zimbra.cs.account.AttributeInfo) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) HashSet(java.util.HashSet)

Aggregations

AttributeInfo (com.zimbra.cs.account.AttributeInfo)9 HashMap (java.util.HashMap)5 AttributeManager (com.zimbra.cs.account.AttributeManager)4 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 ServiceException (com.zimbra.common.service.ServiceException)2 Element (com.zimbra.common.soap.Element)2 Account (com.zimbra.cs.account.Account)2 KeyValuePair (com.zimbra.common.soap.Element.KeyValuePair)1 Version (com.zimbra.common.util.Version)1 AccountServiceException (com.zimbra.cs.account.AccountServiceException)1 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)1 AttributeClass (com.zimbra.cs.account.AttributeClass)1 AttributeFlag (com.zimbra.cs.account.AttributeFlag)1 CalendarResource (com.zimbra.cs.account.CalendarResource)1 Cos (com.zimbra.cs.account.Cos)1 GuestAccount (com.zimbra.cs.account.GuestAccount)1 CallbackContext (com.zimbra.cs.account.callback.CallbackContext)1 LdapAccount (com.zimbra.cs.account.ldap.entry.LdapAccount)1 LdapCalendarResource (com.zimbra.cs.account.ldap.entry.LdapCalendarResource)1