Mercurial > hg > OKFNAnnotator
diff annotator_files/lib/plugin/annotateitpermissions.js @ 4:6979313586cf
new version of annotator.
author | casties |
---|---|
date | Mon, 27 Aug 2012 19:05:38 +0200 |
parents | 6356e78ccf5c |
children |
line wrap: on
line diff
--- a/annotator_files/lib/plugin/annotateitpermissions.js Thu Apr 05 19:37:27 2012 +0200 +++ b/annotator_files/lib/plugin/annotateitpermissions.js Mon Aug 27 19:05:38 2012 +0200 @@ -1,12 +1,24 @@ +// Generated by CoffeeScript 1.3.3 var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, - __hasProp = Object.prototype.hasOwnProperty, - __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }, - __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; Annotator.Plugin.AnnotateItPermissions = (function(_super) { __extends(AnnotateItPermissions, _super); + function AnnotateItPermissions() { + this._setAuthFromToken = __bind(this._setAuthFromToken, this); + + this.updateAnnotationPermissions = __bind(this.updateAnnotationPermissions, this); + + this.updatePermissionsField = __bind(this.updatePermissionsField, this); + + this.addFieldsToAnnotation = __bind(this.addFieldsToAnnotation, this); + return AnnotateItPermissions.__super__.constructor.apply(this, arguments); + } + AnnotateItPermissions.prototype.options = { showViewPermissionsCheckbox: true, showEditPermissionsCheckbox: true, @@ -15,33 +27,34 @@ authenticated: 'group:__authenticated__', consumer: 'group:__consumer__' }, - userString: function(user) { - return user; + userId: function(user) { + return user.userId; }, - userAuthorize: function(action, annotation, user, consumer) { - var action_field, permissions, _ref, _ref2, _ref3, _ref4; + userString: function(user) { + return user.userId; + }, + userAuthorize: function(action, annotation, user) { + var action_field, permissions, _ref, _ref1, _ref2, _ref3; permissions = annotation.permissions || {}; action_field = permissions[action] || []; - if (!(user && consumer)) { - return _ref = this.groups.world, __indexOf.call(action_field, _ref) >= 0; - } else { - if (_ref2 = this.groups.world, __indexOf.call(action_field, _ref2) >= 0) { + if (_ref = this.groups.world, __indexOf.call(action_field, _ref) >= 0) { + return true; + } else if ((user != null) && (user.userId != null) && (user.consumerKey != null)) { + if (user.userId === annotation.user && user.consumerKey === annotation.consumer) { return true; - } else if (user === annotation.user && consumer === annotation.consumer) { + } else if (_ref1 = this.groups.authenticated, __indexOf.call(action_field, _ref1) >= 0) { return true; - } else if (_ref3 = this.groups.authenticated, __indexOf.call(action_field, _ref3) >= 0) { + } else if (user.consumerKey === annotation.consumer && (_ref2 = this.groups.consumer, __indexOf.call(action_field, _ref2) >= 0)) { return true; - } else if (consumer === annotation.consumer && (_ref4 = this.groups.consumer, __indexOf.call(action_field, _ref4) >= 0)) { + } else if (user.consumerKey === annotation.consumer && (_ref3 = user.userId, __indexOf.call(action_field, _ref3) >= 0)) { return true; - } else if (consumer === annotation.consumer && __indexOf.call(action_field, user) >= 0) { + } else if (user.consumerKey === annotation.consumer && user.admin) { return true; } else { return false; } } }, - user: '', - consumer: 'annotateit', permissions: { 'read': ['group:__world__'], 'update': [], @@ -50,42 +63,24 @@ } }; - function AnnotateItPermissions(element, options) { - this._setAuthFromToken = __bind(this._setAuthFromToken, this); - this.updateAnnotationPermissions = __bind(this.updateAnnotationPermissions, this); - this.updatePermissionsField = __bind(this.updatePermissionsField, this); - this.addFieldsToAnnotation = __bind(this.addFieldsToAnnotation, this); AnnotateItPermissions.__super__.constructor.apply(this, arguments); - if (this.options.consumer) { - this.setConsumer(this.options.consumer); - delete this.options.consumer; - } - } - - AnnotateItPermissions.prototype.setConsumer = function(consumer) { - return this.consumer = consumer; - }; - - AnnotateItPermissions.prototype.authorize = function(action, annotation, user, consumer) { - if (user === void 0) user = this.user; - if (consumer === void 0) consumer = this.consumer; - if (this.options.userAuthorize) { - return this.options.userAuthorize.call(this.options, action, annotation, user, consumer); - } else { - return true; - } - }; - AnnotateItPermissions.prototype.addFieldsToAnnotation = function(annotation) { AnnotateItPermissions.__super__.addFieldsToAnnotation.apply(this, arguments); - if (annotation && this.consumer) return annotation.consumer = this.consumer; + if (annotation && this.user) { + return annotation.consumer = this.user.consumerKey; + } }; AnnotateItPermissions.prototype.updatePermissionsField = function(action, field, annotation) { var input; field = $(field).show(); input = field.find('input').removeAttr('disabled'); - if (!this.authorize('admin', annotation)) field.hide(); - if (this.authorize(action, annotation || {}, '__nonexistentuser__')) { + if (!this.authorize('admin', annotation)) { + field.hide(); + } + if (this.user && this.authorize(action, annotation || {}, { + userId: '__nonexistentuser__', + consumerKey: this.user.consumerKey + })) { return input.attr('checked', 'checked'); } else { return input.removeAttr('checked'); @@ -94,7 +89,9 @@ AnnotateItPermissions.prototype.updateAnnotationPermissions = function(type, field, annotation) { var dataKey; - if (!annotation.permissions) annotation.permissions = this.options.permissions; + if (!annotation.permissions) { + annotation.permissions = this.options.permissions; + } dataKey = type + '-permissions'; if ($(field).find('input').is(':checked')) { return annotation.permissions[type] = [type === 'read' ? this.options.groups.world : this.options.groups.consumer]; @@ -104,8 +101,7 @@ }; AnnotateItPermissions.prototype._setAuthFromToken = function(token) { - AnnotateItPermissions.__super__._setAuthFromToken.apply(this, arguments); - return this.setConsumer(token.consumerKey); + return this.setUser(token); }; return AnnotateItPermissions;