incomplete support to user and project management
Change-Id: I9238ec321a292bb23c5c5797681819794dcfd0f0
Signed-off-by: lombardofr <lombardo@everyup.it>
diff --git a/authosm/__init__.py b/authosm/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/authosm/__init__.py
diff --git a/authosm/admin.py b/authosm/admin.py
new file mode 100644
index 0000000..8c38f3f
--- /dev/null
+++ b/authosm/admin.py
@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.
diff --git a/authosm/apps.py b/authosm/apps.py
new file mode 100644
index 0000000..c3d4506
--- /dev/null
+++ b/authosm/apps.py
@@ -0,0 +1,7 @@
+from __future__ import unicode_literals
+
+from django.apps import AppConfig
+
+
+class AuthosmConfig(AppConfig):
+ name = 'authosm'
diff --git a/authosm/backend.py b/authosm/backend.py
new file mode 100644
index 0000000..1b92156
--- /dev/null
+++ b/authosm/backend.py
@@ -0,0 +1,51 @@
+#
+# Copyright 2018 EveryUP Srl
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from .models import OsmUser
+
+
+class OsmBackend(object):
+
+ def authenticate(self, **kwargs):
+ '''
+ kwargs will receive the python dict that may contain
+ {username, password, project-id} to authenticate
+ '''
+ if all(k in kwargs for k in ('username', 'password', 'project_id')):
+ username = kwargs['username']
+ password = kwargs['password']
+ project_id = kwargs['project_id']
+
+ print username
+ print password
+ print project_id
+
+ try:
+
+ return OsmUser.objects.get(username=username)
+ except OsmUser.DoesNotExist:
+ # Create a new user. There's no need to set a password
+ # we will keep just some preferences
+ user = OsmUser(username=username)
+ user.save()
+ return user
+
+ return None
+
+ def get_user(self, user_id):
+ try:
+ return OsmUser.objects.get(pk=user_id)
+ except OsmUser.DoesNotExist:
+ return None
\ No newline at end of file
diff --git a/authosm/models.py b/authosm/models.py
new file mode 100644
index 0000000..c48f738
--- /dev/null
+++ b/authosm/models.py
@@ -0,0 +1,92 @@
+#
+# Copyright 2018 EveryUP Srl
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from __future__ import unicode_literals
+from django.db import models
+from django.utils.translation import ugettext_lazy as _
+from django.contrib.auth.models import User, AbstractBaseUser, BaseUserManager, PermissionsMixin
+from django.utils import timezone
+from lib.osm.osmclient.client import Client
+import uuid
+
+
+class OsmUserManager(BaseUserManager):
+ """Custom manager for OsmUser."""
+
+ def _create_user(self, username, password, is_staff, is_superuser, **extra_fields):
+ """Create and save a CustomUser with the given username and password. """
+ now = timezone.now()
+
+ if not username:
+ raise ValueError('The given username must be set')
+
+ is_active = extra_fields.pop("is_active", True)
+ user = self.model(username=username, is_staff=is_staff, is_active=is_active,
+ is_superuser=is_superuser, last_login=now,
+ date_joined=now, **extra_fields)
+ user.set_password(password)
+ user.save(using=self._db)
+ return user
+
+ """Create and save an OsmUser with the given username and password."""
+ def create_superuser(self, username, password, **extra_fields):
+ return self._create_user(username, password, True, True, is_admin=True,
+ **extra_fields)
+
+
+class AbstractOsmUser(AbstractBaseUser, PermissionsMixin):
+ """Abstract User with the same behaviour as Django's default User.
+
+
+ Inherits from both the AbstractBaseUser and PermissionMixin.
+
+ The following attributes are inherited from the superclasses:
+ * password
+ * last_login
+ * is_superuser
+
+ """
+ username = models.CharField(_('username'), max_length=255, unique=True, db_index=True)
+ current_project = models.CharField(_('project_id'), max_length=255)
+ token_project = models.CharField(_('token'), max_length=36)
+ is_admin = models.BooleanField(_('admin status'), default=False)
+ is_basic_user = models.BooleanField(_('basic_user status'), default=False)
+
+ objects = OsmUserManager()
+
+ USERNAME_FIELD = 'username'
+ REQUIRED_FIELDS = []
+
+ class Meta:
+ verbose_name = _('custom user')
+ verbose_name_plural = _('custom users')
+ abstract = True
+
+
+
+class OsmUser(AbstractOsmUser):
+ """
+ Concrete class of AbstractCustomUser.
+
+ Use this if you don't need to extend CustomUser.
+
+ """
+
+ class Meta(AbstractOsmUser.Meta):
+ swappable = 'AUTH_USER_MODEL'
+
+ def get_projects(self):
+ client = Client()
+ return []
diff --git a/authosm/tests.py b/authosm/tests.py
new file mode 100644
index 0000000..7ce503c
--- /dev/null
+++ b/authosm/tests.py
@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.
diff --git a/authosm/views.py b/authosm/views.py
new file mode 100644
index 0000000..f97e8e1
--- /dev/null
+++ b/authosm/views.py
@@ -0,0 +1,47 @@
+#
+# Copyright 2018 EveryUP Srl
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+from django.shortcuts import render
+from django.contrib.auth import login, logout, authenticate
+from django.http import HttpResponseRedirect
+import urllib
+
+
+# Create your views here.
+def user_login(request):
+
+ logout(request)
+
+ error_message = ''
+ if request.POST:
+ print request.POST.get('username')
+ print request.POST.get('password')
+ next_page = request.POST.get('next')
+ next_page = urllib.unquote(next_page).decode('iso-8859-2')
+ user = authenticate(username=request.POST.get('username'),
+ password=request.POST.get('password'),
+ project_id=request.POST.get('project_id'))
+
+ if user and user.is_active:
+ if user.is_authenticated():
+ login(request, user)
+ print next_page
+ if next_page == "" or next_page is None:
+ return HttpResponseRedirect('/home')
+ else:
+ return HttpResponseRedirect(next_page)
+ else:
+ error_message = 'Login failed!'
+ return render(request, 'login.html', {'error_message':error_message, 'collapsed_sidebar': False})