Vulcan/packages/telescope-lib/lib/collections.js
Maxime Quandalle 94c6121d91 Improve jsHint consistency
This commit touch a lot of lines of code with the goal to be more
rigorous about JavaScript code conventions defined in the `.jshintrc`.

Some modification:

* Add a list of used global symbols in the corresponding section of
  `.jshintrc`
* Use local variables instead of global in a lot of places where the
  keyword `var` was mistakenly forgotten
* Add missing semi-colons after instructions
* Add new lines at the end of files
* Remove trailing whitespaces
* Use newer name of some Meteor APIs, eg `addFiles` instead of
  `add_files`
* Add missing `break` statements in `switch` blocks
* Use `===` instead of `==` and `!==` instead of `!=`
* Remove unused variables

This commit should also fix a few bugs due to this lack of rigor. One
example of that was the test `typeof navElements === "array"` that was
never true because in JavaScript, `typeof [] === "object"`, we
replaced this test by the `_.isArray` method provided by underscore.
It might also fix some potential collision related to global
variables.

There is still plenty of work until Telescope code base passes jsHint
validation, but at least this commit is a step in the right direction.
2015-05-01 18:38:27 +02:00

78 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Add an additional field to a schema.
* @param {Object} field
*/
Meteor.Collection.prototype.registerField = function (field) {
var collection = this;
var fieldSchema = {};
fieldSchema[field.propertyName] = field.propertySchema;
// add field schema to collection schema
collection.attachSchema(fieldSchema);
};
/**
* Remove a field from a schema.
* @param {String} fieldName
*/
Meteor.Collection.prototype.removeField = function (fieldName) {
var collection = this;
var schema = _.omit(collection.simpleSchema()._schema, fieldName);
// add field schema to collection schema
collection.attachSchema(schema, {replace: true});
};
/**
* Check if an operation is allowed
* @param {Object} collection the collection to which the document belongs
* @param {String} userId the userId of the user performing the operation
* @param {Object} document the document being modified
* @param {[String]} fieldNames the names of the fields being modified
* @param {Object} modifier the modifier
*/
Telescope.allowCheck = function (collection, userId, document, fieldNames, modifier) {
var schema = collection.simpleSchema();
var user = Meteor.users.findOne(userId);
var allowedFields = schema.getEditableFields(user);
var fields = [];
// fieldNames only contains top-level fields, so loop over modifier to get real list of fields
_.each(modifier, function (operation) {
fields = fields.concat(_.keys(operation));
});
// allow update only if:
// 1. user has rights to edit the document
// 2. there is no fields in fieldNames that are not also in allowedFields
return Users.can.edit(userId, document) && _.difference(fields, allowedFields).length == 0;
};
// Note: using the prototype doesn't work in allow/deny for some reason
Meteor.Collection.prototype.allowCheck = function (userId, document, fieldNames, modifier) {
Telescope.allowCheck(this, userId, document, fieldNames, modifier);
};
/**
* Global schemas object. Note: not reactive, won't be updated after initialization
* @namespace Telescope.schemas
*/
Telescope.schemas = {};
/**
* Get a list of all fields editable by a specific user for a given schema
* @param {Object} user the user for which to check field permissions
*/
SimpleSchema.prototype.getEditableFields = function (user) {
var schema = this._schema;
var fields = _.filter(_.keys(schema), function (fieldName) {
var field = schema[fieldName];
return Users.can.editField(user, field);
});
return fields;
};