2016-02-15 107 views
1

我試圖創建的用戶在使用NPM loopback-component-passport與基於條件的用戶角色迴環,設置用戶角色

  1. 如果用戶是有產權isAdmin:true他應該有admin
  2. 作用,如果用戶是有產權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的內置模型中設置用戶角色?

+0

*「我該如何設置用戶角色?」*問題太模糊了......設置它們在哪裏?根本不清楚你的問題是什麼 – charlietfl

+0

@charlietfl更新了問題。 –

回答

1

最初創建一個文件,並在數據庫中添加角色
服務器的/ 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 /用戶.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上查看完整的示例