Skip to content

Conversation

@mogorman
Copy link

@mogorman mogorman commented Jan 6, 2024

Breaking change

Proposed change

Current implementation does not ever match the users that exist in the system. my changes fix that. It also changes new user creation. I mirrored creating a user the way to the frontend does so that you get a person associated with the new user.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mogorman

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link

home-assistant bot commented Jan 6, 2024

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@MartinHjelmare MartinHjelmare changed the title fix for command_line authentication provider Fix for command_line authentication provider Jan 7, 2024
@SuperSandro2000
Copy link

With the following patch existing users are created as a person, too.

--- a/homeassistant/auth/providers/command_line.py	2024-02-04 01:41:34.460181490 +0100
+++ b/homeassistant/auth/providers/command_line.py	2024-02-04 01:46:55.952650748 +0100
@@ -118,6 +118,13 @@
         username = flow_result["username"].strip().casefold()

         users = await self.store.async_get_users()
+        hass = async_get_hass()
+        meta = self._user_meta.get(flow_result["username"], {})
+
+        pretty_name = meta.get("fullname")
+        if not pretty_name:
+            pretty_name = flow_result["username"]
+
         for user in users:
             if user.name and user.name.strip().casefold() != username:
                 continue
@@ -127,28 +134,34 @@

             for credential in await self.async_credentials():
                 if credential.data["username"] and credential.data["username"].strip().casefold() == username:
+                    coll: person.PersonStorageCollection = hass.data[person.DOMAIN][1]
+                    found = False
+                    for pers in coll.async_items():
+                        if pers.get(person.ATTR_USER_ID) == user.id:
+                            found = True
+                            break
+
+                    if "person" in hass.config.components and not found:
+                        await person.async_create_person(hass, pretty_name, user_id=user.id)
+
                     return credential

             cred = self.async_create_credentials({"username": username})
             await self.store.async_link_user(user, cred)
             return cred

-        hass = async_get_hass()
-        meta = self._user_meta.get(flow_result["username"], {})
-
         provider = _async_get_hass_provider(hass)
         await provider.async_initialize()

         user = await hass.auth.async_create_user(flow_result["username"], group_ids=[meta.get("group")])
         cred = await provider.async_get_or_create_credentials({"username": flow_result["username"]})

-        pretty_name = meta.get("fullname")
-        if not pretty_name:
-            pretty_name = flow_result["username"]
         await provider.data.async_save()
         await hass.auth.async_link_user(user, cred)
+
         if "person" in hass.config.components:
             await person.async_create_person(hass, pretty_name, user_id=user.id)
+
         # Create new credentials.
         return cred

def _async_get_hass_provider(hass):
"""Get the Home Assistant auth provider."""
for prv in hass.auth.auth_providers:
if prv.type == "homeassistant":
Copy link

@SuperSandro2000 SuperSandro2000 Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if prv.type == "homeassistant":
if prv.type == "command_line":

apparently I was to fast and this does not actaully work...

@Losenmann
Copy link

Losenmann commented Mar 24, 2024

I suggest adding additional functionality, such as automatically creating a user photo. I have my own LDAP script in Python that authenticates a user and uploads their photo to the ./image/ directory. It would be convenient to create a user immediately with a photo.

provide more accurate error

Co-authored-by: Sandro <sandro.jaeckel@gmail.com>
@Daniel-dev22

This comment was marked as off-topic.

@github-actions
Copy link

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

@github-actions github-actions bot added the stale label Jun 29, 2024
@SuperSandro2000

This comment was marked as off-topic.

@github-actions github-actions bot removed the stale label Jul 1, 2024
@frenck
Copy link
Member

frenck commented Jul 7, 2024

Because there hasn't been any activity on this PR for quite some time now, I've decided to close it for being stale.

Feel free to re-open this PR when you are ready to pick up work on it again 👍

../Frenck

@frenck frenck closed this Jul 7, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Command line authentication providers not working meta

5 participants