2015-04-04 18:10:39 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2014-09-30 09:49:07 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2015-05-18 15:21:42 +00:00
|
|
|
from orchestra.api.serializers import SetPasswordHyperlinkedSerializer, RelatedHyperlinkedModelSerializer
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.accounts.serializers import AccountSerializerMixin
|
2014-09-30 09:49:07 +00:00
|
|
|
|
2014-09-30 16:39:47 +00:00
|
|
|
from .models import SystemUser
|
2015-04-05 22:34:47 +00:00
|
|
|
from .validators import validate_home
|
2014-09-30 09:49:07 +00:00
|
|
|
|
2014-09-30 16:39:47 +00:00
|
|
|
|
2015-05-18 15:21:42 +00:00
|
|
|
class RelatedGroupSerializer(AccountSerializerMixin, RelatedHyperlinkedModelSerializer):
|
2014-10-07 13:08:59 +00:00
|
|
|
class Meta:
|
|
|
|
model = SystemUser
|
2015-04-30 09:51:55 +00:00
|
|
|
fields = ('url', 'id', 'username',)
|
2014-10-07 13:08:59 +00:00
|
|
|
|
|
|
|
|
2015-04-23 14:34:04 +00:00
|
|
|
class SystemUserSerializer(AccountSerializerMixin, SetPasswordHyperlinkedSerializer):
|
2015-05-18 15:21:42 +00:00
|
|
|
groups = RelatedGroupSerializer(many=True, required=False)
|
2014-09-30 09:49:07 +00:00
|
|
|
|
|
|
|
class Meta:
|
2014-09-30 16:39:47 +00:00
|
|
|
model = SystemUser
|
2014-09-30 09:49:07 +00:00
|
|
|
fields = (
|
2015-04-30 09:51:55 +00:00
|
|
|
'url', 'id', 'username', 'password', 'home', 'directory', 'shell', 'groups', 'is_active',
|
2014-09-30 09:49:07 +00:00
|
|
|
)
|
2015-04-23 14:34:04 +00:00
|
|
|
postonly_fields = ('username', 'password')
|
2014-09-30 09:49:07 +00:00
|
|
|
|
2014-11-14 22:19:58 +00:00
|
|
|
def validate(self, attrs):
|
2015-04-23 14:34:04 +00:00
|
|
|
attrs = super(SystemUserSerializer, self).validate(attrs)
|
2014-11-14 22:19:58 +00:00
|
|
|
user = SystemUser(
|
2015-04-23 14:34:04 +00:00
|
|
|
username=attrs.get('username') or self.instance.username,
|
|
|
|
shell=attrs.get('shell') or self.instance.shell,
|
2014-11-14 22:19:58 +00:00
|
|
|
)
|
2015-05-18 15:21:42 +00:00
|
|
|
validate_home(user, attrs, self.get_account())
|
2014-11-14 22:19:58 +00:00
|
|
|
return attrs
|
|
|
|
|
2014-10-16 15:11:52 +00:00
|
|
|
def validate_groups(self, attrs, source):
|
|
|
|
groups = attrs.get(source)
|
|
|
|
if groups:
|
|
|
|
for group in groups:
|
|
|
|
if group.username == attrs['username']:
|
|
|
|
raise serializers.ValidationError(
|
|
|
|
_("Do not make the user member of its group"))
|
|
|
|
return attrs
|