OpenVeo server API for plugins

API Docs for: 3.0.0
Show:

ContentModel

Extends EntityModel
Module: models

Summary

Defines a ContentModel class to manipulate content entities.

A content entity associate a user to the entity and add controls for CRUD operations.

ContentModel must not be used directly. Create a sub class instead.

Constructor

ContentModel

Syntax

ContentModel

(
  • user
  • provider
)

Summary

Parameters:

  • user Object

    The user that will manipulate the entities

  • provider EntityProvider

    The entity provider

Example:

// Example for implementing a new ContentModel named "CustomModel"

// CustomModel.js

var util = require('util');
var api = require('@openveo/api');
var CustomProvider = process.require('CustomProvider.js');

function CustomModel(user) {

  // Initialize the content model with a dedicated provider
  api.ContentModel.call(this, user, new CustomProvider(api.applicationStorage.getDatabase()));

}

// CustomModel must extends ContentModel
module.exports = CustomModel;
util.inherits(CustomModel, api.ContentModel);
// Example for how to use CustomModel defined in previous example

var api = require('@openveo/api');

var CustomModel = process.require('CustomModel.js');
var model = new CustomModel();

Methods

add

Inherited from EntityModel but overwritten in lib/models/ContentModel.js:236

Syntax

add

(
  • data
  • callback
)
async

Summary

Adds a new content entity.

Information about the user (which becomes the owner) is added to the entity before recording.

Parameters:

  • data Object

    Entity data to store into the collection, its structure depends on the type of entity

  • callback Function

    The function to call when it's done

    • Error The error if an error occurred, null otherwise
    • Number The total amount of items inserted
    • Object The added entity

addAccessFilter

Syntax

addAccessFilter

(
  • filter
)
Object

Summary

Adds access rule to the given filter reference.

Parameters:

  • filter Object

    The filter to add the access rule to

Returns:

Object:

The filter

get

Inherited from EntityModel but overwritten in lib/models/ContentModel.js:199

Syntax

get

(
  • filter
  • callback
)
async

Summary

Gets all content entities.

Only entities that the user can manipulate are returned.

Parameters:

  • filter Object

    A MongoDB filter

  • callback Function

    The function to call when it's done

    • Error The error if an error occurred, null otherwise
    • Array The list of entities

getOne

Inherited from EntityModel but overwritten in lib/models/ContentModel.js:176

Syntax

getOne

(
  • id
  • filter
  • callback
)
async

Summary

Gets a single content entity by its id.

If the user has not the necessary permissions, an error will be returned.

Parameters:

  • id String

    The entity id

  • filter Object

    A MongoDB filter

  • callback Function

    The function to call when it's done

    • Error The error if an error occurred, null otherwise
    • Object The entity

getPaginatedFilteredEntities

Inherited from EntityModel but overwritten in lib/models/ContentModel.js:215

Syntax

getPaginatedFilteredEntities

(
  • [filter]
  • [limit]
  • [page]
  • [sort]
  • [populate]
  • callback
)
async

Summary

Gets an ordered list of entities by page.

Only entities that the user can manipulate are returned.

Parameters:

  • [filter] Object optional

    MongoDB filter

  • [limit] Number optional

    The maximum number of expected entities

  • [page] Number optional

    The expected page

  • [sort] Object optional

    A sort object

  • [populate] Boolean optional

    true to automatically populate results with additional information

  • callback Function

    The function to call when it's done

    • Error The error if an error occurred, null otherwise
    • Array The list of entities
    • Object Pagination information

getUserAuthorizedGroups

Syntax

getUserAuthorizedGroups

(
  • operation
)
Array private

Summary

Gets the list of groups of a user, with authorization on a certain operation.

All user groups with authorization on the operation are returned.

Parameters:

  • operation String

    The operation (get, update or delete)

Returns:

Array:

The list of user group ids with authorization on the operation

getUserGroups

Syntax

getUserGroups

(
  • user
)
Object private

Summary

Gets the list of groups from a user.

Parameters:

  • user Object

    The user to extract groups from

Returns:

Object:

Groups organized by ids

Example:

// Example of user permissions
['get-group-Jekrn20Rl', 'update-group-Jekrn20Rl', 'delete-group-YldO3Jie3', 'some-other-permission']

// Example of returned groups
{
  'Jekrn20Rl': ['get', 'update'], // User only has get / update permissions on group 'Jekrn20Rl'
  'YldO3Jie3': ['delete'], // User only has delete permission on group 'YldO3Jie3'
  ...
}

isUserAdmin

Syntax

isUserAdmin

() Boolean

Summary

Tests if user is the administrator.

Returns:

Boolean:

true if the user is the administrator, false otherwise

isUserAuthorized

Syntax

isUserAuthorized

(
  • entity
  • operation
)
Boolean

Summary

Validates that the user is authorized to manipulate a content entity.

User is authorized to manipulate the entity if one of the following conditions is met :

  • No user is associated to the model
  • The entity belongs to the anonymous user
  • User is the super administrator
  • User is the owner of the entity
  • Entity has associated groups and user has permission to perform the operation on the group

Parameters:

  • entity Object

    The entity to manipulate

  • operation String

    The operation to perform on the entity

Returns:

Boolean:

true if the user can manipulate the entity, false otherwise

remove

Inherited from EntityModel but overwritten in lib/models/ContentModel.js:287

Syntax

remove

(
  • ids
  • callback
)
async

Summary

Removes one or several entities.

User must have permission to remove the entity.

Parameters:

  • ids String | Array

    Id(s) of the document(s) to remove from the collection

  • callback Function

    The function to call when it's done

    • Error The error if an error occurred, null otherwise
    • Number The number of deleted entities

update

Inherited from EntityModel but overwritten in lib/models/ContentModel.js:261

Syntax

update

(
  • id
  • data
  • callback
)
async

Summary

Updates an entity.

User must have permission to update the entity.

Parameters:

  • id String

    The id of the entity to update

  • data Object

    Entity data, its structure depends on the type of entity

  • callback Function

    The function to call when it's done

    • Error The error if an error occurred, null otherwise
    • Number The number of updated items