-
Notifications
You must be signed in to change notification settings - Fork 0
/
kii-sdk.js
executable file
·5457 lines (4462 loc) · 167 KB
/
kii-sdk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var ctor = (function() { // generated by build.sh for running on Node.js
// Generated by CoffeeScript 1.6.2
var KiiRequest, KiiUtilities, root, _Kii, _KiiSocialConnect,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__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; },
_this = this;
root = ((typeof exports) !== "undefined") && (exports !== null) ? new Object() : this;
root.KiiSocialNetworkName = {
FACEBOOK: 1
};
root.KiiSite = {
US: "https://api.kii.com/api",
JP: "https://api-jp.kii.com/api",
CN: "https://api-cn2.kii.com/api"
};
root.KiiSiteInternal = {
DEV: "https://dev-jp.internal.kii.com/api",
STG: "https://qa21.internal.kii.com/api"
};
/**
@class The main SDK class
@exports root.Kii as Kii
This class must be initialized before any Kii SDK functions are performed. This class also allows the application to make some high-level user calls and access some application-wide data at any time using static methods.
*/
root.Kii = (function() {
var _instance;
function Kii() {}
_instance = null;
/**
Kii SDK Build Number
@returns {String} current build number of the SDK
*/
Kii.getBuildNumber = function() {
return "1";
};
/**
Kii SDK Version Number
@returns {String} current version number of the SDK
*/
Kii.getSDKVersion = function() {
return "2.1.6";
};
Kii.getBaseURL = function() {
return _instance._baseURL;
};
/**
Retrieve the current app ID
@returns {String} The current app ID
*/
Kii.getAppID = function() {
return _instance._appID;
};
/**
Retrieve the current app key
@returns {String} The current app key
*/
Kii.getAppKey = function() {
return _instance._appKey;
};
Kii.isLogging = function() {
return _instance._logging;
};
Kii.setLogging = function(logging) {
root.Kii.logger("Setting logging: " + logging);
return _instance._logging = logging;
};
/** Initialize the Kii SDK with a specific URL
Should be the first Kii SDK action your application makes
@param String appID The application ID found in your Kii developer console
@param String appKey The application key found in your Kii developer console
@param KiiSite site Can be one of the constants KiiSite.US, KiiSite.JP or KiiSite.CN depending on your location.
@example
Kii.initializeWithSite("my-app-id", "my-app-key", KiiSite.JP);
*/
Kii.initializeWithSite = function(appID, appKey, site) {
if (_instance == null) {
_instance = new _Kii(appID, appKey, site);
}
return root.Kii.logger("Initialized " + appID + ", " + appKey + ", " + site);
};
/** Initialize the Kii SDK
Should be the first Kii SDK action your application makes
@param String appID The application ID found in your Kii developer console
@param String appKey The application key found in your Kii developer console
@example
Kii.initialize("my-app-id", "my-app-key");
*/
Kii.initialize = function(appID, appKey) {
return root.Kii.initializeWithSite(appID, appKey, root.KiiSite.US);
};
Kii.error = function(message) {
return console.log("KiiSDK Error => " + message);
};
Kii.logger = function(message) {
if (_instance._logging) {
return console.log(message);
}
};
/**
Creates a reference to a bucket for this user
<br><br>The bucket will be created/accessed within this app's scope
@param String bucketName The name of the bucket the app should create/access
@returns {KiiBucket} A working KiiBucket object
@example
var bucket = Kii.bucketWithName("myBucket");
*/
Kii.bucketWithName = function(bucketName) {
return new root.KiiBucket._bucketWithName(bucketName, null);
};
/**
Creates a reference to a group with the given name
@param {String} groupName An application-specific group name
@returns {KiiGroup} A new KiiGroup reference
@example
var group = new Kii.groupWithName("myGroup");
*/
Kii.groupWithName = function(groupName) {
return new root.Kii.groupWithNameAndMembers(groupName, null);
};
/**
Creates a reference to a group with the given name and a list of default members
@param {String} groupName An application-specific group name
@param {Array} members An array of KiiUser objects to add to the group
@returns {KiiGroup} A new KiiGroup reference
@example
var group = new KiiGroup.groupWithName("myGroup", members);
*/
Kii.groupWithNameAndMembers = function(groupName, members) {
return new root.KiiGroup.groupWithNameAndMembers(groupName, members);
};
Kii.logOut = function() {
return _instance._currentUser = null;
};
Kii.loggedIn = function() {
return _instance._currentUser != null;
};
Kii.getCurrentUser = function() {
var user;
if (_instance._currentUser != null) {
user = new root.KiiUser();
user._uuid = _instance._currentUser._uuid;
user._username = _instance._currentUser._username;
user._displayName = _instance._currentUser._displayName;
user._password = _instance._currentUser._password;
user._emailAddress = _instance._currentUser._emailAddress;
user._phoneNumber = _instance._currentUser._phoneNumber;
user._country = _instance._currentUser._country;
user._created = _instance._currentUser._created;
user._modified = _instance._currentUser._modified;
user._emailVerified = _instance._currentUser._emailVerified;
user._phoneVerified = _instance._currentUser._phoneVerified;
user._accessToken = _instance._currentUser._accessToken;
user._customInfo = _instance._currentUser._customInfo;
root.Kii.logger("my instance:");
root.Kii.logger(user);
return user;
} else {
return null;
}
};
Kii.setCurrentUser = function(user) {
_instance._currentUser = new root.KiiUser();
_instance._currentUser._uuid = user._uuid;
_instance._currentUser._username = user._username;
_instance._currentUser._displayName = user._displayName;
_instance._currentUser._password = user._password;
_instance._currentUser._emailAddress = user._emailAddress;
_instance._currentUser._phoneNumber = user._phoneNumber;
_instance._currentUser._country = user._country;
_instance._currentUser._created = user._created;
_instance._currentUser._modified = user._modified;
_instance._currentUser._emailVerified = user._emailVerified;
_instance._currentUser._phoneVerified = user._phoneVerified;
_instance._currentUser._accessToken = user._accessToken;
_instance._currentUser._customInfo = user._customInfo;
root.Kii.logger("my instance:");
return root.Kii.logger(_instance._currentUser);
};
/** Authenticate as app admin.
<br><br>
<b>This api call must not placed on code which can be accessed by browser.
This api is intended to be used by server side code like Node.js.
If you use this api in code accessible by browser, your application id and application secret could be stolen.
Attacker will be act as appadmin and all the data in your application will be suffered.
</b>
@param {String} clientId assigned to your application.
@param {String} clientSecret assigned to your application.
@param {Method} callbacks.success The callback method called when authentication succeeded.
@param {Method} callbacks.failure The callback method called when authentication failed.
@example
Kii.authenticateAsAppAdmin("your client id", "your client secret", {
success: function(adminContext) {
// adminContext : KiiAppAdminContext instance
// Operate entities with adminContext.
},
failure: function(error, statusCode) {
// Authentication failed.
}
);
*/
Kii.authenticateAsAppAdmin = function(clientId, clientSecret, callbacks) {
var authCallbacks, request,
_this = this;
request = new KiiRequest("/oauth2/token", false);
request.setAnonymous(true);
request.setMethod("POST");
request.setData({
'client_id': clientId,
'client_secret': clientSecret
});
authCallbacks = {
success: function(data) {
var admin, id, token;
token = data.access_token;
id = data.id;
root.Kii.logger("token: " + token);
root.Kii.logger("id: " + id);
admin = new root.KiiAppAdminContext({
token: token,
id: id
});
if (callbacks != null) {
return callbacks.success(admin);
}
},
failure: function(error, statusCode) {
root.Kii.logger("error: " + error);
root.Kii.logger("statusCode: " + statusCode);
if (callbacks != null) {
return callbacks.failure(error, statusCode);
}
}
};
return request.execute(authCallbacks, false);
};
return Kii;
})();
_Kii = (function() {
_Kii.prototype._logging = false;
_Kii.prototype._baseURL = null;
_Kii.prototype._currentUser = null;
function _Kii(appID, appKey, site) {
console.log("Setting site: " + site);
this._appKey = appKey;
this._appID = appID;
this._baseURL = site;
}
return _Kii;
})();
/**
@class Represents a KiiACL object
@exports root.KiiACL as KiiACL
*/
root.KiiACL = (function() {
var _entries, _parent, _thisACL;
_thisACL = null;
_entries = null;
_parent = null;
function KiiACL() {
this._userWithID = __bind(this._userWithID, this);
this._groupWithID = __bind(this._groupWithID, this);
this._getRequest = __bind(this._getRequest, this);
this._saveItr = __bind(this._saveItr, this);
this.save = __bind(this.save, this);
this.removeACLEntry = __bind(this.removeACLEntry, this);
this.putACLEntry = __bind(this.putACLEntry, this);
this.listACLEntries = __bind(this.listACLEntries, this);
this.aclPath = __bind(this.aclPath, this);
this._setParent = __bind(this._setParent, this); this._entries = [];
}
KiiACL.prototype._setParent = function(_parent) {
this._parent = _parent;
};
KiiACL.prototype.aclPath = function() {
var bucket, bucketName, group, object, objectId, path, user;
if (this._parent instanceof root.KiiObject) {
object = this._parent;
if (object.getBucket().getUser() != null) {
user = object.getBucket().getUser();
} else if (object.getBucket().getGroup() != null) {
group = object.getBucket().getGroup();
}
bucketName = object.getBucket().getBucketName();
objectId = object.getUUID();
} else if (this._parent instanceof root.KiiBucket) {
bucket = this._parent;
if (bucket.getUser() != null) {
user = bucket.getUser();
} else if (bucket.getGroup() != null) {
group = bucket.getGroup();
}
bucketName = bucket.getBucketName();
} else {
root.Kii.error("Invalid ACL parent. Must belong to a KiiObject");
}
path = "/";
if (group != null) {
path += "groups/" + (group.getUUID()) + "/";
} else if (user != null) {
path += "users/" + (user.getUUID()) + "/";
}
if (objectId != null) {
path += "buckets/" + bucketName + "/objects/" + objectId + "/acl";
} else {
path += "buckets/" + bucketName + "/acl";
}
return path;
};
/** Get the list of active ACLs associated with this object from the server
@param {Object} callbacks An object with callback methods defined
@param {Method} callbacks.success The callback method to call on a successful list request
@param {Method} callbacks.failure The callback method to call on a failed list request
@example
var acl = . . .; // a KiiACL object
acl.listACLEntries({
success: function(theACL, theEntries) {
// do something
},
failure: function(theACL, anErrorString) {
// do something with the error response
}
});
*/
KiiACL.prototype.listACLEntries = function(callbacks) {
var listCallbacks, request,
_this = this;
_thisACL = this;
root.Kii.logger("Listing ACL entries");
request = this._getRequest({
path: this.aclPath(),
withApp: true
});
listCallbacks = {
success: function(data, statusCode) {
var action, entity, key, results, subject, value, _i, _len;
if (statusCode < 300 && statusCode >= 200) {
results = [];
for (key in data) {
value = data[key];
if (key === "WRITE_EXISTING_OBJECT") {
action = root.KiiACLAction.KiiACLObjectActionWrite;
} else if (key === "READ_EXISTING_OBJECT") {
action = root.KiiACLAction.KiiACLObjectActionRead;
} else if (key === "QUERY_OBJECTS_IN_BUCKET") {
action = root.KiiACLAction.KiiACLBucketActionQueryObjects;
} else if (key === "CREATE_OBJECTS_IN_BUCKET") {
action = root.KiiACLAction.KiiACLBucketActionCreateObjects;
} else if (key === "DROP_BUCKET_WITH_ALL_CONTENT") {
action = root.KiiACLAction.KiiACLBucketActionDropBucket;
}
for (_i = 0, _len = value.length; _i < _len; _i++) {
entity = value[_i];
if (entity.groupID != null) {
subject = _this._groupWithID(entity.groupID);
} else if (entity.userID != null) {
subject = _this._userWithID(entity.userID);
}
results.push(KiiACLEntry.entryWithSubject(subject, action));
}
}
return callbacks.success(_thisACL, results);
} else {
return callbacks.failure(_thisACL, "Unable to retrieve ACL list");
}
},
failure: function(error, statusCode) {
return callbacks.failure(_thisACL, error);
}
};
return request.execute(listCallbacks, false);
};
/** Add a KiiACLEntry to the local object, if not already present. This does not explicitly grant any permissions, which should be done through the KiiACLEntry itself. This method simply adds the entry to the local ACL object so it can be saved to the server.
@param {KiiACLEntry} entry The KiiACLEntry to add
@example
var aclEntry = . . .; // a KiiACLEntry object
var acl = . . .; // a KiiACL object
acl.putACLEntry(aclEntry);
*/
KiiACL.prototype.putACLEntry = function(entry) {
if (($.inArray(entry, this._entries)) === -1) {
return this._entries.push(entry);
}
};
/** Remove a KiiACLEntry to the local object. This does not explicitly revoke any permissions, which should be done through the KiiACLEntry itself. This method simply removes the entry from the local ACL object and will not be saved to the server.
@param {KiiACLEntry} entry The KiiACLEntry to remove
@example
var aclEntry = . . .; // a KiiACLEntry object
var acl = . . .; // a KiiACL object
acl.removeACLEntry(aclEntry);
*/
KiiACL.prototype.removeACLEntry = function(entry) {
var ndx;
ndx = $.inArray(entry, this._entries);
if (ndx > -1) {
return KiiUtilities.arrayRemove(this._entries, ndx, ndx);
}
};
/** Save the list of ACLEntry objects associated with this ACL object to the server
@param {Object} callbacks An object with callback methods defined
@param {Method} callbacks.success The callback method to call on a successful save request
@param {Method} callbacks.failure The callback method to call on a failed save request
@example
var obj = . . .; // a KiiObject
obj.save({
success: function(theSavedACL) {
// do something with the saved acl
},
failure: function(theACL, anErrorString) {
// do something with the error response
}
});
*/
KiiACL.prototype.save = function(callbacks) {
root.Kii.logger("SAving acl");
return this._saveItr(0, callbacks);
};
KiiACL.prototype._saveItr = function(idx, callbacks) {
var aclEntry, path, request, _errorStr,
_this = this;
_this = this;
aclEntry = this._entries[idx];
path = "" + (this.aclPath()) + "/" + (aclEntry.getActionString()) + "/" + (aclEntry.getEntityString());
root.Kii.logger("Saving single @path: " + path);
request = this._getRequest({
path: path,
withApp: true
});
request.setMethod(aclEntry.getGrant() === true ? "PUT" : "DELETE");
_errorStr = null;
return request.execute({
success: function(data, status, xhr) {
if (idx < _this._entries.length - 1) {
return _this._saveItr(idx + 1, callbacks);
} else {
return callbacks.success(_this);
}
},
failure: function(errorStr, statusCode, errorCode) {
return callbacks.failure(_this, errorStr);
}
}, false);
};
KiiACL.aclWithParent = function(parent) {
var acl;
acl = new root.KiiACL();
acl._setParent(parent);
return acl;
};
KiiACL.prototype._getRequest = function(spec) {
var path, request, withApp;
path = spec.path;
withApp = spec.withApp;
request = new KiiRequest(path, withApp);
return request;
};
KiiACL.prototype._groupWithID = function(id) {
var group;
group = root.KiiGroup._groupWithID(id);
return group;
};
KiiACL.prototype._userWithID = function(id) {
var user;
user = root.KiiUser._userWithID(id);
return user;
};
return KiiACL;
}).call(this);
root.KiiACLAction = {
KiiACLBucketActionCreateObjects: 0,
KiiACLBucketActionQueryObjects: 1,
KiiACLBucketActionDropBucket: 2,
KiiACLObjectActionRead: 3,
KiiACLObjectActionWrite: 4
};
/**
@class Represents a KiiACLEntry object
@exports root.KiiACLEntry as KiiACLEntry
*/
root.KiiACLEntry = (function() {
var _action, _grant, _subject;
_action = null;
_subject = null;
_grant = null;
function KiiACLEntry() {
this.getEntityString = __bind(this.getEntityString, this);
this.getActionString = __bind(this.getActionString, this);
this.getGrant = __bind(this.getGrant, this);
this.setGrant = __bind(this.setGrant, this);
this.getSubject = __bind(this.getSubject, this);
this.setSubject = __bind(this.setSubject, this);
this.getAction = __bind(this.getAction, this);
this.setAction = __bind(this.setAction, this); this._action = -1;
this._grant = true;
}
/** The action that is being permitted/restricted. Possible values:
<br><br>
KiiACLAction.KiiACLBucketActionCreateObjects,<br>
KiiACLAction.KiiACLBucketActionQueryObjects, <br>
KiiACLAction.KiiACLBucketActionDropBucket,<br>
KiiACLAction.KiiACLObjectActionRead,<br>
KiiACLAction.KiiACLObjectActionWrite
@param {KiiACLAction} value The action being permitted/restricted
@throws {InvalidACLAction} If the value is not one of the permitted values
*/
KiiACLEntry.prototype.setAction = function(value) {
if (value >= root.KiiACLAction.KiiACLBucketActionCreateObjects && value <= root.KiiACLAction.KiiACLObjectActionWrite) {
return this._action = value;
} else {
throw new InvalidACLAction;
}
};
/** Get the action that is being permitted/restricted in this entry
@returns {KiiACLAction}
*/
KiiACLEntry.prototype.getAction = function() {
return this._action;
};
/** The KiiUser or KiiGroup entity that is being permitted/restricted
@param {KiiUser|KiiGroup} value The entity being permitted/restricted
@throws {InvalidACLSubject} If the value is not one of the permitted values
*/
KiiACLEntry.prototype.setSubject = function(value) {
if (value instanceof root.KiiGroup || value instanceof root.KiiUser || value instanceof root.KiiAnyAuthenticatedUser || value instanceof root.KiiAnonymousUser) {
return this._subject = value;
} else {
throw new InvalidACLSubject;
}
};
/** Get the subject that is being permitted/restricted in this entry
@returns {KiiUser|KiiGroup}
*/
KiiACLEntry.prototype.getSubject = function() {
return this._subject;
};
/** Set whether or not the action is being permitted to the subject
@param {Boolean} value true if the action is permitted, false otherwise
@throws {InvalidACLGrant} If the value is not a boolean type
*/
KiiACLEntry.prototype.setGrant = function(value) {
if (value === true || value === false) {
return this._grant = value;
} else {
throw new InvalidACLGrant;
}
};
/** Get whether or not the action is being permitted to the subject
@returns {Boolean}
*/
KiiACLEntry.prototype.getGrant = function() {
return this._grant;
};
/** Create a KiiACLEntry object with a subject and action
The entry will not be applied on the server until the KiiACL object is explicitly saved. This method simply returns a working KiiACLEntry with a specified subject and action.
@param {KiiGroup|KiiUser|KiiAnyAuthenticatedUser|KiiAnonymousUser} subject A KiiGroup or KiiUser object to which the action/grant is being applied
@param {KiiACLAction} action One of the specified KiiACLAction values the permissions is being applied to
@return A KiiACLEntry object with the specified attributes
*/
KiiACLEntry.entryWithSubject = function(subject, action) {
var entry;
root.Kii.logger("EWS: " + subject + ", " + action);
entry = new root.KiiACLEntry();
entry.setSubject(subject);
entry.setAction(action);
return entry;
};
KiiACLEntry.prototype.getActionString = function() {
var retString;
root.Kii.logger("Action: " + this.action);
switch (this._action) {
case root.KiiACLAction.KiiACLBucketActionCreateObjects:
retString = "CREATE_OBJECTS_IN_BUCKET";
break;
case root.KiiACLAction.KiiACLBucketActionQueryObjects:
retString = "QUERY_OBJECTS_IN_BUCKET";
break;
case root.KiiACLAction.KiiACLBucketActionDropBucket:
retString = "DROP_BUCKET_WITH_ALL_CONTENT";
break;
case root.KiiACLAction.KiiACLObjectActionRead:
retString = "READ_EXISTING_OBJECT";
break;
case root.KiiACLAction.KiiACLObjectActionWrite:
retString = "WRITE_EXISTING_OBJECT";
break;
default:
return retString;
}
return retString;
};
KiiACLEntry.prototype.getEntityString = function() {
var entityId, type;
if (this._subject instanceof root.KiiGroup) {
entityId = this._subject.getUUID();
type = "GroupID";
} else if (this._subject instanceof root.KiiUser) {
entityId = this._subject.getUUID();
type = "UserID";
} else if (this._subject instanceof root.KiiAnyAuthenticatedUser) {
type = "UserID";
entityId = "ANY_AUTHENTICATED_USER";
} else if (this._subject instanceof root.KiiAnonymousUser) {
type = "UserID";
entityId = "ANONYMOUS_USER";
}
return "" + type + ":" + entityId;
};
return KiiACLEntry;
}).call(this);
/**
@class Represents a KiiBucket object
@exports root.KiiBucket as KiiBucket
*/
root.KiiBucket = (function() {
var _bucketName, _group, _thisBucket, _user;
KiiBucket.prototype._className = "KiiBucket";
_thisBucket = null;
_bucketName = null;
_user = null;
_group = null;
KiiBucket.prototype.getUser = function() {
return this._user;
};
KiiBucket.prototype._setUser = function(_user) {
this._user = _user;
};
KiiBucket.prototype.getGroup = function() {
return this._group;
};
KiiBucket.prototype._setGroup = function(_group) {
this._group = _group;
};
/** The name of this bucket
@returns {String}
*/
KiiBucket.prototype.getBucketName = function() {
return this._bucketName;
};
KiiBucket.prototype._setBucketName = function(_bucketName) {
this._bucketName = _bucketName;
};
/** Create a KiiObject within the current bucket
<br><br>The object will not be created on the server until the KiiObject is explicitly saved. This method simply returns an empty working KiiObject.
@returns {KiiObject} An empty KiiObject with no specific type
@example
var bucket = . . .; // a KiiBucket
var object = bucket.createObject();
*/
KiiBucket.prototype.createObject = function() {
return root.KiiObject.objectWithBucket(this, null);
};
/** Create a KiiObject within the current bucket, with type
<br><br>The object will not be created on the server until the KiiObject is explicitly saved. This method simply returns an empty working KiiObject with a specified type. The type allows for better indexing and improved query results. It is recommended to use this method - but for lazy creation, the createObject method is also available.
@param String type A string representing the desired object type
@returns An empty KiiObject with specified type
@example
var bucket = . . .; // a KiiBucket
var object = bucket.createObjectWithType("scores");
*/
KiiBucket.prototype.createObjectWithType = function(type) {
return root.KiiObject.objectWithBucket(this, type);
};
/** Get the ACL handle for this bucket
<br><br>Any KiiACLEntry objects added or revoked from this ACL object will be appended to/removed from the server on ACL save.
@returns {KiiACL} A KiiACL object associated with this KiiObject
@example
var bucket = . . .; // a KiiBucket
var acl = bucket.acl();
*/
KiiBucket.prototype.acl = function() {
return root.KiiACL.aclWithParent(this);
};
/** Perform a query on the given bucket
<br><br>The query will be executed against the server, returning a result set.
@param KiiQuery query An object with callback methods defined
@param Object callbacks An object with callback methods defined
@param {Method} callbacks.success The callback method to call on a successful query request
@param {Method} callbacks.failure The callback method to call on a failed query request
@example
var bucket = . . .; // a KiiBucket
var queryObject = . . .; // a KiiQuery
// define the callbacks (stored in a variable for reusability)
var queryCallbacks = {
success: function(queryPerformed, resultSet, nextQuery) {
// do something with the results
for(var i=0; i<resultSet.length; i++) {
// do something with the object
// resultSet[i]; // could be KiiObject, KiiGroup, KiiUser, etc
}
// if there are more results to be retrieved
if(nextQuery != null) {
// get them and repeat recursively until no results remain
bucket.executeQuery(nextQuery, queryCallbacks);
}
},
failure: function(queryPerformed, anErrorString) {
// do something with the error response
}
};
bucket.executeQuery(queryObject, queryCallbacks);
*/
KiiBucket.prototype.executeQuery = function(query, callbacks) {
var clauseData, data, executeCallbacks, path, request,
_this = this;
_thisBucket = this;
path = this._generatePath() + "/query";
data = {};
if (query != null) {
clauseData = query._dictValue();
data.bestEffortLimit = query.getLimit();
if (query.getPaginationKey() != null) {
data.paginationKey = query.getPaginationKey();
}
} else {
clauseData = {
"clause": root.KiiQuery._emptyDictValue()
};
}
request = this._getRequest({
path: path,
withApp: true
});
request.setMethod("POST");
request.setContentType("application/vnd.kii.QueryRequest+json");
data.bucketQuery = clauseData;
request.setData(data);
executeCallbacks = {
success: function(data, statusCode) {
var nextQuery, result, resultSet, _i, _len, _ref;
if (statusCode < 300 && statusCode >= 200) {
resultSet = [];
_ref = data.results;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
result = _ref[_i];
resultSet.push(_thisBucket.objectWithJSON(result));
}
if (data.nextPaginationKey != null) {
nextQuery = jQuery.extend(true, {}, query);
nextQuery.setPaginationKey(data.nextPaginationKey);
}
if (callbacks != null) {
return callbacks.success(query, resultSet, nextQuery);
}
} else if (callbacks != null) {
return callbacks.failure(query, "Unable to parse response");
}
},
failure: function(error, statusCode) {
if (callbacks != null) {
return callbacks.failure(_thisBucket, error);
}
}
};
return request.execute(executeCallbacks, false);
};
/** Delete the given bucket from the server
@param Object callbacks An object with callback methods defined
@param {Method} callbacks.success The callback method to call on a successful query request
@param {Method} callbacks.failure The callback method to call on a failed query request
@example
var bucket = . . .; // a KiiBucket
bucket.delete({
success: function(deletedBucket) {
// do something with the result
},
failure: function(bucketToDelete, anErrorString) {
// do something with the error response
}
});
*/
KiiBucket.prototype["delete"] = function(callbacks) {
var executeCallbacks, request,
_this = this;
_thisBucket = this;
request = this._getRequest({
path: this._generatePath(),
withApp: true
});
request.setMethod("DELETE");
executeCallbacks = {
success: function(data, statusCode) {
if (callbacks != null) {
return callbacks.success(_thisBucket);
}
},
failure: function(error, statusCode) {
if (callbacks != null) {
return callbacks.failure(_thisBucket, error);
}
}
};
return request.execute(executeCallbacks, true);
};
function KiiBucket(bucketName, parent) {
this.objectWithJSON = __bind(this.objectWithJSON, this);
this._generatePath = __bind(this._generatePath, this);
this._getRequest = __bind(this._getRequest, this);
this["delete"] = __bind(this["delete"], this);
this.executeQuery = __bind(this.executeQuery, this);
this.acl = __bind(this.acl, this);
this.createObjectWithType = __bind(this.createObjectWithType, this);
this.createObject = __bind(this.createObject, this);
this._setBucketName = __bind(this._setBucketName, this);
this.getBucketName = __bind(this.getBucketName, this);
this._setGroup = __bind(this._setGroup, this);
this.getGroup = __bind(this.getGroup, this);
this._setUser = __bind(this._setUser, this);
this.getUser = __bind(this.getUser, this); this._bucketName = bucketName;
if (parent != null) {
if (parent instanceof root.KiiGroup) {
this._group = parent;
} else if (parent instanceof root.KiiUser) {
this._user = parent;