Tuesday, 15 March 2011

Roles - Parse.com Javascript -


I currently have javascript which allows all users to log in from the _User table. I played a role in the role table called (Admin) and this role was assigned to a user. Will this be a statement?

The user logs in successfully this way

  $ 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 a user is related to a role. The only difficult part is that the investigation involves a question, and therefore an asynchronous First of all, a normal operation The function of the mass role check is:

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

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

  currentUser = Parse.User.current (); // Does the user " Admin "? UserHasRole (current user, "admin"). Then (function (isAdmin) is {console.log (isAdmin)? "User is administrator": "User is not administrator");});  

In the Apply it to your code view:

  & lt; Form role = "form" name = "login form" & gt; & Lt; Div class = "form-group" & gt; & Lt; Label & gt; Email & lt; / Labels & gt; & Lt; Input type = "email" class = "form-control" name = "email" ng-model = "user.username" /> & Lt; / Div & gt; & Lt; Div class = "form-group" & gt; & Lt; Label & gt; Password & lt; / Labels & gt; & Lt; Input type = "password" class = "form-control" name = "password" ng-model = "user.password" /> & Lt; / Div & gt; & Lt; Div class = "form-group" & gt; & Lt; Button class = "BTN BTN-AR BTN-Primary" ng-click = "Pressed Login ()" & gt; Sign in & lt; / Button & gt; & Lt; / Div & gt; & Lt; / Form & gt;  

and in the controller:

  (function () {'strict use'; angular module ('myApp.controllers'). LogInController '; LogInController $ Inject = [' $ scope ']; Function LogInController ($ scope) {$ scope.user = {username: "", password: ""}; function userHasRole (user, roleName ) {// as really defined above // ​​my real application is user service function (), and it will be kept there better} $ scope.pressedLogIn = if ($ scope.loginForm. Valid) {Parse. User.logIn ($ scope .user.username, $ scope.user.password) then (function (user) {$ scope.user = user; returned userHasRole (user, "admin");}.) (Function (isAdmin) {warning (" User admin = "+ hasadmin);}, function (e) {warning (error message);})}}}}}}) ();  

No comments:

Post a Comment