-
-
Notifications
You must be signed in to change notification settings - Fork 695
Crp certificate issuer #5446
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
Open
anurag2787
wants to merge
24
commits into
OWASP:feature/contributor-recognition-program
Choose a base branch
from
anurag2787:crp-certificate-issuer
base: feature/contributor-recognition-program
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Crp certificate issuer #5446
Changes from 8 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8271ab7
Updated crp-model to include deafult certificate
anurag2787 d9e205d
address review
anurag2787 2b46bcf
Address review
anurag2787 9a7060c
Updated order
anurag2787 e47a422
fixed link display
anurag2787 a205c2c
Merge branch 'feature/contributor-recognition-program' into crp-model…
anurag2787 4dd3367
Added Certificate Issuer for Leader
anurag2787 bafa332
Address coderabbit review
anurag2787 f047c86
Address review
anurag2787 acaf3be
fixed formatiing
anurag2787 1fc37e8
Address review
anurag2787 41d4aeb
Update
anurag2787 6949be5
Fixed project key not taking
anurag2787 309eeb9
fixed sonar issue
anurag2787 82d7a64
address review
anurag2787 bafa3a9
Added frontend
anurag2787 18f8970
Updated the frontend to limit body and title text
anurag2787 28c1116
Address review
anurag2787 be28096
Fixed
anurag2787 0a50d4b
move the certificate under /my
anurag2787 57dfdd4
Merge branch 'feature/contributor-recognition-program' into crp-certi…
anurag2787 da929e8
Updated the certificate to plural
anurag2787 51edc06
Added validation
anurag2787 920ed61
Merge branch 'feature/contributor-recognition-program' into crp-certi…
anurag2787 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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,3 @@ | ||
| """OWASP GraphQL mutations.""" | ||
|
|
||
| from .certificate import CertificateMutation |
154 changes: 154 additions & 0 deletions
154
backend/src/apps/owasp/api/internal/mutations/certificate.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,154 @@ | ||
| """OWASP Certificate GraphQL Mutations.""" | ||
|
|
||
| import logging | ||
| import operator | ||
| from functools import reduce | ||
|
|
||
| import strawberry | ||
| from django.core.exceptions import PermissionDenied, ValidationError | ||
| from django.db import transaction | ||
| from django.db.models import Q | ||
| from graphql import GraphQLError | ||
|
|
||
| from apps.github.models.user import User as GithubUser | ||
| from apps.nest.api.internal.permissions import IsAuthenticated | ||
| from apps.owasp.api.internal.nodes.certificate import CertificateNode | ||
| from apps.owasp.models.chapter import Chapter | ||
| from apps.owasp.models.crp.certificate import Certificate | ||
| from apps.owasp.models.project import Project | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @strawberry.input | ||
| class IssueCertificateInput: | ||
| """Input type for issuing a certificate.""" | ||
|
|
||
| recipient_login: str | None = None | ||
| recipient_logins: list[str] | None = None | ||
| title: str | ||
| message: str = "" | ||
| project_key: str | None = None | ||
| chapter_key: str | None = None | ||
|
|
||
|
|
||
| MAX_TITLE_LENGTH = 255 | ||
|
|
||
|
|
||
| @strawberry.type | ||
| class CertificateMutation: | ||
| """GraphQL mutations related to certificates.""" | ||
|
|
||
| @strawberry.mutation(permission_classes=[IsAuthenticated]) | ||
| @transaction.atomic | ||
| def issue_certificate( | ||
| self, info: strawberry.Info, input_data: IssueCertificateInput | ||
| ) -> list[CertificateNode]: | ||
| """Issue generic certificates to contributors (project or chapter leaders only).""" | ||
| user = info.context.request.user | ||
|
|
||
| if not user.github_user or ( | ||
|
anurag2787 marked this conversation as resolved.
Outdated
anurag2787 marked this conversation as resolved.
Outdated
|
||
| not user.github_user.is_project_leader and not user.github_user.chapters.exists() | ||
| ): | ||
| msg = "You must be a project leader or chapter leader to issue certificates." | ||
| logger.warning( | ||
| "Permission denied for user '%s' to issue a certificate.", | ||
| user.username, | ||
| ) | ||
| raise PermissionDenied(msg) | ||
|
anurag2787 marked this conversation as resolved.
Outdated
|
||
|
|
||
| logins = [] | ||
| if input_data.recipient_logins: | ||
| logins = [ | ||
| login.strip() for login in input_data.recipient_logins if login and login.strip() | ||
| ] | ||
| elif input_data.recipient_login and input_data.recipient_login.strip(): | ||
| logins = [input_data.recipient_login.strip()] | ||
|
|
||
| if not logins: | ||
| msg = "Recipient login cannot be empty." | ||
| raise ValidationError(msg) | ||
|
|
||
| title = input_data.title.strip() | ||
| if not title: | ||
| msg = "Certificate title cannot be empty." | ||
| raise ValidationError(msg) | ||
|
|
||
| if len(title) > MAX_TITLE_LENGTH: | ||
| msg = "Certificate title cannot exceed 255 characters." | ||
| raise ValidationError(msg) | ||
|
|
||
| has_project = bool(input_data.project_key and input_data.project_key.strip()) | ||
| has_chapter = bool(input_data.chapter_key and input_data.chapter_key.strip()) | ||
|
|
||
| if has_project and has_chapter: | ||
| msg = "Provide either project or chapter, not both." | ||
| raise ValidationError(msg) | ||
|
|
||
| if not has_project and not has_chapter: | ||
| msg = "Either project or chapter must be provided." | ||
| raise ValidationError(msg) | ||
|
|
||
| project = None | ||
| chapter = None | ||
|
|
||
| if input_data.project_key: | ||
|
anurag2787 marked this conversation as resolved.
Outdated
|
||
| clean_p = input_data.project_key.strip().removeprefix("www-project-") | ||
| try: | ||
| project = Project.objects.get(key=f"www-project-{clean_p}") | ||
| except Project.DoesNotExist as err: | ||
| msg = f"Project with key '{input_data.project_key}' not found." | ||
| raise GraphQLError( | ||
| msg, | ||
| extensions={"code": "NOT_FOUND", "field": "projectKey"}, | ||
| ) from err | ||
|
|
||
| if input_data.chapter_key: | ||
| clean_c = input_data.chapter_key.strip().removeprefix("www-chapter-") | ||
| try: | ||
| chapter = Chapter.objects.get(key=f"www-chapter-{clean_c}") | ||
| except Chapter.DoesNotExist as err: | ||
| msg = f"Chapter with key '{input_data.chapter_key}' not found." | ||
| raise GraphQLError( | ||
| msg, | ||
| extensions={"code": "NOT_FOUND", "field": "chapterKey"}, | ||
| ) from err | ||
|
|
||
| filter_q = reduce(operator.or_, (Q(login__iexact=login_name) for login_name in logins)) | ||
| recipients = GithubUser.objects.filter(filter_q) | ||
| found = {r.login.lower(): r for r in recipients} | ||
|
|
||
| missing = [login_name for login_name in logins if login_name.lower() not in found] | ||
| if missing: | ||
| msg = ( | ||
| f"GitHub user '{missing[0]}' not found." | ||
| if len(missing) == 1 | ||
| else f"GitHub users not found: {', '.join(missing)}." | ||
| ) | ||
| logger.warning("GitHub user(s) not found: %s", ", ".join(missing)) | ||
| raise GraphQLError( | ||
| msg, | ||
| extensions={"code": "NOT_FOUND", "field": "recipientLogins"}, | ||
| ) | ||
|
|
||
| certificates = [] | ||
| for recipient_login in logins: | ||
| recipient = found[recipient_login.lower()] | ||
| certificate = Certificate.objects.create( | ||
| recipient=recipient, | ||
| issuer=user.github_user, | ||
| title=title, | ||
| message=input_data.message.strip(), | ||
| project=project, | ||
| chapter=chapter, | ||
| ) | ||
|
anurag2787 marked this conversation as resolved.
|
||
| certificates.append(certificate) | ||
|
|
||
| logger.info( | ||
| "User '%s' issued certificate '%s' to '%s'.", | ||
| user.username, | ||
| certificate.title, | ||
| recipient.login, | ||
| ) | ||
|
|
||
| return certificates | ||
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 |
|---|---|---|
| @@ -1,33 +1,66 @@ | ||
| """OWASP Certificate GraphQL node.""" | ||
|
|
||
| from typing import TYPE_CHECKING, Annotated | ||
|
|
||
| import strawberry | ||
| import strawberry_django | ||
|
|
||
| from apps.github.api.internal.nodes.user import UserNode | ||
| from apps.owasp.models.crp.certificate import Certificate | ||
|
|
||
| if TYPE_CHECKING: | ||
| from apps.owasp.api.internal.nodes.chapter import ChapterNode | ||
| from apps.owasp.api.internal.nodes.project import ProjectNode | ||
|
|
||
|
|
||
| @strawberry_django.type( | ||
| Certificate, | ||
| fields=[ | ||
| "id", | ||
| "issued_at", | ||
| "message", | ||
| "score", | ||
| "title", | ||
| ], | ||
| ) | ||
| class CertificateNode: | ||
| """Certificate node.""" | ||
|
|
||
| @strawberry_django.field(select_related=["github_user"]) | ||
| @strawberry_django.field(select_related=["chapter"]) | ||
| def chapter( | ||
| self, root: Certificate | ||
| ) -> Annotated["ChapterNode", strawberry.lazy("apps.owasp.api.internal.nodes.chapter")] | None: | ||
| """Resolve associated chapter.""" | ||
| return root.chapter | ||
|
|
||
| @strawberry_django.field(select_related=["recipient"]) | ||
|
anurag2787 marked this conversation as resolved.
|
||
| def github_user(self, root: Certificate) -> UserNode: | ||
| """Resolve the associated GitHub user.""" | ||
| return root.github_user | ||
| """Resolve the associated GitHub user (alias for recipient).""" | ||
| return root.recipient | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| @strawberry_django.field | ||
| def is_verified(self, root: Certificate) -> bool: | ||
| """Resolve whether the certificate is active/verified.""" | ||
| return root.is_verified | ||
|
|
||
| @strawberry_django.field(select_related=["issuer"]) | ||
| def issuer(self, root: Certificate) -> UserNode | None: | ||
| """Resolve the issuer user.""" | ||
| return root.issuer | ||
|
|
||
| @strawberry_django.field(select_related=["project"]) | ||
| def project( | ||
| self, root: Certificate | ||
| ) -> Annotated["ProjectNode", strawberry.lazy("apps.owasp.api.internal.nodes.project")] | None: | ||
| """Resolve associated project.""" | ||
| return root.project | ||
|
|
||
| @strawberry_django.field(select_related=["recipient"]) | ||
| def recipient(self, root: Certificate) -> UserNode: | ||
| """Resolve the recipient user.""" | ||
| return root.recipient | ||
|
|
||
| @strawberry_django.field | ||
| def tier(self, root: Certificate) -> str: | ||
| """Resolve the human-readable tier level (e.g. 'Level 1').""" | ||
| return root.get_tier_display() | ||
| return root.get_tier_display() if root.tier else "" | ||
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.