简体   繁体   中英

Roles - Parse.com Javascript

At the moment I have javascript that allows all users from the (_User) table to log in. I have set up a Role called (Admins) within the role table and assigned one user to this role. Would this be an if statement?

At the moment this is how the user logs in successfully

$scope.logIn = function(form) {
Parse.User.logIn(form.username, form.password, {
  success: function(user) {
    $scope.currentUser = user;
    $scope.$apply();
    window.location.href = "TEST.html";
  },

It's easy to check whether any user belongs to a role. The only tricky part is to realize that the check includes a query, and is therefore an asynchronous operation. So first, a general purpose role checking function:

function userHasRole(user, roleName) {
    var query = new Parse.Query(Parse.Role);
    query.equalTo("name", roleName);
    query.equalTo("users", user);
    return query.find().then(function(roles) {
        return roles.length > 0;
    });
}

This returns a promise that will be fulfilled with a boolean, so you can call it like this:

var currentUser = Parse.User.current();
// is the user an "admin"?
userHasRole(currentUser, "admin").then(function(isAdmin) {
    console.log((isAdmin)? "user is admin" : "user is not admin");
});

Apply it like this in your code. In the view:

<form role="form" name="loginForm">
    <div class="form-group">
        <label>Email</label>
        <input type="email" class="form-control" name="email" ng-model="user.username" />
    </div>
    <div class="form-group">
        <label>Password</label>
        <input type="password" class="form-control" name="password" ng-model="user.password" />
    </div>
    <div class="form-group">
        <button class="btn btn-ar btn-primary" ng-click="pressedLogIn()">Log in</button>
    </div>
</form>

And in the controller:

(function() {
    'use strict';
    angular.module('myApp.controllers').controller('LogInController', LogInController);

    LogInController.$inject = ['$scope'];

    function LogInController($scope) {
        $scope.user = { username:"", password:""};

        function userHasRole(user, roleName) {
            // defined exactly as above
            // my real app has a user service, and this would be better placed there
        }

        $scope.pressedLogIn = function() {
            if ($scope.loginForm.$valid) {
                Parse.User.logIn($scope.user.username, $scope.user.password).then(function(user) {
                    $scope.user = user;
                    return userHasRole(user, "administrator");
                }).then(function(isAdmin) {
                    alert("user is admin = " + isAdmin);
                }, function(e) {
                    alert(error.message);
                });
            }
        };
    }
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM