禁用所有用户ACL:公共读/写时,如何检查用户名是否使用parse.com? [英] How do I check if a username is taken with parse.com when all User ACL:Public read/write is disable?

查看:73
本文介绍了禁用所有用户ACL:公共读/写时,如何检查用户名是否使用parse.com?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在用户类内的所有用户中都禁用ACL:公共读/写时,如何检查用户名是否使用parse.com Javascript SDK?

说明: 出于安全原因,我在类/表User中的所有用户都具有私有 ACL(访问控制列表),或者说,用于公共读/写的ACL被禁用,这意味着经过身份验证的用户可以读取他们自己的信息.

您可以想象,对用户的任何查询都会对未登录的用户为空,因此无法通过使用对用户类别的查询来检查用户是否已被使用

我设法通过选择一个新用户来解决此问题,然后Parse将返回400错误并提供一些有用的信息:

{code: 202, error: "username Test already taken"}

这种方法的问题在于,当用户在文本区域中键入内容时,我会实时进行验证:

HTML AngularJS:

<form name="form">
    <h3>e-mail</h3>
    <input class="form-control"
           name="email"
           placeholder="try john.doe@mail.com or bad@domain.com"
           type="email"
           required ng-model="email"
           ng-model-options="{ debounce: 100 }"
           ui-validate="{blacklist: 'notBlackListed($value)'}"
           ui-validate-async="{alreadyExists: 'doesNotExist($modelValue)'}"
    >

    <span ng-show='form.email.$error.blacklist'>This e-mail is black-listed!</span>
    <span ng-show='form.email.$error.alreadyExists'>This e-mail is <b>already taken!</b></span>
    <span ng-show='form.email.$pending'>Verifying e-mail on server...</span>
    <br>is form valid: {{form.$valid}}
</form>

Javascript AngularJS:

$scope.doesNotExist = function (value) {
            Parse.initialize(KEY0, KEY1);
            var deferral = $q.defer();
            var user = new Parse.User();
            user.set("username", "Test");
            user.set("password", "anyapssword");
            user.signUp(null, {
                success: function(user) {
                    // Hooray! Let them use the app now.
                    console.log("success!");
                    // Holly shit now I have to delete the user :( and wait for the full form to be submmited
                    user.destroy({
                    success: function(myObject) {
                        // The object was deleted from the Parse Cloud.
                        console.log("destroy!!!!!!!!!!!");
                    },
                    error: function(myObject, error) {
                        // The delete failed.
                        // error is a Parse.Error with an error code and message.
                        console.log("failed destroy!!!!!!!!!!!");
                    }
                });
                    deferral.resolve(user);
                },
                error: function(user, error) {
                    console.log("Error: " + error.code + " " + error.message);
                    deferral.reject("mierda!");
                }
            });
            return deferral.promise;
        };

那么在禁用ACL:公共读/写时,如何检查parse.com是否使用了用户名?

我正在使用AngularUI插件进行快速验证:解决方案

我找到了自己的答案.如果有人在这里看我在AngularJS和Parse.com中所做的事情.为了节省时间,我使用了来自AngularUI的Validate插件.

HTML:

<!DOCTYPE html>
<html lang="en" ng-app="demo">

<head>
    <meta charset="utf-8">
    <title>AngularJS ui-validate</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.js"></script>
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css"/>
    <script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.6.14.min.js"></script>

    <!-- ui-validate files -->
    <script src="../dist/validate.js"></script>
</head>

<body class="container">
<script>
    var app = angular.module('demo', ['ui.validate']);
    app.controller('ValidateCtrl', function ($scope, $timeout, $q) {
        $scope.doesNotExist = function (value) {
            Parse.initialize("uO...rPt", "8Bu...1mxFr");    
            var deferral = $q.defer();
            Parse.Cloud.run('mgxIsNameAlreadyTaken', {username: value}, {
                success: function (result) {
                    console.log("httpRequest resultado: " + result);
                    // result is 'Hello world!'
                    deferral.resolve();
                },
                error: function (error) {
                    console.log("si! ya esta tomado!: " + error);
                    deferral.reject();
                }
            });
            return deferral.promise;
            //return true;
        };
    });
</script>
<section id="validate" ng-controller="ValidateCtrl">
    <div class="page-header">
        <h1>Validate</h1>
    </div>
    <h3>What?</h3>
    <div class="row">
        <div class="col-md-6">
            <p>The
                <code>ui-validate</code> and <code>ui-validate-async</code> directives makes it very easy to create
                custom validator expressions.</p>
            <div class="well">
                <form name="form">
                    <h3>e-mail</h3>
                    <input class="form-control"
                           name="email"
                           placeholder="try john.doe@mail.com or bad@domain.com"
                           type="email"
                           required ng-model="email"
                           ng-model-options="{ debounce: 500 }"
                           ui-validate-async="{alreadyExists: 'doesNotExist($modelValue)'}"
                    >
                    <span ng-show='form.email.$error.blacklist'>This e-mail is black-listed!</span>
                    <span ng-show='form.email.$error.alreadyExists'>This e-mail is <b>already taken!</b></span>
                    <span ng-show='form.email.$pending'>Verifying e-mail on server...</span>
                    <span ng-show='form.email.$valid'>This Email is Valid.</span>
                    <br>is form valid: {{form.$valid}}
                    <br>
                </form>
            </div>
        </div>
    </div>
</section>
</body>

</html>

解析云代码:

Parse.Cloud.define("mgxIsNameAlreadyTaken", function(request, response) {
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query(Parse.User);
    query.equalTo("username", request.params.username);
    query.first({
        success: function(user) {
            if (user) {
                response.error("The username has already been taken.");
            } else {
                response.success("The username "+request.params.username+" is available.");
            }
        },
        error: function(error) {
            console.error("Error en el query de user." + error.code + " : " + error.message);
            response.error("Error in mgxIsNameAlreadyTaken: " + error.code + " : " + error.message);
        }
    });
});

祝你好运!

How do I check if a username is taken with parse.com Javascript SDK when ACL:Public read/write is disable in all users inside User's class?

Explanation: For security reasons all my users in class/table User have a private ACL (Access control list), or in other words the ACL for public read/write is disable, it means that authenticated users can read only their own information.

As you can imagine any query to Users will get empty to non logged In users so there is no way to check if a user is already taken by using Query on User Class

I manage to work around this by singUp a new user and then Parse will return a 400 error with some good information:

{code: 202, error: "username Test already taken"}

The problem with that approach is that I'm doing the validation on real time while the user is typing on the text area field:

HTML AngularJS:

<form name="form">
    <h3>e-mail</h3>
    <input class="form-control"
           name="email"
           placeholder="try john.doe@mail.com or bad@domain.com"
           type="email"
           required ng-model="email"
           ng-model-options="{ debounce: 100 }"
           ui-validate="{blacklist: 'notBlackListed($value)'}"
           ui-validate-async="{alreadyExists: 'doesNotExist($modelValue)'}"
    >

    <span ng-show='form.email.$error.blacklist'>This e-mail is black-listed!</span>
    <span ng-show='form.email.$error.alreadyExists'>This e-mail is <b>already taken!</b></span>
    <span ng-show='form.email.$pending'>Verifying e-mail on server...</span>
    <br>is form valid: {{form.$valid}}
</form>

Javascript AngularJS:

$scope.doesNotExist = function (value) {
            Parse.initialize(KEY0, KEY1);
            var deferral = $q.defer();
            var user = new Parse.User();
            user.set("username", "Test");
            user.set("password", "anyapssword");
            user.signUp(null, {
                success: function(user) {
                    // Hooray! Let them use the app now.
                    console.log("success!");
                    // Holly shit now I have to delete the user :( and wait for the full form to be submmited
                    user.destroy({
                    success: function(myObject) {
                        // The object was deleted from the Parse Cloud.
                        console.log("destroy!!!!!!!!!!!");
                    },
                    error: function(myObject, error) {
                        // The delete failed.
                        // error is a Parse.Error with an error code and message.
                        console.log("failed destroy!!!!!!!!!!!");
                    }
                });
                    deferral.resolve(user);
                },
                error: function(user, error) {
                    console.log("Error: " + error.code + " " + error.message);
                    deferral.reject("mierda!");
                }
            });
            return deferral.promise;
        };

So how can I check if a username is taken with parse.com when ACL:Public read/write is disable?

I'm using the AngularUI plugIn to fast validation: https://htmlpreview.github.io/?https://github.com/angular-ui/ui-validate/master/demo/index.html

Thanks!

解决方案

I found my own answer. In case someone is looking here what I did in AngularJS and Parse.com. To save some time I'm using Validate plugIn from AngularUI.

The HTML:

<!DOCTYPE html>
<html lang="en" ng-app="demo">

<head>
    <meta charset="utf-8">
    <title>AngularJS ui-validate</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.js"></script>
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css"/>
    <script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.6.14.min.js"></script>

    <!-- ui-validate files -->
    <script src="../dist/validate.js"></script>
</head>

<body class="container">
<script>
    var app = angular.module('demo', ['ui.validate']);
    app.controller('ValidateCtrl', function ($scope, $timeout, $q) {
        $scope.doesNotExist = function (value) {
            Parse.initialize("uO...rPt", "8Bu...1mxFr");    
            var deferral = $q.defer();
            Parse.Cloud.run('mgxIsNameAlreadyTaken', {username: value}, {
                success: function (result) {
                    console.log("httpRequest resultado: " + result);
                    // result is 'Hello world!'
                    deferral.resolve();
                },
                error: function (error) {
                    console.log("si! ya esta tomado!: " + error);
                    deferral.reject();
                }
            });
            return deferral.promise;
            //return true;
        };
    });
</script>
<section id="validate" ng-controller="ValidateCtrl">
    <div class="page-header">
        <h1>Validate</h1>
    </div>
    <h3>What?</h3>
    <div class="row">
        <div class="col-md-6">
            <p>The
                <code>ui-validate</code> and <code>ui-validate-async</code> directives makes it very easy to create
                custom validator expressions.</p>
            <div class="well">
                <form name="form">
                    <h3>e-mail</h3>
                    <input class="form-control"
                           name="email"
                           placeholder="try john.doe@mail.com or bad@domain.com"
                           type="email"
                           required ng-model="email"
                           ng-model-options="{ debounce: 500 }"
                           ui-validate-async="{alreadyExists: 'doesNotExist($modelValue)'}"
                    >
                    <span ng-show='form.email.$error.blacklist'>This e-mail is black-listed!</span>
                    <span ng-show='form.email.$error.alreadyExists'>This e-mail is <b>already taken!</b></span>
                    <span ng-show='form.email.$pending'>Verifying e-mail on server...</span>
                    <span ng-show='form.email.$valid'>This Email is Valid.</span>
                    <br>is form valid: {{form.$valid}}
                    <br>
                </form>
            </div>
        </div>
    </div>
</section>
</body>

</html>

Parse Cloud Code:

Parse.Cloud.define("mgxIsNameAlreadyTaken", function(request, response) {
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query(Parse.User);
    query.equalTo("username", request.params.username);
    query.first({
        success: function(user) {
            if (user) {
                response.error("The username has already been taken.");
            } else {
                response.success("The username "+request.params.username+" is available.");
            }
        },
        error: function(error) {
            console.error("Error en el query de user." + error.code + " : " + error.message);
            response.error("Error in mgxIsNameAlreadyTaken: " + error.code + " : " + error.message);
        }
    });
});

Good luck!

这篇关于禁用所有用户ACL:公共读/写时,如何检查用户名是否使用parse.com?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆