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