コード例 #1
0
def login(request):
    try:
        params = json.loads(request.body.decode("utf-8"))
        username = params["username"]
        password = params["password"]

    except Exception:
        username = request.POST.get("username", None)
        password = request.POST.get("password", None)

    # Comprobamos usuario con pineboo
    try:
        apiuser = APIQSA.login(username, password)
        responseUser = {}
        if type(apiuser) is int or type(apiuser) is str:
            authusername = apiuser
            responseUser["user"] = apiuser
        else:
            authusername = apiuser["user"]
            responseUser = apiuser
        if authusername:
            user = User.objects.filter(username=str(authusername))
            if user.exists():
                authuser = authenticate(username=str(authusername),
                                        password=password)
                if authuser is None:
                    user = User.objects.get(username__exact=str(authusername))
                    user.set_password(password)
                    user.save()
                    authuser = authenticate(username=str(authusername),
                                            password=password)
            else:
                user = User.objects.create_user(username=str(authusername),
                                                password=password)
                user.is_staff = False
                user.save()
                authuser = authenticate(username=str(authusername),
                                        password=password)
            token, _ = Token.objects.get_or_create(user=authuser)
            print(responseUser)
            responseUser["token"] = token.key
            resul = HttpResponse(json.dumps(responseUser), status=200)
    except Exception as e:
        print("-----------------------")
        print(e)
        resul = HttpResponse(json.dumps({'error': str(e)}), status=404)
    resul['Access-Control-Allow-Origin'] = '*'
    return resul
コード例 #2
0
    def gesttare_token_auth(self, request):
        try:
            params = json.loads(request.body.decode("utf-8"))
            username = params["username"]
            password = params["password"]

        except Exception:
            username = request.POST.get("username", None)
            password = request.POST.get("password", None)

        # Comprobamos usuario con pineboo
        try:
            authuser = APIQSA.login(username, password)
            if authuser:
                # user = User.objects.filter(username=str(authusername))
                # if user.exists():
                #     authuser = authenticate(username=str(authusername), password=password)
                #     if authuser is None:
                #         user = User.objects.get(username__exact=str(authusername))
                #         user.set_password(password)
                #         user.save()
                #         authuser = authenticate(username=str(authusername), password=password)
                # else:
                #     user = User.objects.create_user(username=str(authusername), password=password)
                #     user.is_staff = False
                #     user.save()
                #     authuser = authenticate(username=str(authusername), password=password)
                # token, _ = Token.objects.get_or_create(user=authuser)
                resul = HttpResponse(json.dumps({'token': authuser["token"]}),
                                     status=200)
        except Exception as e:
            print("-----------------------")
            print(e)
            resul = HttpResponse(json.dumps({'error': str(e)}), status=404)
        resul['Access-Control-Allow-Origin'] = '*'
        return resul
コード例 #3
0
    def gesttare_auth_login(self, request):
        if request.method == "POST":
            action = request.POST.get("action", None)
            username = request.POST.get("username", None)
            password = request.POST.get("password", None)
            redirect_uri = None
            state = None
            try:
                redirect_uri = request.POST.get("redirect_uri", None)
                state = request.POST.get("state", None)
                print(redirect_uri, "    ", state)
            except Exception as e:
                print(e)

            if action == "login":
                if username == "admin":
                    user = authenticate(username=username, password=password)
                    if user is not None:
                        login_auth(request, user)
                        accessControl.accessControl.registraAC()
                    else:
                        return self.iface.login(request,
                                                'Error de autentificación')
                    return HttpResponseRedirect("/")
                try:
                    authuser = APIQSA.login(username, password)
                    authusername = authuser["user"]

                    if authusername:
                        id_usuario = qsatype.FLUtil.sqlSelect(
                            "aqn_user", "idusuario",
                            "email = '" + str(username) + "'")

                        # ultimo_login = qsatype.FLUtil.sqlSelect("auth_user", "last_login", "username = '******'")
                        # if ultimo_login is None:
                        #     APIQSA.entry_point('post', "aqn_companies", "", str(username), "enviar_wiki")
                        # APIQSA.entry_point('post', "aqn_companies", "", "", "enviar_wiki")
                        usuario = aqn_user.objects.filter(
                            email__exact=username)
                        if usuario.exists():
                            authuser = authenticate(username=str(authusername),
                                                    password=password)
                            id_compania = qsatype.FLUtil.sqlSelect(
                                "aqn_user", "idcompany",
                                "idusuario = " + str(authusername))
                            cantidad_compania = qsatype.FLUtil.sqlSelect(
                                "aqn_user", "COUNT(idusuario)",
                                "idcompany = " + str(id_compania))
                            is_superuser = qsatype.FLUtil.sqlSelect(
                                "auth_user", "is_superuser",
                                "username = '******'")
                            if cantidad_compania == 1 and not is_superuser:
                                # user = User.objects.create_user(username=id_usuario, password="******", first_name=username)
                                nombre = qsatype.FLUtil.sqlSelect(
                                    "aqn_user", "usuario",
                                    "idusuario = " + str(authusername))

                                user = User.objects.get(
                                    username__exact=str(authusername))
                                user.is_superuser = True
                                user.first_name = nombre
                                user.save()
                                # qsatype.FLUtil.sqlUpdate("auth_user", "is_superuser", True, "username = '******'")
                                # qsatype.FLUtil.sqlUpdate("auth_user", "is_superuser", True, "username = '******'")
                            if authuser is None:
                                user = User.objects.get(
                                    username__exact=str(authusername))
                                user.set_password(password)
                                user.save()
                                authuser = authenticate(
                                    username=str(authusername),
                                    password=password)
                        else:
                            usuario = User.objects.create_user(
                                username=str(authusername), password=password)
                            usuario.is_staff = False
                            usuario.save()
                            authuser = authenticate(username=str(authusername),
                                                    password=password)

                        # if len(usuario) == 0:
                        #     return self.iface.login(request, 'No existe el usuario')
                        # if usuario[0].activo is False:
                        #     return self.iface.login(request, 'No existe el usuario')
                        # md5passwd = hashlib.md5(password.encode('utf-8')).hexdigest()
                        # # print("falla por aqui??", md5passwd, usuario[0].password)
                        # if usuario[0].password != md5passwd:
                        #     return self.iface.login(request, 'Error de autentificación')
                        idusuario = authusername
                        if redirect_uri and redirect_uri != None and redirect_uri != "None":
                            url = redirect_uri + "?state=" + state + "&code=" + username + "&token=prueba"
                            return HttpResponseRedirect(url)
                        # user = authenticate(username=idusuario, password="******")
                        if authuser is not None:
                            login_auth(request, authuser)
                        else:
                            return self.iface.login(
                                request, "Error de autentificación")
                        accessControl.accessControl.registraAC()
                        return HttpResponseRedirect("/")
                    else:
                        return self.iface.login(request,
                                                'No existe el usuario')
                except Exception as e:
                    print("_____________________")
                    print(e)
                    return self.iface.login(request, str(e))
        return self.iface.login(request)