Guest User

Untitled

a guest
Jun 13th, 2018
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Kuzzle = require('kuzzle-sdk');
  2.  
  3. const kuzzle = new Kuzzle('localhost', (err, resp) => {
  4.  
  5.   const roles = {
  6.     anonymous: {
  7.       "controllers": {
  8.         "*": {
  9.           "actions": {
  10.             "*": false
  11.           }
  12.         },
  13.         "index": {
  14.           "actions": {
  15.             "list": true
  16.           }
  17.         },
  18.         "collection": {
  19.           "actions": {
  20.             "list": true,
  21.             "getMapping": true
  22.           }
  23.         },
  24.         "document": {
  25.           "actions": {
  26.             "get": true,
  27.             "search": true
  28.           }
  29.         },
  30.         "notify": {
  31.           "actions": {
  32.             "document": true
  33.           }
  34.         },
  35.         "auth": {
  36.           "actions": {
  37.             "login": true,
  38.             "logout": true,
  39.             "checkToken": true,
  40.             "getCurrentUser": true, // Add this
  41.             "getMyRights": true // Add this
  42.           }
  43.         }
  44.       }
  45.     }
  46.   };
  47.  
  48.   const profiles = {
  49.     "opendatasoft-profile" : {
  50.       "policies": [
  51.         {
  52.           "roleId": "anonymous"
  53.         },
  54.         {
  55.           "roleId": "admin",
  56.           "restrictedTo": [
  57.             {
  58.               "index": "opendatasoft",
  59.               "collections": [
  60.                 "event",
  61.                 "cantines-toulouse",
  62.                 "cantines-rennes",
  63.                 "cantines-st-malo",
  64.                 "buses-rennes",
  65.                 "mobile-network"
  66.               ]
  67.             }
  68.           ]
  69.         }
  70.       ]
  71.     }
  72.   };
  73.  
  74.   const databases = {
  75.     opendatasoft: [
  76.       "event",
  77.       "cantines-toulouse",
  78.       "cantines-rennes",
  79.       "cantines-st-malo",
  80.       "buses-rennes",
  81.       "col-pas-toi",
  82.       "mobile-network"
  83.     ],
  84.     "index-pas-toi": [
  85.       "interdit"
  86.     ]
  87.   };
  88.  
  89.   const users = {
  90.     a: {
  91.       content: {
  92.         profileIds: [ 'admin' ]
  93.       },
  94.       credentials: {
  95.         local: {
  96.           username: 'a',
  97.           password: 'a'
  98.         }
  99.       }
  100.     },
  101.     opendatasoft: {
  102.       content: {
  103.         profileIds: [ 'opendatasoft-profile' ]
  104.       },
  105.       credentials: {
  106.         local: {
  107.           username: 'opendatasoft',
  108.           password: 'opendatasoft'
  109.         }
  110.       }
  111.     }
  112.   }
  113.  
  114.   const credentials = {
  115.     username: 'admin',
  116.     password: 'password'
  117.   }
  118.  
  119.   const args = {
  120.     controller: 'security',
  121.     action: 'createFirstAdmin'
  122.   }
  123.  
  124.   const query = {
  125.     body: {
  126.       content: {
  127.  
  128.       },
  129.       credentials: {
  130.         local: {
  131.           username: 'admin',
  132.           password: 'password'
  133.         }
  134.       }
  135.     }
  136.   }
  137.  
  138.   kuzzle
  139.     .queryPromise(args, query)
  140.     .then(() => kuzzle.loginPromise('local', credentials))
  141.     .then(() => {
  142.       return Promise.all(Object.entries(databases).map(([index, collections]) => {
  143.         return kuzzle
  144.           .createIndexPromise(index)
  145.           .then(() => Promise.all(collections.map(collection => kuzzle.collection(collection, index).createPromise())))
  146.       }))
  147.     })
  148.     .then(() => {
  149.       return Promise.all(Object.entries(roles).map(([role, definition]) => kuzzle.security.createRolePromise(role, definition, { replaceIfExist: true })))
  150.     })
  151.     .then(() => {
  152.       return Promise.all(Object.entries(profiles).map(([profile, definition]) => kuzzle.security.createProfilePromise(profile, definition.policies, { replaceIfExist: true })))
  153.     })
  154.     .then(() => {
  155.       return Promise.all(Object.entries(users).map(([user, definition]) => kuzzle.security.createUserPromise(user, definition, { replaceIfExist: true })))
  156.     })
  157.     .then(response => console.log((response)))
  158.     .catch(error => console.log(error))
  159.     .finally(() => kuzzle.disconnect())
  160. });
Add Comment
Please, Sign In to add comment