core icon indicating copy to clipboard operation
core copied to clipboard

Use proper IDs as user references in DB instead of usernames

Open gmta opened this issue 11 years ago • 24 comments

Currently, OC uses the username as a reference to the user for just about every table in the database. This makes it very hard to rename a user. By separating the username from the user reference, renaming a user can be supported.

Renaming a user is very important:

  • People change their name if they get married or if they divorce. They want their username to reflect that.
  • Some systems require interoperability by keeping the usernames the same across all installs. These systems do not have the luxury of abstracting using an ID. For example, see Z-Push.
  • People like to have control of their credentials.
  • It makes OC a bit more professional ;-) changing a username shouldn't be rocket science and if it is, it could be indicative of some design issues.

Things to fix:

  • Instead of using usernames everywhere, use an INT field referencing to a new primary key in oc_users.
  • Use foreign keys whenever possible so DB consistency is maintained.
  • Make sure applications either do not use usernames as a reference for anything (rows in database, files on server) or support renaming a user.

I understand that this is not an easy thing to fix, but I really think separating username/password credentials from the actual user identity would be a Good Thing™. I'm very close to crying right now as I'm writing a script to update the OC database, rename directories and files on the server, and flushing caches and rebuilding lucene indices...

gmta avatar Feb 24 '15 23:02 gmta

We have this discussed in some other places as well in the bug tracker but this is a nice write-up and the others were kinda cluttered. I'm in favour of keeping this open as well, though I'm not sure how easy to fix this considering all backends and the required migration work…

cc @DeepDiver1975 FYI

LukasReschke avatar Feb 25 '15 00:02 LukasReschke

This change will break every app ever and tons of core code. Worse: the changes will be very invasive, so if you want to migrate to this change you basically need rewrite huge portions of your app. :-1:

You can however change your full name in your personal settings, the user id should just be a login detail, so a change should not be needed at all.

PS: we cant use foreign keys (yet). If they are supported at some point, together with cascading the renaming we dont need any changes at all except for new FKs to support user id renaming, so basically: get FKs in and your issue will be fixed

BernhardPosselt avatar Feb 26 '15 09:02 BernhardPosselt

@Raydiation

I understand where you're coming from. There are some thing I would like to discuss:

  • The foreign keys are an addition and not required for this issue. So let's forget about them for now.
  • Yes, the changes will be invasive, but that should not be a reason to NOT do something. Just because OC was once designed like this, does not mean it cannot change ever, right?
  • The user's full name is not relevant to the discussion, because that is not the thing you use to login with. If an administrator uses someone's full name for their username, they will be unable to rename them afterwards. Most systems nowadays allow you to change your username, so a lot of administrators will expect this from a platform like OC.
  • OwnCloud proudly supports CalDav and CardDav so integration with other systems seems to be important. Not being able to change usernames means that you put the responsibility of supplying the right usernames with the remote side instead of supporting it in OC. Calling it a "login detail" doesn't really do the credentials justice when they're used in numerous interfaces both within the OC app ecosystem and external interfaces.

Now I can think of a solution that would not require you to rewrite all your apps:

  • Rewrite the OC core to support user IDs and allow changing usernames
  • Rewrite apps that are rewritten easily to use user IDs instead of usernames
  • Add a table to OC core mapping apps to usernames by ID. Apps that do not report that they support user IDs, should be assigned the user's current username when the user first uses the app. Whenever a user logs in and uses such an app, OC core looks up the username assigned to that app for that specific user and passes it along like it does now.

Once all apps support user IDs, you can remove this subsystem in a new major version that breaks compatibility.

There are other solutions possible, and I would like to be as constructive as possible to enable this change. So please just let me know if you have any doubts or questions.

gmta avatar Feb 26 '15 10:02 gmta

@GMTA your only point is that you want to rename usernames. You can do that by simply adding FKs. The only thing apps need to do then is to add a foreign key to their tables and then theyre done. Renaming works!

Your changes would require app and core devs to rewrite the full app logic which is IMHO not worth to consider.

BernhardPosselt avatar Feb 26 '15 10:02 BernhardPosselt

@Raydiation That would only work if apps wouldn't use usernames for paths on disk (which some do, for example the Files app).

gmta avatar Feb 26 '15 10:02 gmta

@Raydiation I'm actually a bit surprised by "rewrite the full app logic". Is really that much application logic dependent on the username? Wouldn't it be a lot of s/->getUsername()/->getId()/g (extremely simplified, I know) and a migration script?

gmta avatar Feb 26 '15 11:02 gmta

@GMTA not a problem for apps since this is dynamic. Also we have hooks which allow us to rename file directories after a username was changed. This can be done once in core and work for all apps that store things under data/$userName which should be the default storage for anything related to users.

BernhardPosselt avatar Feb 26 '15 11:02 BernhardPosselt

And yes you have to change validation which operates on anything that comes into contact with the id, you need to change everything related to the session, authentication, middleware etc. Again not worth.

BernhardPosselt avatar Feb 26 '15 11:02 BernhardPosselt

PS: this will also break APIs for mobile devices.

BernhardPosselt avatar Feb 26 '15 11:02 BernhardPosselt

@Raydiation OK that's clear talk, sounds like there are bigger fish to fry before you can even think about changing something like this :)

PS: this will also break APIs for mobile devices.

You mean the OC mobile client? Do they not communicate using a username and password? This would still be the case, right?

gmta avatar Feb 26 '15 11:02 gmta

If you are exposing ids instead of usernames for the mobile APIs this will break all sorts of assumptions. This is not only about core but about all apps.

BernhardPosselt avatar Feb 26 '15 11:02 BernhardPosselt

You do not actually need to expose the user ID to mobile APIs if you don't want to. Mobile apps still need to authenticate using a username, so it's not as if everything changes to user ID all of the sudden. You could still respond with usernames in places where the mobile API responds with usernames now.

This is more of an issue of "best practices" in database/class design than something else. For example, see this SA answer and specifically rule 3: the username is the "problem primary key".

gmta avatar Feb 26 '15 11:02 gmta

What if i want to get all folders and their users?

BernhardPosselt avatar Feb 26 '15 11:02 BernhardPosselt

Just as you would do it now, only with a join with the oc_users table I suspect? If you need the users and not only their usernames you wouldn't even have to do that join; just use a deferred load on the association.

gmta avatar Feb 26 '15 12:02 gmta

As discussed on IRC theres actually a third solution: Keep everything as is but add a separate table/field for the user login id. This wouldnt require any FKs and allows you to change the login username.

BernhardPosselt avatar Feb 26 '15 12:02 BernhardPosselt

Sounds like a solution that prevents a major overhaul of the core and apps and doesn't really have a practical downside. Like I said, there are a lot of possible solutions and if that one is within the realm of possibilities, I'm all for it!

gmta avatar Feb 26 '15 12:02 gmta

@Raydiation I've been programming a bit in OC core for the first time and I'm wondering: the IUserManager::checkPassword($loginname, $password) method already has a parameter called $loginname instead of the usual $uid - does this mean that the public contract is to pass the login name of the user?

As you can understand, I'm trying not to break the public API of the OC core so if apps are using $loginname like it's the same as $uid (for example, by not asking the user for his or her login name but just passing the $uid as an argument) any change might break this behavior.

gmta avatar Feb 26 '15 23:02 gmta

@GMTA Applications such as user_ldap allow users to authenticate with arbitrary attributes configured by the system administrator. Often CN or Email or so.

So the UID stored in the DB is not necessarily what users use when authenticating. In cases of LDAP often the UUID is stored as it doesn't change.

LukasReschke avatar Feb 26 '15 23:02 LukasReschke

@LukasReschke So if I understand you correctly, this means that $loginname should always be the user's input for their login name and never the UID, right? Because otherwise a password check would fail in the case of LDAP.

gmta avatar Feb 26 '15 23:02 gmta

Well. It can be whatever an authentication backend expects. If a backend supports only a UID such as the regular database backend then it's the UID that the users do enter ;-)

LukasReschke avatar Feb 26 '15 23:02 LukasReschke