Merge firstname and lastname info

This commit is contained in:
Alexandre Aubin 2022-10-09 17:01:57 +02:00
parent a355f48580
commit 5347c6afeb
4 changed files with 74 additions and 68 deletions

View file

@ -73,19 +73,28 @@ user:
pattern: &pattern_username pattern: &pattern_username
- !!str ^[a-z0-9_]+$ - !!str ^[a-z0-9_]+$
- "pattern_username" - "pattern_username"
-F:
full: --fullname
help: The full name of the user. For example 'Camille Dupont'
extra:
ask: ask_fullname
required: False
pattern: &pattern_fullname
- !!str ^([^\W\d_]{1,30}[ ,.'-]{0,3})+$
- "pattern_fullname"
-f: -f:
full: --firstname full: --firstname
help: Deprecated. Use --fullname instead.
extra: extra:
ask: ask_firstname required: False
required: True
pattern: &pattern_firstname pattern: &pattern_firstname
- !!str ^([^\W\d_]{1,30}[ ,.'-]{0,3})+$ - !!str ^([^\W\d_]{1,30}[ ,.'-]{0,3})+$
- "pattern_firstname" - "pattern_firstname"
-l: -l:
full: --lastname full: --lastname
help: Deprecated. Use --fullname instead.
extra: extra:
ask: ask_lastname required: False
required: True
pattern: &pattern_lastname pattern: &pattern_lastname
- !!str ^([^\W\d_]{1,30}[ ,.'-]{0,3})+$ - !!str ^([^\W\d_]{1,30}[ ,.'-]{0,3})+$
- "pattern_lastname" - "pattern_lastname"
@ -136,12 +145,19 @@ user:
arguments: arguments:
username: username:
help: Username to update help: Username to update
-F:
full: --fullname
help: The full name of the user. For example 'Camille Dupont'
extra:
pattern: *pattern_fullname
-f: -f:
full: --firstname full: --firstname
help: Deprecated. Use --fullname instead.
extra: extra:
pattern: *pattern_firstname pattern: *pattern_firstname
-l: -l:
full: --lastname full: --lastname
help: Deprecated. Use --fullname instead.
extra: extra:
pattern: *pattern_lastname pattern: *pattern_lastname
-m: -m:
@ -1520,25 +1536,18 @@ tools:
required: True required: True
-u: -u:
full: --username full: --username
help: Username for the first (admin) user help: Username for the first (admin) user. For example 'camille'
extra: extra:
ask: ask_username ask: ask_admin_username
pattern: *pattern_username pattern: *pattern_username
required: True required: True
-f: -F:
full: --firstname full: --fullname
help: Firstname for the first (admin) user help: The full name for the first (admin) user. For example 'Camille Dupont'
extra: extra:
ask: ask_firstname ask: ask_admin_fullname
required: True required: True
pattern: *pattern_firstname pattern: *pattern_fullname
-l:
full: --lastname
help: Lastname for the first (admin) user
extra:
ask: ask_lastname
required: True
pattern: *pattern_lastname
-p: -p:
full: --password full: --password
help: YunoHost admin password help: YunoHost admin password

View file

@ -38,9 +38,9 @@ def setup_function(function):
global maindomain global maindomain
maindomain = _get_maindomain() maindomain = _get_maindomain()
user_create("alice", "Alice", "White", maindomain, "test123Ynh", admin=True) user_create("alice", maindomain, "test123Ynh", admin=True, fullname="Alice White")
user_create("bob", "Bob", "Snow", maindomain, "test123Ynh") user_create("bob", maindomain, "test123Ynh", fullname="Bob Snow")
user_create("jack", "Jack", "Black", maindomain, "test123Ynh") user_create("jack", maindomain, "test123Ynh", fullname="Jack Black")
user_group_create("dev") user_group_create("dev")
user_group_create("apps") user_group_create("apps")
@ -94,7 +94,7 @@ def test_list_groups():
def test_create_user(mocker): def test_create_user(mocker):
with message(mocker, "user_created"): with message(mocker, "user_created"):
user_create("albert", "Albert", "Good", maindomain, "test123Ynh") user_create("albert", maindomain, "test123Ynh", fullname="Albert Good")
group_res = user_group_list()["groups"] group_res = user_group_list()["groups"]
assert "albert" in user_list()["users"] assert "albert" in user_list()["users"]
@ -211,17 +211,17 @@ def test_del_group(mocker):
def test_create_user_with_password_too_simple(mocker): def test_create_user_with_password_too_simple(mocker):
with raiseYunohostError(mocker, "password_listed"): with raiseYunohostError(mocker, "password_listed"):
user_create("other", "Alice", "White", maindomain, "12") user_create("other", maindomain, "12", fullname="Alice White")
def test_create_user_already_exists(mocker): def test_create_user_already_exists(mocker):
with raiseYunohostError(mocker, "user_already_exists"): with raiseYunohostError(mocker, "user_already_exists"):
user_create("alice", "Alice", "White", maindomain, "test123Ynh") user_create("alice", maindomain, "test123Ynh", fullname="Alice White")
def test_create_user_with_domain_that_doesnt_exists(mocker): def test_create_user_with_domain_that_doesnt_exists(mocker):
with raiseYunohostError(mocker, "domain_unknown"): with raiseYunohostError(mocker, "domain_unknown"):
user_create("alice", "Alice", "White", "doesnt.exists", "test123Ynh") user_create("alice", "doesnt.exists", "test123Ynh", fullname="Alice White")
def test_update_user_with_mail_address_already_taken(mocker): def test_update_user_with_mail_address_already_taken(mocker):
@ -255,7 +255,7 @@ def test_del_group_all_users(mocker):
with raiseYunohostError(mocker, "group_cannot_be_deleted"): with raiseYunohostError(mocker, "group_cannot_be_deleted"):
user_group_delete("all_users") user_group_delete("all_users")
/
def test_del_group_that_does_not_exist(mocker): def test_del_group_that_does_not_exist(mocker):
with raiseYunohostError(mocker, "group_unknown"): with raiseYunohostError(mocker, "group_unknown"):
user_group_delete("doesnt_exist") user_group_delete("doesnt_exist")
@ -271,8 +271,13 @@ def test_update_user(mocker):
user_update("alice", firstname="NewName", lastname="NewLast") user_update("alice", firstname="NewName", lastname="NewLast")
info = user_info("alice") info = user_info("alice")
assert info["firstname"] == "NewName" assert info["fullname"] == "NewName NewLast"
assert info["lastname"] == "NewLast"
with message(mocker, "user_updated"):
user_update("alice", fullname="New2Name New2Last")
info = user_info("alice")
assert info["fullname"] == "New2Name New2Last"
def test_update_group_add_user(mocker): def test_update_group_add_user(mocker):

View file

@ -146,8 +146,7 @@ def tools_postinstall(
operation_logger, operation_logger,
domain, domain,
username, username,
firstname, fullname,
lastname,
password, password,
ignore_dyndns=False, ignore_dyndns=False,
force_diskspace=False, force_diskspace=False,
@ -226,7 +225,7 @@ def tools_postinstall(
domain_add(domain, dyndns) domain_add(domain, dyndns)
domain_main_domain(domain) domain_main_domain(domain)
user_create(username, firstname, lastname, domain, password, admin=True) user_create(username, domain, password, admin=True, fullname=fullname)
# Update LDAP admin and create home dir # Update LDAP admin and create home dir
tools_rootpw(password) tools_rootpw(password)

View file

@ -134,15 +134,29 @@ def user_list(fields=None):
def user_create( def user_create(
operation_logger, operation_logger,
username, username,
firstname,
lastname,
domain, domain,
password, password,
fullname=None,
firstname=None,
lastname=None,
mailbox_quota="0", mailbox_quota="0",
admin=False, admin=False,
from_import=False, from_import=False,
): ):
if firstname or lastname:
logger.warning("Options --firstname / --lastname of 'yunohost user create' are deprecated. We recommend using --fullname instead.")
if not fullname.strip():
if not firstname.strip():
raise YunohostValidationError("You should specify the fullname of the user using option -F")
lastname = lastname or " " # Stupid hack because LDAP requires the sn/lastname attr, but it accepts a single whitespace...
fullname = f"{firstname} {lastname}".strip()
else:
fullname = fullname.strip()
firstname = fullname.split()[0]
lastname = ' '.join(fullname.split()[1:]) or " " # Stupid hack because LDAP requires the sn/lastname attr, but it accepts a single whitespace...
from yunohost.domain import domain_list, _get_maindomain, _assert_domain_exists from yunohost.domain import domain_list, _get_maindomain, _assert_domain_exists
from yunohost.hook import hook_callback from yunohost.hook import hook_callback
from yunohost.utils.password import ( from yunohost.utils.password import (
@ -219,9 +233,6 @@ def user_create(
uid = str(random.randint(1001, 65000)) uid = str(random.randint(1001, 65000))
uid_guid_found = uid not in all_uid and uid not in all_gid uid_guid_found = uid not in all_uid and uid not in all_gid
# Adapt values for LDAP
fullname = f"{firstname} {lastname}"
attr_dict = { attr_dict = {
"objectClass": [ "objectClass": [
"mailAccount", "mailAccount",
@ -292,14 +303,7 @@ def user_create(
@is_unit_operation([("username", "user")]) @is_unit_operation([("username", "user")])
def user_delete(operation_logger, username, purge=False, from_import=False): def user_delete(operation_logger, username, purge=False, from_import=False):
"""
Delete user
Keyword argument:
username -- Username to delete
purge
"""
from yunohost.hook import hook_callback from yunohost.hook import hook_callback
from yunohost.utils.ldap import _get_ldap_interface from yunohost.utils.ldap import _get_ldap_interface
@ -357,22 +361,14 @@ def user_update(
remove_mailalias=None, remove_mailalias=None,
mailbox_quota=None, mailbox_quota=None,
from_import=False, from_import=False,
fullname=None,
): ):
"""
Update user informations
Keyword argument: if fullname.strip():
lastname fullname = fullname.strip()
mail firstname = fullname.split()[0]
firstname lastname = ' '.join(fullname.split()[1:]) or " " # Stupid hack because LDAP requires the sn/lastname attr, but it accepts a single whitespace...
add_mailalias -- Mail aliases to add
remove_mailforward -- Mailforward addresses to remove
username -- Username of user to update
add_mailforward -- Mailforward addresses to add
change_password -- New password to set
remove_mailalias -- Mail aliases to remove
"""
from yunohost.domain import domain_list, _get_maindomain from yunohost.domain import domain_list, _get_maindomain
from yunohost.app import app_ssowatconf from yunohost.app import app_ssowatconf
from yunohost.utils.password import ( from yunohost.utils.password import (
@ -402,20 +398,20 @@ def user_update(
if firstname: if firstname:
new_attr_dict["givenName"] = [firstname] # TODO: Validate new_attr_dict["givenName"] = [firstname] # TODO: Validate
new_attr_dict["cn"] = new_attr_dict["displayName"] = [ new_attr_dict["cn"] = new_attr_dict["displayName"] = [
firstname + " " + user["sn"][0] (firstname + " " + user["sn"][0]).strip()
] ]
env_dict["YNH_USER_FIRSTNAME"] = firstname env_dict["YNH_USER_FIRSTNAME"] = firstname
if lastname: if lastname:
new_attr_dict["sn"] = [lastname] # TODO: Validate new_attr_dict["sn"] = [lastname] # TODO: Validate
new_attr_dict["cn"] = new_attr_dict["displayName"] = [ new_attr_dict["cn"] = new_attr_dict["displayName"] = [
user["givenName"][0] + " " + lastname (user["givenName"][0] + " " + lastname).strip()
] ]
env_dict["YNH_USER_LASTNAME"] = lastname env_dict["YNH_USER_LASTNAME"] = lastname
if lastname and firstname: if lastname and firstname:
new_attr_dict["cn"] = new_attr_dict["displayName"] = [ new_attr_dict["cn"] = new_attr_dict["displayName"] = [
firstname + " " + lastname (firstname + " " + lastname).strip()
] ]
# change_password is None if user_update is not called to change the password # change_password is None if user_update is not called to change the password
@ -547,7 +543,7 @@ def user_info(username):
ldap = _get_ldap_interface() ldap = _get_ldap_interface()
user_attrs = ["cn", "mail", "uid", "maildrop", "givenName", "sn", "mailuserquota"] user_attrs = ["cn", "mail", "uid", "maildrop", "mailuserquota"]
if len(username.split("@")) == 2: if len(username.split("@")) == 2:
filter = "mail=" + username filter = "mail=" + username
@ -564,8 +560,6 @@ def user_info(username):
result_dict = { result_dict = {
"username": user["uid"][0], "username": user["uid"][0],
"fullname": user["cn"][0], "fullname": user["cn"][0],
"firstname": user["givenName"][0],
"lastname": user["sn"][0],
"mail": user["mail"][0], "mail": user["mail"][0],
"mail-aliases": [], "mail-aliases": [],
"mail-forward": [], "mail-forward": [],
@ -859,10 +853,9 @@ def user_import(operation_logger, csvfile, update=False, delete=False):
user_update( user_update(
new_infos["username"], new_infos["username"],
new_infos["firstname"], firstname=new_infos["firstname"],
new_infos["lastname"], lastname=new_infos["lastname"],
new_infos["mail"], password=new_infos["password"],
new_infos["password"],
mailbox_quota=new_infos["mailbox-quota"], mailbox_quota=new_infos["mailbox-quota"],
mail=new_infos["mail"], mail=new_infos["mail"],
add_mailalias=new_infos["mail-alias"], add_mailalias=new_infos["mail-alias"],
@ -902,12 +895,12 @@ def user_import(operation_logger, csvfile, update=False, delete=False):
try: try:
user_create( user_create(
user["username"], user["username"],
user["firstname"],
user["lastname"],
user["domain"], user["domain"],
user["password"], user["password"],
user["mailbox-quota"], user["mailbox-quota"],
from_import=True, from_import=True,
firstname=user["firstname"],
lastname=user["lastname"],
) )
update(user) update(user)
result["created"] += 1 result["created"] += 1