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