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