On 02/07/2012 10:31 PM, Rob Crittenden wrote:
Petr Viktorin wrote:
On 02/07/2012 01:52 PM, Petr Viktorin wrote:
Honor the default home directory base when creating a new user. Test
included. I also cleaned up the way home directory was created.

This patch removes the default from the --homedirectory option, letting
the server fill it in pre_callback. If I'm reading this correctly,
default_from and create_default run on the client-side, so they can't
get to the config without round-tripping to the server.

https://fedorahosted.org/freeipa/ticket/2332

Also, I've cleaned up the home directory generation to use
posixpath.join instead of '%s/%s' and ad-hoc cleanup. This should be
more robust. (It will also behave differently if the username starts
with '/' or maybe similar cases of the user asking for trouble.)

A question: Do we want to use posixpath here, or os.path? Put another
way, should the home directories separated by '\' if the server runs on
Windows?


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

Martin told me I need to make two changes: remove autofill along with
default_from, and since I have touched the API, update API.txt.

Attaching the updated patch.

This works well. I noticed that the default shell has the same problem.

I wonder if we should roll that similar change in or open a separate
ticket.

rob

Good point.
I went ahead and rolled it in, since I noticed I also didn't update API.txt correctly. Attaching updated patch.


--
PetrĀ³
>From 88ce5596c7fe138997e9af9370c34a8ac69428e8 Mon Sep 17 00:00:00 2001
From: Petr Viktorin <pvikt...@redhat.com>
Date: Tue, 7 Feb 2012 07:13:52 -0500
Subject: [PATCH] Honor default home directory and login shell in user_add

The homedirectory argument had a default_from '/home/<name>', ignoring
the ipahomesrootdir config setting. This patch removes that default,
and adds a test case for ipahomesrootdir.

https://fedorahosted.org/freeipa/ticket/2332

The login shell had the same problem. Again this patch removes the
client-side default and adds a test.

Building the home directory from the default is changed to use
posixpath.join instead of string formatting and ad-hoc cleanup,
and to use '/home' instead of failing when the ipahomesrootdir
setting is not present for some reason.
---
 API.txt                               |    8 +-
 ipalib/plugins/user.py                |   15 ++--
 tests/test_xmlrpc/test_user_plugin.py |  130 +++++++++++++++++++++++++++++++++
 3 files changed, 140 insertions(+), 13 deletions(-)

diff --git a/API.txt b/API.txt
index d031039a6a6cf0a8dadb0422851fc84909b6bba2..8c8494f8a8d3332fe38b2dc56a7079fbfeeda1a4 100644
--- a/API.txt
+++ b/API.txt
@@ -3093,9 +3093,9 @@ option: Str('sn', attribute=True, cli_name='last', multivalue=False, required=Tr
 option: Str('cn', attribute=True, autofill=True, cli_name='cn', multivalue=False, required=True)
 option: Str('displayname', attribute=True, autofill=True, cli_name='displayname', multivalue=False, required=False)
 option: Str('initials', attribute=True, autofill=True, cli_name='initials', multivalue=False, required=False)
-option: Str('homedirectory', attribute=True, autofill=True, cli_name='homedir', multivalue=False, required=True)
+option: Str('homedirectory', attribute=True, cli_name='homedir', multivalue=False, required=False)
 option: Str('gecos', attribute=True, autofill=True, cli_name='gecos', multivalue=False, required=False)
-option: Str('loginshell', attribute=True, cli_name='shell', default=u'/bin/sh', multivalue=False, required=False)
+option: Str('loginshell', attribute=True, cli_name='shell', multivalue=False, required=False)
 option: Str('krbprincipalname', attribute=True, autofill=True, cli_name='principal', multivalue=False, required=False)
 option: Str('mail', attribute=True, cli_name='email', multivalue=True, required=False)
 option: Password('userpassword', attribute=True, cli_name='password', exclude='webui', multivalue=False, required=False)
@@ -3153,7 +3153,7 @@ option: Str('displayname', attribute=True, autofill=False, cli_name='displayname
 option: Str('initials', attribute=True, autofill=False, cli_name='initials', multivalue=False, query=True, required=False)
 option: Str('homedirectory', attribute=True, autofill=False, cli_name='homedir', multivalue=False, query=True, required=False)
 option: Str('gecos', attribute=True, autofill=False, cli_name='gecos', multivalue=False, query=True, required=False)
-option: Str('loginshell', attribute=True, autofill=False, cli_name='shell', default=u'/bin/sh', multivalue=False, query=True, required=False)
+option: Str('loginshell', attribute=True, autofill=False, cli_name='shell', multivalue=False, query=True, required=False)
 option: Str('krbprincipalname', attribute=True, autofill=False, cli_name='principal', multivalue=False, query=True, required=False)
 option: Str('mail', attribute=True, autofill=False, cli_name='email', multivalue=True, query=True, required=False)
 option: Password('userpassword', attribute=True, autofill=False, cli_name='password', exclude='webui', multivalue=False, query=True, required=False)
@@ -3202,7 +3202,7 @@ option: Str('displayname', attribute=True, autofill=False, cli_name='displayname
 option: Str('initials', attribute=True, autofill=False, cli_name='initials', multivalue=False, required=False)
 option: Str('homedirectory', attribute=True, autofill=False, cli_name='homedir', multivalue=False, required=False)
 option: Str('gecos', attribute=True, autofill=False, cli_name='gecos', multivalue=False, required=False)
-option: Str('loginshell', attribute=True, autofill=False, cli_name='shell', default=u'/bin/sh', multivalue=False, required=False)
+option: Str('loginshell', attribute=True, autofill=False, cli_name='shell', multivalue=False, required=False)
 option: Str('mail', attribute=True, autofill=False, cli_name='email', multivalue=True, required=False)
 option: Password('userpassword', attribute=True, autofill=False, cli_name='password', exclude='webui', multivalue=False, required=False)
 option: Flag('random', attribute=False, autofill=True, cli_name='random', default=False, multivalue=False, required=False)
diff --git a/ipalib/plugins/user.py b/ipalib/plugins/user.py
index 70a111dd34c66ff22f906834a1348c65dbfd1bd5..8c4cc49a0475d3106aa909ff6b4f98a8a73a50de 100644
--- a/ipalib/plugins/user.py
+++ b/ipalib/plugins/user.py
@@ -27,6 +27,7 @@ import copy
 from ipalib import _, ngettext
 from ipapython.ipautil import ipa_generate_password
 import string
+import posixpath
 
 __doc__ = _("""
 Users
@@ -207,11 +208,9 @@ class user(LDAPObject):
             default_from=lambda givenname, sn: '%c%c' % (givenname[0], sn[0]),
             autofill=True,
         ),
-        Str('homedirectory',
+        Str('homedirectory?',
             cli_name='homedir',
             label=_('Home directory'),
-            default_from=lambda uid: '/home/%s' % uid,
-            autofill=True,
         ),
         Str('gecos?',
             label=_('GECOS field'),
@@ -221,7 +220,6 @@ class user(LDAPObject):
         Str('loginshell?',
             cli_name='shell',
             label=_('Login shell'),
-            default=u'/bin/sh',
         ),
         Str('krbprincipalname?', validate_principal,
             cli_name='principal',
@@ -413,17 +411,16 @@ class user_add(LDAPCreate):
                         len = int(config.get('ipamaxusernamelength')[0])
                     )
                 )
-        entry_attrs.setdefault('loginshell', config.get('ipadefaultloginshell'))
+        default_shell = config.get('ipadefaultloginshell', ['/bin/sh'])[0]
+        entry_attrs.setdefault('loginshell', default_shell)
         # hack so we can request separate first and last name in CLI
         full_name = '%s %s' % (entry_attrs['givenname'], entry_attrs['sn'])
         entry_attrs.setdefault('cn', full_name)
         if 'homedirectory' not in entry_attrs:
             # get home's root directory from config
-            homes_root = config.get('ipahomesrootdir', '/home')[0]
+            homes_root = config.get('ipahomesrootdir', ['/home'])[0]
             # build user's home directory based on his uid
-            home_dir = '%s/%s' % (homes_root, keys[-1])
-            home_dir = home_dir.replace('//', '/').rstrip('/')
-            entry_attrs['homedirectory'] = home_dir
+            entry_attrs['homedirectory'] = posixpath.join(homes_root, keys[-1])
         entry_attrs.setdefault('krbpwdpolicyreference', 'cn=global_policy,cn=%s,cn=kerberos,%s' % (api.env.realm, api.env.basedn))
         entry_attrs.setdefault('krbprincipalname', '%s@%s' % (entry_attrs['uid'], api.env.realm))
 
diff --git a/tests/test_xmlrpc/test_user_plugin.py b/tests/test_xmlrpc/test_user_plugin.py
index 0370ec74d294f15b742aadacbdbf6fa296ebb718..c8984c51b306a52a0400b64c41834a45c43bc6b6 100644
--- a/tests/test_xmlrpc/test_user_plugin.py
+++ b/tests/test_xmlrpc/test_user_plugin.py
@@ -909,5 +909,135 @@ class test_user(Declarative):
             expected=errors.MalformedUserPrincipal(principal='%s@b...@notfound.org' % user1),
         ),
 
+        dict(
+            desc='Delete %r' % user1,
+            command=('user_del', [user1], {}),
+            expected=dict(
+                result=dict(failed=u''),
+                summary=u'Deleted user "tuser1"',
+                value=user1,
+            ),
+        ),
+
+        dict(
+            desc='Change default home directory',
+            command=(
+                'config_mod', [], dict(ipahomesrootdir=u'/other-home'),
+            ),
+            expected=lambda x: True,
+        ),
+
+        dict(
+            desc='Create user %r with different default home directory' % user1,
+            command=(
+                'user_add', [user1], dict(givenname=u'Test', sn=u'User1')
+            ),
+            expected=dict(
+                value=user1,
+                summary=u'Added user "tuser1"',
+                result=dict(
+                    gecos=[u'Test User1'],
+                    givenname=[u'Test'],
+                    homedirectory=[u'/other-home/tuser1'],
+                    krbprincipalname=[u'tuser1@' + api.env.realm],
+                    loginshell=[u'/bin/sh'],
+                    objectclass=objectclasses.user,
+                    sn=[u'User1'],
+                    uid=[user1],
+                    uidnumber=[fuzzy_digits],
+                    gidnumber=[fuzzy_digits],
+                    displayname=[u'Test User1'],
+                    cn=[u'Test User1'],
+                    initials=[u'TU'],
+                    ipauniqueid=[fuzzy_uuid],
+                    krbpwdpolicyreference=lambda x: [DN(i) for i in x] == \
+                        [DN(('cn','global_policy'),('cn',api.env.realm),
+                            ('cn','kerberos'),api.env.basedn)],
+                    mepmanagedentry=lambda x: [DN(i) for i in x] == \
+                        [DN(('cn',user1),('cn','groups'),('cn','accounts'),
+                            api.env.basedn)],
+                    memberof_group=[u'ipausers'],
+                    has_keytab=False,
+                    has_password=False,
+                    dn=lambda x: DN(x) == \
+                        DN(('uid','tuser1'),('cn','users'),('cn','accounts'),
+                           api.env.basedn),
+                ),
+            ),
+        ),
+
+
+        dict(
+            desc='Reset default home directory',
+            command=(
+                'config_mod', [], dict(ipahomesrootdir=u'/home'),
+            ),
+            expected=lambda x: True,
+        ),
+
+        dict(
+            desc='Delete %r' % user1,
+            command=('user_del', [user1], {}),
+            expected=dict(
+                result=dict(failed=u''),
+                summary=u'Deleted user "%s"' % user1,
+                value=user1,
+            ),
+        ),
+
+        dict(
+            desc='Change default login shell',
+            command=(
+                'config_mod', [], dict(ipadefaultloginshell=u'/usr/bin/ipython'),
+            ),
+            expected=lambda x: True,
+        ),
+
+        dict(
+            desc='Create user %r with different default login shell' % user1,
+            command=(
+                'user_add', [user1], dict(givenname=u'Test', sn=u'User1')
+            ),
+            expected=dict(
+                value=user1,
+                summary=u'Added user "tuser1"',
+                result=dict(
+                    gecos=[u'Test User1'],
+                    givenname=[u'Test'],
+                    homedirectory=[u'/home/tuser1'],
+                    krbprincipalname=[u'tuser1@' + api.env.realm],
+                    loginshell=[u'/usr/bin/ipython'],
+                    objectclass=objectclasses.user,
+                    sn=[u'User1'],
+                    uid=[user1],
+                    uidnumber=[fuzzy_digits],
+                    gidnumber=[fuzzy_digits],
+                    displayname=[u'Test User1'],
+                    cn=[u'Test User1'],
+                    initials=[u'TU'],
+                    ipauniqueid=[fuzzy_uuid],
+                    krbpwdpolicyreference=lambda x: [DN(i) for i in x] == \
+                        [DN(('cn','global_policy'),('cn',api.env.realm),
+                            ('cn','kerberos'),api.env.basedn)],
+                    mepmanagedentry=lambda x: [DN(i) for i in x] == \
+                        [DN(('cn',user1),('cn','groups'),('cn','accounts'),
+                            api.env.basedn)],
+                    memberof_group=[u'ipausers'],
+                    has_keytab=False,
+                    has_password=False,
+                    dn=lambda x: DN(x) == \
+                        DN(('uid','tuser1'),('cn','users'),('cn','accounts'),
+                           api.env.basedn),
+                ),
+            ),
+        ),
+
+        dict(
+            desc='Reset default login shell',
+            command=(
+                'config_mod', [], dict(ipadefaultloginshell=u'/bin/sh'),
+            ),
+            expected=lambda x: True,
+        ),
 
     ]
-- 
1.7.7.6

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

Reply via email to