Skip to content

Commit

Permalink
Merge 59d2fb7 into 2c40e9a
Browse files Browse the repository at this point in the history
  • Loading branch information
javierbrea committed Sep 29, 2018
2 parents 2c40e9a + 59d2fb7 commit e29ce22
Show file tree
Hide file tree
Showing 29 changed files with 495 additions and 170 deletions.
6 changes: 3 additions & 3 deletions lib/api/abilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ const Operations = (service, commands) => ({
}
},
getAbility: {
handler: (params, body, res) => commands.ability.getById(params.path._id)
handler: (params, body, res) => commands.ability.getById(params.path.id)
},
updateAbility: {
auth: (userData, params, body) => {
return commands.ability.getById(params.path._id).then(abilityData => {
return commands.ability.getById(params.path.id).then(abilityData => {
if (abilityData._user === userData._id) {
return Promise.resolve()
}
return Promise.reject(new service.errors.Forbidden())
})
},
handler: (params, body, res) => commands.ability.update(params.path._id, body).then(abilityData => {
handler: (params, body, res) => commands.ability.update(params.path.id, body).then(abilityData => {
res.status(204)
res.header('location', `/api/abilities/${abilityData._id}`)
return Promise.resolve()
Expand Down
57 changes: 51 additions & 6 deletions lib/api/abilities.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,26 @@
"description": "Ability is able to dispatch event",
"type": "boolean"
},
"eventDescription": {
"description": "Description of the ability event",
"type": "string"
},
"action": {
"description": "Ability is able to handle an action",
"type": "boolean"
},
"actionDescription": {
"description": "Description of the ability action",
"type": "string"
},
"state": {
"description": "Ability has an state",
"type": "boolean"
},
"stateDescription": {
"description": "Description of the ability state",
"type": "string"
},
"type": {
"description": "Type of ability data",
"type": "string",
Expand Down Expand Up @@ -116,8 +128,11 @@
"_user": "132defwer4321312",
"description": "Rele control",
"event": true,
"eventDescription": "Rele has changed state",
"action": true,
"actionDescription": "Changes rele state",
"state": true,
"stateDescription": "Current rele state",
"type": "boolean",
"createdAt": "2018-07-28T17:13:08.718Z",
"updatedAt": "2018-07-28T17:13:09.730Z"
Expand All @@ -140,14 +155,26 @@
"description": "Ability is able to dispatch event",
"type": "boolean"
},
"eventDescription": {
"description": "Description of the ability event",
"type": "string"
},
"action": {
"description": "Ability is able to handle an action",
"type": "boolean"
},
"actionDescription": {
"description": "Description of the ability action",
"type": "string"
},
"state": {
"description": "Ability has an state",
"type": "boolean"
},
"stateDescription": {
"description": "Description of the ability state",
"type": "string"
},
"type": {
"description": "Type of ability data",
"type": "string",
Expand Down Expand Up @@ -211,8 +238,11 @@
"name": "foo-ability-name",
"description": "Rele control",
"event": true,
"eventDescription": "Rele has changed state",
"action": true,
"actionDescription": "Changes rele state",
"state": true,
"stateDescription": "Current rele state",
"type": "boolean"
}
},
Expand All @@ -223,11 +253,26 @@
"description": {
"description": "Description of the ability",
"type": "string"
},
"eventDescription": {
"description": "Description of the ability event",
"type": "string"
},
"actionDescription": {
"description": "Description of the ability action",
"type": "string"
},
"stateDescription": {
"description": "Description of the ability state",
"type": "string"
}
},
"additionalProperties": false,
"example": {
"description": "Rele control"
"description": "Rele control",
"eventDescription": "Rele has changed state",
"actionDescription": "Changes rele state",
"stateDescription": "Current rele state"
}
},
"Abilities": {
Expand Down Expand Up @@ -311,17 +356,17 @@
}]
}
},
"/abilities/{_id}": {
"/abilities/{id}": {
"get": {
"parameters": [
{
"in": "path",
"name": "_id",
"name": "id",
"schema": {
"type": "string"
},
"required": true,
"description": "Ability _id"
"description": "Ability id"
}
],
"tags": ["ability"],
Expand Down Expand Up @@ -350,12 +395,12 @@
"parameters": [
{
"in": "path",
"name": "_id",
"name": "id",
"schema": {
"type": "string"
},
"required": true,
"description": "Ability _id"
"description": "Ability id"
}
],
"tags": ["ability"],
Expand Down
4 changes: 2 additions & 2 deletions lib/api/securityTokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"type": {
"description": "Token type",
"type": "string",
"enum": ["apikey", "jwt"]
"enum": ["apiKey", "jwt"]
},
"createdAt": {
"description": "Creation date timestamp",
Expand Down Expand Up @@ -58,7 +58,7 @@
"name": "type",
"schema": {
"type": "string",
"enum": ["apikey", "jwt"]
"enum": ["apiKey", "jwt"]
},
"description": "Type of token"
},
Expand Down
6 changes: 3 additions & 3 deletions lib/api/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ const Operations = (service, commands) => ({
handler: (params, body, res) => commands.service.getFiltered()
},
getService: {
handler: (params, body, res) => commands.service.getById(params.path._id)
handler: (params, body, res) => commands.service.getById(params.path.id)
},
updateService: {
auth: (userData, params, body) => commands.service.getById(params.path._id).then(serviceData => {
auth: (userData, params, body) => commands.service.getById(params.path.id).then(serviceData => {
if (serviceData._user === userData._id) {
return Promise.resolve()
}
return Promise.reject(new service.errors.Forbidden())
}),
handler: (params, body, res) => commands.service.update(params.path._id, body).then(serviceData => {
handler: (params, body, res) => commands.service.update(params.path.id, body).then(serviceData => {
res.status(204)
res.header('location', `/api/services/${serviceData._id}`)
return Promise.resolve()
Expand Down
10 changes: 5 additions & 5 deletions lib/api/services.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,17 @@
}]
}
},
"/services/{_id}": {
"/services/{id}": {
"get": {
"parameters": [
{
"in": "path",
"name": "_id",
"name": "id",
"schema": {
"type": "string"
},
"required": true,
"description": "Service _id"
"description": "Service id"
}
],
"tags": ["service"],
Expand Down Expand Up @@ -253,12 +253,12 @@
"parameters": [
{
"in": "path",
"name": "_id",
"name": "id",
"schema": {
"type": "string"
},
"required": true,
"description": "Service _id"
"description": "Service id"
}
],
"tags": ["service"],
Expand Down
21 changes: 16 additions & 5 deletions lib/api/users.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
'use strict'

const { omitBy, isUndefined } = require('lodash')

const definition = require('./users.json')
const { onlyAdmin, roles } = require('../security/utils')
const { roles } = require('../security/utils')

const Operations = (service, commands) => ({
getUsers: {
auth: onlyAdmin,
handler: () => commands.user.getAll()
auth: (userData, params, body) => (
userData.role === roles.ADMIN ||
(userData.role === roles.SERVICE_REGISTERER && params.query.role === roles.SERVICE)
),
handler: (params, body, res) => {
const filter = omitBy({
name: params.query.name,
role: params.query.role
}, isUndefined)
return commands.user.getFiltered(filter)
}
},
addUser: {
auth: (userData, params, body) => (
Expand All @@ -21,8 +32,8 @@ const Operations = (service, commands) => ({
})
},
getUser: {
auth: (userData, params, body) => (userData.role === roles.ADMIN || params.path._id === userData._id),
handler: (params, body, res) => commands.user.getById(params.path._id)
auth: (userData, params, body) => (userData.role === roles.ADMIN || params.path.id === userData._id),
handler: (params, body, res) => commands.user.getById(params.path.id)
},
getUserMe: {
handler: (params, body, res, userData) => commands.user.getById(userData._id)
Expand Down
27 changes: 23 additions & 4 deletions lib/api/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"role": {
"description": "Role assigned to the user",
"type": "string",
"enum": ["admin", "service", "plugin"]
"enum": ["admin", "operator", "service", "plugin", "service-registerer"]
},
"createdAt": {
"description": "Creation date timestamp",
Expand Down Expand Up @@ -68,7 +68,7 @@
"role": {
"description": "Role assigned to the user",
"type": "string",
"enum": ["admin", "operator", "service", "service-registerer", "plugin"]
"enum": ["admin", "operator", "service", "plugin", "service-registerer"]
}
},
"required": ["name", "role"],
Expand Down Expand Up @@ -101,6 +101,25 @@
"paths": {
"/users": {
"get": {
"parameters": [
{
"in": "query",
"name": "name",
"schema": {
"type": "string"
},
"description": "Service name"
},
{
"in": "query",
"name": "role",
"schema": {
"type": "string",
"enum": ["admin", "operator", "service", "plugin", "service-registerer"]
},
"description": "Service name"
}
],
"tags": ["user"],
"summary": "Return users",
"description": "Returns an array with all registered users",
Expand Down Expand Up @@ -176,12 +195,12 @@
}]
}
},
"/users/{_id}": {
"/users/{id}": {
"get": {
"parameters": [
{
"in": "path",
"name": "_id",
"name": "id",
"schema": {
"type": "string"
},
Expand Down
3 changes: 3 additions & 0 deletions lib/commands/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const Commands = (service, models, client) => {

const getAll = () => models.User.find({}, PUBLIC_FIELDS)

const getFiltered = (filter = {}) => models.User.find(filter, PUBLIC_FIELDS)

const get = (filter = {}) => models.User.findOne(filter, PUBLIC_FIELDS).then(ensureUser)

const getById = id => models.User.findById(id, PUBLIC_FIELDS)
Expand All @@ -41,6 +43,7 @@ const Commands = (service, models, client) => {
return {
add,
getAll,
getFiltered,
get,
getById,
remove,
Expand Down
9 changes: 9 additions & 0 deletions lib/models/ability.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@ const Model = service => {
type: Boolean,
default: false
},
eventDescription: {
type: String
},
action: {
type: Boolean,
default: false
},
actionDescription: {
type: String
},
state: {
type: Boolean,
default: false
},
stateDescription: {
type: String
},
type: {
type: String,
required: [true, templates.abilityTypeRequired()],
Expand Down
Loading

0 comments on commit e29ce22

Please sign in to comment.