setattr and addattr can now be used both to set all values of
ANY attribute. the last setattr always resets the attribute to
the specified value and all addattr append to it.

Examples:
user-mod testuser --setattr=title=msc
  title: msc
user-mod testuser --setattr=title=msb
  title: msb
user-mod testuser --addattr=title=msc
  title: msb, msc
user-mod testuser --setattr=title=
  title:
user-mod testuser --setattr=title=msc --addattr=msb
  title: msc, msb
user-mod testuser --setattr=title=ing --addattr=bc
  title: ing, bc
user-mod testuser --setattr=title=doc
  title: doc

It's not very user friendly, but it's going to be used very very
rarely in special conditions in the CLI and we can use it to save
lots of JSON-RPC roundtrips in the webUI.

Pavel
From 5467a93dc7e4e24e82ba3559b333ac5e55814127 Mon Sep 17 00:00:00 2001
From: Pavel Zuna <pz...@redhat.com>
Date: Mon, 9 Aug 2010 19:43:00 -0400
Subject: [PATCH 2/4] Change the behaviour of addattr/setattr parameters.

setattr and addattr can now be used both to set all values of
ANY attribute. the last setattr always resets the attribute to
the specified value and all addattr append to it.

Examples:
user-mod testuser --setattr=title=msc
  title: msc
user-mod testuser --setattr=title=msb
  title: msb
user-mod testuser --addattr=title=msc
  title: msb, msc
user-mod testuser --setattr=title=
  title:
user-mod testuser --setattr=title=msc --addattr=msb
  title: msc, msb
user-mod testuser --setattr=title=ing --addattr=bc
  title: ing, bc
user-mod testuser --setattr=title=doc
  title: doc

It's not very user friendly, but it's going to be used very very
rarely in special conditions in the CLI and we can use it to save
lots of JSON-RPC roundtrips in the webUI.
---
 ipalib/frontend.py         |   15 +++++++----
 ipalib/plugins/baseldap.py |   58 ++++++++++++++++++++++----------------------
 2 files changed, 38 insertions(+), 35 deletions(-)

diff --git a/ipalib/frontend.py b/ipalib/frontend.py
index d320f02..950fa7b 100644
--- a/ipalib/frontend.py
+++ b/ipalib/frontend.py
@@ -519,11 +519,10 @@ class Command(HasParam):
             if len(value) == 0:
                 # None means "delete this attribute"
                 value = None
-            if attr not in self.params:
-                if append and attr in newdict:
-                    newdict[attr].append(value)
-                else:
-                    newdict[attr] = [value]
+            if append and attr in newdict:
+                newdict[attr].append(value)
+            else:
+                newdict[attr] = [value]
         return newdict
 
     def __attributes_2_entry(self, kw):
@@ -540,7 +539,11 @@ class Command(HasParam):
             adddict = self.__convert_2_dict(kw['setattr'], append=False)
 
         if kw.get('addattr'):
-            adddict.update(self.__convert_2_dict(kw['addattr']))
+            for (k, v) in self.__convert_2_dict(kw['addattr']).iteritems():
+                if k in adddict:
+                    adddict[k] += v
+                else:
+                    adddict[k] = v
 
         for name in adddict:
             value = adddict[name]
diff --git a/ipalib/plugins/baseldap.py b/ipalib/plugins/baseldap.py
index 52f32e3..c995a61 100644
--- a/ipalib/plugins/baseldap.py
+++ b/ipalib/plugins/baseldap.py
@@ -415,6 +415,35 @@ class LDAPUpdate(LDAPQuery, crud.Update):
 
         entry_attrs = self.args_options_2_entry(**options)
 
+        """
+        Some special handling is needed because we need to update the
+        values here rather than letting ldap.update_entry() do the work. We
+        have to do the work of adding new values to an existing attribute
+        because if we pass just what is addded only the new values get
+        set.
+        """
+        if 'addattr' in options:
+            setset = set(get_attributes(options.get('setattr', [])))
+            addset = set(get_attributes(options.get('addattr', [])))
+            difflist = list(addset.difference(setset))
+            if difflist:
+                try:
+                    (dn, old_entry) = ldap.get_entry(
+                        dn, difflist, normalize=self.obj.normalize_dn
+                    )
+                except errors.ExecutionError, e:
+                    try:
+                        (dn, old_entry) = self._call_exc_callbacks(
+                            keys, options, e, ldap.get_entry, dn, attrs_list,
+                            normalize=self.obj.normalize_dn
+                        )
+                    except errors.NotFound:
+                        self.obj.handle_not_found(*keys)
+                for a in old_entry:
+                    if not isinstance(entry_attrs[a], (list, tuple)):
+                        entry_attrs[a] = [entry_attrs[a]]
+                    entry_attrs[a] += old_entry[a]
+
         if options.get('all', False):
             attrs_list = ['*']
         else:
@@ -432,35 +461,6 @@ class LDAPUpdate(LDAPQuery, crud.Update):
                     self, ldap, dn, entry_attrs, attrs_list, *keys, **options
                 )
 
-        """
-        Some special handling is needed because we need to update the
-        values here rather than letting ldap.update_entry() do the work. We
-        have to do the work of adding new values to an existing attribute
-        because if we pass just what is addded only the new values get
-        set.
-        """
-        if 'addattr' in options:
-            try:
-                (dn, old_entry) = ldap.get_entry(
-                    dn, attrs_list, normalize=self.obj.normalize_dn
-                )
-            except errors.ExecutionError, e:
-                try:
-                    (dn, old_entry) = self._call_exc_callbacks(
-                        keys, options, e, ldap.get_entry, dn, attrs_list,
-                        normalize=self.obj.normalize_dn
-                    )
-                except errors.NotFound:
-                    self.obj.handle_not_found(*keys)
-            attrlist = get_attributes(options['addattr'])
-            for attr in attrlist:
-                if attr in old_entry:
-                    if type(entry_attrs[attr]) in (tuple,list):
-                        entry_attrs[attr] = old_entry[attr] + entry_attrs[attr]
-                    else:
-                        old_entry[attr].append(entry_attrs[attr])
-                        entry_attrs[attr] = old_entry[attr]
-
         try:
             ldap.update_entry(dn, entry_attrs, normalize=self.obj.normalize_dn)
         except errors.ExecutionError, e:
-- 
1.7.1.1

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to