| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2018 EveryUP Srl |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | from __future__ import unicode_literals |
| 17 | from django.db import models |
| 18 | from django.utils.translation import ugettext_lazy as _ |
| 19 | from django.contrib.auth.models import User, AbstractBaseUser, BaseUserManager, PermissionsMixin |
| 20 | from django.utils import timezone |
| lombardofr | 99f922f | 2018-07-17 17:27:36 +0200 | [diff] [blame] | 21 | |
| 22 | from authosm.exceptions import OSMAuthException |
| 23 | from lib.osm.osmclient.clientv2 import Client |
| 24 | import utils |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class OsmUserManager(BaseUserManager): |
| 28 | """Custom manager for OsmUser.""" |
| 29 | |
| 30 | def _create_user(self, username, password, is_staff, is_superuser, **extra_fields): |
| 31 | """Create and save a CustomUser with the given username and password. """ |
| 32 | now = timezone.now() |
| 33 | |
| 34 | if not username: |
| 35 | raise ValueError('The given username must be set') |
| 36 | |
| 37 | is_active = extra_fields.pop("is_active", True) |
| 38 | user = self.model(username=username, is_staff=is_staff, is_active=is_active, |
| 39 | is_superuser=is_superuser, last_login=now, |
| 40 | date_joined=now, **extra_fields) |
| 41 | user.set_password(password) |
| 42 | user.save(using=self._db) |
| 43 | return user |
| 44 | |
| 45 | """Create and save an OsmUser with the given username and password.""" |
| 46 | def create_superuser(self, username, password, **extra_fields): |
| 47 | return self._create_user(username, password, True, True, is_admin=True, |
| 48 | **extra_fields) |
| 49 | |
| 50 | |
| 51 | class AbstractOsmUser(AbstractBaseUser, PermissionsMixin): |
| 52 | """Abstract User with the same behaviour as Django's default User. |
| 53 | |
| 54 | |
| 55 | Inherits from both the AbstractBaseUser and PermissionMixin. |
| 56 | |
| 57 | The following attributes are inherited from the superclasses: |
| 58 | * password |
| 59 | * last_login |
| 60 | * is_superuser |
| 61 | |
| 62 | """ |
| lombardofr | 99f922f | 2018-07-17 17:27:36 +0200 | [diff] [blame] | 63 | username = models.CharField(_('username'), primary_key=True, max_length=255, unique=True, db_index=True) |
| 64 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 65 | is_admin = models.BooleanField(_('admin status'), default=False) |
| 66 | is_basic_user = models.BooleanField(_('basic_user status'), default=False) |
| lombardofr | 99f922f | 2018-07-17 17:27:36 +0200 | [diff] [blame] | 67 | current_project = models.CharField(_('project_id'), max_length=255) |
| 68 | |
| 69 | psw = models.CharField(_('psw'), max_length=36) |
| tierno | fab2fa4 | 2020-02-17 10:20:17 +0000 | [diff] [blame^] | 70 | token = models.CharField(_('token'), max_length=512) |
| lombardofr | 99f922f | 2018-07-17 17:27:36 +0200 | [diff] [blame] | 71 | project_id = models.CharField(_('project_id'), max_length=36) |
| lombardofr | e5a130a | 2019-07-15 09:17:59 +0200 | [diff] [blame] | 72 | project_name = models.CharField(_('project_name'), max_length=36, default='') |
| lombardofr | 99f922f | 2018-07-17 17:27:36 +0200 | [diff] [blame] | 73 | token_expires = models.FloatField(_('token_expires'), max_length=36) |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 74 | |
| 75 | objects = OsmUserManager() |
| 76 | |
| 77 | USERNAME_FIELD = 'username' |
| 78 | REQUIRED_FIELDS = [] |
| 79 | |
| lombardofr | 99f922f | 2018-07-17 17:27:36 +0200 | [diff] [blame] | 80 | @property |
| 81 | def is_authenticated(self): |
| 82 | """Checks for a valid authentication.""" |
| 83 | if self.token is not None and utils.is_token_valid({'expires': self.token_expires}): |
| 84 | return True |
| 85 | else: |
| 86 | return False |
| 87 | |
| 88 | def get_token(self): |
| 89 | if self.is_authenticated: |
| 90 | return {'id': self.token, 'expires': self.token_expires, 'project_id': self.project_id} |
| 91 | return None |
| 92 | |
| lombardofr | 835a0b7 | 2018-07-18 15:26:16 +0200 | [diff] [blame] | 93 | def get_projects(self): |
| 94 | client = Client() |
| lombardofr | e5a130a | 2019-07-15 09:17:59 +0200 | [diff] [blame] | 95 | user_info = client.get_user_info(self.get_token(), self.username) |
| lombardofr | 5bc5ebb | 2019-08-19 09:33:56 +0200 | [diff] [blame] | 96 | if 'error' in user_info and user_info['error'] is True: |
| lombardofr | 835a0b7 | 2018-07-18 15:26:16 +0200 | [diff] [blame] | 97 | return [] |
| 98 | else: |
| lombardofr | 5bc5ebb | 2019-08-19 09:33:56 +0200 | [diff] [blame] | 99 | return user_info['data']['project_role_mappings'] |
| lombardofr | 835a0b7 | 2018-07-18 15:26:16 +0200 | [diff] [blame] | 100 | |
| lombardofr | 99f922f | 2018-07-17 17:27:36 +0200 | [diff] [blame] | 101 | def switch_project(self, project_id): |
| 102 | client = Client() |
| 103 | result = client.switch_project({'project_id': project_id, 'username': self.username, 'password': self.psw}) |
| 104 | if 'error' in result and result['error'] is True: |
| 105 | raise OSMAuthException(result['data']) |
| 106 | else: |
| 107 | self.token = result['data']['id'] |
| 108 | self.project_id = result['data']['project_id'] |
| lombardofr | e5a130a | 2019-07-15 09:17:59 +0200 | [diff] [blame] | 109 | self.project_name = result['data']['project_name'] |
| lombardofr | 99f922f | 2018-07-17 17:27:36 +0200 | [diff] [blame] | 110 | self.token_expires = result['data']['expires'] |
| 111 | self.save() |
| 112 | return True |
| 113 | return False |
| 114 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 115 | class Meta: |
| 116 | verbose_name = _('custom user') |
| 117 | verbose_name_plural = _('custom users') |
| 118 | abstract = True |
| 119 | |
| 120 | |
| lombardofr | 099364f | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 121 | class OsmUser(AbstractOsmUser): |
| 122 | """ |
| 123 | Concrete class of AbstractCustomUser. |
| 124 | |
| 125 | Use this if you don't need to extend CustomUser. |
| 126 | |
| 127 | """ |
| 128 | |
| 129 | class Meta(AbstractOsmUser.Meta): |
| 130 | swappable = 'AUTH_USER_MODEL' |
| 131 | |