0

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

说明:出于安全原因,我在类/表用户中的所有用户都有一个私有ACL(访问控制列表),或者换句话说,公共读/写的 ACL 被禁用,这意味着经过身份验证的用户只能读取自己的信息。

正如您可以想象的那样,对用户的任何查询对于未登录的用户来说都是空的,因此无法通过对用户类使用查询来检查用户是否已经被占用

我设法通过 singUp 一个新用户来解决这个问题,然后 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:Public 读/写被禁用时,如何检查用户名是否与 parse.com 一起使用?

我正在使用 AngularUI 插件进行快速验证:https ://htmlpreview.github.io/?https://github.com/angular-ui/ui-validate/master/demo/index.html

谢谢!

4

2 回答 2

0

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

的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);
        }
    });
});

祝你好运!

于 2016-01-17T15:27:52.463 回答
0

您可以创建一个云代码函数,使您能够检查是否使用了用户名——您可以使用 masterKey() 查找用户名,它允许您从 User 表中读取。

如果您需要更多信息,请告诉我。

于 2016-01-16T05:27:42.133 回答