def system_wide_role(self): """For choosing the role string to show to the user; of all the roles in the system-wide context, it shows the highest ranked one (if there are multiple) or "No Access" if there are none. """ if self.email in getattr(settings, "BOOTSTRAP_ADMIN_USERS", []): return SystemWideRoles.SUPERUSER from ggrc.utils.user_generator import is_app_2_app_user_email if is_app_2_app_user_email(self.email): return SystemWideRoles.SUPERUSER role_hierarchy = { SystemWideRoles.ADMINISTRATOR: 0, SystemWideRoles.EDITOR: 1, SystemWideRoles.READER: 2, SystemWideRoles.CREATOR: 3, } unique_roles = set([ user_role.role.name for user_role in self.user_roles if user_role.role.name in role_hierarchy ]) if not unique_roles: return u"No Access" # -1 as default to make items not in this list appear on top # and thus shown to the user sorted_roles = sorted(unique_roles, key=lambda x: role_hierarchy.get(x, -1)) return sorted_roles[0]
def is_external_app_user(): """Checks if the current user is an external application. Account for external application is defined in settings. External application requests require special processing and validations. """ user = _get_current_logged_user() if not user or user.is_anonymous(): return False from ggrc.utils.user_generator import is_app_2_app_user_email return is_app_2_app_user_email(user.email)
def get_ggrc_user(request, mandatory): """Find user from email in "X-GGRC-user" header.""" email = parse_user_email(request, "X-GGRC-user", mandatory=mandatory) if not email: return None if is_app_2_app_user_email(email): # External Application User should be created if doesn't exist. user = get_external_app_user(email) else: user = all_models.Person.query.filter_by(email=email).first() if not user: raise exceptions.BadRequest("No user with such email: %s" % email) return user
def test_is_external_app_user_email_equals(self): """EXTERNAL_APP_USER email is equals to given email.""" self.assertTrue( user_generator.is_app_2_app_user_email('*****@*****.**'))
def test_is_external_app_user_email_corrupted_email(self): """In EXTERNAL_APP_USER is corrupted email.""" self.assertFalse( user_generator.is_app_2_app_user_email('*****@*****.**'))
def test_is_external_app_user_email_no_setting(self): """No EXTERNAL_APP_USER presented in settings.""" self.assertFalse( user_generator.is_app_2_app_user_email('*****@*****.**'))