繁体   English   中英

使用回送组件密码到内置模型中来设置回送中的用户角色

[英]Set User Role in loopback using loopback-component-passport into inbuilt models

我正在尝试使用npm loopback-component-passport和基于条件的用户角色在回送中创建用户,

  1. 如果用户具有isAdmin:true属性, isAdmin:true他应具有admin角色
  2. 如果用户具有isAdmin:false属性, isAdmin:false他应该具有user角色

这是我的代码:

if (form.$valid) {
        AuthService.register({
          name: $scope.user.name,
          email: $scope.user.email,
          password: $scope.user.password,
          isAdmin: $scope.user.isAdmin
        })
          .then(function() {
            // Account created, redirect to home
            $state.go('home');
          })
          .catch(function(err) {
            err = err.data;
            $scope.errors = {};

            // Update validity of form fields that match the mongoose errors
            angular.forEach(err.errors, function(error, field) {
              form[field].$setValidity('mongoose', false);
              $scope.errors[field] = error.message;
            });
        });
}

厂:

angular
  .module('app')
  .factory('AuthService', ['User', '$q', '$rootScope', function(User, $q, $rootScope) {
    function login(email, password) {
      return User
        .login({email: email, password: password})
        .$promise
        .then(function(response) {
          $rootScope.currentUser = {
            id: response.user.id,
            tokenId: response.id,
            email: email
          };
        });
    }

    function logout() {
      return User
        .logout()
        .$promise
        .then(function() {
          $rootScope.currentUser = null;
        });
    }

    function register(user) {
      return User.create(user).$promise;
    }

    return {
      login: login,
      logout: logout,
      register: register
    };
  }]);

护照策略:

 "local": {
    "provider": "local",
    "module": "passport-local",
    "usernameField": "email",
    "passwordField": "password",
    "authPath": "/auth/local",
    "successRedirect": "/auth/account",
    "failureRedirect": "/local",
    "failureFlash": true
      },
   ... 
   ...

模型config.json

{ 
  "User": {
    "dataSource": "db"
  },
  "AccessToken": {
    "dataSource": "db",
    "public": false
  },
  "ACL": {
    "dataSource": "db",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "db",
    "public": false
  },
  "Role": {
    "dataSource": "db",
    "public": false
  }
}

但是现在我不知道如何添加角色,以便可以区分具有用户角色的用户。 如何在loopback-component-passport的内置模型中设置用户角色?

最初创建文件并在数据库中添加角色
server / boot / role.js

module.exports = function(app) {
  var Role = app.models.Role;
  Role.create([
      {name: 'admin'},
      {name: 'user'}]
    , function(err, role) {
      if (err) throw err;
      console.log('Created roles:', role);
    });
};

使用模型挂钩方法https://docs.strongloop.com/display/public/LB/Operation+hooks

服务器的/ boot / user.js的

module.exports = function(app) {
  var User = app.models.user;
  var Role = app.models.Role;
  var RoleMapping = app.models.RoleMapping;
  User.observe('after save', function setDefaultUsername(ctx, next) {
    if (ctx.instance) {
      if (ctx.isNewInstance) {
        var userRole = ctx.instance.isAdmin ? 'admin' : 'user';
        Role.findOne({where: {name: userRole}}, function(err, role) {
          if (err) {
            return console.log(err);
          }
          RoleMapping.create({
            principalType: RoleMapping.USER,
            principalId: ctx.instance.id,
            roleId: role.id
          }, function(err, roleMapping) {
            if (err) {
              return console.log(err);
            }
          });
        });
      }
    }
    next();
  });
};

查看有关Github的完整示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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