flexmeasures.auth.policy

Tooling & docs for implementing our auth policy

Functions

flexmeasures.auth.policy.can_manage_user_with_roles(user, managed_user) → bool

Check that a user is at least as privileged as the user they manage.

Used for changes that could hand over or lock out another user’s account, such as their email address, their active status or a password reset. For every role of the managed user, the managing user must either have that role themselves, or be allowed to add and remove it (see can_modify_role). So an account-admin cannot change the email address of an admin in their account, while an admin can manage another admin.

Parameters:
  • user – The user attempting the change.

  • managed_user – The user whose account would be changed.

Returns:

True if the managing user may make such changes, False otherwise.

flexmeasures.auth.policy.can_modify_role(user, roles_to_modify, modified_user) → bool

For a set of supported roles, check if the current user can modify the roles.

Parameters:
  • user – The current attempting to modify a role.

  • roles_to_modify – A list of roles to modify - can be a Role or a role ID.

  • modified_user – The user whose roles are being modified.

Returns:

True if the user can modify each of the roles, False otherwise.

The roles are:

  • admin: can only be changed in CLI / directly in the DB, so not here

  • admin-reader: can be added and removed by admins

  • account-admin: can be added and removed by admins and consultants (in consultancy account)

  • consultant: can be added and removed by admins and account-admins (in same account); account-admins can only add it in an account with the Consultancy account role

  • account-member, account-reader, account-data-integrator: can be changed by admins, account-admins of the user’s account, and consultants of its consultancy account

flexmeasures.auth.policy.check_access(context: AuthModelMixin, permission: str)

Check if current user can access this auth context if this permission is required, either with admin rights or principal(s).

Raises 401 or 403 otherwise.

flexmeasures.auth.policy.check_account_membership(user, principal: str) → bool
flexmeasures.auth.policy.check_account_role(user, principal: str) → bool
flexmeasures.auth.policy.check_user_identity(user, principal: str) → bool
flexmeasures.auth.policy.check_user_role(user, principal: str) → bool
flexmeasures.auth.policy.user_can_add_accounts() → bool

Check if the current user can create new accounts.

Uses the ACL system to verify the user has permission to create accounts on the FlexMeasures platform.

Returns:

True if user has permission, False otherwise.

flexmeasures.auth.policy.user_can_reset_own_password(user) → bool

Allow self-service password recovery only with an eligible role grant.

flexmeasures.auth.policy.user_has_admin_access(user, permission: str) → bool
flexmeasures.auth.policy.user_has_scoped_permission(user, permission: str, principals: str | tuple[str] | list[str | tuple[str] | None] | None) → bool

Require a scoped role grant or an explicit legacy custom-role ACL grant.

flexmeasures.auth.policy.user_matches_principals(user, principals: str | tuple[str] | list[str | tuple[str] | None] | None) → bool

Tests if the user matches all passed principals. Returns False if no principals are passed.

Classes

class flexmeasures.auth.policy.AuthModelMixin
class flexmeasures.auth.policy.FlexMeasuresPlatform

Virtual platform resource to authorize top-level creations.