Skip to content

feat: Hand an automation's run user to someone else, through the API and the UI - #2942

Open
BelhsanHmida wants to merge 10 commits into
mainfrom
feat/2912-assign-run-user
Open

BelhsanHmida wants to merge 10 commits into
mainfrom
feat/2912-assign-run-user

Conversation

@BelhsanHmida

@BelhsanHmida BelhsanHmida commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

The CLI can choose whom an automation runs as (flexmeasures edit automation --run-as), but the API and the edit form could only take it over for yourself (run-as-me).
This PR lets admins and the account admins of the asset's organisation hand an automation to someone else, for example before a colleague leaves: PATCH /assets/5/automations/12 with {"run-as": 2}, or Edit → Run as someone else → pick the user → Save.

  • PATCH /assets/(id)/automations/(id) takes run-as, a user ID. Naming yourself is the same as run-as-me, and the two cannot be combined.
  • Only admins and the account admins of the asset's organisation may name someone else (403 otherwise), the same rule as switching monitoring, now in user_administers_automations.
  • The edit is refused (403) when that user could not run the automation, as every run would then be refused. The CLI and the API share this check (run_user_problem).
  • A user who is missing, inactive, or not one you may see is refused alike (422), so the refusal tells nothing about users of other organisations.
  • An edit that also hands the runs over is checked against the new run user, not the one it replaces.
  • The edit form gets a Run as someone else picker next to Run as me, for the same users. It lists the active users of the asset's organisation from the users API, and a refusal on Save shows in the form.
    Since every member may run the organisation's own automations, that only happens for an automation that reads sensors of another organisation.
  • The asset's audit log names both users, as it did for run-as-me.
  • API change log and automation docs updated.
  • Added changelog item in documentation/changelog.rst

Look & Feel

PATCH /api/v3_0/assets/5/automations/12
{"run-as": 2}

200 OK
{..., "runs-as": {"id": 2, "name": "Test Prosumer User"}, ...}

A member who is not an account admin:

403 FORBIDDEN
{"message": "Only admins, and the account admins of the asset's organisation, may choose someone else for an automation to run as. Send run-as-me to have it run as you.", ...}

A user who could not run it:

403 FORBIDDEN
{"message": "The automation was not changed, as the user to run it as could not run it: Automation 12 runs as Test Dummy User (user 4), who does not have 'read' permission on asset 5.", ...}

How to test

pytest flexmeasures/api/v3_0/tests/test_automations_api.py flexmeasures/ui/tests/js/test_automation_edit.py flexmeasures/cli/tests/test_automations.py -k "run_as or run_user or hands_the_runs or named_user or cannot_see or exclusive"

Or by hand: log in as an account admin, open an automation's Edit form on the asset's Automations page, pick a colleague under Run as someone else and save; the automation's details and the asset's audit log now name that colleague.
Log in as a plain member: the picker is not shown, and Run as me still is.

Further Improvements

Related Items

Closes #2912
Follow-up to #2779


Sign-off

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on code under GPL or other license that is incompatible with FlexMeasures

`PATCH /assets/(id)/automations/(id)` takes `run-as`, the ID of the user to run the automation as from now on, as `flexmeasures edit automation --run-as` already could.

- Only admins and the account admins of the asset's organisation may name someone else, as every run then uses that user's access without them choosing to take it over; naming yourself is the same as `run-as-me`.
- The edit is refused when that user could not run the edited automation, as every run would then be refused. The CLI now shares this check, rather than making it itself.
- A user who is missing, inactive, or not one the editor may see is refused alike, so the refusal tells nothing about users of other organisations.
- An edit that also hands the runs over is checked against the new run user, not the one it replaces.
- `user_may_change_monitoring` becomes `user_administers_automations`, as it now decides both.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Admins and the account admins of the asset's organisation get a *Run as someone else* picker next to *Run as me*, listing the organisation's active users, apart from you and whom the automation runs as already.
The list comes from the users API, so it shows nobody the editor could not see already.
Saving checks that the chosen user can run the automation, and shows the refusal in the form otherwise, which in practice only happens for an automation reading sensors of another organisation.
Picking a user and ticking *Run as me* clear each other.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Also say that the CLI refuses an edit of what an automation computes when its run user could not run it, rather than only warning.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
@BelhsanHmida BelhsanHmida self-assigned this Dec 1, 2026
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
@BelhsanHmida BelhsanHmida changed the title Hand an automation's run user to someone else, through the API and the UI feat: Hand an automation's run user to someone else, through the API and the UI Dec 1, 2026
@BelhsanHmida
BelhsanHmida requested a review from Flix6x December 1, 2026 06:25
@read-the-docs-community

read-the-docs-community Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Main's automation dependencies (#2706) brought a `_current_user_may_read` for automations,
so the one checking whether you may see the user you name is renamed `_current_user_may_see_user`;
otherwise the later definition replaced it.
The API change log line for `run-as` moves along with the #2779 lines into v3.0-38, as v3.0-37 was released with v1.1.3.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

@Flix6x Flix6x left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, Mohamed. I tried this on a local instance: as an account admin through the API and the edit form, as a plain member, as a host admin of another account, and with the CLI. It works as described: handing a deactivated colleague's automation to someone else, the 403 for members naming someone else (and the takeover when they name themselves), the refusal for a user who could not run it with nothing changed, the same 422 for a user who is missing, inactive or of another organisation, and run-as with run-as-me refused. The picker lists the right users and is hidden for members. The automations API, CLI and edit-form tests pass (248).

One addition and four small points, for all of which I am pushing commits to this branch myself, right after this review, so nothing is left for you to do unless you disagree with them.

Consultants may hand an automation over too. Consultants of the asset's organisation may already create and update things in that account, users included, so leaving them out of user_administers_automations was the odd one out (the PR listed it under Further Improvements). I am adding them there, which also lets them switch monitoring on or off, with tests and the docs updated.

The small points:

  1. (I am pushing a fix for this.) The picker's note said "Lists your organisation's users", also for a host admin editing another organisation's asset, where it lists that organisation's users. The feature docs say it right ("the users of the asset's organisation"). It will keep the short wording when you belong to the asset's organisation, and names the asset's organisation otherwise; the same for the text when the users cannot be listed.
  2. (I am pushing a fix for this.) A refusal for the named user said the automation "runs as" them: "The automation was not changed, as the user to run it as could not run it: Automation 2 runs as toy-colleague (user 4), who does not have 'read' permission on sensor 10." It doesn't, as nothing was changed. The CLI on main worded it the same way; I am changing it for both to e.g. "toy-colleague (user 4) does not have 'read' permission on sensor 10".
  3. (I am pushing a fix for this.) The new 403 had no reason or refusal, while the API change log line it extends says a refusal for the run user carries reason: run-user-lacks-access. Clients can only tell it from the "only admins" 403 by the message. Reusing run-user-lacks-access would make the form suggest Run as me, so it gets its own, named-run-user-lacks-access, with refusal, mentioned in the change log.
  4. (I am pushing a fix for this.) The 422 for a user you may not name said "missing or inactive" also for an active user of another organisation ("User 3 is missing or inactive, so the automation cannot run as them." for demo-analyst). It keeps one shared text for all three cases, so it still says nothing about other organisations' users, but will cover them all: "... does not exist, is inactive or is not one you may name ...". It also came as a plain string under json, while the other run-as error comes under json.run-as; it will come under run-as too.

Flix6x and others added 5 commits December 1, 2026 15:09
Refusing a handover said "Automation 2 runs as toy-colleague (user 4), who does not have 'read' permission on sensor 10.",
while nothing changed, so the automation does not run as them.
It now says "toy-colleague (user 4) does not have 'read' permission on sensor 10." in the API, the edit form and the CLI's --run-as,
while refusals for the user it does run as keep their wording.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
The 403 for a `run-as` user who could not run the automation carried only a message,
so clients could tell it from the "only admins" 403 by its wording alone.
It now carries `reason: named-run-user-lacks-access` and the `refusal`, like the refusal of the user it runs as does.
It is not `run-user-lacks-access`, which tells clients (and the edit form) to take the automation over with run-as-me,
no answer to a user it does not run as.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ords that fit all three cases

Naming an active user of another organisation was refused as "User 3 is missing or inactive, ...", which is not what they are,
and the 422 came under `json` as a plain string, while the other refusal of the field ("cannot be combined with run-as-me") comes under `json.run-as`.
It now says "User 3 does not exist, is inactive or is not one you may name, so the automation cannot run as them.", still one text for all three,
so it tells nothing about other organisations' users, and comes under `run-as`.
The CLI, which sees every user, keeps saying "missing or inactive".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
… and switch their monitoring

Consultants of a client organisation may already create and update things in it, users included,
so leaving them out of `user_administers_automations` was the odd one out.
They may now choose someone else for an automation to run as (`run-as`, and the edit form's picker),
and switch its monitoring on or off, which the same function decides, as the account admins of the asset's organisation may.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ne from another

The note said "Lists your organisation's users." also to a host admin editing another organisation's asset,
where the picker lists that organisation's users, which matters more now that its consultants may pick too.
It keeps the short wording for the asset's own users, and says e.g. "Lists the users of Toy Account." otherwise,
as does the text for when the users cannot be listed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x

Flix6x commented Dec 1, 2026

Copy link
Copy Markdown
Member

As promised in the review, I pushed the fixes to this branch, one commit each, with tests that fail without them:

  • Consultants of the asset's organisation may now hand an automation over, and switch its monitoring, as its account admins may (aa3b9e9). The picker shows for them too, as it follows the same check.
  • The picker's note keeps "Lists your organisation's users." for the asset's own users, and says "Lists the users of Toy Account." otherwise, as does the text for when the users cannot be listed (43c8b1a).
  • A refusal for a named user no longer says the automation "runs as" them: "toy-colleague (user 4) does not have 'read' permission on sensor 10." in the API, the form and the CLI. Refusals for the user it does run as keep their wording (2624805).
  • The new 403 carries reason: named-run-user-lacks-access and the refusal, also in the endpoint docs and the API change log (6dcb4f8).
  • The 422 for a user you may not name now says "User 3 does not exist, is inactive or is not one you may name, so the automation cannot run as them.", still one text for all three cases, and comes under run-as. The CLI, which sees every user, keeps "missing or inactive" (ececc09).

The automations tests of the API, the CLI, the UI (including the form's JavaScript checks) and the data layer pass locally: 909 passed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hand an automation's run user to someone else, through the API and the UI

2 participants