comparison annotator_files/lib/plugin/permissions.js @ 4:6979313586cf

new version of annotator.
author casties
date Mon, 27 Aug 2012 19:05:38 +0200
parents 6356e78ccf5c
children
comparison
equal deleted inserted replaced
3:6356e78ccf5c 4:6979313586cf
1 // Generated by CoffeeScript 1.3.3
1 var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, 2 var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
2 __hasProp = Object.prototype.hasOwnProperty, 3 __hasProp = {}.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 __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
5 Annotator.Plugin.Permissions = (function(_super) { 6 Annotator.Plugin.Permissions = (function(_super) {
6 7
7 __extends(Permissions, _super); 8 __extends(Permissions, _super);
8 9
21 }, 22 },
22 userAuthorize: function(action, annotation, user) { 23 userAuthorize: function(action, annotation, user) {
23 var token, tokens, _i, _len; 24 var token, tokens, _i, _len;
24 if (annotation.permissions) { 25 if (annotation.permissions) {
25 tokens = annotation.permissions[action] || []; 26 tokens = annotation.permissions[action] || [];
26 if (tokens.length === 0) return true; 27 if (tokens.length === 0) {
28 return true;
29 }
27 for (_i = 0, _len = tokens.length; _i < _len; _i++) { 30 for (_i = 0, _len = tokens.length; _i < _len; _i++) {
28 token = tokens[_i]; 31 token = tokens[_i];
29 if (this.userId(user) === token) return true; 32 if (this.userId(user) === token) {
33 return true;
34 }
30 } 35 }
31 return false; 36 return false;
32 } else if (annotation.user) { 37 } else if (annotation.user) {
33 return user && this.userId(user) === this.userId(annotation.user); 38 return user && this.userId(user) === this.userId(annotation.user);
34 } 39 }
43 } 48 }
44 }; 49 };
45 50
46 function Permissions(element, options) { 51 function Permissions(element, options) {
47 this._setAuthFromToken = __bind(this._setAuthFromToken, this); 52 this._setAuthFromToken = __bind(this._setAuthFromToken, this);
53
48 this.updateViewer = __bind(this.updateViewer, this); 54 this.updateViewer = __bind(this.updateViewer, this);
55
49 this.updateAnnotationPermissions = __bind(this.updateAnnotationPermissions, this); 56 this.updateAnnotationPermissions = __bind(this.updateAnnotationPermissions, this);
57
50 this.updatePermissionsField = __bind(this.updatePermissionsField, this); 58 this.updatePermissionsField = __bind(this.updatePermissionsField, this);
51 this.addFieldsToAnnotation = __bind(this.addFieldsToAnnotation, this); Permissions.__super__.constructor.apply(this, arguments); 59
60 this.addFieldsToAnnotation = __bind(this.addFieldsToAnnotation, this);
61 Permissions.__super__.constructor.apply(this, arguments);
52 if (this.options.user) { 62 if (this.options.user) {
53 this.setUser(this.options.user); 63 this.setUser(this.options.user);
54 delete this.options.user; 64 delete this.options.user;
55 } 65 }
56 } 66 }
57 67
58 Permissions.prototype.pluginInit = function() { 68 Permissions.prototype.pluginInit = function() {
59 var createCallback, self, 69 var createCallback, self,
60 _this = this; 70 _this = this;
61 if (!Annotator.supported()) return; 71 if (!Annotator.supported()) {
72 return;
73 }
62 self = this; 74 self = this;
63 createCallback = function(method, type) { 75 createCallback = function(method, type) {
64 return function(field, annotation) { 76 return function(field, annotation) {
65 return self[method].call(self, type, field, annotation); 77 return self[method].call(self, type, field, annotation);
66 }; 78 };
92 label: Annotator._t('User'), 104 label: Annotator._t('User'),
93 property: 'user', 105 property: 'user',
94 isFiltered: function(input, user) { 106 isFiltered: function(input, user) {
95 var keyword, _i, _len, _ref; 107 var keyword, _i, _len, _ref;
96 user = _this.options.userString(user); 108 user = _this.options.userString(user);
97 if (!(input && user)) return false; 109 if (!(input && user)) {
110 return false;
111 }
98 _ref = input.split(/\s*/); 112 _ref = input.split(/\s*/);
99 for (_i = 0, _len = _ref.length; _i < _len; _i++) { 113 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
100 keyword = _ref[_i]; 114 keyword = _ref[_i];
101 if (user.indexOf(keyword) === -1) return false; 115 if (user.indexOf(keyword) === -1) {
116 return false;
117 }
102 } 118 }
103 return true; 119 return true;
104 } 120 }
105 }); 121 });
106 } 122 }
111 }; 127 };
112 128
113 Permissions.prototype.addFieldsToAnnotation = function(annotation) { 129 Permissions.prototype.addFieldsToAnnotation = function(annotation) {
114 if (annotation) { 130 if (annotation) {
115 annotation.permissions = this.options.permissions; 131 annotation.permissions = this.options.permissions;
116 if (this.user) return annotation.user = this.user; 132 if (this.user) {
133 return annotation.user = this.user;
134 }
117 } 135 }
118 }; 136 };
119 137
120 Permissions.prototype.authorize = function(action, annotation, user) { 138 Permissions.prototype.authorize = function(action, annotation, user) {
121 if (user === void 0) user = this.user; 139 if (user === void 0) {
140 user = this.user;
141 }
122 if (this.options.userAuthorize) { 142 if (this.options.userAuthorize) {
123 return this.options.userAuthorize.call(this.options, action, annotation, user); 143 return this.options.userAuthorize.call(this.options, action, annotation, user);
124 } else { 144 } else {
125 return true; 145 return true;
126 } 146 }
128 148
129 Permissions.prototype.updatePermissionsField = function(action, field, annotation) { 149 Permissions.prototype.updatePermissionsField = function(action, field, annotation) {
130 var input; 150 var input;
131 field = $(field).show(); 151 field = $(field).show();
132 input = field.find('input').removeAttr('disabled'); 152 input = field.find('input').removeAttr('disabled');
133 if (!this.authorize('admin', annotation)) field.hide(); 153 if (!this.authorize('admin', annotation)) {
154 field.hide();
155 }
134 if (this.authorize(action, annotation || {}, null)) { 156 if (this.authorize(action, annotation || {}, null)) {
135 return input.attr('checked', 'checked'); 157 return input.attr('checked', 'checked');
136 } else { 158 } else {
137 return input.removeAttr('checked'); 159 return input.removeAttr('checked');
138 } 160 }
139 }; 161 };
140 162
141 Permissions.prototype.updateAnnotationPermissions = function(type, field, annotation) { 163 Permissions.prototype.updateAnnotationPermissions = function(type, field, annotation) {
142 var dataKey; 164 var dataKey;
143 if (!annotation.permissions) annotation.permissions = this.options.permissions; 165 if (!annotation.permissions) {
166 annotation.permissions = this.options.permissions;
167 }
144 dataKey = type + '-permissions'; 168 dataKey = type + '-permissions';
145 if ($(field).find('input').is(':checked')) { 169 if ($(field).find('input').is(':checked')) {
146 return annotation.permissions[type] = []; 170 return annotation.permissions[type] = [];
147 } else { 171 } else {
148 return annotation.permissions[type] = [this.user]; 172 return annotation.permissions[type] = [this.user];
157 user = Annotator.$.escape(this.options.userString(annotation.user)); 181 user = Annotator.$.escape(this.options.userString(annotation.user));
158 field.html(user).addClass('annotator-user'); 182 field.html(user).addClass('annotator-user');
159 } else { 183 } else {
160 field.remove(); 184 field.remove();
161 } 185 }
162 if (!this.authorize('update', annotation)) controls.hideEdit(); 186 if (!this.authorize('update', annotation)) {
163 if (!this.authorize('delete', annotation)) return controls.hideDelete(); 187 controls.hideEdit();
188 }
189 if (!this.authorize('delete', annotation)) {
190 return controls.hideDelete();
191 }
164 }; 192 };
165 193
166 Permissions.prototype._setAuthFromToken = function(token) { 194 Permissions.prototype._setAuthFromToken = function(token) {
167 return this.setUser(token.userId); 195 return this.setUser(token.userId);
168 }; 196 };