2014-09-30 09:49:07 +00:00
|
|
|
from django.forms import widgets
|
2014-10-16 15:11:52 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
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
|
|
|
|
|
2014-10-15 21:18:50 +00:00
|
|
|
from orchestra.api.serializers import HyperlinkedModelSerializer
|
2014-09-30 09:49:07 +00:00
|
|
|
from orchestra.apps.accounts.serializers import AccountSerializerMixin
|
|
|
|
from orchestra.core.validators import validate_password
|
|
|
|
|
2014-09-30 16:39:47 +00:00
|
|
|
from .models import SystemUser
|
2014-09-30 09:49:07 +00:00
|
|
|
|
2014-09-30 16:39:47 +00:00
|
|
|
|
2014-10-16 15:11:52 +00:00
|
|
|
class GroupSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
|
2014-10-07 13:08:59 +00:00
|
|
|
class Meta:
|
|
|
|
model = SystemUser
|
2014-10-16 15:11:52 +00:00
|
|
|
fields = ('url', 'username',)
|
2014-10-07 13:08:59 +00:00
|
|
|
|
|
|
|
def from_native(self, data, files=None):
|
2014-10-16 15:11:52 +00:00
|
|
|
queryset = self.opts.model.objects.filter(account=self.account)
|
|
|
|
return get_object_or_404(queryset, username=data['username'])
|
2014-10-07 13:08:59 +00:00
|
|
|
|
|
|
|
|
2014-10-15 21:18:50 +00:00
|
|
|
class SystemUserSerializer(AccountSerializerMixin, HyperlinkedModelSerializer):
|
2014-09-30 09:49:07 +00:00
|
|
|
password = serializers.CharField(max_length=128, label=_('Password'),
|
|
|
|
validators=[validate_password], write_only=True, required=False,
|
|
|
|
widget=widgets.PasswordInput)
|
2014-10-07 13:08:59 +00:00
|
|
|
groups = GroupSerializer(many=True, allow_add_remove=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 = (
|
2014-11-18 13:59:21 +00:00
|
|
|
'url', 'username', 'password', 'home', 'directory', 'shell', 'groups', 'is_active',
|
2014-09-30 09:49:07 +00:00
|
|
|
)
|
2014-10-15 21:18:50 +00:00
|
|
|
postonly_fields = ('username',)
|
2014-09-30 09:49:07 +00:00
|
|
|
|
2014-11-14 22:19:58 +00:00
|
|
|
def validate(self, attrs):
|
|
|
|
user = SystemUser(
|
|
|
|
username=attrs.get('username') or self.object.username,
|
|
|
|
shell=attrs.get('shell') or self.object.shell,
|
|
|
|
)
|
2014-11-14 23:50:06 +00:00
|
|
|
user.validate_home(attrs, self.account)
|
2014-11-14 22:19:58 +00:00
|
|
|
return attrs
|
|
|
|
|
2014-09-30 09:49:07 +00:00
|
|
|
def validate_password(self, attrs, source):
|
|
|
|
""" POST only password """
|
2014-10-01 16:42:40 +00:00
|
|
|
if self.object:
|
2014-09-30 09:49:07 +00:00
|
|
|
if 'password' in attrs:
|
|
|
|
raise serializers.ValidationError(_("Can not set password"))
|
|
|
|
elif 'password' not in attrs:
|
|
|
|
raise serializers.ValidationError(_("Password required"))
|
|
|
|
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
|
2014-10-07 13:08:59 +00:00
|
|
|
|
2014-09-30 09:49:07 +00:00
|
|
|
def save_object(self, obj, **kwargs):
|
|
|
|
# FIXME this method will be called when saving nested serializers :(
|
|
|
|
if not obj.pk:
|
|
|
|
obj.set_password(obj.password)
|
2014-09-30 16:39:47 +00:00
|
|
|
super(SystemUserSerializer, self).save_object(obj, **kwargs)
|