gpt4 book ai didi

javascript - Angular 色 - Parse.com Javascript

转载 作者:行者123 更新时间:2023-12-03 10:34:16 25 4
gpt4 key购买 nike

目前,我有 javascript,允许 (_User) 表中的所有用户登录。我在 Angular 色表中设置了一个名为 (Admins) 的 Angular 色,并为该 Angular 色分配了一个用户。这是一个 if 语句吗?

目前这是用户成功登录的方式

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

最佳答案

很容易检查任何用户是否属于某个 Angular 色。唯一棘手的部分是要意识到该检查包含查询,因此是一个异步操作。首先,一个通用的 Angular 色检查函数:

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

这会返回一个将通过 bool 值实现的 promise ,因此您可以这样调用它:

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

在您的代码中像这样应用它。在 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>

在 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);
});
}
};
}
})();

关于javascript - Angular 色 - Parse.com Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29077382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com