-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/add_csp
- Loading branch information
Showing
26 changed files
with
1,215 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 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
Empty file.
This file contains 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,82 @@ | ||
import requests | ||
import json | ||
from typing import List, Dict | ||
|
||
from django.conf import settings | ||
|
||
|
||
class ConnectRESTClient: | ||
def __init__(self): | ||
self.base_url = settings.CONNECT_API_URL | ||
self.headers = { | ||
"Content-Type": "application/json; charset: utf-8", | ||
"Authorization": self.get_auth_token(), | ||
} | ||
|
||
def get_auth_token(self) -> str: | ||
request = requests.post( | ||
url=settings.OIDC_OP_TOKEN_ENDPOINT, | ||
data={ | ||
"client_id": settings.OIDC_RP_CLIENT_ID, | ||
"client_secret": settings.OIDC_RP_CLIENT_SECRET, | ||
"grant_type": "client_credentials", | ||
}, | ||
) | ||
token = request.json().get("access_token") | ||
return f"Bearer {token}" | ||
|
||
def list_classifiers( | ||
self, project_uuid: str, user_email: str | ||
) -> List[Dict[str, str]]: | ||
request = requests.get( | ||
url=f"{self.base_url}/v1/organization/project/list_classifier/", | ||
headers=self.headers, | ||
params={"project_uuid": project_uuid, "user_email": user_email}, | ||
) | ||
|
||
return request.json() | ||
|
||
def list_authorizations(self, project_uuid: str, user_email: str) -> List[str]: | ||
classifiers = self.list_classifiers( | ||
project_uuid=project_uuid, user_email=user_email | ||
) | ||
|
||
return [classifier.get("authorization_uuid") for classifier in classifiers] | ||
|
||
def get_authorization_classifier( | ||
self, project_uuid: str, authorization_uuid: str, user_email: str | ||
) -> str: | ||
""" | ||
Recives a authorization UUID and returns the respective classifier UUID | ||
""" | ||
classifiers = self.list_classifiers(project_uuid, user_email) | ||
classifier = filter( | ||
lambda classifier: classifier["authorization_uuid"] == authorization_uuid, | ||
classifiers, | ||
) | ||
|
||
return classifier.get("uuid") | ||
|
||
def remove_authorization( | ||
self, project_uuid: str, authorization_uuid: str, user_email: str | ||
): | ||
classifier_uuid = self.get_authorization_classifier( | ||
project_uuid, | ||
authorization_uuid, | ||
user_email, | ||
) | ||
request = requests.delete( | ||
url=f"{self.base_url}/v1/organization/project/destroy_classifier/", | ||
headers=self.headers, | ||
json=json.dumps({"uuid": classifier_uuid, "user_email": user_email}), | ||
) | ||
|
||
return request.json() | ||
|
||
def create_classifier(self, **kwargs): | ||
request = requests.post( | ||
url=f"{self.base_url}/v1/organization/project/create_classifier/", | ||
headers=self.headers, | ||
json=json.dumps({**kwargs, "classifier_type": "bothub"}), | ||
) | ||
return request.json() |
Empty file.
This file contains 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,11 @@ | ||
from rest_framework import permissions | ||
|
||
from bothub.utils import get_user | ||
|
||
|
||
class InternalOrganizationAdminHasPermission(permissions.BasePermission): | ||
def has_object_permission(self, request, view, obj): | ||
authorization = obj.organization.get_organization_authorization( | ||
get_user(request.query_params.get("user_email", None)) | ||
) | ||
return authorization.is_admin |
This file contains 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,83 @@ | ||
from django.db.models import F | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from rest_framework import serializers | ||
from rest_framework.exceptions import ValidationError | ||
|
||
from bothub.authentication.models import User | ||
from bothub.common.models import Organization, OrganizationAuthorization | ||
|
||
|
||
class OrganizationSerializer(serializers.ModelSerializer): | ||
|
||
users = serializers.SerializerMethodField() | ||
|
||
def get_users(self, org: Organization): | ||
return list( | ||
org.organization_authorizations.exclude( | ||
role=OrganizationAuthorization.LEVEL_NOTHING | ||
) | ||
.annotate( | ||
org_user_id=F("user__user_owner__pk"), | ||
org_user_email=F("user__user_owner__email"), | ||
org_user_nickname=F("user__user_owner__nickname"), | ||
org_user_name=F("user__user_owner__name"), | ||
) | ||
.values( | ||
"org_user_id", "org_user_email", "org_user_nickname", "org_user_name" | ||
) | ||
) | ||
|
||
class Meta: | ||
model = Organization | ||
fields = ["id", "name", "users"] | ||
|
||
|
||
class OrgCreateSerializer(serializers.ModelSerializer): | ||
|
||
organization_name = serializers.CharField() | ||
user_email = serializers.CharField() | ||
|
||
def validate_user_email(self, value: str) -> str: | ||
try: | ||
User.objects.get(email=value) | ||
except User.DoesNotExist: | ||
raise ValidationError(_("{} not found!").format(value)) | ||
|
||
return value | ||
|
||
class Meta: | ||
model = Organization | ||
fields = ["organization_name", "user_email"] | ||
|
||
|
||
class OrgUpdateSerializer(serializers.ModelSerializer): | ||
|
||
id = serializers.IntegerField() | ||
name = serializers.CharField(required=False) | ||
|
||
def validate_id(self, value): | ||
try: | ||
Organization.objects.get(pk=value) | ||
except Organization.DoesNotExist: | ||
raise ValidationError(f"{value} not found!") | ||
|
||
return value | ||
|
||
def save(self): | ||
data = dict(self.validated_data) | ||
|
||
org = Organization.objects.get(pk=data.get("id")) | ||
|
||
updated_fields = self.get_updated_fields(data) | ||
|
||
if updated_fields: | ||
org.__dict__.update(**updated_fields) | ||
org.save() | ||
|
||
def get_updated_fields(self, data): | ||
return {key: value for key, value in data.items() if key not in ["id"]} | ||
|
||
class Meta: | ||
model = Organization | ||
fields = ["id", "name"] |
Oops, something went wrong.