Uploading users in bulk in Django

Hi @Mdkhamru,

  1. The CSV can be located anywhere as long as you reference the correct path to it when reading from it. In the example above, it was just in the root of the KPI directory.
  2. There is no right or wrong way to do it — my code above is merely a suggested approach to it. If you do want to follow that approach then you can just have multiple lines with the same username and project_uid but different permissions in the permissions.csv file:
username project_uid permission
johndoe aGwegnsvligsgh view_asset
johndoe aGwegnsvligsgh view_submissions
2 Likes

@Josh
Is there a way to add users and assign permissions from an external system using a similar method? I mean other than a clunky set of SSH commands?

@ks_1, yes for assigning permissions, no for creating users — you can just integrate the external system with the appropriate API endpoints. I would suggest looking at the network tab when in the KPI UI and see what endpoints are called and then just emulate that behaviour.

For creating users, you could potentially have a script that fills in the HTML sign-up form, but I haven’t tested that.

2 Likes

@Josh,
Is there any development plan to implement APIs to add/remove users? If not, I’m planning to get it developed from our end and will create a pull request once done.

1 Like

@ks_1, there’s nothing in the pipeline at the moment, but I would suggest we consult with @jnm about the approach to implementing this. It’s best to develop features that can be merged into master to serve the rest of the community rather than creating a fork that you need to maintain.

@ks_1, I’ve just discovered that there is in fact a way to create users if you are maintaining your own instance:

Instructions from @jnm:

  1. Go to https://kf.your.host/admin/kpi/authorizedapplication/add/ and add a new entry. The name doesn’t matter: it’s just for your own record keeping. The key is used in the next step.
  2. Once that’s done, you can POST to https://kf.your.host/authorized_application/users/ using the header Authorization: Token key_you_added and send the following fields:
  • username
  • password
  • first_name
  • last_name
  • email
  1. The user should be created in KoBo immediately, without email confirmation. See here for the code that supports this. The complete PR that added this is User creation and authentication for external apps by jnm · Pull Request #368 · kobotoolbox/kpi · GitHub
3 Likes

Thanks a lot @Josh! I’ll try this out with our developers.

1 Like

It works perfectly. :slight_smile: So adding users in bulk is now a piece of cake. Thanks @Josh and @jnm.

I also have some groups set up for different types of users, and would like to change these from APIs as well. Is there existing functionality for this using Authorized Application?

1 Like

Hi @ks_1, that’s great! I’m not aware of an endpoint that’s exposed to do that, but I’ll leave that open to @jnm to comment. It looks like you can manage this sort of thing just through the existing permissions endpoints as a work-around? You could have a spreadsheet or whatever that links permissions to groups and then assign those to users. Team management is something that’s in the pipeline, so hopefully you can manage this without a work-around soon.

1 Like

We haven’t done anything with Django’s group features yet.

Hello @Josh

Can we update bulk users’ passwords for existing users?

Hi @Mdkhamru, you can refer to the Django docs for this:

https://docs.djangoproject.com/en/dev/topics/auth/default/#user-objects

3 Likes

@Josh thanks

2 Likes

We always faced some credential issues when using this method, only now did we realize the problem…
When creating users through this method, the credentials work on KPI, but they don’t work on KC (although the users are created there as well). Users can log into kf.url, but if they try to sign in on ODK Collect using the KC endpoint, the credentials don’t work.
The only way to get it to work is to go to kf.url/admin and reset the passwords for these users.

I think this is a bug.

1 Like

ya right even some times after password reset they are still not able to login kobo collect and credentials works only in web

Thanks for reporting this issue. It looks like a workaround is to log in to KPI first with the newly created user’s credentials, for example:

john@world:~$ curl --user special_api_user:not_very_secret http://kf.kobo.local:9000/me/; echo
{"username":"special_api_user","first_name":"","last_name":"","email":"foo@bar.baz","server_time":"2021-11-18T01:41:37Z","date_joined":"2021-11-18T01:40:48Z","projects_url":"http://kc.kobo.local:9000/special_api_user","is_superuser":false,"gravatar":"https://www.gravatar.com/avatar/9050f8ea6abf7524e8ca40107fc8b00c?s=40","is_staff":false,"last_login":null,"extra_details":{"require_auth":false},"git_rev":false}

After that, the KoBoCAT OpenRosa endpoints, which use digest authentication, should work for the new user. I’ve filed a bug; please check the reproduction steps and add comments if your experience differs.

3 Likes

@jnm
We’ve edited the v2 KPI endpoints to create users and edit permissions (also all other parameters) using superuser credentials… Can I directly submit a pull request?

Thanks for the pull request. Did you intend to expose the usernames, names, email addresses, etc. to anyone who accessed the endpoint, including anonymous users and non-superusers? You also seem to remove an entire method, migrate(), for no apparent reason.

I would request that you please review the checklist in the PR template (none of the boxes are checked). Unit tests are absolutely mandatory for a change like this, and the unit tests should illustrate what you expect the API to do. For example, I would be able to tell if you intentionally wanted to give the general public that level of access to your user list if there were a unit test—and if you didn’t, the unit test would’ve caught the mistake.

Thanks again.

2 Likes

Does this code only work in kobo?

how you add new users