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
|
|
|
|
2015-07-29 09:05:07 +00:00
|
|
|
def validate_directory(self, directory):
|
|
|
|
return directory.lstrip('/')
|
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
data = super(SystemUserSerializer, self).validate(data)
|
2014-11-14 22:19:58 +00:00
|
|
|
user = SystemUser(
|
2015-07-29 09:05:07 +00:00
|
|
|
username=data.get('username') or self.instance.username,
|
|
|
|
shell=data.get('shell') or self.instance.shell,
|
2014-11-14 22:19:58 +00:00
|
|
|
)
|
2015-07-29 09:05:07 +00:00
|
|
|
validate_home(user, data, self.get_account())
|
|
|
|
groups = data.get('groups')
|
2014-10-16 15:11:52 +00:00
|
|
|
if groups:
|
|
|
|
for group in groups:
|
2015-07-29 09:05:07 +00:00
|
|
|
if group.username == data['username']:
|
2014-10-16 15:11:52 +00:00
|
|
|
raise serializers.ValidationError(
|
|
|
|
_("Do not make the user member of its group"))
|
2015-07-29 09:05:07 +00:00
|
|
|
return data
|