84 lines
2.1 KiB
Python
84 lines
2.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
# ITerativ GmbH
|
|
# http://www.iterativ.ch/
|
|
#
|
|
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
|
|
#
|
|
# Created on 2019-10-01
|
|
# @author: chrigu <christian.cueni@iterativ.ch>
|
|
|
|
import graphene
|
|
from django.conf import settings
|
|
from django.contrib.auth import authenticate, login
|
|
from graphene import relay
|
|
|
|
from core.hep_client import HepClient
|
|
from registration.models import License
|
|
|
|
|
|
class LoginError(graphene.ObjectType):
|
|
field = graphene.String()
|
|
|
|
|
|
class Login(relay.ClientIDMutation):
|
|
class Input:
|
|
username_input = graphene.String()
|
|
password_input = graphene.String()
|
|
|
|
success = graphene.Boolean()
|
|
errors = graphene.List(LoginError) # todo: change for consistency
|
|
|
|
@classmethod
|
|
def mutate_and_get_payload(cls, root, info, **kwargs):
|
|
|
|
# get token
|
|
# wrong password
|
|
#
|
|
# is email verified
|
|
# -> show screen
|
|
# local license
|
|
# -> show coupon
|
|
# get hep orders
|
|
# -> show coupon
|
|
# save role information
|
|
# login
|
|
|
|
if not settings.USE_LOCAL_REGISTRATION:
|
|
user = authenticate(username=kwargs.get('username_input'), password=kwargs.get('password_input'))
|
|
if user is None:
|
|
error = LoginError(field='invalid_credentials')
|
|
return cls(success=False, errors=[error])
|
|
|
|
else:
|
|
hep_client = HepClient()
|
|
|
|
# Todo catch error
|
|
token = hep_client.customer_token(kwargs.get('username_input'), kwargs.get('password_input'))
|
|
# Todo save token
|
|
#verify email
|
|
|
|
|
|
|
|
user_license = None
|
|
|
|
try:
|
|
user_license = License.objects.get(licensee=user)
|
|
except License.DoesNotExist:
|
|
# current users have no license, allow them to login
|
|
pass
|
|
|
|
if user_license is not None and not user_license.license_type.active:
|
|
error = LoginError(field='license_inactive')
|
|
return cls(success=False, errors=[error])
|
|
|
|
login(info.context, user)
|
|
return cls(success=True, errors=[])
|
|
|
|
|
|
class UserMutations:
|
|
login = Login.Field()
|
|
|
|
|
|
|