-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'core-auth' of 'https://github.com/jjmerchante/grimoirel…
- Loading branch information
Showing
11 changed files
with
237 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) GrimoireLab Contributors | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
|
||
from django.conf import settings | ||
from rest_framework.permissions import BasePermission | ||
|
||
|
||
class IsAuthenticated(BasePermission): | ||
""" | ||
Allows access only to authenticated users. | ||
When `GRIMOIRELAB_AUTHENTICATION_REQUIRED` setting is False it always has permissions. | ||
""" | ||
def has_permission(self, request, view): | ||
if not settings.GRIMOIRELAB_AUTHENTICATION_REQUIRED: | ||
return True | ||
|
||
return bool(request.user and request.user.is_authenticated) |
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
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
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,50 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) GrimoireLab Contributors | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
|
||
from django.contrib.auth import authenticate, login | ||
from rest_framework import permissions | ||
from rest_framework.decorators import ( | ||
api_view, | ||
permission_classes, | ||
) | ||
from rest_framework.response import Response | ||
|
||
|
||
@api_view(['POST']) | ||
@permission_classes([permissions.AllowAny]) | ||
def api_login(request): | ||
username = request.data.get('username') | ||
password = request.data.get('password') | ||
|
||
if username is None or password is None: | ||
return Response({'detail': 'Please provide username and password.'}, status=400) | ||
|
||
user = authenticate(request, username=username, password=password) | ||
|
||
if user is None: | ||
response = { | ||
'errors': 'Invalid credentials.' | ||
} | ||
return Response(response, status=403) | ||
else: | ||
login(request, user) | ||
response = { | ||
'user': username, | ||
'isAdmin': user.is_superuser, | ||
} | ||
return Response(response) |
Oops, something went wrong.