1 | (function() {
|
---|
2 | var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
|
---|
3 | for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
|
---|
4 | function ctor() { this.constructor = child; }
|
---|
5 | ctor.prototype = parent.prototype;
|
---|
6 | child.prototype = new ctor;
|
---|
7 | child.__super__ = parent.prototype;
|
---|
8 | return child;
|
---|
9 | };
|
---|
10 | Annotator.Plugin.Permissions = (function() {
|
---|
11 | __extends(Permissions, Annotator.Plugin);
|
---|
12 | Permissions.prototype.events = {
|
---|
13 | 'beforeAnnotationCreated': 'addFieldsToAnnotation'
|
---|
14 | };
|
---|
15 | Permissions.prototype.options = {
|
---|
16 | showViewPermissionsCheckbox: true,
|
---|
17 | showEditPermissionsCheckbox: true,
|
---|
18 | userId: function(user) {
|
---|
19 | return user;
|
---|
20 | },
|
---|
21 | userString: function(user) {
|
---|
22 | return user;
|
---|
23 | },
|
---|
24 | userAuthorize: function(user, token) {
|
---|
25 | return this.userId(user) === token;
|
---|
26 | },
|
---|
27 | user: '',
|
---|
28 | permissions: {
|
---|
29 | 'read': [],
|
---|
30 | 'update': [],
|
---|
31 | 'delete': [],
|
---|
32 | 'admin': []
|
---|
33 | }
|
---|
34 | };
|
---|
35 | function Permissions(element, options) {
|
---|
36 | this.updateViewer = __bind(this.updateViewer, this);
|
---|
37 | this.updateAnnotationPermissions = __bind(this.updateAnnotationPermissions, this);
|
---|
38 | this.updatePermissionsField = __bind(this.updatePermissionsField, this);
|
---|
39 | this.addFieldsToAnnotation = __bind(this.addFieldsToAnnotation, this); Permissions.__super__.constructor.apply(this, arguments);
|
---|
40 | if (this.options.user) {
|
---|
41 | this.setUser(this.options.user);
|
---|
42 | delete this.options.user;
|
---|
43 | }
|
---|
44 | }
|
---|
45 | Permissions.prototype.pluginInit = function() {
|
---|
46 | var createCallback, self;
|
---|
47 | if (!Annotator.supported()) {
|
---|
48 | return;
|
---|
49 | }
|
---|
50 | self = this;
|
---|
51 | createCallback = function(method, type) {
|
---|
52 | return function(field, annotation) {
|
---|
53 | return self[method].call(self, type, field, annotation);
|
---|
54 | };
|
---|
55 | };
|
---|
56 | if (this.options.showViewPermissionsCheckbox === true) {
|
---|
57 | this.annotator.editor.addField({
|
---|
58 | type: 'checkbox',
|
---|
59 | label: 'Allow anyone to <strong>view</strong> this annotation',
|
---|
60 | load: createCallback('updatePermissionsField', 'read'),
|
---|
61 | submit: createCallback('updateAnnotationPermissions', 'read')
|
---|
62 | });
|
---|
63 | }
|
---|
64 | if (this.options.showEditPermissionsCheckbox === true) {
|
---|
65 | this.annotator.editor.addField({
|
---|
66 | type: 'checkbox',
|
---|
67 | label: 'Allow anyone to <strong>edit</strong> this annotation',
|
---|
68 | load: createCallback('updatePermissionsField', 'update'),
|
---|
69 | submit: createCallback('updateAnnotationPermissions', 'update')
|
---|
70 | });
|
---|
71 | }
|
---|
72 | this.annotator.viewer.addField({
|
---|
73 | load: this.updateViewer
|
---|
74 | });
|
---|
75 | if (this.annotator.plugins.Filter) {
|
---|
76 | return this.annotator.plugins.Filter.addFilter({
|
---|
77 | label: 'User',
|
---|
78 | property: 'user',
|
---|
79 | isFiltered: __bind(function(input, user) {
|
---|
80 | var keyword, _i, _len, _ref;
|
---|
81 | user = this.options.userString(user);
|
---|
82 | if (!(input && user)) {
|
---|
83 | return false;
|
---|
84 | }
|
---|
85 | _ref = input.split(/\s*/);
|
---|
86 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
---|
87 | keyword = _ref[_i];
|
---|
88 | if (user.indexOf(keyword) === -1) {
|
---|
89 | return false;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | return true;
|
---|
93 | }, this)
|
---|
94 | });
|
---|
95 | }
|
---|
96 | };
|
---|
97 | Permissions.prototype.setUser = function(user) {
|
---|
98 | return this.user = user;
|
---|
99 | };
|
---|
100 | Permissions.prototype.addFieldsToAnnotation = function(annotation) {
|
---|
101 | if (annotation) {
|
---|
102 | annotation.permissions = this.options.permissions;
|
---|
103 | if (this.user) {
|
---|
104 | return annotation.user = this.user;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | };
|
---|
108 | Permissions.prototype.authorize = function(action, annotation, user) {
|
---|
109 | var token, tokens, _i, _len;
|
---|
110 | if (user === void 0) {
|
---|
111 | user = this.user;
|
---|
112 | }
|
---|
113 | if (annotation.permissions) {
|
---|
114 | tokens = annotation.permissions[action] || [];
|
---|
115 | if (tokens.length === 0) {
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 | for (_i = 0, _len = tokens.length; _i < _len; _i++) {
|
---|
119 | token = tokens[_i];
|
---|
120 | if (this.options.userAuthorize.call(this.options, user, token)) {
|
---|
121 | return true;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return false;
|
---|
125 | } else if (annotation.user) {
|
---|
126 | return user && this.options.userId(user) === annotation.user;
|
---|
127 | }
|
---|
128 | return true;
|
---|
129 | };
|
---|
130 | Permissions.prototype.updatePermissionsField = function(action, field, annotation) {
|
---|
131 | var dummy, input;
|
---|
132 | field = $(field).show();
|
---|
133 | input = field.find('input').removeAttr('disabled');
|
---|
134 | if (!this.authorize('admin', annotation)) {
|
---|
135 | field.hide();
|
---|
136 | }
|
---|
137 | if (this.authorize(action, annotation || {}, null)) {
|
---|
138 | input.attr('checked', 'checked');
|
---|
139 | dummy = {
|
---|
140 | permissions: this.options.permissions
|
---|
141 | };
|
---|
142 | if (this.authorize(action, dummy, null)) {
|
---|
143 | return input.attr('enabled', 'enabled');
|
---|
144 | }
|
---|
145 | } else {
|
---|
146 | return input.removeAttr('checked');
|
---|
147 | }
|
---|
148 | };
|
---|
149 | Permissions.prototype.updateAnnotationPermissions = function(type, field, annotation) {
|
---|
150 | var dataKey, permissions;
|
---|
151 | if (!annotation.permissions) {
|
---|
152 | annotation.permissions = this.options.permissions;
|
---|
153 | }
|
---|
154 | dataKey = type + '-permissions';
|
---|
155 | if ($(field).find('input').is(':checked')) {
|
---|
156 | $.data(annotation, dataKey, annotation.permissions[type]);
|
---|
157 | return annotation.permissions[type] = [];
|
---|
158 | } else {
|
---|
159 | permissions = $.data(annotation, dataKey);
|
---|
160 | if (permissions) {
|
---|
161 | return annotation.permissions[type] = permissions;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | };
|
---|
165 | Permissions.prototype.updateViewer = function(field, annotation, controls) {
|
---|
166 | var user, username;
|
---|
167 | field = $(field);
|
---|
168 | username = this.options.userString(annotation.user);
|
---|
169 | if (annotation.user && username && typeof username === 'string') {
|
---|
170 | user = Annotator.$.escape(this.options.userString(annotation.user));
|
---|
171 | field.html(user).addClass('annotator-user');
|
---|
172 | } else {
|
---|
173 | field.remove();
|
---|
174 | }
|
---|
175 | if (annotation.permissions) {
|
---|
176 | if (!this.authorize('update', annotation)) {
|
---|
177 | controls.hideEdit();
|
---|
178 | }
|
---|
179 | if (!this.authorize('delete', annotation)) {
|
---|
180 | return controls.hideDelete();
|
---|
181 | }
|
---|
182 | } else if (annotation.user && !this.authorize(null, annotation)) {
|
---|
183 | controls.hideEdit();
|
---|
184 | return controls.hideDelete();
|
---|
185 | }
|
---|
186 | };
|
---|
187 | return Permissions;
|
---|
188 | })();
|
---|
189 | }).call(this);
|
---|