Пример #1
0
    def get_logged_in_user_details(token, **kwargs):
        """
		@param token: the generated token of the user
		@type token:char
		@param kwargs: Extra key arguments passed to the method
		@return: Response code dictionary
		"""
        try:
            app_user = OauthService().filter(
                token=token).values('app_user').first()
            user = AppUserService().filter(id=app_user.get('app_user')).values(
                userName=F('user__username'),
                email=F('user__email'),
                superUser=F('user__is_superuser'),
                firstName=F('user__first_name'),
                lastName=F('user__last_name'),
                log=F('user__logentry'),
                staff=F('user__is_staff'),
                phoneNumber=F('user__phone_number'),
                password=F('user__password')).first()
            if user.get('superUser'):
                user.update(role='Admin')
            elif user.get('staff'):
                user.update(role='Staff')
            else:
                user.update(role='User')
            return {'code': '800.200.001', "data": user}
        except Exception as ex:
            lgr.exception("Logged in user exception %s" % ex)
        return {"code": "800.400.001"}
Пример #2
0
def get_access_token(request):
	"""
	Generates an access token for valid app users
	@param request:
	@type request: DJANGO WSGIRequest
	@return: An access token and its expiry time or a response code indicating invalid credentials supplied
	@rtype: dict
	"""
	try:
		data = get_request_data(request)
		app_user = AppUserService().get(user__username = data.get('username'))
		if app_user is not None:
			user = check_password(data.get('password'), app_user.user.password)
			if user:
				oauth = OauthService().filter(
					app_user = app_user, expires_at__gt = timezone.now(), state__name = 'Active').first()
				if oauth:
					oauth = OauthService().update(pk = oauth.id, expires_at = token_expiry())
				else:
					oauth = OauthService().create(
						app_user = app_user, token = generate_access_token(),
						state = StateService().get(name = 'Active')
					)
				if not oauth:
					return JsonResponse({'code': '800.400.001'})
				return JsonResponse({
					                    'code': '800.200.001', 'data': {
						'token': str(oauth.token), 'expires_at': calendar.timegm(oauth.expires_at.timetuple())
					}
				                    })
		return JsonResponse({'code': '800.403.001'})
	except Exception as ex:
		lgr.exception("Get Access token Exception %s " % ex)
	return JsonResponse({'code': '800.400.001'})
Пример #3
0
 def test_filter(self):
     """
     Test System filter service
     """
     mixer.cycle(3).blend('api.AppUser')
     app_user = AppUserService().filter()
     assert len(app_user) == 3, 'Should have 3 app objects'
Пример #4
0
 def test_update(self):
     """
     Test System update service
     """
     app = mixer.blend('api.App', name="Helaplan")
     app_user = mixer.blend('api.AppUser')
     app_user = AppUserService().update(app_user.id, app=app)
     assert app_user.app.name == "Helaplan", 'Should have the same name'
Пример #5
0
 def test_get(self):
     """
     Test System get service
     """
     app = mixer.blend('api.App', name="Helaplan")
     user = mixer.blend(User, name="Kevin")
     mixer.blend('api.AppUser', app=app, user=user)
     app_user = AppUserService().get(app_id__name="Helaplan")
     assert app_user is not None, 'Should have an app object'
Пример #6
0
 def test_create(self):
     """
     Test System create service
     """
     state = mixer.blend('base.State')
     app = mixer.blend('api.App', name="Helaplan")
     user = mixer.blend(User, name="Kevin")
     app_user = AppUserService().create(app=app, state=state, user=user)
     assert app_user is not None, 'Should have a System object'
     assert app_user.user.name == "Kevin", "Created App name is equals to Helaplan"
Пример #7
0
    def create_user(username,
                    password,
                    email,
                    first_name=None,
                    last_name=None,
                    phone_number=None,
                    **kwargs):
        """
		Creates a user.
		@param username: Username of the user to be created
		@type username: str
		@param email: Email of the user to be created
		@type email: str
		@param password: Password of the user to be created
		@type password: str
		@param first_name: First name of the user
		@type first_name: str | None
		@param last_name: Last name of the user
		@type last_name: str | None
		@param phone_number: Phone number of the user to be created
		@type email: str | None
		@param kwargs: Extra key-value arguments to pass for user creation
		@return: Response code dictionary to indicate if the user was created or not
		@rtype: dict
		"""
        try:
            if User.objects.filter(username=username).exists():
                return {
                    "code": "800.400.001",
                    'message': 'Username already in use'
                }
            if User.objects.filter(email=email).exists():
                return {
                    "code": "800.400.001",
                    'message': 'Email already in use'
                }
            user = User.objects.create_user(username,
                                            email,
                                            password,
                                            first_name=first_name,
                                            last_name=last_name,
                                            phone_number=phone_number)
            app = AppService().filter(system__name='Helaplan',
                                      state__name='Active').values().first()
            if not app:
                return {
                    "code": "800.400.002",
                    "message": 'No Authentication app tied to this system'
                }
            user = User.objects.filter(id=user.id).values().first()
            if user:
                app_user = AppUserService().create(
                    app=AppService().get(system__name='Helaplan'),
                    user=User.objects.get(email=user.get('email')),
                    state=StateService().get(name='Active'))
                if not app_user:
                    return {
                        'code':
                        '800.400.003',
                        "message":
                        "Failed to create an app user %s %s" %
                        (user.get('id'), app)
                    }
                return {'code': '800.200.001', 'data': user}
        except Exception as ex:
            lgr.exception("UserCreation exception %s" % ex)
        return {
            "code": "800.400.001",
            'message': 'User could not be created %s' % ex
        }