Mercurial > hg > OKFNAnnotator
comparison annotator_files/lib/plugin/annotateitpermissions.js @ 3:6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
| author | casties |
|---|---|
| date | Thu, 05 Apr 2012 19:37:27 +0200 |
| parents | |
| children | 6979313586cf |
comparison
equal
deleted
inserted
replaced
| 2:4c6c8835fc5c | 3:6356e78ccf5c |
|---|---|
| 1 var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | |
| 2 __hasProp = Object.prototype.hasOwnProperty, | |
| 3 __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; }, | |
| 4 __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; }; | |
| 5 | |
| 6 Annotator.Plugin.AnnotateItPermissions = (function(_super) { | |
| 7 | |
| 8 __extends(AnnotateItPermissions, _super); | |
| 9 | |
| 10 AnnotateItPermissions.prototype.options = { | |
| 11 showViewPermissionsCheckbox: true, | |
| 12 showEditPermissionsCheckbox: true, | |
| 13 groups: { | |
| 14 world: 'group:__world__', | |
| 15 authenticated: 'group:__authenticated__', | |
| 16 consumer: 'group:__consumer__' | |
| 17 }, | |
| 18 userString: function(user) { | |
| 19 return user; | |
| 20 }, | |
| 21 userAuthorize: function(action, annotation, user, consumer) { | |
| 22 var action_field, permissions, _ref, _ref2, _ref3, _ref4; | |
| 23 permissions = annotation.permissions || {}; | |
| 24 action_field = permissions[action] || []; | |
| 25 if (!(user && consumer)) { | |
| 26 return _ref = this.groups.world, __indexOf.call(action_field, _ref) >= 0; | |
| 27 } else { | |
| 28 if (_ref2 = this.groups.world, __indexOf.call(action_field, _ref2) >= 0) { | |
| 29 return true; | |
| 30 } else if (user === annotation.user && consumer === annotation.consumer) { | |
| 31 return true; | |
| 32 } else if (_ref3 = this.groups.authenticated, __indexOf.call(action_field, _ref3) >= 0) { | |
| 33 return true; | |
| 34 } else if (consumer === annotation.consumer && (_ref4 = this.groups.consumer, __indexOf.call(action_field, _ref4) >= 0)) { | |
| 35 return true; | |
| 36 } else if (consumer === annotation.consumer && __indexOf.call(action_field, user) >= 0) { | |
| 37 return true; | |
| 38 } else { | |
| 39 return false; | |
| 40 } | |
| 41 } | |
| 42 }, | |
| 43 user: '', | |
| 44 consumer: 'annotateit', | |
| 45 permissions: { | |
| 46 'read': ['group:__world__'], | |
| 47 'update': [], | |
| 48 'delete': [], | |
| 49 'admin': [] | |
| 50 } | |
| 51 }; | |
| 52 | |
| 53 function AnnotateItPermissions(element, options) { | |
| 54 this._setAuthFromToken = __bind(this._setAuthFromToken, this); | |
| 55 this.updateAnnotationPermissions = __bind(this.updateAnnotationPermissions, this); | |
| 56 this.updatePermissionsField = __bind(this.updatePermissionsField, this); | |
| 57 this.addFieldsToAnnotation = __bind(this.addFieldsToAnnotation, this); AnnotateItPermissions.__super__.constructor.apply(this, arguments); | |
| 58 if (this.options.consumer) { | |
| 59 this.setConsumer(this.options.consumer); | |
| 60 delete this.options.consumer; | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 AnnotateItPermissions.prototype.setConsumer = function(consumer) { | |
| 65 return this.consumer = consumer; | |
| 66 }; | |
| 67 | |
| 68 AnnotateItPermissions.prototype.authorize = function(action, annotation, user, consumer) { | |
| 69 if (user === void 0) user = this.user; | |
| 70 if (consumer === void 0) consumer = this.consumer; | |
| 71 if (this.options.userAuthorize) { | |
| 72 return this.options.userAuthorize.call(this.options, action, annotation, user, consumer); | |
| 73 } else { | |
| 74 return true; | |
| 75 } | |
| 76 }; | |
| 77 | |
| 78 AnnotateItPermissions.prototype.addFieldsToAnnotation = function(annotation) { | |
| 79 AnnotateItPermissions.__super__.addFieldsToAnnotation.apply(this, arguments); | |
| 80 if (annotation && this.consumer) return annotation.consumer = this.consumer; | |
| 81 }; | |
| 82 | |
| 83 AnnotateItPermissions.prototype.updatePermissionsField = function(action, field, annotation) { | |
| 84 var input; | |
| 85 field = $(field).show(); | |
| 86 input = field.find('input').removeAttr('disabled'); | |
| 87 if (!this.authorize('admin', annotation)) field.hide(); | |
| 88 if (this.authorize(action, annotation || {}, '__nonexistentuser__')) { | |
| 89 return input.attr('checked', 'checked'); | |
| 90 } else { | |
| 91 return input.removeAttr('checked'); | |
| 92 } | |
| 93 }; | |
| 94 | |
| 95 AnnotateItPermissions.prototype.updateAnnotationPermissions = function(type, field, annotation) { | |
| 96 var dataKey; | |
| 97 if (!annotation.permissions) annotation.permissions = this.options.permissions; | |
| 98 dataKey = type + '-permissions'; | |
| 99 if ($(field).find('input').is(':checked')) { | |
| 100 return annotation.permissions[type] = [type === 'read' ? this.options.groups.world : this.options.groups.consumer]; | |
| 101 } else { | |
| 102 return annotation.permissions[type] = []; | |
| 103 } | |
| 104 }; | |
| 105 | |
| 106 AnnotateItPermissions.prototype._setAuthFromToken = function(token) { | |
| 107 AnnotateItPermissions.__super__._setAuthFromToken.apply(this, arguments); | |
| 108 return this.setConsumer(token.consumerKey); | |
| 109 }; | |
| 110 | |
| 111 return AnnotateItPermissions; | |
| 112 | |
| 113 })(Annotator.Plugin.Permissions); |
