Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New, Dynamic user registration role #1410

Merged
merged 3 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
JMESPath documentation
  • Loading branch information
szczeles committed Jun 22, 2020
commit f3305a96e25057e1a9f9174376e0b0e8547f2fa6
29 changes: 29 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ Use config.py to configure the following parameters. By default it will use SQLL
| | exist. Mandatory when using user | |
| | registration | |
+----------------------------------------+--------------------------------------------+-----------+
| AUTH_USER_REGISTRATION_ROLE_JMESPATH | The `JMESPath <http://jmespath.org/>`_ | No |
| | expression used to evaluate user role on | |
| | registration. If set, takes precedence | |
| | over ``AUTH_USER_REGISTRATION_ROLE``. | |
| | Requires ``jmespath`` to be installed. | |
| | See :ref:`jmespath-examples` for examples | |
+----------------------------------------+--------------------------------------------+-----------+
| AUTH_LDAP_SERVER | define your ldap server when AUTH_TYPE=2 | Cond. |
| | example: | |
| | | |
Expand Down Expand Up @@ -261,3 +268,25 @@ Next you only have to import them to the Flask app object, like this
app.config.from_object('config')

Take a look at the skeleton `config.py <https://github.com/dpgaspar/Flask-AppBuilder-Skeleton/blob/master/config.py>`_


.. _jmespath-examples:

Using JMESPath to map user registration role
--------------------------------------------

If user self registration is enabled and ``AUTH_USER_REGISTRATION_ROLE_JMESPATH`` is set, it is
used as a `JMESPath <http://jmespath.org/>`_ expression to evalate user registration role. The input
values is ``userinfo`` dict, returned by ``get_oauth_user_info`` function of Security Manager.
Usage of JMESPath expressions requires `jmespath <https://pypi.org/project/jmespath/>`_ package
to be installed.

In case of Google OAuth, userinfo contains user's email that can be used to map some users as admins
and rest of the domain users as read only users. For example, this expression:
``contains(['user1@domain.com', 'user2@domain.com'], email) && 'Admin' || 'Viewer'``
causes users 1 and 2 to be registered with role ``Admin`` and rest with the role ``Viewer``.

JMESPath expression allow more groups to be evaluated:
``email == 'user1@domain.com' && 'Admin' || (email == 'user2@domain.com' && 'Op' || 'Viewer')``

For more example, see `specification <https://jmespath.org/specification.html>`_.
2 changes: 1 addition & 1 deletion examples/oauth/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
AUTH_USER_REGISTRATION_ROLE = "Admin"

# Self registration role based on user info
AUTH_USER_REGISTRATION_ROLE_JMESPATH = "contains(['alice', 'celine'], username) && 'Admin' || 'Public'"
AUTH_USER_REGISTRATION_ROLE_JMESPATH = "contains(['alice@example.com', 'celine@example.com'], email) && 'Admin' || 'Public'"

# When using LDAP Auth, setup the ldap server
# AUTH_LDAP_SERVER = "ldap://ldapserver.new"
Expand Down
2 changes: 1 addition & 1 deletion flask_appbuilder/security/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def auth_user_registration_role(self):
return self.appbuilder.get_app.config["AUTH_USER_REGISTRATION_ROLE"]

@property
def auth_user_registration_role_jmespath(self):
def auth_user_registration_role_jmespath(self) -> str:
return self.appbuilder.get_app.config["AUTH_USER_REGISTRATION_ROLE_JMESPATH"]

@property
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ mysqlclient>=1.4.2, < 2.0.0
cython==0.29.17
pymssql==2.1.4
black==19.3b0
jmespath==0.9.5
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ flask==1.1.1
idna==2.9 # via email-validator
itsdangerous==1.1.0 # via flask
jinja2==2.10.1 # via flask, flask-babel
jmespath==0.9.5
jsonschema==3.0.1
markupsafe==1.1.1 # via jinja2
marshmallow-enum==1.5.1
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def desc():
"PyJWT>=1.7.1",
"sqlalchemy-utils>=0.32.21, <1",
],
extras_require={"jmespath": ["jmespath>=0.9.5"]},
tests_require=["nose>=1.0", "mockldap>=0.3.0"],
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down