def register(): """ Register a new user with Stormpath. """ form = RegistrationForm() # If we received a POST request with valid information, we'll continue # processing. if form.validate_on_submit(): data = {} # Attempt to create the user's account on Stormpath. try: # email and password data['email'] = form.email.data data['password'] = form.password.data # given_name and surname are required fields data['given_name'] = 'Anonymous' data['surname'] = 'Anonymous' # create a tenant ID tenant_id = str(uuid.uuid4()) data['custom_data'] = {'tenant_id': tenant_id, 'site_admin': True} # Create the user account on Stormpath. If this fails, an # exception will be raised. account = User.create(**data) # create a new stormpath group directory = stormpath_manager.application.default_account_store_mapping.account_store tenant_group = directory.groups.create({ 'name': tenant_id, 'description': data['email'] }) # assign new user to the newly created group account.add_group(tenant_group) account.add_group('site_admin') # If we're able to successfully create the user's account, # we'll log the user in (creating a secure session using # Flask-Login), then redirect the user to the # STORMPATH_REDIRECT_URL setting. login_user(account, remember=True) # redirect to dashboard redirect_url = current_app.config['STORMPATH_REDIRECT_URL'] return redirect(redirect_url) except StormpathError as err: flash(err.message.get('message')) return render_template( 'account/register.html', form=form, )
def register(): """ Register a new user with Stormpath. """ form = RegistrationForm() # If we received a POST request with valid information, we'll continue # processing. if form.validate_on_submit(): data = {} # Attempt to create the user's account on Stormpath. try: # email and password data["email"] = form.email.data data["password"] = form.password.data # given_name and surname are required fields data["given_name"] = "Anonymous" data["surname"] = "Anonymous" # create a tenant ID tenant_id = str(uuid.uuid4()) data["custom_data"] = {"tenant_id": tenant_id, "site_admin": True} # Create the user account on Stormpath. If this fails, an # exception will be raised. account = User.create(**data) # create a new stormpath group directory = stormpath_manager.application.default_account_store_mapping.account_store tenant_group = directory.groups.create({"name": tenant_id, "description": data["email"]}) # assign new user to the newly created group account.add_group(tenant_group) account.add_group("site_admin") # If we're able to successfully create the user's account, # we'll log the user in (creating a secure session using # Flask-Login), then redirect the user to the # STORMPATH_REDIRECT_URL setting. login_user(account, remember=True) # redirect to dashboard redirect_url = current_app.config["STORMPATH_REDIRECT_URL"] return redirect(redirect_url) except StormpathError as err: flash(err.message.get("message")) return render_template("account/register.html", form=form)
def add_user_confirm(token): """ Decode invite token and create new user account """ form = RegistrationForm() decoded = None try: ts = URLSafeTimedSerializer(current_app.config['SECRET_KEY']) decoded = ts.loads(token, max_age=86400) email = decoded[0] except: abort(404) if form.validate_on_submit(): try: tenant_id = decoded[1] data = {} data['email'] = email data['password'] = request.form['password'] # given_name and surname are required fields data['given_name'] = 'Anonymous' data['surname'] = 'Anonymous' # set tenant id and site_admin status data['custom_data'] = { 'tenant_id': tenant_id, 'site_admin': 'False' } # create account account = User.create(**data) # add user to tenant group account.add_group(tenant_id) # login user login_user(account, remember=True) # success redirect return render_template('account/add_user_complete.html') except StormpathError as err: flash(err.message.get('message')) return render_template('account/add_user_setpassword.html', form=form, email=email)
def add_user_confirm(token): """ Decode invite token and create new user account """ form = RegistrationForm() decoded = None try: ts = URLSafeTimedSerializer(current_app.config["SECRET_KEY"]) decoded = ts.loads(token, max_age=86400) email = decoded[0] except: abort(404) if form.validate_on_submit(): try: tenant_id = decoded[1] data = {} data["email"] = email data["password"] = request.form["password"] # given_name and surname are required fields data["given_name"] = "Anonymous" data["surname"] = "Anonymous" # set tenant id and site_admin status data["custom_data"] = {"tenant_id": tenant_id, "site_admin": "False"} # create account account = User.create(**data) # add user to tenant group account.add_group(tenant_id) # login user login_user(account, remember=True) # success redirect return render_template("account/add_user_complete.html") except StormpathError as err: flash(err.message.get("message")) return render_template("account/add_user_setpassword.html", form=form, email=email)
def setUp(self): """Provision a single user account and some groups for testing.""" # Call the parent setUp method first -- this will bootstrap our tests. super(TestGroupsRequired, self).setUp() with self.app.app_context(): # Create our Stormpath user. self.user = User.create( given_name = 'Randall', surname = 'Degges', email = '*****@*****.**', password = '******', ) # Create two groups. self.admins = self.application.groups.create({ 'name': 'admins', }) self.developers = self.application.groups.create({ 'name': 'developers', })
def test_create(self): with self.app.app_context(): # Ensure all defaults fields are properly set. user = User.create( email = '*****@*****.**', password = '******', given_name = 'Cookie', surname = 'Monster', ) self.assertEqual(user.email, '*****@*****.**') self.assertEqual(user.given_name, 'Cookie') self.assertEqual(user.surname, 'Monster') self.assertEqual(user.username, '*****@*****.**') self.assertEqual(user.middle_name, None) self.assertEqual(dict(user.custom_data), {}) # Ensure we can the middle name and username fields to custom # entities. user = User.create( username = '******', email = '*****@*****.**', password = '******', given_name = 'Austin', surname = 'Powers', middle_name = 'Danger', ) self.assertEqual(user.username, 'powerful') self.assertEqual(user.middle_name, 'Danger') # Ensure we can set custom data when we create a user. user = User.create( email = '*****@*****.**', password = '******', given_name = 'Snoop', surname = 'Dogg', custom_data = { 'favorite_state': 'California', 'favorite_genre': 'reggae', 'bank_details': { 'name': 'Bank of America', 'amount': 9999999.99, 'branch': { 'name': 'Bank of Venice', 'address': '111 9th Street', 'city': 'Venice', 'state': 'CA', }, }, }, ) self.assertEqual(dict(user.custom_data), { 'favorite_state': 'California', 'favorite_genre': 'reggae', 'bank_details': { 'name': 'Bank of America', 'amount': 9999999.99, 'branch': { 'name': 'Bank of Venice', 'address': '111 9th Street', 'city': 'Venice', 'state': 'CA', }, }, })