Metadata-Version: 2.5
Name: trytond_maestro_users_web
Version: 8.0.0.3
Summary: Maestro AD Users Web Accounts
Author: Clemens Hupka
Maintainer-email: Clemens Hupka <clemens.hupka@backend.at>
License-Expression: GPL-3.0-or-later
License-File: COPYRIGHT
License-File: LICENSE
Keywords: active directory,maestro,portal,tryton,web user
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Environment :: Plugins
Classifier: Framework :: Tryton
Classifier: Topic :: Office/Business
Requires-Python: >=3.10
Requires-Dist: trytond-maestro-users<8.1,>=8.0
Requires-Dist: trytond-maestro<8.1,>=8.0
Requires-Dist: trytond-party<8.1,>=8.0
Requires-Dist: trytond-web-user<8.1,>=8.0
Requires-Dist: trytond<8.1,>=8.0
Provides-Extra: test
Requires-Dist: trytond-maestro-sidecar<8.1,>=8.0; extra == 'test'
Description-Content-Type: text/x-rst

Maestro Users Web
=================

The ``maestro_users_web`` module links Tryton web (portal) accounts to the
Active Directory users mirrored by the maestro modules and creates those
accounts in bulk.

Features
--------

- **AD user link**: ``web.user`` gains a ``maestro_user`` field, restricted
  to one web account per AD user, and ``maestro.user`` reports the linked
  web account and whether it is active.
- **Portal identity**: ``web.user`` also gains a ``res_user`` field, the
  Tryton identity the portal authenticates as.  Provisioning a web account
  creates or links that identity too, keyed on the ``sAMAccountName`` of
  the AD user, so ``keycloak_auth`` resolves the AD identity of every
  portal user (issue #821).
- **Bulk provisioning**: the *Create Web Accounts* wizard selects the AD
  users by secondary group, primary group and AD server, previews exactly
  what will happen and then creates or links the accounts and the
  identities.
- **Single user provisioning**: the *Create Web Account* button of the AD
  user form provisions one user with the same rules.
- **Idempotent**: running the wizard twice never creates a second account
  nor a second identity for the same AD user.
- **Email based linking**: an existing web account that already holds the
  email address of the AD user is linked instead of duplicated, and an
  existing ``party.party`` holding that address is linked to new accounts.
- **No credentials**: the web accounts and the identities are created
  without a password and without a group, and no email is sent.  A web
  account goes through the email validation and password reset flow of the
  ``web_user`` module; an identity is usable through ``keycloak_auth``
  only, so it never unlocks the Tryton client.
- **Audited**: every created or linked account and identity is recorded in
  the ``maestro.operation.log``.
- **Scheduled reconciliation**: a provisioning configuration per scope keeps
  the accounts and the identities in line with the directory on an
  ``ir.cron`` schedule, without an operator running the wizard again (see
  `Automatic provisioning`_).

Rules
-----

The wizard classifies every selected AD user into one of four outcomes:

- *Create*: the AD user has an email address and no web account holds it.
  The new account mirrors the ``active`` flag of the AD user.
- *Link*: an existing web account holds the email address.  Only the link
  is written; the data of the existing account is left untouched.
- *Skip*: the AD user has no email address or already has a web account.
- *Fail*: the email address belongs to the web account of another AD user,
  or two AD users of the same run share one address.

An AD user without an email address can be given one with the
*Generate Email* button of the ``maestro_users`` module and provisioned
afterwards.

The party lookup matches the email address exactly, trying the address as
stored in the AD user, its normalized form and its lowercased form.  A
party whose address is stored with a different spelling is not linked.

Identity rules
--------------

The ``res.user`` of an AD user is the identity ``keycloak_auth`` resolves
an access token against, so it is keyed on the exact login of the
``sAMAccountName`` of the AD user and is never guessed.  It is classified
next to the web account:

- *Create*: no ``res.user`` holds the login.  The identity is created with
  the display name of the AD user (the ``sAMAccountName`` when it has none)
  and the ``active`` flag of the AD user.
- *Link*: a ``res.user`` already holds the login, whether a previous run
  created it, ``ldap_authentication`` created it on the first client login
  or an administrator created it by hand.  Nothing is duplicated: the
  identity is linked to the web account, a name it does not have yet is
  filled in and its ``active`` flag is kept in sync with the AD user.  The
  *link* outcome of the web account provisions the identity as well.
- *Skip*: the AD user carries no ``sAMAccountName``, or its web account was
  skipped for want of an email address.
- *Fail*: several ``res.user`` records share the login, which is a data
  integrity fault of the installation, or two AD users of the run share one
  login.

An identity that holds a group or a password is an administrative account
of the installation that happens to share the login: it is reported, but
never modified, and a web account that is not already bound to it is left
without an identity -- a portal never authenticates as an administrative
account of the installation.

The module grants no ``res.user`` right of its own.  The identity is
materialised with the access checks disabled inside
``maestro.user.create_web_accounts``, which keeps the transaction user, so
the created record names the administrator that ran the provisioning, and
writes nothing but the login, the name and the active flag -- never a group
and never a password.  Model level write on ``res.user`` would let a
delegated administrator put itself into ``res.group_admin`` through
``res.user.groups``, which is why no such right is granted.

Nothing is ever written on the ``maestro.user`` record the identity
mirrors: no AD attribute is set and no ``ad_object_guid`` is touched, so
the write guard of ``maestro_sidecar`` is not involved and provisioning
never trips it.  The identity is also compatible with
``ldap_authentication.create_user``, which keys on the same
``sAMAccountName`` and creates the same bare account -- no password, no
group -- on the first client login: whichever path runs first, the other
finds and links the account.

Automatic provisioning
----------------------

The wizard provisions the AD users an operator selects, once. A
*Provisioning Configuration* (``maestro.user.web.provision.config``)
provisions them on a schedule instead, so the accounts follow the
directory: a user mirrored by ``maestro_sidecar`` after the last wizard run
is provisioned, and an account created by the wizard follows an AD email
change, an AD rename and a deactivated AD user.

A configuration carries the scope and the policy of a run:

- **Scope**: the AD server, the primary group and the secondary group
  covered, exactly like the selection of the wizard. An empty field does not
  restrict the run.
- **Sync mode**: ``add_only`` creates the accounts of the AD users that have
  none and leaves the existing ones alone, which is what the wizard does;
  ``add_update`` also keeps the email address, the name and the active flag
  of an existing account in line with the directory; and
  ``add_update_deactivate`` also deactivates the accounts of the AD users
  that left the scope of the configuration.
- **Schedule**: the minutes between two runs and the hours between two full
  reconciles.

Every run goes through ``maestro.user.run_provisioning``, the very
engine of the wizard: the two paths share the planner and the writer, so an
AD user is classified the same way whichever one provisions it. Only the
treatment of an AD user that already has an account depends on the sync
mode.

Scheduling
~~~~~~~~~~

An ``ir.cron`` job calls ``execute_cron`` on
``maestro.user.web.provision.run`` every five minutes; each active
configuration is only executed when its own ``schedule_interval`` has
elapsed, and a running marker with a stale-run timeout protects against
overlapping runs. Configurations run sequentially and are isolated from each
other, so one failing configuration does not block the remaining ones.

After the first full run, the scheduled runs are incremental: only the AD
users the synchronization engine changed since the recorded watermark are
visited. A full reconcile of the whole scope is forced when the
``full_reconcile_interval`` has elapsed and by the *Run Full Reconcile*
button, which clears the watermark and reprocesses everything - it is what
sees the AD users that left the scope of the configuration.

The watermark is only promoted by a successful run, so a failed run retries
the same AD users; a dry run promotes nothing either, it only reports.

Reconciliation
~~~~~~~~~~~~~~

A reconciliation mode mirrors, on the linked account and identity:

- the **email address** of the AD user, unless another web account already
  holds it - the address is never taken away from another user, the account
  keeps the one it has and the run reports the conflict;
- the **display name** of the AD user, on an identity that carries neither a
  group nor a password;
- the **active flag** of the AD user.

An identity that already holds a group or a password is an administrative
account of the installation that happens to share the login: it is reported
and never modified, and a web account that is not already bound to it is
left without an identity, so a portal never authenticates as an
administrative account - on the scheduled path exactly as on the
interactive one. Everything else the module has always refused stays
refused: no password, no group, no write on an AD-managed record, no delete
of an account or of an identity, and no rewrite of the login of an identity
(a renamed AD user is reported, its ``sAMAccountName`` contract with
``keycloak_auth`` is not broken).

An account whose AD user leaves the scope - moved to another group, to
another AD server or to another site - is deactivated, never deleted, in the
``add_update_deactivate`` mode, on a full reconcile.

Log
~~~

Every run records its counters - created, linked, updated, deactivated,
unchanged, skipped and failed accounts, and the same for the identities - in
``maestro.user.web.provision.log``, with the notes that explain them: the
aggregated reason of the AD users that were skipped, failed or left alone.
An AD user that could not be provisioned is never a bare count.

Rolling out
~~~~~~~~~~~

A new configuration is created inactive, so installing or upgrading the
module starts no provisioning at all. Enable it with *Dry Run* first: the
run only reports what it would do. Then turn the dry run off and go from
``add_only`` to ``add_update``, and to ``add_update_deactivate`` once the
accounts are in place.

Dependencies
------------

- Tryton modules: ``maestro``, ``maestro_users``, ``web_user``, ``party``,
  ``ir``, ``res``.

Models
------

- extends ``web.user`` — adds the ``maestro_user`` field linking the
  account to its AD user and the ``res_user`` field linking the Tryton
  identity the portal authenticates as.
- extends ``maestro.user`` — adds the functional fields ``web_user`` and
  ``web_user_active`` and the ``create_web_account`` button.
- ``maestro.user.web.provision.config`` — the scope, the sync mode and the
  schedule of the automatic provisioning.
- ``maestro.user.web.provision.run`` — the ``ir.cron`` entry point of the
  scheduled provisioning.
- ``maestro.user.web.provision.log`` — the counters and the notes of every
  provisioning run.

Wizards
-------

- ``maestro.user.web.create`` — *Create Web Accounts*: bulk provisioning of
  the web accounts of the AD users.

Scheduled Jobs
--------------

- ``maestro.user.web.provision.run|execute_cron`` — runs every provisioning
  configuration that is due.
