def get_context_from_subscription_plan_by_activation_key(request): """ Helper function to return the permission context (i.e., enterprise customer uuid) from active subscription plan associated with the license identified by the ``activation_key`` query param on a request and the ``email`` provided in the request's JWT. Params: ``request`` - A DRF Request object. Returns: The ``enterprise_customer_uuid`` associated with the user's license. """ today = localized_utcnow() activation_key = get_activation_key_from_request(request) try: user_license = License.objects.get( activation_key=activation_key, user_email=get_email_from_request(request), subscription_plan__is_active=True, subscription_plan__start_date__lte=today, subscription_plan__expiration_date__gte=today, ) except License.DoesNotExist as exc: decoded_jwt = get_decoded_jwt(request) lms_user_id = get_key_from_jwt(decoded_jwt, 'user_id') logger.exception( 'License not found for activation key %s for user %s', activation_key, lms_user_id ) raise Http404('No License matches the given query.') from exc return user_license.subscription_plan.customer_agreement.enterprise_customer_uuid
def has_implicit_access_to_subscriptions_learner(user, subscription_plan): # pylint: disable=unused-argument """ Check that if request user has implicit access to the given SubscriptionPlan for the `SUBSCRIPTIONS_LEARNER_ROLE` feature role. Returns: boolean: whether the request user has access. """ if not subscription_plan: return False return request_user_has_implicit_access_via_jwt( get_decoded_jwt(crum.get_current_request()), constants.SUBSCRIPTIONS_LEARNER_ROLE, str(subscription_plan.enterprise_customer_uuid), )
def has_implicit_access_to_subscriptions_admin(user, enterprise_customer_uuid): # pylint: disable=unused-argument """ Check that if request user has implicit access to the given enterprise UUID for the `SUBSCRIPTIONS_ADMIN_ROLE` feature role. Returns: boolean: whether the request user has access. """ if not enterprise_customer_uuid: return False return request_user_has_implicit_access_via_jwt( get_decoded_jwt(crum.get_current_request()), constants.SUBSCRIPTIONS_ADMIN_ROLE, str(enterprise_customer_uuid), )
def get_email_from_request(request): """ Helper to get the ``email`` value provided in a request's JWT. """ decoded_jwt = get_decoded_jwt(request) return get_key_from_jwt(decoded_jwt, 'email')
def current_decoded_jwt(): return get_decoded_jwt(crum.get_current_request())