-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdreamfactory-user-management.js
executable file
·1086 lines (780 loc) · 40.4 KB
/
dreamfactory-user-management.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
'use strict';
// Module definition and dependencies
angular.module('dfUserManagement', ['ngRoute', 'ngCookies'])
// Set constants for path resolution.
.constant('MODUSRMNGR_ROUTER_PATH', '/user-management')
.constant('MODUSRMNGR_ASSET_PATH', 'bower_components/dreamfactory-user-management/')
// Define a router to handle module routes. None used here
.config(['$routeProvider', 'MODUSRMNGR_ROUTER_PATH', 'MODUSRMNGR_ASSET_PATH',
function ($routeProvider, MODUSRMNGR_ROUTER_PATH, MODUSRMNGR_ASSET_PATH) {
$routeProvider
.when(MODUSRMNGR_ROUTER_PATH, {
templateUrl: MODUSRMNGR_ASSET_PATH + 'views/main.html'
});
}])
.run(['$cookieStore', '$http', 'UserDataService', function ($cookieStore, $http, UserDataService) {
// Let us know what the module is up to
//console.log('RUN BLOCK: Check for and set current user');
var cookie = $cookieStore.get('CurrentUserObj');
// Check if there is a CurrentUserObj in the cookie
if (cookie) {
// There is so store it for a sec
UserDataService.setCurrentUser($cookieStore.get('CurrentUserObj'));
$http.defaults.headers.common['X-DreamFactory-Session-Token'] = cookie.session_id;
}
}])
// Part of the DreamFactory Angular module definition. We don't use this yet.
// Future versions will also include directives/templates for editing current user profile
// and password stuff to complete the module.
.controller('UserManagementCtrl', ['$scope', function ($scope) {
}])
// Part of the DreamFactory Angular module definition. We don't use this yet.
// Future versions will require this as a nav component to move between sections of the
// module for editing user profile/password information
.directive('modusrmngrNavigation', ['MODUSRMNGR_ASSET_PATH',
function (MODUSRMNGR_ASSET_PATH) {
return {
restrict: 'E',
templateUrl: MODUSRMNGR_ASSET_PATH + 'views/navigation.html',
link: function (scope, elem, attrs) {
}
}
}])
// Directive for Login. This is does our login work and provides the attachment point for
// the login portion of our module.
.directive('dreamfactoryUserLogin', ['MODUSRMNGR_ASSET_PATH', 'DSP_URL', '$http', '$cookies', '$cookieStore', 'UserEventsService', 'UserDataService', 'dfObjectService',
function (MODUSRMNGR_ASSET_PATH, DSP_URL, $http, $cookies, $cookieStore, UserEventsService, UserDataService, dfObjectService) {
return {
// only allow as HTML tag
restrict: 'E',
// don't show directive tag
replace: true,
// isolate scope
scope: {
// define optional options attribute
options: '=?'
},
// template path
templateUrl: MODUSRMNGR_ASSET_PATH + 'views/login.html',
// link it up
link: function (scope, elem, attrs) {
// CREATE SHORT NAMES
scope.es = UserEventsService.login;
// PUBLIC VARS
// This holds our options object. If we don't provide an options object
// it defaults to showing the template. This is currently the only option
var defaults = {showTemplate: true};
scope.options = dfObjectService.mergeObjects(scope.options, defaults);
// This is included on the top level tag of our directive template and
// controls whether the template is rendered or not.
scope.showTemplate = scope.options.showTemplate;
scope.loginActive = true;
// PUBLIC API
// The public api section contains any functions that we wish to call
// in our HTML templates. Functions placed here should be the only
// functions that are 'accessible' or called through our HTML template.
// The only logic that should ever be included is logic pertaining to whether
// or not we should run the complex implementation. Things like using a confirm
// function to decide whether a record should be deleted or not go here.
// This is the function we call in the UI for login.
scope.login = function (credsDataObj) {
// This calls our complex implementation of login()
scope._login(credsDataObj);
};
scope.showForgotPassword = function () {
scope._toggleForms();
};
scope.showLoginForm = function () {
scope._toggleForms();
};
// PRIVATE API
// The private api section contains functions that do most of our heavy lifting
// Although they are on the $scope(scope) of the directive we never call these
// from the HTML templates. They are meant to be called from the 'Complex Implementation'
// section below.
// POST to the DreamFactory(DF) rest api to login
scope._loginRequest = function (credsDataObj) {
// Return the posted request data as a promise
return $http.post(DSP_URL + '/rest/user/session', credsDataObj);
};
// Set the session token
scope._setSessionToken = function (sessionDataObj) {
// Set the session id from a passed in session data object
// as a cookie
$cookies.PHPSESSID = sessionDataObj.session_id;
};
// Store the logged in user
scope._setCurrentUser = function (sessionDataObj) {
// Stores the logged in user in a cookie
$cookieStore.put('CurrentUserObj', sessionDataObj)
};
// Call our cookie setting functions
scope._setCookies = function (sessionDataObj) {
// Check if the session id has been updated. If so use that to set the cookie
// If it hasn't just use the old session id
$cookies.PHPSESSID = $cookies.PHPSESSID === sessionDataObj.session_id ? $cookies.PHPSESSID : sessionDataObj.session_id;
// call set current user with the session data obj
scope._setCurrentUser(sessionDataObj);
};
// toggle login/forgot password forms
scope._toggleFormsState = function () {
scope.loginActive = !scope.loginActive;
scope.resetPasswordActive = !scope.resetPasswordActive;
};
// COMPLEX IMPLEMENTATION
// The complex implementation section is where our Private Api is called to action.
// This is where the magic happens for our public api. Generally, these functions relate
// directly with our Public Api and are denoted as so with an underscore preceding the
// function name.
// Run login implementation
scope._login = function (credsDataObj) {
// call private login request function with a credentials object
scope._loginRequest(credsDataObj).then(
// success method
function (result) {
// remove unnecessary apps data
// this is temporary and cleans up our
// session obj that is returned by the login function
// this data will be removed from the session object in
// DSP v2 so if you use it for anything currently
// beware and use at your own risk
delete result.data.no_group_apps;
delete result.data.app_groups;
// Set the cookies
scope._setCookies(result.data);
// Set the DreamFactory session header
$http.defaults.headers.common['X-DreamFactory-Session-Token'] = $cookies.PHPSESSID;
// Set the current user in the UserDataService service
UserDataService.setCurrentUser(result.data);
// Emit a success message so we can hook in
scope.$emit(scope.es.loginSuccess, result.data);
},
// Error method
function (reject) {
// Throw a DreamFactory error object
throw {
module: 'DreamFactory User Management',
type: 'error',
provider: 'dreamfactory',
exception: reject
}
}
)
};
scope._toggleForms = function () {
scope._toggleFormsState();
};
// WATCHERS AND INIT
// We define any watchers or init code that needs to be run here.
// HANDLE MESSAGES
// We handle messages passed to our directive here. Most commonly this will
// serve as a way to invoke directive functionality without the need to call
// the public api function directly
// Invoke the complex implementation for _login(). This requires you
// to pass the proper creds object
scope.$on(scope.es.loginRequest, function (e, userDataObj) {
// Call the complex implementation to handle the login request
scope._login(userDataObj);
});
}
}
}])
// Forgot Password Email Confirmation
.directive('dreamfactoryForgotPwordEmail', ['MODUSRMNGR_ASSET_PATH', 'DSP_URL', '$http', 'UserEventsService', function (MODUSRMNGR_ASSET_PATH, DSP_URL, $http, UserEventsService) {
return {
restrict: 'E',
replace: true,
scope: false,
templateUrl: MODUSRMNGR_ASSET_PATH + 'views/fp-email-conf.html',
link: function (scope, elem, attrs) {
// CREATE SHORT NAMES
scope.es = UserEventsService.password;
// PUBLIC API
scope.requestPasswordReset = function (emailDataObj) {
// Pass email address in object to the _requestPasswordReset function
scope._requestPasswordReset(emailDataObj);
};
// PRIVATE API
scope._resetPasswordRequest = function (requestDataObj) {
// Post request for password change and return promise
return $http.post(DSP_URL + '/rest/user/password', requestDataObj);
};
// COMPLEX IMPLEMENTATION
scope._requestPasswordReset = function (requestDataObj) {
// Add property to the request data
// this contains an object with the email address
requestDataObj['reset'] = true;
// Ask the DSP to resset the password via email confirmation
scope._resetPasswordRequest(requestDataObj).then(
// handle successful password reset
function (data) {
// Emit a confirm message indicating that is the next step
scope.$emit(scope.es.passwordResetRequestSuccess, requestDataObj.email);
},
// handle error
function (reject) {
// Throw a DreamFactory error object
throw {
module: 'DreamFactory User Management',
type: 'error',
provider: 'dreamfactory',
exception: reject
}
}
)
};
// WATCHERS AND INIT
// HANDLE MESSAGES
scope.$on(scope.es.passwordResetRequest, function (e, resetDataObj) {
scope._requestPasswordReset(resetDataObj);
});
}
}
}])
// Forgot Password Security Question
.directive('dreamfactoryForgotPwordQuestion', ['MODUSRMNGR_ASSET_PATH', function (MODUSRMNGR_ASSET_PATH) {
return {
restrict: 'E',
scope: false,
templateUrl: MODUSRMNGR_ASSET_PATH + 'views/fp-security-question.html',
link: function (scope, elem, attrs) {
}
}
}])
.directive('dreamfactoryPasswordReset', ['MODUSRMNGR_ASSET_PATH', 'DSP_URL', '$http', 'UserEventsService', 'dfStringService', 'dfObjectService',
function (MODUSRMNGR_ASSET_PATH, DSP_URL, $http, UserEventsService, dfStringService, dfObjectService) {
return {
restrict: 'E',
scope: {
options: '=?'
},
templateUrl: MODUSRMNGR_ASSET_PATH + 'views/password-reset.html',
link: function (scope, elem, attrs) {
// CREATE SHORT NAMES
scope.es = UserEventsService.password;
// PUBLIC VARS
// This holds our options object. If we don't provide an options object
// it defaults to showing the template.
var defaults = {showTemplate: true, login: false};
scope.options = dfObjectService.mergeObjects(scope.options, defaults);
// This is included on the top level tag of our directive template and
// controls whether the template is rendered or not.
scope.showTemplate = scope.options.showTemplate;
// Holds value to for identical password check
scope.identical = true;
// PUBLIC API
scope.resetPassword = function (credsDataObj) {
if (scope.identical) {
scope._resetPassword(credsDataObj)
} else {
throw 'Passwords do not match.'
}
};
scope.verifyPassword = function (user) {
scope._verifyPassword(user);
};
// PRIVATE API
scope._setPasswordRequest = function (requestDataObj) {
return $http({
url: DSP_URL + '/rest/user/password',
method: 'POST',
params: {
login: scope.options.login
},
data: requestDataObj
});
};
// Test if our entered passwords are identical
scope._verifyPassword = function (userDataObj) {
scope.identical = dfStringService.areIdentical(userDataObj.new_password, userDataObj.verify_password);
};
// COMPLEX IMPLEMENTATION
scope._resetPassword = function (credsDataObj) {
var requestDataObj = {
email: credsDataObj.email,
code: credsDataObj.code,
new_password: credsDataObj.new_password
};
scope._setPasswordRequest(requestDataObj).then(
function (result) {
console.log(result);
scope.$emit(scope.es.passwordSetSuccess)
},
function (reject) {
// Throw a DreamFactory error object
throw {
module: 'DreamFactory User Management',
type: 'error',
provider: 'dreamfactory',
exception: reject
}
}
)
};
// WATCHERS AND INIT
//HANDLE MESSAGES
scope.$on(scope.es.passwordSetRequest, function (e, credsDataObj) {
scope._resetPassword(credsDataObj);
});
}
}
}])
// Logout Directive
.directive('dreamfactoryUserLogout', ['DSP_URL', '$http', '$cookieStore', 'UserEventsService', 'UserDataService',
function (DSP_URL, $http, $cookieStore, UserEventsService, UserDataService) {
return {
restrict: 'E',
scope: {},
link: function (scope, elem, attrs) {
// CREATE SHORT NAMES
scope.es = UserEventsService.logout;
// PUBLIC API ** See login directive for more info **
// No methods defined here.
// PRIVATE API ** See login directive for more info **
// DELETE request for logging out user
scope._logoutRequest = function () {
// return a promise object from the rest call
return $http.delete(DSP_URL + '/rest/user/session');
};
// COMPLEX IMPLEMENTATION ** See login directive for more info **
scope._logout = function () {
// Call to server for logout request
scope._logoutRequest().then(
// success method
function () {
// remove session cookie
$cookieStore.remove('PHPSESSID');
// remove current user cookie
$cookieStore.remove('CurrentUserObj');
// remove user from UserDataService
UserDataService.unsetCurrentUser();
// Unset DreamFactory header
$http.defaults.headers.common['X-DreamFactory-Session-Token'] = '';
// Emit success message so we can hook in
scope.$emit(scope.es.logoutSuccess, false);
},
// Error method
function (reject) {
// Throw DreamFactory error object
throw {
module: 'DreamFactory User Management',
type: 'error',
provider: 'dreamfactory',
exception: reject
}
})
};
// WATCHERS AND INIT ** See login directive for more info **
// No watchers defined
// HANDLE MESSAGES ** See login directive for more info **
// Handle logout request message from application
scope.$on(scope.es.logoutRequest, function (e) {
// call complex implementation of logout
scope._logout();
});
// CALL METHOD ON INVOKE
// If we include our logout directive in a template this will automatically
// run when we hit the route and subsequently log us out.
scope._logout();
}
}
}])
// Register Directive. Takes care of registering a user for our application
.directive('dreamfactoryRegisterUser', ['MODUSRMNGR_ASSET_PATH', 'DSP_URL', '$http', '$rootScope', '$cookieStore', 'UserEventsService', 'dfStringService', 'dfObjectService', 'dfXHRHelper',
function (MODUSRMNGR_ASSET_PATH, DSP_URL, $http, $rootScope, $cookieStore, UserEventsService, dfStringService, dfObjectService, dfXHRHelper) {
return {
restrict: 'E',
templateUrl: MODUSRMNGR_ASSET_PATH + 'views/register.html',
scope: {
options: '=?'
},
link: function (scope, elem, attrs) {
// CREATE SHORT NAMES
scope.es = UserEventsService.register;
// PUBLIC VARS
// This holds our options object. If we don't provide an options object
// it defaults to showing the template. It also defines a confirmationRequired attribute
// which can be set at the time of instantiation. If it's not set then it will default
// to the DSP settings.
var defaults = {showTemplate: true, login: false};
scope.options = dfObjectService.mergeObjects(scope.options, defaults);
// This is included on the top level tag of our directive template and
// controls whether the template is rendered or not.
scope.showTemplate = scope.options.showTemplate;
// Holds value to for identical password check
scope.identical = true;
// PUBLIC API ** See login directive for more info **
// Public register function used in our HTML template
scope.register = function (registerDataObj) {
// Call complex implementation
scope._register(registerDataObj);
};
scope.verifyPassword = function (user) {
scope._verifyPassword(user);
};
// PRIVATE API ** See login directive for more info **
// Registers a user via REST API
scope._registerRequest = function (registerDataObj) {
return $http({
url: DSP_URL + '/rest/user/register',
method: 'POST',
params: {
login: scope.options.login
},
data: registerDataObj
});
};
// Returns the system configuration object
scope._getSystemConfig = function () {
return $http.get(DSP_URL + '/rest/system/config');
};
// COMPLEX IMPLEMENTATION ** See login directive for more info **
scope._register = function (registerDataObj) {
if (scope.identical != true) {
// Throw an error
throw {
module: 'DreamFactory User Management',
type: 'error',
provider: 'dreamfactory',
exception: 'Password and confirm password do not match.'
}
}
// Store our implementation of registering a user
scope._runRegister = function (registerDataObj) {
// Add auto login bool
// Pass registerDataObj to scope._registerRequest function and
// then handle the response
scope._registerRequest(registerDataObj).then(
// success
function (result) {
// The scope.options.confirmationRequired value should be set to
// the value of the System Config's open_reg_email_service_id value.
// This let's us know if the admin has required email confirmation for the
// system. Null means no confirmation required.
// Do we need confirmation
if (scope.options.confirmationRequired == null) {
// Build a login object
var userCredsObj = {
email: registerDataObj.email,
password: registerDataObj.new_password
};
// No we don't. Send the success event and the registered user data
scope.$emit(scope.es.registerSuccess, userCredsObj);
} else {
// We do require confirmation so Send the confirmation event and the user data
scope.$emit(scope.es.registerConfirmation, result.data);
}
},
// error
function (reject) {
// Throw an error
throw {
module: 'DreamFactory User Management',
type: 'error',
provider: 'dreamfactory',
exception: reject
}
});
};
// If we have a SystemConfig and we have passed in the proper value(see scope.options explanation above)
// then we don't waste a call to the system.
// If we have not then we need to know this about the system.
if (scope.options.confirmationRequired == null) {
// We did not pass in a usable options object
// Ask the server for the config
scope._getSystemConfig().then(
// success
function (result) {
// store the config object
var systemConfigDataObj = result.data;
// Set the options object to the proper values
// The success method of scope._registerRequest function looks
// for this property to determine which message to emit back up to
// the application. If this value is null the scope._registerRequest will emit a
// 'user:register:success' method indicating that we are done registering
// the user. If it contains a value (denoting that an email service has been selected)
// We emit a 'user:register:confirmation' message. How you handle these messages is left
// up to you. We just notify you of the current state and the actions that have been taken as
// a result of your config.
scope.options.confirmationRequired = systemConfigDataObj.open_reg_email_service_id;
// Now that we have all the info we need, lets run the
// register routine
scope._runRegister(registerDataObj);
},
// There was an error retrieving the config
function (reject) {
// Throw an error
throw {
module: 'DreamFactory User Management',
type: 'error',
provider: 'dreamfactory',
exception: reject
}
}
)
}
else {
// We were passed an options object
// Run the register routine
scope._runRegister(registerDataObj);
}
};
// Test if our entered passwords are identical
scope._verifyPassword = function (userDataObj) {
scope.identical = dfStringService.areIdentical(userDataObj.new_password, userDataObj.verify_password);
};
// WATCHERS AND INIT ** See login directive for more info **
// Watch our options object
scope.$watchCollection('options', function (newValue, oldValue) {
// If we don't have a confirmationRequiredProperty set
if (!newValue.hasOwnProperty('confirmationRequired')) {
// We go ask the server to get it. Everything stops until this guy returns.
// SYNCHRONOUS
//scope.options.confirmationRequired = xhrHelper.getSystemConfigFromServer().allow_open_registration;
var config = dfXHRHelper.get({
url: 'system/config'
});
scope.options.confirmationRequired = config.allow_open_registration && config.open_reg_email_service_id ? true : null;
}
});
// HANDLE MESSAGES ** See login directive for more info **
// We received a message to register a user.
scope.$on(scope.es.registerRequest, function (e, registerDataObj) {
// register the user
scope._register(registerDataObj);
});
}
}
}])
// This service gives us a way to pass namespaced events around our application
// We inject this service in order to request and respond to different module events.
.service('UserEventsService', [function () {
return {
login: {
loginRequest: 'user:login:request',
loginSuccess: 'user:login:success',
loginError: 'user:login:error'
},
logout: {
logoutRequest: 'user:logout:request',
logoutSuccess: 'user:logout:success',
logoutError: 'user:logout:error'
},
register: {
registerRequest: 'user:register:request',
registerSuccess: 'user:register:success',
registerError: 'user:register:error',
registerConfirmation: 'user:register:confirmation'
},
password: {
passwordResetRequest: 'user:passwordreset:request',
passwordResetRequestSuccess: 'user:passwordreset:requestsuccess',
passwordSetRequest: 'user:passwordset:request',
passwordSetSuccess: 'user:passwordset:success'
}
}
}])
// This service gives us access to the current user. While it's pretty sparse
// at the moment it does give us access to critical user/session data. Inject this
// service where ever you need to access the current user.
.service('UserDataService', [function () {
// Stored user.
var currentUser = false;
// Private methods
// return current user
function _getCurrentUser() {
return currentUser;
}
// set the current user
function _setCurrentUser(userDataObj) {
currentUser = userDataObj;
}
// remove/unset current user
function _unsetCurrentUser() {
currentUser = false;
}
// check if we have a user
function _hasUser() {
return !!currentUser;
}
return {
// Public methods
// These can be called via UserDataService.METHOD_NAME
getCurrentUser: function () {
return _getCurrentUser();
},
setCurrentUser: function (userDataObj) {
_setCurrentUser(userDataObj);
},
unsetCurrentUser: function () {
_unsetCurrentUser();
},
hasUser: function () {
return _hasUser();
}
}
}])
.service('dfStringService', [function () {
return {
areIdentical: function (stringA, stringB) {
stringA = stringA || '';
stringB = stringB || '';
function _sameLength(stringA, stringB) {
return stringA.length == stringB.length;
}
function _sameLetters(stringA, stringB) {
var l = Math.min(stringA.length, stringB.length);
for (var i = 0; i < l; i++) {
if (stringA.charAt(i) !== stringB.charAt(i)) {
return false;
}
}
return true;
}
if (_sameLength(stringA, stringB) && _sameLetters(stringA, stringB)) {
return true;
}
return false;
}
}
}])
.service('dfObjectService', [function () {
return {
self: this,
mergeObjects: function (obj1, obj2) {
for (var key in obj1) {
obj2[key] = obj1[key]
}
return obj2;
},
deepMergeObjects: function (obj1, obj2) {
var self = this;
for (var _key in obj1) {
if (obj2.hasOwnProperty(_key)) {
if(typeof obj2[_key] === 'object') {
obj2[_key] = self.deepMergeObjects(obj1[_key], obj2[_key]);
}else {
obj2[_key] = obj1[_key];
}
}
}
return obj2;
}
}
}])
.service('dfXHRHelper', ['DSP_URL', 'DSP_API_KEY', '$cookies', function (DSP_URL, DSP_API_KEY, $cookies) {
function _isEmpty(obj) {
// null and undefined are "empty"
if (obj == null) return true;
// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj.length > 0) return false;
if (obj.length === 0) return true;
// Otherwise, does it have any properties of its own?
// Note that this doesn't handle
// toString and valueOf enumeration bugs in IE < 9
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) return false;
}
return true;
}
// Set DreamFactory Headers as well as additional passed in headers
function _setHeaders(_xhrObj, _headersDataObj) {
// Setting Dreamfactory Headers
_xhrObj.setRequestHeader("X-DreamFactory-Application-Name", DSP_API_KEY);
_xhrObj.setRequestHeader("X-DreamFactory-Session-Token", $cookies.PHPSESSID);
// Set additional headers
for (var _key in _headersDataObj) {
xhr_obj.setRequestHeader(_key, _headersDataObj[_key]);
}
}
// Create url params
function _setParams(_paramsDataObj) {
// Set a return var
var params = '';
// Check if we have any params
if (!_isEmpty(_paramsDataObj)) {
// We do.
// begin query string
params = '?';
// Loop through object
for (var _key in _paramsDataObj) {
// Create URL params out of object properties/values
params += _key + '=' + _paramsDataObj[_key] + '&';
}
}
// Check if params is empty string
// Did we have any params
if (params !== '') {
// We did so trim of the trailing '&' from building the string
params = params.substring(0, params.length -1);
// Encode the params
encodeURI(params);
}
// Return our final params value
return params;
}
function _makeRequest(_method, _url, _async, _params, _headers, _mimeType) {
var xhr;