-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
267 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" | ||
serializer for aliyun Role SSO | ||
""" | ||
from rest_framework import serializers | ||
from rest_framework.exceptions import ValidationError | ||
|
||
from common.django.drf.serializer import DynamicFieldsModelSerializer | ||
from oneid_meta.models import AliyunSSORole, User | ||
|
||
|
||
class AliyunSSORoleSerializer(DynamicFieldsModelSerializer): | ||
"""Serializer for Aliyun SSO Role""" | ||
|
||
user_id = serializers.IntegerField(source='user.id', required=True) | ||
role = serializers.ListField(required=False, child=serializers.CharField(allow_blank=False)) | ||
session_duration = serializers.IntegerField(required=False) | ||
is_active = serializers.BooleanField(required=False) | ||
|
||
class Meta: # pylint: disable=missing-docstring | ||
model = AliyunSSORole | ||
|
||
fields = ('user_id', 'role', 'session_duration', 'is_active') | ||
|
||
def create(self, validated_data): | ||
"""create sso role""" | ||
user_id = validated_data.pop('user')['id'] | ||
user = User.valid_objects.filter(id=user_id).first() | ||
role = AliyunSSORole.objects.create(user=user, **validated_data) | ||
return role | ||
|
||
def update(self, instance, validated_data): # pylint: disable=too-many-statements,too-many-branches | ||
"""update sso role""" | ||
role = instance | ||
|
||
user_id = validated_data.pop('user')['id'] | ||
if user_id and user_id != role.user.id: | ||
raise ValidationError({'user_id': ['this field is immutable']}) | ||
|
||
role.__dict__.update(validated_data) | ||
role.save() | ||
return role | ||
|
||
def validate_user_id(self, value): | ||
""" | ||
校验user是否已经配置阿里云角色SSO信息 | ||
""" | ||
if not User.valid_objects.filter(id=value).exists(): | ||
raise ValidationError(['user not existed']) | ||
exclude = {'pk': self.instance.pk} if self.instance else {} | ||
if self.Meta.model.valid_objects.filter(user__id=value).exclude(**exclude).exists(): | ||
raise ValidationError(['aliyun sso role existed']) | ||
return value |
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
Oops, something went wrong.