Repository navigation
feat: Hand an automation's run user to someone else, through the API and the UI - #2942
BelhsanHmida wants to merge 10 commits into
Conversation
`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>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Documentation build overview
|
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
left a comment
There was a problem hiding this comment.
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:
- (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.
- (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".
- (I am pushing a fix for this.) The new 403 had no
reasonorrefusal, while the API change log line it extends says a refusal for the run user carriesreason: run-user-lacks-access. Clients can only tell it from the "only admins" 403 by the message. Reusingrun-user-lacks-accesswould make the form suggest Run as me, so it gets its own,named-run-user-lacks-access, withrefusal, mentioned in the change log. - (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 otherrun-aserror comes underjson.run-as; it will come underrun-astoo.
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>
|
As promised in the review, I pushed the fixes to this branch, one commit each, with tests that fail without them:
The automations tests of the API, the CLI, the UI (including the form's JavaScript checks) and the data layer pass locally: 909 passed. |
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/12with{"run-as": 2}, or Edit → Run as someone else → pick the user → Save.PATCH /assets/(id)/automations/(id)takesrun-as, a user ID. Naming yourself is the same asrun-as-me, and the two cannot be combined.user_administers_automations.run_user_problem).Since every member may run the organisation's own automations, that only happens for an automation that reads sensors of another organisation.
run-as-me.documentation/changelog.rstLook & Feel
A member who is not an account admin:
A user who could not run it:
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