-
Notifications
You must be signed in to change notification settings - Fork 10
Edit prompt template from studio #183
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4cce2c5
feat: adding backend API to list all profiles within the current scope
felipemontoya 2d54a9b
feat: adding a UI to show the list of profiles
felipemontoya 9909628
feat: adding scopes to each profile in the list
felipemontoya 984abb8
feat: showing scopes in the UI
felipemontoya 67f81df
feat: showing prompt templates in studio and allowing updates
felipemontoya e202981
feat: moving permission checks to its own file and adding granularity…
felipemontoya 17c3d62
feat: addressing feedback to not use authz, but courseacessrole instead
felipemontoya e44009b
feat: sending the course context from the UI
felipemontoya bc8c216
fix: addressing comments made by qa process
felipemontoya 2ab2e0a
feat: moving all user facing strings to i18n messages
felipemontoya 25e9738
feat: making the number of profiles affected by a template change ava…
felipemontoya 6e1ae6e
fix: all the qa addressed
felipemontoya c754525
fix: addressing comments by @henrrypg
felipemontoya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
backend/openedx_ai_extensions/api/v1/workflows/permissions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| """ | ||
| DRF permission classes and shared request utilities for AI Workflows API. | ||
| """ | ||
|
|
||
| import json | ||
|
|
||
| from django.core.exceptions import ValidationError | ||
| from opaque_keys import InvalidKeyError | ||
| from opaque_keys.edx.keys import CourseKey, UsageKey | ||
| from rest_framework.permissions import BasePermission | ||
|
|
||
| try: | ||
| from openedx_authz import api as authz_api | ||
| except ImportError: | ||
| authz_api = None | ||
|
|
||
| _COURSE_ADVANCED_SETTINGS_ACTION = "courses.manage_advanced_settings" | ||
|
|
||
|
|
||
| def get_context_from_request(request): | ||
| """ | ||
| Extract and validate context from request query parameters. | ||
|
|
||
| Validates course_id and location_id formats using Open edX opaque_keys. | ||
| Returns a dict with snake_case keys. | ||
|
|
||
| Args: | ||
| request: Django request object with query parameters | ||
|
|
||
| Returns: | ||
| dict: Context with validated course_id and location_id in snake_case | ||
|
|
||
| Raises: | ||
| ValidationError: If course_id or location_id are invalid | ||
| """ | ||
| if hasattr(request, "GET"): | ||
| context_str = request.GET.get("context", "{}") | ||
| else: | ||
| context_str = request.query_params.get("context", "{}") | ||
|
|
||
| try: | ||
| context = json.loads(context_str) | ||
| except json.JSONDecodeError as e: | ||
| raise ValidationError("Invalid JSON format in 'context' parameter.") from e | ||
| validated_context = {} | ||
|
|
||
| # Validate and convert courseId to course_id | ||
| course_id_raw = context.get("courseId") or context.get("course_id") | ||
| if course_id_raw: | ||
| try: | ||
| CourseKey.from_string(course_id_raw) | ||
| validated_context["course_id"] = course_id_raw | ||
| except InvalidKeyError as e: | ||
| raise ValidationError(f"Invalid course_id format: {course_id_raw}") from e | ||
|
|
||
| # Validate and convert locationId to location_id | ||
|
felipemontoya marked this conversation as resolved.
Outdated
|
||
| location_id_raw = context.get("locationId") or context.get("location_id") | ||
| if location_id_raw: | ||
| try: | ||
| UsageKey.from_string(location_id_raw) | ||
| validated_context["location_id"] = location_id_raw | ||
| except InvalidKeyError as e: | ||
| raise ValidationError(f"Invalid location_id format: {location_id_raw}") from e | ||
|
|
||
| # Pass ui_slot_selector_id as-is (plain string, no special validation needed) | ||
|
felipemontoya marked this conversation as resolved.
Outdated
|
||
| ui_slot_selector_id_raw = context.get("uiSlotSelectorId") or context.get("ui_slot_selector_id") | ||
| if ui_slot_selector_id_raw: | ||
| validated_context["ui_slot_selector_id"] = str(ui_slot_selector_id_raw) | ||
|
|
||
| return validated_context | ||
|
|
||
|
|
||
| class CourseAdvancedSettingsPermission(BasePermission): | ||
| """ | ||
| Restricts access to users who are authorised to manage advanced settings | ||
| for a course — roughly equivalent to the course instructor/admin role. | ||
|
|
||
| This permission is intentionally written to be forward-compatible with the | ||
| openedx-authz RBAC system introduced in Ulmo. The behaviour differs by | ||
| platform, but the intent is the same on both: | ||
|
|
||
| * **Teak and earlier** (openedx-authz not installed): falls back to | ||
| Django's ``is_staff`` flag, which is the coarsest available gate on | ||
| platforms that do not yet ship openedx-authz. | ||
|
|
||
| * **Ulmo and later** (openedx-authz installed): enforces | ||
| ``courses.manage_advanced_settings`` via the Casbin policy engine, | ||
| scoped to the course identified by the ``context`` query param. | ||
| Staff and superusers are always allowed regardless of policy. | ||
| Requests without a valid ``course_id`` in context are denied. | ||
| """ | ||
|
|
||
| def has_permission(self, request, view): | ||
| if not request.user or not request.user.is_authenticated: | ||
| return False | ||
|
|
||
| if authz_api is None: | ||
| return bool(request.user.is_staff) | ||
|
felipemontoya marked this conversation as resolved.
Outdated
|
||
|
|
||
| if request.user.is_staff or request.user.is_superuser: | ||
| return True | ||
|
|
||
| try: | ||
| context = get_context_from_request(request) | ||
| except ValidationError: | ||
| return False | ||
|
|
||
| course_id = context.get("course_id") | ||
| if not course_id: | ||
| return False | ||
|
|
||
| try: | ||
| return authz_api.is_user_allowed( | ||
|
felipemontoya marked this conversation as resolved.
Outdated
|
||
| request.user.username, | ||
| _COURSE_ADVANCED_SETTINGS_ACTION, | ||
| course_id, | ||
| ) | ||
| except Exception: # pylint: disable=broad-exception-caught | ||
| return False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.