-
Notifications
You must be signed in to change notification settings - Fork 1
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
12 changed files
with
212 additions
and
8 deletions.
There are no files selected for viewing
Binary file not shown.
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,32 @@ | ||
/** | ||
* Register controller | ||
* @namespace thinkster.authentication.controllers | ||
*/ | ||
(function () { | ||
'use strict'; | ||
|
||
angular | ||
.module('workflow.authentication.controllers') | ||
.controller('RegisterController', RegisterController); | ||
|
||
RegisterController.$inject = ['$location', '$scope', 'Authentication']; | ||
|
||
/** | ||
* @namespace RegisterController | ||
*/ | ||
function RegisterController($location, $scope, Authentication) { | ||
var vm = this; | ||
|
||
vm.register = register; | ||
|
||
/** | ||
* @name register | ||
* @desc Register a new user | ||
* @memberOf thinkster.authentication.controllers.RegisterController | ||
*/ | ||
function register() { | ||
Authentication.register(vm.email, vm.password, vm.username); | ||
} | ||
} | ||
})(); | ||
|
Binary file not shown.
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,8 @@ | ||
from rest_framework import permissions | ||
|
||
|
||
class IsAccountOwner(permissions.BasePermission): | ||
def has_object_permission(self, request, view, account): | ||
if request.user: | ||
return account == request.user | ||
return False |
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,42 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="author" content="Nuthan" /> | ||
<title>Workflow - Authentication</title> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> | ||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> | ||
</head> | ||
|
||
<body> | ||
|
||
<div class="row"> | ||
<div class="col-md-4 col-md-offset-4"> | ||
<h1>Register</h1> | ||
|
||
<div class="well"> | ||
<form role="form" ng-submit="vm.register()"> | ||
<div class="form-group"> | ||
<label for="register__email">Email</label> | ||
<input type="email" class="form-control" id="register__email" ng-model="vm.email" placeholder="ex. [email protected]" /> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label for="register__username">Username</label> | ||
<input type="text" class="form-control" id="register__username" ng-model="vm.username" placeholder="ex. john" /> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label for="register__password">Password</label> | ||
<input type="password" class="form-control" id="register__password" ng-model="vm.password" placeholder="ex. thisisnotgoogleplus" /> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<button type="submit" class="btn btn-primary">Submit</button> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
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,37 @@ | ||
|
||
#from django.contrib.auth import update_session_auth_hash | ||
|
||
from rest_framework import serializers | ||
|
||
from users.models import UserAccount | ||
|
||
|
||
class UserAccountSerializer(serializers.ModelSerializer): | ||
password = serializers.CharField(write_only=True, required=False) | ||
confirm_password = serializers.CharField(write_only=True, required=False) | ||
|
||
class Meta: | ||
model = UserAccount | ||
fields = ('id', 'email', 'username', 'created', 'modified', 'first_name', 'last_name', 'tagline', 'password', 'confirm_password',) | ||
read_only_fields = ('created', 'modified',) | ||
|
||
def create(self, validated_data): | ||
return UserAccount.objects.create(**validated_data) | ||
|
||
def update(self, instance, validated_data): | ||
instance.username = validated_data.get('username', instance.username) | ||
instance.tagline = validated_data.get('tagline', instance.tagline) | ||
|
||
instance.save() | ||
|
||
password = validated_data.get('password', None) | ||
confirm_password = validated_data.get('confirm_password', None) | ||
|
||
if password and confirm_password and password == confirm_password: | ||
instance.set_password(password) | ||
instance.save() | ||
|
||
# this is required to update the seesion otherwise user has to loing again | ||
#update_session_auth_hash(self.context.get('request'), instance) | ||
return instance | ||
|
Binary file not shown.
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,48 @@ | ||
/** | ||
* Authentication | ||
* @namespace thinkster.authentication.services | ||
*/ | ||
(function () { | ||
'use strict'; | ||
|
||
angular | ||
.module('workflow.authentication.services') | ||
.factory('Authentication', Authentication); | ||
|
||
Authentication.$inject = ['$cookies', '$http']; | ||
|
||
/** | ||
* @namespace Authentication | ||
* @returns {Factory} | ||
*/ | ||
function Authentication($cookies, $http) { | ||
/** | ||
* @name Authentication | ||
* @desc The Factory to be returned | ||
*/ | ||
var Authentication = { | ||
register: register | ||
}; | ||
|
||
return Authentication; | ||
|
||
//////////////////// | ||
|
||
/** | ||
* @name register | ||
* @desc Try to register a new user | ||
* @param {string} username The username entered by the user | ||
* @param {string} password The password entered by the user | ||
* @param {string} email The email entered by the user | ||
* @returns {Promise} | ||
* @memberOf thinkster.authentication.services.Authentication | ||
*/ | ||
function register(email, password, username) { | ||
return $http.post('/api/v1/accounts/', { | ||
username: username, | ||
password: password, | ||
email: email | ||
}); | ||
} | ||
} | ||
})(); |
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 |
---|---|---|
@@ -1,3 +1,35 @@ | ||
from django.shortcuts import render | ||
from rest_framework import permissions, viewsets | ||
|
||
from users.models import UserAccount | ||
from users.permissions import IsAccountOwner | ||
from users.serializers import UserAccountSerializer | ||
|
||
# Create your views here. | ||
|
||
class AccountViewSet(viewsets.ModelViewSet): | ||
lookup_field = 'username' | ||
queryset = Account.objects.all() | ||
serializer_class = UserAccountSerializer | ||
|
||
def get_permissions(self): | ||
if self.request.method in permissions.SAFE_METHODS: | ||
return (permissions.AllowAny(),) | ||
|
||
if self.request.method == 'POST': | ||
return (permissions.AllowAny(),) | ||
|
||
return (permissions.IsAuthenticated(), IsAccountOwner(),) | ||
|
||
def create(self, request): | ||
serializer = self.serializer_class(data=request.data) | ||
|
||
if serializer.is_valid(): | ||
UserAccount.objects.create_user(**serializer.validated_data) | ||
|
||
return Response(serializer.validated_data, status=status.HTTP_201_CREATED) | ||
|
||
return Response({ | ||
'status': 'Bad request', | ||
'message': 'Account could not be created with received data.' | ||
}, status=status.HTTP_400_BAD_REQUEST) |
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
Binary file not shown.
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
from django.conf.urls import patterns, include, url | ||
# .. Imports | ||
from rest_framework_nested import routers | ||
|
||
from django.contrib import admin | ||
admin.autodiscover() | ||
from authentication.views import AccountViewSet | ||
|
||
urlpatterns = patterns('', | ||
# Examples: | ||
# url(r'^$', 'workflow.views.home', name='home'), | ||
# url(r'^blog/', include('blog.urls')), | ||
router = routers.SimpleRouter() | ||
router.register(r'accounts', AccountViewSet) | ||
|
||
url(r'^admin/', include(admin.site.urls)), | ||
urlpatterns = patterns( | ||
'', | ||
# ... URLs | ||
url(r'^api/v1/', include(router.urls)), | ||
|
||
url('^.*$', IndexView.as_view(), name='index'), | ||
) |