Skip to content

Commit

Permalink
Add extra method in bsdUsers resource to change password
Browse files Browse the repository at this point in the history
  • Loading branch information
william-gr committed Aug 27, 2013
1 parent f20b6d7 commit e51f95c
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion gui/api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from django.utils.translation import ugettext as _

from freenasUI import choices
from freenasUI.account.forms import bsdUserCreationForm
from freenasUI.account.forms import bsdUserCreationForm, bsdUserPasswordForm
from freenasUI.account.models import bsdUsers, bsdGroups
from freenasUI.api.utils import (
DojoResource, DojoModelResource
Expand All @@ -49,6 +49,7 @@
from freenasUI.storage.forms import VolumeManagerForm
from freenasUI.storage.models import Disk, Volume
from tastypie import fields
from tastypie.http import HttpMethodNotAllowed
from tastypie.exceptions import ImmediateHttpResponse
from tastypie.utils import trailing_slash
from tastypie.validation import FormValidation
Expand Down Expand Up @@ -860,6 +861,42 @@ class Meta:
'bsdusr_uid')
validation = FormValidation(form_class=bsdUserCreationForm)

def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/password%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('change_password')),
]

def change_password(self, request, **kwargs):
if request.method != 'POST':
response = HttpMethodNotAllowed('POST')
response['Allow'] = 'POST'
raise ImmediateHttpResponse(response=response)

try:
bundle = self.build_bundle(data={'pk': kwargs['pk']}, request=request)
obj = self.cached_obj_get(bundle=bundle, **self.remove_api_resource_names(kwargs))
except ObjectDoesNotExist:
return HttpGone()
except MultipleObjectsReturned:
return HttpMultipleChoices("More than one resource is found at this URI.")

deserialized = self.deserialize(request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json'))
form = bsdUserPasswordForm(
instance=obj,
data={
'bsdusr_username': obj.bsdusr_username,
'bsdusr_password': deserialized.get('bsdusr_password'),
},
confirm=False,
)
if not form.is_valid():
raise ImmediateHttpResponse(
response=self.error_response(request, form.errors)
)
else:
form.save()
return self.get_detail(request, **kwargs)

def dehydrate(self, bundle):
bundle = super(BsdUserResourceMixin, self).dehydrate(bundle)
bundle.data['bsdusr_group'] = bundle.obj.bsdusr_group.bsdgrp_gid
Expand Down

0 comments on commit e51f95c

Please sign in to comment.