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
Next Next commit
Blacked
  • Loading branch information
szczeles committed Jun 22, 2020
commit 3b0a3f49c4ab5ee7e6af63f39bd63532a74ffa07
6 changes: 3 additions & 3 deletions flask_appbuilder/security/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,16 +1028,16 @@ def auth_user_oauth(self, userinfo):
role_name = self.auth_user_registration_role
if self.auth_user_registration_role_jmespath:
import jmespath

role_name = jmespath.search(
self.auth_user_registration_role_jmespath,
userinfo
self.auth_user_registration_role_jmespath, userinfo
)
user = self.add_user(
username=userinfo["username"],
first_name=userinfo.get("first_name", ""),
last_name=userinfo.get("last_name", ""),
email=userinfo.get("email", ""),
role=self.find_role(role_name)
role=self.find_role(role_name),
)
if not user:
log.error("Error creating a new OAuth user %s" % userinfo["username"])
Expand Down
24 changes: 10 additions & 14 deletions flask_appbuilder/tests/_test_oauth_registration_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class OAuthRegistrationRoleTestCase(unittest.TestCase):

def setUp(self):
self.app = Flask(__name__)
self.app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
Expand All @@ -25,10 +24,8 @@ def tearDown(self):
def test_self_registration_not_enabled(self):
self.app.config["AUTH_USER_REGISTRATION"] = False
self.appbuilder = AppBuilder(self.app, self.db.session)

result = self.appbuilder.sm.auth_user_oauth(
userinfo={'username': 'testuser'}
)

result = self.appbuilder.sm.auth_user_oauth(userinfo={"username": "testuser"})

self.assertIsNone(result)
self.assertEqual(len(self.appbuilder.sm.get_all_users()), 0)
Expand All @@ -37,27 +34,26 @@ def test_register_and_attach_static_role(self):
self.app.config["AUTH_USER_REGISTRATION"] = True
self.app.config["AUTH_USER_REGISTRATION_ROLE"] = "Public"
self.appbuilder = AppBuilder(self.app, self.db.session)

user = self.appbuilder.sm.auth_user_oauth(
userinfo={'username': 'testuser'}
)

user = self.appbuilder.sm.auth_user_oauth(userinfo={"username": "testuser"})

self.assertEqual(user.roles[0].name, "Public")

def test_register_and_attach_dynamic_role(self):
self.app.config["AUTH_USER_REGISTRATION"] = True
self.app.config["AUTH_USER_REGISTRATION_ROLE_JMESPATH"] = \
"contains(['alice', 'celine'], username) && 'Admin' || 'Public'"
self.app.config[
"AUTH_USER_REGISTRATION_ROLE_JMESPATH"
] = "contains(['alice', 'celine'], username) && 'Admin' || 'Public'"
self.appbuilder = AppBuilder(self.app, self.db.session)

# Role for admin
user = self.appbuilder.sm.auth_user_oauth(
userinfo={'username': 'alice', 'email': 'alice@example.com'}
userinfo={"username": "alice", "email": "alice@example.com"}
)
self.assertEqual(user.roles[0].name, "Admin")

# Role for non-admin
user = self.appbuilder.sm.auth_user_oauth(
userinfo={'username': 'bob', 'email': 'bob@example.com'}
userinfo={"username": "bob", "email": "bob@example.com"}
)
self.assertEqual(user.roles[0].name, "Public")