Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Kuzzle = require('kuzzle-sdk');
- const kuzzle = new Kuzzle('localhost', (err, resp) => {
- const roles = {
- anonymous: {
- "controllers": {
- "*": {
- "actions": {
- "*": false
- }
- },
- "index": {
- "actions": {
- "list": true
- }
- },
- "collection": {
- "actions": {
- "list": true,
- "getMapping": true
- }
- },
- "document": {
- "actions": {
- "get": true,
- "search": true
- }
- },
- "notify": {
- "actions": {
- "document": true
- }
- },
- "auth": {
- "actions": {
- "login": true,
- "logout": true,
- "checkToken": true,
- "getCurrentUser": true, // Add this
- "getMyRights": true // Add this
- }
- }
- }
- }
- };
- const profiles = {
- "opendatasoft-profile" : {
- "policies": [
- {
- "roleId": "anonymous"
- },
- {
- "roleId": "admin",
- "restrictedTo": [
- {
- "index": "opendatasoft",
- "collections": [
- "event",
- "cantines-toulouse",
- "cantines-rennes",
- "cantines-st-malo",
- "buses-rennes",
- "mobile-network"
- ]
- }
- ]
- }
- ]
- }
- };
- const databases = {
- opendatasoft: [
- "event",
- "cantines-toulouse",
- "cantines-rennes",
- "cantines-st-malo",
- "buses-rennes",
- "col-pas-toi",
- "mobile-network"
- ],
- "index-pas-toi": [
- "interdit"
- ]
- };
- const users = {
- a: {
- content: {
- profileIds: [ 'admin' ]
- },
- credentials: {
- local: {
- username: 'a',
- password: 'a'
- }
- }
- },
- opendatasoft: {
- content: {
- profileIds: [ 'opendatasoft-profile' ]
- },
- credentials: {
- local: {
- username: 'opendatasoft',
- password: 'opendatasoft'
- }
- }
- }
- }
- const credentials = {
- username: 'admin',
- password: 'password'
- }
- const args = {
- controller: 'security',
- action: 'createFirstAdmin'
- }
- const query = {
- body: {
- content: {
- },
- credentials: {
- local: {
- username: 'admin',
- password: 'password'
- }
- }
- }
- }
- kuzzle
- .queryPromise(args, query)
- .then(() => kuzzle.loginPromise('local', credentials))
- .then(() => {
- return Promise.all(Object.entries(databases).map(([index, collections]) => {
- return kuzzle
- .createIndexPromise(index)
- .then(() => Promise.all(collections.map(collection => kuzzle.collection(collection, index).createPromise())))
- }))
- })
- .then(() => {
- return Promise.all(Object.entries(roles).map(([role, definition]) => kuzzle.security.createRolePromise(role, definition, { replaceIfExist: true })))
- })
- .then(() => {
- return Promise.all(Object.entries(profiles).map(([profile, definition]) => kuzzle.security.createProfilePromise(profile, definition.policies, { replaceIfExist: true })))
- })
- .then(() => {
- return Promise.all(Object.entries(users).map(([user, definition]) => kuzzle.security.createUserPromise(user, definition, { replaceIfExist: true })))
- })
- .then(response => console.log((response)))
- .catch(error => console.log(error))
- .finally(() => kuzzle.disconnect())
- });
Add Comment
Please, Sign In to add comment