How to add or remove users from projects using the command line

Goal

Add or remove a user to one or more Platform.sh projects using the platform CLI tool.

Assumptions

You have one or more Platform.sh projects and you have installed the platform CLI tool.

You will need to know the project IDs. You will need the email address for each user you wish to add or remove.

Problems

Sometimes it is necessary to automate user access to projects, especially when managing fleets of sites. This is a case for using the CLI.

Steps

1. Single projects

Add user user1@example.com with the project-wide role of viewer. Confirm the change with Y.

platform user:add -p <project id> -r viewer user1@example.com
Adding the user user1@example.com to <project name> (<project id>):
  Project role: viewer

Adding users can result in additional charges.

Are you sure you want to add this user? [Y/n] Y

Add user user1@example.com to the staging branch as a contributor:

platform user:add -p <project id> -r staging:contributor user1@example.com
Adding the user user1@example.com to <project name> (<project id>):
  Project role: viewer
    Role on staging: contributor

Adding users can result in additional charges.

Are you sure you want to add this user? [Y/n] Y

2. Multiple projects

Remove a user (user1@example.com) from multiple projects:

platform multi --projects <project1 id>,<project2 id> \ 
'user:delete user1@example.com'

This is the output:

Running command 'user:delete user1@example.com' on 2 projects.

* Project: <project1 name> (<project1 id>)
Are you sure you want to delete the user user1@example.com? [Y/n] y
User user1@example.com deleted

* Project: <project2 name> (<project2 id>)
Are you sure you want to delete the user user1@example.com? [Y/n] y
User user1@example.com deleted

Conclusion

User management, including adding, deleting, and role assignment, can be done from the command line using the Platform.sh CLI. Applying the platform multi command allows you to make changes on multiple projects at once.